Debian Codegraph @

bin/behave2cucumber_json.py

raw
1 #!/usr/bin/env python
2 # =============================================================================
3 # CONVERT: behave JSON dialect to cucumber JSON dialect
4 # =============================================================================
5 # STATUS: __PROTOTYPE__
6 # REQUIRES: Python >= 2.6
7 # REQUIRES: https://github.com/behalfinc/b2c/
8 # SEE: https://github.com/behave/behave/issues/267#issuecomment-249607191
9 # =============================================================================
10 """
11 Convert a file with behave JSON data into a file with cucumber JSON data.
12 Note that both JSON dialects differ slightly.
13 """
14
15 from __future__ import print_function
16 import json
17 import sys
18 import os.path
19 try:
20 import b2c
21 except ImportError:
22 print("REQUIRE: https://github.com/behalfinc/b2c/ (not installed yet)")
23 print("INSTALL: pip install b2c")
24 sys.exit(2)
25
26
27 NAME = os.path.basename(__file__)
28
29 def convert_behave_to_cucumber_json(behave_filename, cucumber_filename,
30 encoding="UTF-8", pretty=True):
31 """Convert behave JSON dialect into cucumber JSON dialect.
32
33 .. param behave_filename: Input filename with behave JSON data.
34 .. param cucumber_filename: Output filename with cucumber JSON data.
35 """
36 dump_kwargs = {"encoding": encoding}
37 if pretty:
38 dump_kwargs.update(indent=2, sort_keys=True)
39
40 with open(behave_filename, "r") as behave_json:
41 with open(cucumber_filename, "w+") as output_file:
42 cucumber_json = b2c.convert(json.load(behave_json, encoding))
43 # cucumber_text = json.dumps(cucumber_json, **dump_kwargs)
44 # output_file.write(cucumber_text)
45 json.dump(cucumber_json, output_file, **dump_kwargs)
46 return 0
47
48 def main(args=None):
49 """Main function to run the script."""
50 if args is None:
51 args = sys.argv[1:]
52 if len(args) != 2:
53 print("USAGE: %s BEHAVE_FILE.json CUCUMBER_FILE.json" % NAME)
54 print("Converts behave JSON dialect to cucumber JSON dialect.")
55 return 1
56
57 behave_filename = args[0]
58 cucumber_filename = args[1]
59 return convert_behave_to_cucumber_json(behave_filename, cucumber_filename)
60
61 # -- AUTO-MAIN:
62 if __name__ == "__main__":
63 sys.exit(main())