File: behave2cucumber_json.py

package info (click to toggle)
behave 1.2.6-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,160 kB
  • sloc: python: 19,857; makefile: 137; sh: 18
file content (63 lines) | stat: -rwxr-xr-x 2,258 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python
# =============================================================================
# CONVERT: behave JSON dialect to cucumber JSON dialect
# =============================================================================
# STATUS: __PROTOTYPE__
# REQUIRES: Python >= 2.6
# REQUIRES: https://github.com/behalfinc/b2c/
# SEE: https://github.com/behave/behave/issues/267#issuecomment-249607191
# =============================================================================
"""
Convert a file with behave JSON data into a file with cucumber JSON data.
Note that both JSON dialects differ slightly.
"""

from __future__ import print_function
import json
import sys
import os.path
try:
    import b2c
except ImportError:
    print("REQUIRE: https://github.com/behalfinc/b2c/  (not installed yet)")
    print("INSTALL: pip install b2c")
    sys.exit(2)


NAME = os.path.basename(__file__)

def convert_behave_to_cucumber_json(behave_filename, cucumber_filename,
                                    encoding="UTF-8", pretty=True):
    """Convert behave JSON dialect into cucumber JSON dialect.

    .. param behave_filename:       Input filename with behave JSON data.
    .. param cucumber_filename:     Output filename with cucumber JSON data.
    """
    dump_kwargs = {"encoding": encoding}
    if pretty:
        dump_kwargs.update(indent=2, sort_keys=True)

    with open(behave_filename, "r") as behave_json:
        with open(cucumber_filename, "w+") as output_file:
            cucumber_json = b2c.convert(json.load(behave_json, encoding))
            # cucumber_text = json.dumps(cucumber_json, **dump_kwargs)
            # output_file.write(cucumber_text)
            json.dump(cucumber_json, output_file, **dump_kwargs)
    return 0

def main(args=None):
    """Main function to run the script."""
    if args is None:
        args = sys.argv[1:]
    if len(args) != 2:
        print("USAGE: %s BEHAVE_FILE.json CUCUMBER_FILE.json" % NAME)
        print("Converts behave JSON dialect to cucumber JSON dialect.")
        return 1

    behave_filename = args[0]
    cucumber_filename = args[1]
    return convert_behave_to_cucumber_json(behave_filename, cucumber_filename)

# -- AUTO-MAIN:
if __name__ == "__main__":
    sys.exit(main())