Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/metadata_dictionary.py
Show All 35 Lines | def merge_values(v1, v2): | ||||
>>> merge_values('a', 'b') | >>> merge_values('a', 'b') | ||||
['a', 'b'] | ['a', 'b'] | ||||
>>> merge_values(['a', 'b'], 'c') | >>> merge_values(['a', 'b'], 'c') | ||||
['a', 'b', 'c'] | ['a', 'b', 'c'] | ||||
>>> merge_values({'@list': ['a', 'b']}, {'@list': ['c']}) | >>> merge_values({'@list': ['a', 'b']}, {'@list': ['c']}) | ||||
{'@list': ['a', 'b', 'c']} | {'@list': ['a', 'b', 'c']} | ||||
""" | """ | ||||
if isinstance(v1, dict) and set(v1) == {'@list'}: | if v1 is None: | ||||
return v2 | |||||
elif v2 is None: | |||||
return v1 | |||||
elif isinstance(v1, dict) and set(v1) == {'@list'}: | |||||
assert isinstance(v1['@list'], list) | assert isinstance(v1['@list'], list) | ||||
if isinstance(v2, dict) and set(v2) == {'@list'}: | if isinstance(v2, dict) and set(v2) == {'@list'}: | ||||
assert isinstance(v2['@list'], list) | assert isinstance(v2['@list'], list) | ||||
return {'@list': v1['@list'] + v2['@list']} | return {'@list': v1['@list'] + v2['@list']} | ||||
else: | else: | ||||
raise ValueError('Cannot merge %r and %r' % (v1, v2)) | raise ValueError('Cannot merge %r and %r' % (v1, v2)) | ||||
else: | else: | ||||
if isinstance(v2, dict) and '@list' in v2: | if isinstance(v2, dict) and '@list' in v2: | ||||
▲ Show 20 Lines • Show All 267 Lines • ▼ Show 20 Lines | def normalize_license(self, s): | ||||
return None | return None | ||||
def normalize_homepage(self, s): | def normalize_homepage(self, s): | ||||
"""https://docs.npmjs.com/files/package.json#homepage | """https://docs.npmjs.com/files/package.json#homepage | ||||
>>> NpmMapping().normalize_homepage('https://example.org/~john.doe') | >>> NpmMapping().normalize_homepage('https://example.org/~john.doe') | ||||
{'@id': 'https://example.org/~john.doe'} | {'@id': 'https://example.org/~john.doe'} | ||||
""" | """ | ||||
if isinstance(s, str): | |||||
return {"@id": 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 20 Lines • Show All 249 Lines • ▼ Show 20 Lines | def eval_ruby_expression(self, expr): | ||||
# of such strings). | # of such strings). | ||||
tree = ast.parse(expr, mode='eval') | tree = ast.parse(expr, mode='eval') | ||||
except (SyntaxError, ValueError): | except (SyntaxError, ValueError): | ||||
return | return | ||||
if isinstance(tree, ast.Expression): | if isinstance(tree, ast.Expression): | ||||
return evaluator(tree.body) | return evaluator(tree.body) | ||||
def normalize_homepage(self, s): | def normalize_homepage(self, s): | ||||
if isinstance(s, str): | |||||
return {"@id": s} | return {"@id": s} | ||||
def normalize_license(self, s): | def normalize_license(self, s): | ||||
if isinstance(s, str): | if isinstance(s, str): | ||||
return [{"@id": "https://spdx.org/licenses/" + s}] | return [{"@id": "https://spdx.org/licenses/" + s}] | ||||
def normalize_licenses(self, licenses): | def normalize_licenses(self, licenses): | ||||
if isinstance(licenses, list): | if isinstance(licenses, list): | ||||
return [{"@id": "https://spdx.org/licenses/" + license} | return [{"@id": "https://spdx.org/licenses/" + license} | ||||
for license in licenses | for license in licenses | ||||
if isinstance(license, str)] | if isinstance(license, str)] | ||||
def normalize_author(self, author): | def normalize_author(self, author): | ||||
if isinstance(author, str): | |||||
return {"@list": [author]} | return {"@list": [author]} | ||||
def normalize_authors(self, authors): | def normalize_authors(self, authors): | ||||
return {"@list": authors} | if isinstance(authors, list): | ||||
return {"@list": [author for author in authors | |||||
if isinstance(author, str)]} | |||||
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() |