Introduce samovar.main module so we don't clutter driver script.
Chris Pressey
3 years ago
11 | 11 | |
12 | 12 | sys.path.insert(0, join(dirname(realpath(sys.argv[0])), '..', 'src')) |
13 | 13 | |
14 | from argparse import ArgumentParser | |
15 | import codecs | |
16 | import json | |
17 | ||
18 | from samovar.parser import Parser | |
19 | from samovar.generator import Generator | |
20 | from samovar.randomness import CannedRandomness | |
21 | ||
22 | ||
23 | def generate_fifty_thousand_words(): | |
24 | import random | |
25 | with codecs.open('eg/chairs.samovar', 'r', encoding='UTF-8') as f: | |
26 | text = f.read() | |
27 | p = Parser(text) | |
28 | ast = p.world() | |
29 | random.seed(0) | |
30 | g = Generator(ast, ast.scenarios[0]) | |
31 | g.generate_events(8000) | |
32 | ||
33 | ||
34 | def main(args): | |
35 | argparser = ArgumentParser() | |
36 | ||
37 | argparser.add_argument('input_files', nargs='+', metavar='FILENAME', type=str, | |
38 | help='Source files containing the scenario descriptions' | |
39 | ) | |
40 | argparser.add_argument("--debug", action="store_true", | |
41 | help="Show state before and after each move" | |
42 | ) | |
43 | argparser.add_argument("--verbose", action="store_true", | |
44 | help="Show some progress information" | |
45 | ) | |
46 | argparser.add_argument("--dump-ast", | |
47 | action="store_true", | |
48 | help="Just show the AST and stop") | |
49 | argparser.add_argument("--generate-scenarios", | |
50 | type=str, default=None, | |
51 | help="If given, generate only these scenarios") | |
52 | argparser.add_argument("--lengthen-factor", | |
53 | type=float, default=2.0, | |
54 | help="When scenario goal was not met, multiply number of events to generate by this") | |
55 | argparser.add_argument("--min-events", | |
56 | type=int, default=1, | |
57 | help="Generate at least this many events for each scenario") | |
58 | argparser.add_argument("--max-events", | |
59 | type=int, default=1000000, | |
60 | help="Assume something's gone wrong if more than this many events are generated") | |
61 | argparser.add_argument("--output-type", | |
62 | choices=('naive-text', 'events-json', 'scenarios-json',), | |
63 | default='naive-text', | |
64 | help="Specify what to output and in what format") | |
65 | argparser.add_argument("--profile", | |
66 | action="store_true", | |
67 | help="Run cProfile on standard 'heavy load' case and exit") | |
68 | argparser.add_argument("--randomness-type", | |
69 | choices=('python', 'canned',), | |
70 | default='python', | |
71 | help="Specify what provides random values to the generator") | |
72 | argparser.add_argument("--seed", | |
73 | type=int, default=None, | |
74 | help="Set random seed (to select moves deterministically, when randomness-type=python)") | |
75 | ||
76 | options = argparser.parse_args(args) | |
77 | ||
78 | if options.profile: | |
79 | import cProfile | |
80 | cProfile.run('generate_fifty_thousand_words()') | |
81 | sys.exit(0) | |
82 | ||
83 | text = '' | |
84 | for arg in options.input_files: | |
85 | with codecs.open(arg, 'r', encoding='UTF-8') as f: | |
86 | text += f.read() | |
87 | ||
88 | p = Parser(text) | |
89 | ast = p.world() | |
90 | if options.dump_ast: | |
91 | print(ast) | |
92 | sys.exit(0) | |
93 | ||
94 | if options.randomness_type == 'python': | |
95 | import random | |
96 | if options.seed is not None: | |
97 | random.seed(options.seed) | |
98 | randomness = random | |
99 | elif options.randomness_type == 'canned': | |
100 | randomness = CannedRandomness() | |
101 | else: | |
102 | raise NotImplementedError('Not a valid randomness-type: {}'.format(options.randomness_type)) | |
103 | ||
104 | event_buckets = [] | |
105 | for n, scenario in enumerate(ast.scenarios): | |
106 | if options.verbose: | |
107 | sys.stderr.write("{}. {}\n".format(n, scenario.name)) | |
108 | if scenario.goal is None: | |
109 | continue | |
110 | if options.generate_scenarios is not None and scenario.name not in options.generate_scenarios: | |
111 | continue | |
112 | g = Generator(randomness, ast, scenario, debug=options.debug, verbose=options.verbose) | |
113 | events = g.generate_events(options.min_events, options.max_events, options.lengthen_factor) | |
114 | event_buckets.append(events) | |
115 | ||
116 | if options.output_type == 'naive-text': | |
117 | for b in event_buckets: | |
118 | for e in b: | |
119 | sys.stdout.write("%s\n" % e) | |
120 | sys.stdout.write("\n") | |
121 | elif options.output_type == 'events-json': | |
122 | def jsonify_bucket(b): | |
123 | return [e.to_json() for e in b] | |
124 | jsonified_buckets = [jsonify_bucket(b) for b in event_buckets] | |
125 | sys.stdout.write(json.dumps(jsonified_buckets, indent=4, sort_keys=True)) | |
126 | elif options.output_type == 'scenarios-json': | |
127 | raise NotImplementedError("'scenarios-json' output-type not currently implemented") | |
128 | else: | |
129 | raise NotImplementedError('Not a valid output-type: {}'.format(options.output_type)) | |
14 | from samovar.main import main | |
130 | 15 | |
131 | 16 | |
132 | 17 | if __name__ == '__main__': |
0 | from argparse import ArgumentParser | |
1 | import codecs | |
2 | import json | |
3 | import sys | |
4 | ||
5 | from samovar.parser import Parser | |
6 | from samovar.generator import Generator | |
7 | from samovar.randomness import CannedRandomness | |
8 | ||
9 | ||
10 | def generate_fifty_thousand_words(): | |
11 | import random | |
12 | with codecs.open('eg/chairs.samovar', 'r', encoding='UTF-8') as f: | |
13 | text = f.read() | |
14 | p = Parser(text) | |
15 | ast = p.world() | |
16 | random.seed(0) | |
17 | g = Generator(ast, ast.scenarios[0]) | |
18 | g.generate_events(8000) | |
19 | ||
20 | ||
21 | def main(args): | |
22 | argparser = ArgumentParser() | |
23 | ||
24 | argparser.add_argument('input_files', nargs='+', metavar='FILENAME', type=str, | |
25 | help='Source files containing the scenario descriptions' | |
26 | ) | |
27 | argparser.add_argument("--debug", action="store_true", | |
28 | help="Show state before and after each move" | |
29 | ) | |
30 | argparser.add_argument("--verbose", action="store_true", | |
31 | help="Show some progress information" | |
32 | ) | |
33 | argparser.add_argument("--dump-ast", | |
34 | action="store_true", | |
35 | help="Just show the AST and stop") | |
36 | argparser.add_argument("--generate-scenarios", | |
37 | type=str, default=None, | |
38 | help="If given, generate only these scenarios") | |
39 | argparser.add_argument("--lengthen-factor", | |
40 | type=float, default=2.0, | |
41 | help="When scenario goal was not met, multiply number of events to generate by this") | |
42 | argparser.add_argument("--min-events", | |
43 | type=int, default=1, | |
44 | help="Generate at least this many events for each scenario") | |
45 | argparser.add_argument("--max-events", | |
46 | type=int, default=1000000, | |
47 | help="Assume something's gone wrong if more than this many events are generated") | |
48 | argparser.add_argument("--output-type", | |
49 | choices=('naive-text', 'events-json', 'scenarios-json',), | |
50 | default='naive-text', | |
51 | help="Specify what to output and in what format") | |
52 | argparser.add_argument("--profile", | |
53 | action="store_true", | |
54 | help="Run cProfile on standard 'heavy load' case and exit") | |
55 | argparser.add_argument("--randomness-type", | |
56 | choices=('python', 'canned',), | |
57 | default='python', | |
58 | help="Specify what provides random values to the generator") | |
59 | argparser.add_argument("--seed", | |
60 | type=int, default=None, | |
61 | help="Set random seed (to select moves deterministically, when randomness-type=python)") | |
62 | ||
63 | options = argparser.parse_args(args) | |
64 | ||
65 | if options.profile: | |
66 | import cProfile | |
67 | cProfile.run('generate_fifty_thousand_words()') | |
68 | sys.exit(0) | |
69 | ||
70 | text = '' | |
71 | for arg in options.input_files: | |
72 | with codecs.open(arg, 'r', encoding='UTF-8') as f: | |
73 | text += f.read() | |
74 | ||
75 | p = Parser(text) | |
76 | ast = p.world() | |
77 | if options.dump_ast: | |
78 | print(ast) | |
79 | sys.exit(0) | |
80 | ||
81 | if options.randomness_type == 'python': | |
82 | import random | |
83 | if options.seed is not None: | |
84 | random.seed(options.seed) | |
85 | randomness = random | |
86 | elif options.randomness_type == 'canned': | |
87 | randomness = CannedRandomness() | |
88 | else: | |
89 | raise NotImplementedError('Not a valid randomness-type: {}'.format(options.randomness_type)) | |
90 | ||
91 | event_buckets = [] | |
92 | for n, scenario in enumerate(ast.scenarios): | |
93 | if options.verbose: | |
94 | sys.stderr.write("{}. {}\n".format(n, scenario.name)) | |
95 | if scenario.goal is None: | |
96 | continue | |
97 | if options.generate_scenarios is not None and scenario.name not in options.generate_scenarios: | |
98 | continue | |
99 | g = Generator(randomness, ast, scenario, debug=options.debug, verbose=options.verbose) | |
100 | events = g.generate_events(options.min_events, options.max_events, options.lengthen_factor) | |
101 | event_buckets.append(events) | |
102 | ||
103 | if options.output_type == 'naive-text': | |
104 | for b in event_buckets: | |
105 | for e in b: | |
106 | sys.stdout.write("%s\n" % e) | |
107 | sys.stdout.write("\n") | |
108 | elif options.output_type == 'events-json': | |
109 | def jsonify_bucket(b): | |
110 | return [e.to_json() for e in b] | |
111 | jsonified_buckets = [jsonify_bucket(b) for b in event_buckets] | |
112 | sys.stdout.write(json.dumps(jsonified_buckets, indent=4, sort_keys=True)) | |
113 | elif options.output_type == 'scenarios-json': | |
114 | raise NotImplementedError("'scenarios-json' output-type not currently implemented") | |
115 | else: | |
116 | raise NotImplementedError('Not a valid output-type: {}'.format(options.output_type)) |