Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/metadata_dictionary.py
Show First 20 Lines • Show All 240 Lines • ▼ Show 20 Lines | class MavenMapping(DictMapping, SingleFileMapping): | ||||
""" | """ | ||||
filename = b'pom.xml' | filename = b'pom.xml' | ||||
mapping = CROSSWALK_TABLE['Java (Maven)'] | mapping = CROSSWALK_TABLE['Java (Maven)'] | ||||
def translate(self, content): | def translate(self, content): | ||||
d = xmltodict.parse(content)['project'] | d = xmltodict.parse(content)['project'] | ||||
metadata = self.translate_dict(d, normalize=False) | metadata = self.translate_dict(d, normalize=False) | ||||
metadata[SCHEMA_URI+'codeRepository'] = self.parse_repositories(d) | metadata[SCHEMA_URI+'codeRepository'] = self.parse_repositories(d) | ||||
metadata[SCHEMA_URI+'license'] = self.parse_licenses(d) | |||||
return self.normalize_translation(metadata) | return self.normalize_translation(metadata) | ||||
_default_repository = {'url': 'https://repo.maven.apache.org/maven2/'} | _default_repository = {'url': 'https://repo.maven.apache.org/maven2/'} | ||||
def parse_repositories(self, d): | def parse_repositories(self, d): | ||||
"""https://maven.apache.org/pom.html#Repositories""" | """https://maven.apache.org/pom.html#Repositories""" | ||||
if 'repositories' not in d: | if 'repositories' not in d: | ||||
return [self.parse_repository(d, self._default_repository)] | return [self.parse_repository(d, self._default_repository)] | ||||
Show All 16 Lines | def parse_repository(self, d, repo): | ||||
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 {"@id": url} | return {"@id": url} | ||||
def normalize_groupId(self, id_): | def normalize_groupId(self, id_): | ||||
return {"@id": id_} | return {"@id": id_} | ||||
def parse_licenses(self, d): | |||||
"""https://maven.apache.org/pom.html#Licenses""" | |||||
zack: What happens when multiple <license> elements are present?
It would be nice to have:
- a… | |||||
Done Inline ActionsThis method always returns a list, so there's nothing interesting here. However, JSON-LD compaction happens later, so I added tests for that. vlorentz: This method always returns a list, so there's nothing interesting here. However, JSON-LD… | |||||
Not Done Inline ActionsThe fact that I wanted documented (in the docstring, type annotation, whatever) was precisely that it always returns a list :-) zack: The fact that I wanted documented (in the docstring, type annotation, whatever) was precisely… | |||||
Done Inline ActionsTrue. I added a task to remember to document all these methods further: T1403 vlorentz: True. I added a task to remember to document all these methods further: T1403 | |||||
licenses = d.get('licenses', {}).get('license', []) | |||||
if isinstance(licenses, dict): | |||||
licenses = [licenses] | |||||
return [{"@id": license['url']} for license in licenses] | |||||
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() |
What happens when multiple <license> elements are present?
It would be nice to have: