Commit 35b4087da29201f1aed5c7108cdd346322af9176
Merge pull request #22 from selurvedu/pretty_output
Prettify representation of Munches
Showing
2 changed files
with
32 additions
and
3 deletions
... | ... | @@ -21,6 +21,18 @@ eggs = |
21 | 21 | robotframework-debuglibrary |
22 | 22 | |
23 | 23 | interpreter = python_interpreter |
24 | +# The following piece of code changes the default output format of Munch | |
25 | +# for Munch-to-str conversion and for generation of so-called repr. | |
26 | +# | |
27 | +# As a result, Robot Framework records pretty human-readable (YAML) data in its | |
28 | +# log files instead of ugly piles of Munch(data=Munch(foo=Munch(...))). | |
29 | +# | |
30 | +# Original idea: https://github.com/Infinidat/munch/blob/2.0.4/README.md#serialization | |
31 | +initialization = | |
32 | + from munch import Munch | |
33 | + Munch.__str__ = lambda self: Munch.toYAML(self, allow_unicode=True, | |
34 | + default_flow_style=False) | |
35 | + Munch.__repr__ = Munch.__str__ | |
24 | 36 | |
25 | 37 | [remotes] |
26 | 38 | gh = git://github.com/ | ... | ... |
... | ... | @@ -58,15 +58,32 @@ def compare_date(data1, data2): |
58 | 58 | return False |
59 | 59 | return True |
60 | 60 | |
61 | -def log_object_data(data, file_name="", format="yaml"): | |
61 | +def log_object_data(data, file_name=None, format="yaml"): | |
62 | + """Log object data in pretty format (JSON or YAML) | |
63 | + | |
64 | + Two output formats are supported: "yaml" and "json". | |
65 | + | |
66 | + If a file name is specified, the output is written into that file. | |
67 | + | |
68 | + If you would like to get similar output everywhere, | |
69 | + use the following snippet somewhere in your code | |
70 | + before actually using Munch. For instance, | |
71 | + put it into your __init__.py, or, if you use zc.buildout, | |
72 | + specify it in "initialization" setting of zc.recipe.egg. | |
73 | + | |
74 | + from munch import Munch | |
75 | + Munch.__str__ = lambda self: Munch.toYAML(self, allow_unicode=True, | |
76 | + default_flow_style=False) | |
77 | + Munch.__repr__ = Munch.__str__ | |
78 | + """ | |
62 | 79 | if not isinstance(data, Munch): |
63 | 80 | data = munchify(data) |
64 | - if format == 'json': | |
81 | + if format.lower() == 'json': | |
65 | 82 | data = data.toJSON(indent=2) |
66 | 83 | else: |
67 | 84 | data = data.toYAML(allow_unicode=True, default_flow_style=False) |
68 | 85 | format = 'yaml' |
69 | - LOGGER.log_message(Message(data, "INFO")) | |
86 | + LOGGER.log_message(Message(data.decode('utf-8'), "INFO")) | |
70 | 87 | if file_name: |
71 | 88 | output_dir = BuiltIn().get_variable_value("${OUTPUT_DIR}") |
72 | 89 | with open(os.path.join(output_dir, file_name + '.' + format), "w") as file_obj: | ... | ... |
Please
register
or
login
to post a comment