Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/metadata_dictionary.py
Show First 20 Lines • Show All 160 Lines • ▼ Show 20 Lines | _schema_shortcuts = { | ||||
'gist': 'https://gist.github.com/', | 'gist': 'https://gist.github.com/', | ||||
'bitbucket': 'https://bitbucket.org/', | 'bitbucket': 'https://bitbucket.org/', | ||||
'gitlab': 'https://gitlab.com/', | 'gitlab': 'https://gitlab.com/', | ||||
} | } | ||||
def normalize_repository(self, d): | def normalize_repository(self, d): | ||||
"""https://docs.npmjs.com/files/package.json#repository""" | """https://docs.npmjs.com/files/package.json#repository""" | ||||
if isinstance(d, dict): | if isinstance(d, dict): | ||||
return '{type}+{url}'.format(**d) | url = '{type}+{url}'.format(**d) | ||||
elif isinstance(d, str): | elif isinstance(d, str): | ||||
if '://' in d: | if '://' in d: | ||||
return d | url = d | ||||
elif ':' in d: | elif ':' in d: | ||||
(schema, rest) = d.split(':', 1) | (schema, rest) = d.split(':', 1) | ||||
if schema in self._schema_shortcuts: | if schema in self._schema_shortcuts: | ||||
return self._schema_shortcuts[schema] + rest | url = self._schema_shortcuts[schema] + rest | ||||
else: | else: | ||||
return None | return None | ||||
else: | else: | ||||
return self._schema_shortcuts['github'] + d | url = self._schema_shortcuts['github'] + d | ||||
else: | else: | ||||
return None | return None | ||||
return {'@id': url} | |||||
def normalize_bugs(self, d): | def normalize_bugs(self, d): | ||||
return '{url}'.format(**d) | return {'@id': '{url}'.format(**d)} | ||||
_parse_author = re.compile(r'^ *' | _parse_author = re.compile(r'^ *' | ||||
r'(?P<name>.*?)' | r'(?P<name>.*?)' | ||||
r'( +<(?P<email>.*)>)?' | r'( +<(?P<email>.*)>)?' | ||||
r'( +\((?P<url>.*)\))?' | r'( +\((?P<url>.*)\))?' | ||||
r' *$') | r' *$') | ||||
def normalize_author(self, d): | def normalize_author(self, d): | ||||
Show All 11 Lines | def normalize_author(self, d): | ||||
url = match.group('url') | url = match.group('url') | ||||
else: | else: | ||||
return None | return None | ||||
if name: | if name: | ||||
author[SCHEMA_URI+'name'] = name | author[SCHEMA_URI+'name'] = name | ||||
if email: | if email: | ||||
author[SCHEMA_URI+'email'] = email | author[SCHEMA_URI+'email'] = email | ||||
if url: | if url: | ||||
author[SCHEMA_URI+'url'] = url | author[SCHEMA_URI+'url'] = {'@id': url} | ||||
return author | return {"@list": [author]} | ||||
def normalize_license(self, s): | |||||
return {"@id": "https://spdx.org/licenses/" + s} | |||||
def normalize_homepage(self, s): | |||||
return {"@id": s} | |||||
@register_mapping | @register_mapping | ||||
class CodemetaMapping(SingleFileMapping): | class CodemetaMapping(SingleFileMapping): | ||||
""" | """ | ||||
dedicated class for CodeMeta (codemeta.json) mapping and translation | dedicated class for CodeMeta (codemeta.json) mapping and translation | ||||
""" | """ | ||||
filename = b'codemeta.json' | filename = b'codemeta.json' | ||||
Show All 36 Lines | class MavenMapping(DictMapping, SingleFileMapping): | ||||
def parse_repository(self, d, repo): | def parse_repository(self, d, repo): | ||||
if repo.get('layout', 'default') != 'default': | if repo.get('layout', 'default') != 'default': | ||||
return # TODO ? | return # TODO ? | ||||
url = repo['url'] | url = repo['url'] | ||||
if d['groupId']: | if d['groupId']: | ||||
url = os.path.join(url, *d['groupId'].split('.')) | url = os.path.join(url, *d['groupId'].split('.')) | ||||
if d['artifactId']: | if d['artifactId']: | ||||
url = os.path.join(url, d['artifactId']) | url = os.path.join(url, d['artifactId']) | ||||
return url | return {"@id": url} | ||||
def normalize_groupId(self, id_): | |||||
return {"@id": id_} | |||||
def main(): | def main(): | ||||
raw_content = """{"name": "test_name", "unknown_term": "ut"}""" | raw_content = """{"name": "test_name", "unknown_term": "ut"}""" | ||||
raw_content1 = b"""{"name": "test_name", | raw_content1 = b"""{"name": "test_name", | ||||
"unknown_term": "ut", | "unknown_term": "ut", | ||||
"prerequisites" :"packageXYZ"}""" | "prerequisites" :"packageXYZ"}""" | ||||
result = MAPPINGS["NpmMapping"].translate(raw_content) | result = MAPPINGS["NpmMapping"].translate(raw_content) | ||||
result1 = MAPPINGS["MavenMapping"].translate(raw_content1) | result1 = MAPPINGS["MavenMapping"].translate(raw_content1) | ||||
print(result) | print(result) | ||||
print(result1) | print(result1) | ||||
if __name__ == "__main__": | if __name__ == "__main__": | ||||
main() | main() |