Populate license data from a .LICENSE file stored as a YAML frontmatter. Note: Unknown fields are ignored and not bound to the License object.
(self, license_file, check_consistency=True)
| 503 | of.write(output) |
| 504 | |
| 505 | def load(self, license_file, check_consistency=True): |
| 506 | """ |
| 507 | Populate license data from a .LICENSE file stored as a YAML frontmatter. |
| 508 | Note: Unknown fields are ignored and not bound to the License object. |
| 509 | """ |
| 510 | try: |
| 511 | content, data = load_frontmatter(license_file) |
| 512 | if check_consistency: |
| 513 | if not data: |
| 514 | raise InvalidLicense( |
| 515 | f'Cannot load License with empty YAML frontmatter: ' |
| 516 | f'{self}: file://{license_file}' |
| 517 | ) |
| 518 | |
| 519 | if not content: |
| 520 | if check_consistency: |
| 521 | if not any( |
| 522 | attribute in data |
| 523 | for attribute in ('is_deprecated', 'is_generic', 'is_unknown') |
| 524 | ): |
| 525 | raise InvalidLicense( |
| 526 | f'Cannot load License with empty text: ' |
| 527 | f'only deprecated, generic or unknown licenses can exist without text ' |
| 528 | f'{self}: file://{license_file}' |
| 529 | ) |
| 530 | |
| 531 | self.text = '' |
| 532 | else: |
| 533 | self.text = content.lstrip("\n") |
| 534 | |
| 535 | for k, v in data.items(): |
| 536 | if k == 'minimum_coverage': |
| 537 | v = as_int(v) |
| 538 | |
| 539 | if k == 'key': |
| 540 | assert self.key == v, ( |
| 541 | 'The license "key" attribute in the YAML frontmatter MUST ' + |
| 542 | 'be the same as the base name of this .LICENSE ' + |
| 543 | 'license file. ' + |
| 544 | f'Yet file name = {self.key} and license key = {v}' |
| 545 | ) |
| 546 | |
| 547 | setattr(self, k, v) |
| 548 | |
| 549 | except Exception as e: |
| 550 | # this is a rare case: fail loudly |
| 551 | print() |
| 552 | print('#############################') |
| 553 | print('INVALID LICENSE YAML FILE:', f'file://{self.license_file}') |
| 554 | print('#############################') |
| 555 | print(traceback.format_exc()) |
| 556 | print('#############################') |
| 557 | raise e |
| 558 | return self |
| 559 | |
| 560 | def spdx_keys(self): |
| 561 | """ |
nothing calls this directly
no test coverage detected