Write headers, files, and other attributes from `codebase` to `output_file` Enable JSON indentation if `pretty` is True
(codebase, output_file, pretty=False, **kwargs)
| 80 | |
| 81 | |
| 82 | def write_results(codebase, output_file, pretty=False, **kwargs): |
| 83 | """ |
| 84 | Write headers, files, and other attributes from `codebase` to `output_file` |
| 85 | |
| 86 | Enable JSON indentation if `pretty` is True |
| 87 | """ |
| 88 | # Set indentation for JSON output if `pretty` is True |
| 89 | # We use a separate dict for jsonstream kwargs since we are passing |
| 90 | # this function's kwargs as arguments to OutputPlugin.get_files() |
| 91 | if pretty: |
| 92 | jsonstreams_kwargs = dict(indent=2, pretty=True) |
| 93 | else: |
| 94 | jsonstreams_kwargs = dict(indent=None, pretty=False) |
| 95 | |
| 96 | # If `output_file` is a path string, open the file at path `output_file` and use it as `output_file` |
| 97 | close_fd = False |
| 98 | if isinstance(output_file, str): |
| 99 | output_file = open(output_file, 'w') |
| 100 | close_fd = True |
| 101 | |
| 102 | # Begin wri'w' JSON to `output_file` |
| 103 | with jsonstreams.Stream( |
| 104 | jsonstreams.Type.OBJECT, |
| 105 | fd=output_file, |
| 106 | close_fd=close_fd, |
| 107 | **jsonstreams_kwargs |
| 108 | ) as s: |
| 109 | # Write headers |
| 110 | codebase.add_files_count_to_current_header() |
| 111 | codebase_headers = codebase.get_headers() |
| 112 | s.write('headers', codebase_headers) |
| 113 | |
| 114 | # Write attributes |
| 115 | if codebase.attributes: |
| 116 | for attribute_key, attribute_value in codebase.attributes.to_dict().items(): |
| 117 | s.write(attribute_key, attribute_value) |
| 118 | |
| 119 | # Write files |
| 120 | codebase_files = OutputPlugin.get_files(codebase, **kwargs) |
| 121 | # OutputPlugin.get_files() returns a generator, not JSON-serializable |
| 122 | codebase_files = list(codebase_files) |
| 123 | s.write('files', codebase_files) |
| 124 | |
| 125 | |
| 126 | def get_results(codebase, as_list=False, **kwargs): |
no test coverage detected