MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / write_results

Function write_results

src/formattedcode/output_json.py:82–123  ·  view source on GitHub ↗

Write headers, files, and other attributes from `codebase` to `output_file` Enable JSON indentation if `pretty` is True

(codebase, output_file, pretty=False, **kwargs)

Source from the content-addressed store, hash-verified

80
81
82def 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
126def get_results(codebase, as_list=False, **kwargs):

Callers 2

process_codebaseMethod · 0.85
process_codebaseMethod · 0.85

Calls 4

get_headersMethod · 0.80
writeMethod · 0.45
to_dictMethod · 0.45

Tested by

no test coverage detected