(_package, path, prefix='package__')
| 294 | |
| 295 | |
| 296 | def flatten_package(_package, path, prefix='package__'): |
| 297 | |
| 298 | # known package columns |
| 299 | |
| 300 | package_columns = get_package_columns() |
| 301 | |
| 302 | pack = dict(path=path) |
| 303 | for k, val in _package.items(): |
| 304 | if k not in package_columns: |
| 305 | continue |
| 306 | |
| 307 | # prefix columns with "package__" |
| 308 | nk = prefix + k |
| 309 | |
| 310 | if k == 'version': |
| 311 | val = val or '' |
| 312 | if val and not val.lower().startswith('v'): |
| 313 | # prefix versions with a v to avoid spreadsheet tools to mistake |
| 314 | # a version for a number or date when reading CSVs (common with |
| 315 | # Excel and LibreOffice). |
| 316 | val = 'v ' + val |
| 317 | pack[nk] = val |
| 318 | continue |
| 319 | |
| 320 | # these may come from a component matched |
| 321 | if k == 'components' and val and isinstance(val, list): |
| 322 | for component in val: |
| 323 | for component_key, component_val in component.items(): |
| 324 | if component_key not in package_columns: |
| 325 | continue |
| 326 | |
| 327 | component_new_key = nk + '__' + component_key |
| 328 | |
| 329 | if component_val is None: |
| 330 | pack[component_new_key] = '' |
| 331 | continue |
| 332 | |
| 333 | if isinstance(component_val, list): |
| 334 | component_val = '\n'.join(component_val) |
| 335 | |
| 336 | if not isinstance(component_val, str): |
| 337 | component_val = repr(component_val) |
| 338 | |
| 339 | existing = pack.get(component_new_key) or [] |
| 340 | if not isinstance(existing, list): |
| 341 | existing = [existing] |
| 342 | |
| 343 | if TRACE: |
| 344 | logger_debug('component_new_key:', component_new_key, 'existing:', type(existing), repr(existing)) |
| 345 | logger_debug('component_key:', component_key, 'component_val:', type(component_val), repr(component_val)) |
| 346 | |
| 347 | pack[component_new_key] = ' \n'.join(existing + [component_val]) |
| 348 | continue |
| 349 | |
| 350 | # everything else |
| 351 | |
| 352 | pack[nk] = '' |
| 353 |
no test coverage detected