forked from resync/resync-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulate-source
More file actions
executable file
·84 lines (67 loc) · 2.76 KB
/
Copy pathsimulate-source
File metadata and controls
executable file
·84 lines (67 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# encoding: utf-8
"""
simulate-source: The ResourceSync command line tool for simulating a changing
data source.
Created by Bernhard Haslhofer on 2012-04-24.
Copyright 2012, ResourceSync.org. All rights reserved.
"""
import optparse
import yaml
import resyncsim
DEFAULT_CONFIG_FILE = 'config/example.yaml'
def main():
# Define simulator options
p = optparse.OptionParser(usage="%prog [options]", version="%prog 0.2")
p.add_option('--config-file', '-c',
default=DEFAULT_CONFIG_FILE,
help="the simulation configuration file")
p.add_option('--port', '-p', type="int",
default=8080,
help="the HTTP interface port")
# Parse command line options and arguments
options, arguments = p.parse_args()
# Load the YAML configuration file
stream = file(options.config_file, 'r')
config = yaml.load(stream)
# Set up the source
source_settings = config['source']
source = resyncsim.Source(source_settings)
# Attach HTTP interface to source
http_interface = resyncsim.HTTPInterface(source)
# Reflectively load, setup and register inventory (if defined)
if config['inventory']:
klass_name = config['inventory']['class']
mod = __import__('resyncsim.inventory', fromlist=[klass_name])
inventory_klass = getattr(mod, klass_name)
inventory = inventory_klass(source, config['inventory'])
http_interface.add_handlers(inventory.handlers)
# Set up and register change memory (if defined)
if config['changememory']:
klass_name = config['changememory']['class']
mod = __import__('resyncsim.changememory', fromlist=[klass_name])
changemem_klass = getattr(mod, klass_name)
changemem = changemem_klass(source, config['changememory'])
http_interface.add_handlers(changemem.handlers)
# Set up and register publishers (if defined)
if config['publisher']:
klass_name = config['publisher']['class']
mod = __import__('resyncsim.publisher', fromlist=[klass_name])
publisher_klass = getattr(mod, klass_name)
publisher = publisher_klass(source, config['publisher'])
# Attach event loggers;
if config['logger']:
klass_name = config['logger']['class']
mod = __import__('resyncsim.event_log', fromlist=[klass_name])
logger_class = getattr(mod, klass_name)
logger = logger_class(source, config['logger'])
# Start the Web interface, run the simulation
try:
http_interface.start()
source.simulate_changes()
except KeyboardInterrupt:
print "Exiting gracefully..."
finally:
http_interface.stop()
if __name__ == '__main__':
main()