|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# ScanCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/nexB/scancode-toolkit for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +import io |
| 11 | +import saneyaml |
| 12 | +from packagedcode import models |
| 13 | + |
| 14 | +""" |
| 15 | +Handle CITATION.cff files, see https://citation-file-format.github.io/ |
| 16 | +https://github.com/citation-file-format/citation-file-format/blob/main/schema-guide.md |
| 17 | +
|
| 18 | +See https://github.com/citation-file-format/citation-file-format/tree/main/examples/1.2.0/pass for examples. |
| 19 | +""" |
| 20 | + |
| 21 | +class CitationHandler(models.DatafileHandler): |
| 22 | + datasource_id = 'citation_cff' |
| 23 | + default_package_type = 'citation' |
| 24 | + path_patterns = ('*/CITATION.cff',) |
| 25 | + description = 'Citation file format' |
| 26 | + documentation_url = 'https://citation-file-format.github.io/' |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def parse(cls, location): |
| 30 | + metayaml = get_cff_data(location) |
| 31 | + title = metayaml.get('title') |
| 32 | + abstract = metayaml.get('abstract') |
| 33 | + keywords = metayaml.get('keywords') |
| 34 | + release_date = metayaml.get('date-released') |
| 35 | + # This is the version of the software/item and not cff-version |
| 36 | + version = metayaml.get('version') |
| 37 | + parties = [] |
| 38 | + authors = metayaml.get('authors') |
| 39 | + if authors: |
| 40 | + for author in authors: |
| 41 | + name = author.get('family-names') or author.get('name') |
| 42 | + email = author.get('email') |
| 43 | + url = author.get('orcid') |
| 44 | + parties.append(models.Party( |
| 45 | + name=name, |
| 46 | + email=email, |
| 47 | + url=url |
| 48 | + )) |
| 49 | + repository_homepage_url = metayaml.get('repository') |
| 50 | + repository_download_url = metayaml.get('repository-code') |
| 51 | + extracted_license_statement = metayaml.get('license') |
| 52 | + homepage_url = metayaml.get('url') |
| 53 | + |
| 54 | + yield models.PackageData( |
| 55 | + datasource_id=cls.datasource_id, |
| 56 | + type=cls.default_package_type, |
| 57 | + name=title, |
| 58 | + description=abstract, |
| 59 | + keywords=keywords, |
| 60 | + release_date=release_date, |
| 61 | + parties=parties, |
| 62 | + version=version, |
| 63 | + repository_homepage_url=repository_homepage_url, |
| 64 | + repository_download_url=repository_download_url, |
| 65 | + extracted_license_statement=extracted_license_statement, |
| 66 | + homepage_url=homepage_url |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +def get_cff_data(location): |
| 71 | + """ |
| 72 | + Return a mapping of yaml/cff data loaded from a CITATION.cff files. |
| 73 | + """ |
| 74 | + |
| 75 | + # TODO: Should this be Jinja-based? |
| 76 | + yaml_lines = [] |
| 77 | + with io.open(location, encoding='utf-8') as citationcff: |
| 78 | + for line in citationcff: |
| 79 | + if not line: |
| 80 | + continue |
| 81 | + yaml_lines.append(line) |
| 82 | + |
| 83 | + return saneyaml.load('\n'.join(yaml_lines)) |
| 84 | + |
| 85 | + |
0 commit comments