Yield ordered dictionaries of key/values flattening the sequence data in a single line-separated value and keying always by path, given a ScanCode `scan` results list. Update the `headers` mapping sequences with seen keys as a side effect.
(scan, headers)
| 105 | |
| 106 | |
| 107 | def flatten_scan(scan, headers): |
| 108 | """ |
| 109 | Yield ordered dictionaries of key/values flattening the sequence |
| 110 | data in a single line-separated value and keying always by path, |
| 111 | given a ScanCode `scan` results list. Update the `headers` mapping |
| 112 | sequences with seen keys as a side effect. |
| 113 | """ |
| 114 | seen = set() |
| 115 | |
| 116 | def collect_keys(mapping, key_group): |
| 117 | """Update the headers with new keys.""" |
| 118 | keys = mapping.keys() |
| 119 | headers[key_group].extend(k for k in keys if k not in seen) |
| 120 | seen.update(keys) |
| 121 | |
| 122 | for scanned_file in scan: |
| 123 | path = scanned_file.pop('path') |
| 124 | |
| 125 | # removing any slash at the begening of the path |
| 126 | path = path.lstrip('/') |
| 127 | |
| 128 | # use a trailing slash for directories |
| 129 | if scanned_file.get('type') == 'directory' and not path.endswith('/'): |
| 130 | path += '/' |
| 131 | |
| 132 | errors = scanned_file.pop('scan_errors', []) |
| 133 | |
| 134 | # FIXME: info are NOT lists: lists are the actual scans |
| 135 | file_info = dict(path=path) |
| 136 | file_info.update( |
| 137 | ( |
| 138 | (k, v) for k, v in scanned_file.items() |
| 139 | if not isinstance(v, (list, dict)) |
| 140 | ) |
| 141 | ) |
| 142 | # Scan errors are joined in a single multi-line value |
| 143 | file_info['scan_errors'] = '\n'.join(errors) |
| 144 | |
| 145 | collect_keys(file_info, 'info') |
| 146 | yield file_info |
| 147 | |
| 148 | for detection in scanned_file.get('license_detections', []): |
| 149 | license_expression = detection["license_expression"] |
| 150 | detection_log = detection.get("detection_log", []) or [] |
| 151 | detection_log = '\n'.join(detection_log) |
| 152 | license_matches = detection["matches"] |
| 153 | for match in license_matches: |
| 154 | lic = dict(path=path) |
| 155 | lic["license_expression"] = license_expression |
| 156 | lic["detection_log"] = detection_log |
| 157 | |
| 158 | for k, val in match.items(): |
| 159 | # do not include matched text for now. |
| 160 | if k == 'matched_text': |
| 161 | continue |
| 162 | |
| 163 | if k == 'licenses': |
| 164 | license_keys = [] |