Skip to content
Snippets Groups Projects
Commit 0cea8313 authored by Kartik Ohri's avatar Kartik Ohri Committed by Phabricator Migration user
Browse files

Remove TypedDict's as per chat

parent a2b39603
No related tags found
1 merge request!305Type annotations in metadata mappings
......@@ -24,28 +24,6 @@ class FileEntry(TypedDict):
dir_id: bytes
SchemaEntry = TypedDict("SchemaEntry", {"@id": str})
Affiliation = TypedDict("Affiliation", {"@type": str, "http://schema.org/name": str})
Author = TypedDict(
"Author",
{
"@type": str,
"@id": str,
"http://schema.org/name": str,
"http://schema.org/email": str,
"http://schema.org/familyName": str,
"http://schema.org/givenName": str,
"http://schema.org/affiliation": Affiliation,
"http://schema.org/url": SchemaEntry,
},
total=False,
)
Authors = TypedDict("Authors", {"@list": List[Author]})
Date = TypedDict("Date", {"@value": str, "@type": str})
class BaseMapping:
"""Base class for mappings to inherit from
......
......@@ -4,7 +4,7 @@ import yaml
from swh.indexer.codemeta import CODEMETA_CONTEXT_URL, CROSSWALK_TABLE, SCHEMA_URI
from .base import Author, Authors, Date, DictMapping, SchemaEntry, SingleFileMapping
from .base import DictMapping, SingleFileMapping
yaml.SafeLoader.yaml_implicit_resolvers = {
k: [r for r in v if r[0] != "tag:yaml.org,2002:timestamp"]
......@@ -29,8 +29,8 @@ class CffMapping(DictMapping, SingleFileMapping):
return metadata
def normalize_authors(self, d) -> Authors:
result: List[Author] = []
def normalize_authors(self, d) -> Dict[str, Any]:
result: List[Dict[str, Any]] = []
for author in d:
author_data: Dict[str, Any] = {"@type": SCHEMA_URI + "Person"}
if "orcid" in author:
......@@ -45,26 +45,26 @@ class CffMapping(DictMapping, SingleFileMapping):
if "given-names" in author:
author_data[SCHEMA_URI + "givenName"] = author["given-names"]
result.append(author_data) # type: ignore
result.append(author_data)
return {"@list": result}
def normalize_doi(self, s) -> Optional[SchemaEntry]:
def normalize_doi(self, s) -> Optional[Dict[str, str]]:
if isinstance(s, str):
return {"@id": "https://doi.org/" + s}
return None
def normalize_license(self, s) -> Optional[SchemaEntry]:
def normalize_license(self, s) -> Optional[Dict[str, str]]:
if isinstance(s, str):
return {"@id": "https://spdx.org/licenses/" + s}
return None
def normalize_repository_code(self, s) -> Optional[SchemaEntry]:
def normalize_repository_code(self, s) -> Optional[Dict[str, str]]:
if isinstance(s, str):
return {"@id": s}
return None
def normalize_date_released(self, s) -> Optional[Date]:
def normalize_date_released(self, s) -> Optional[Dict[str, str]]:
if isinstance(s, str):
return {"@value": s, "@type": SCHEMA_URI + "Date"}
return None
......@@ -4,7 +4,7 @@
# See top-level LICENSE file for more information
import json
from typing import Dict, List, Optional
from typing import Any, Dict, List, Optional
from swh.indexer.codemeta import CODEMETA_TERMS, expand
......@@ -24,7 +24,7 @@ class CodemetaMapping(SingleFileMapping):
def supported_terms(cls) -> List[str]:
return [term for term in CODEMETA_TERMS if not term.startswith("@")]
def translate(self, content: bytes) -> Optional[Dict]:
def translate(self, content: bytes) -> Optional[Dict[str, Any]]:
try:
return self.normalize_translation(expand(json.loads(content.decode())))
except Exception:
......
......@@ -4,14 +4,14 @@
# See top-level LICENSE file for more information
import os
from typing import Dict, List, Optional
from typing import Any, Dict, List, Optional
import xml.parsers.expat
import xmltodict
from swh.indexer.codemeta import CROSSWALK_TABLE, SCHEMA_URI
from .base import DictMapping, SchemaEntry, SingleFileMapping
from .base import DictMapping, SingleFileMapping
class MavenMapping(DictMapping, SingleFileMapping):
......@@ -47,7 +47,7 @@ class MavenMapping(DictMapping, SingleFileMapping):
_default_repository = {"url": "https://repo.maven.apache.org/maven2/"}
def parse_repositories(self, d: Dict) -> Optional[List[Optional[SchemaEntry]]]:
def parse_repositories(self, d: Dict) -> Optional[List[Optional[Dict[str, Any]]]]:
"""https://maven.apache.org/pom.html#Repositories
>>> import xmltodict
......@@ -76,7 +76,7 @@ class MavenMapping(DictMapping, SingleFileMapping):
results = []
return [res for res in results if res] or None
def parse_repository(self, d: Dict, repo) -> Optional[SchemaEntry]:
def parse_repository(self, d: Dict, repo) -> Optional[Dict[str, Any]]:
if not isinstance(repo, dict):
return None
if repo.get("layout", "default") != "default":
......@@ -93,7 +93,7 @@ class MavenMapping(DictMapping, SingleFileMapping):
return {"@id": repo}
return None
def normalize_groupId(self, id_) -> Optional[SchemaEntry]:
def normalize_groupId(self, id_) -> Optional[Dict[str, Any]]:
"""https://maven.apache.org/pom.html#Maven_Coordinates
>>> MavenMapping().normalize_groupId('org.example')
......@@ -103,7 +103,7 @@ class MavenMapping(DictMapping, SingleFileMapping):
return {"@id": id_}
return None
def parse_licenses(self, d) -> Optional[List[SchemaEntry]]:
def parse_licenses(self, d) -> Optional[List[Dict[str, Any]]]:
"""https://maven.apache.org/pom.html#Licenses
>>> import xmltodict
......
......@@ -4,11 +4,11 @@
# See top-level LICENSE file for more information
import re
from typing import Any, Dict, List, Optional, cast
from typing import Any, Dict, List, Optional
from swh.indexer.codemeta import CROSSWALK_TABLE, SCHEMA_URI
from .base import Author, Authors, JsonMapping, SchemaEntry
from .base import JsonMapping
class NpmMapping(JsonMapping):
......@@ -30,7 +30,7 @@ class NpmMapping(JsonMapping):
# 'bitbucket': 'https://bitbucket.org/',
}
def normalize_repository(self, d) -> Optional[SchemaEntry]:
def normalize_repository(self, d) -> Optional[Dict[str, str]]:
"""https://docs.npmjs.com/files/package.json#repository
>>> NpmMapping().normalize_repository({
......@@ -68,7 +68,7 @@ class NpmMapping(JsonMapping):
return {"@id": url}
def normalize_bugs(self, d) -> Optional[SchemaEntry]:
def normalize_bugs(self, d) -> Optional[Dict[str, str]]:
"""https://docs.npmjs.com/files/package.json#bugs
>>> NpmMapping().normalize_bugs({
......@@ -91,7 +91,7 @@ class NpmMapping(JsonMapping):
r"^ *" r"(?P<name>.*?)" r"( +<(?P<email>.*)>)?" r"( +\((?P<url>.*)\))?" r" *$"
)
def normalize_author(self, d) -> Optional[Authors]:
def normalize_author(self, d) -> Optional[Dict[str, Any]]:
"""https://docs.npmjs.com/files/package.json#people-fields-author-contributors'
>>> from pprint import pprint
......@@ -132,10 +132,9 @@ class NpmMapping(JsonMapping):
author[SCHEMA_URI + "email"] = email
if url and isinstance(url, str):
author[SCHEMA_URI + "url"] = {"@id": url}
authors = [cast(Author, author)]
return {"@list": authors}
return {"@list": [author]}
def normalize_license(self, s) -> Optional[SchemaEntry]:
def normalize_license(self, s) -> Optional[Dict[str, str]]:
"""https://docs.npmjs.com/files/package.json#license
>>> NpmMapping().normalize_license('MIT')
......@@ -145,7 +144,7 @@ class NpmMapping(JsonMapping):
return {"@id": "https://spdx.org/licenses/" + s}
return None
def normalize_homepage(self, s) -> Optional[SchemaEntry]:
def normalize_homepage(self, s) -> Optional[Dict[str, str]]:
"""https://docs.npmjs.com/files/package.json#homepage
>>> NpmMapping().normalize_homepage('https://example.org/~john.doe')
......
......@@ -10,7 +10,7 @@ from typing import Dict, List
from swh.indexer.codemeta import CROSSWALK_TABLE, SCHEMA_URI
from .base import DictMapping, SchemaEntry, SingleFileMapping
from .base import DictMapping, SingleFileMapping
_normalize_pkginfo_key = str.lower
......@@ -67,11 +67,11 @@ class PythonPkginfoMapping(DictMapping, SingleFileMapping):
}
return self.normalize_translation(metadata)
def normalize_home_page(self, urls: List[str]) -> List[SchemaEntry]:
def normalize_home_page(self, urls: List[str]) -> List[Dict[str, str]]:
return [{"@id": url} for url in urls]
def normalize_keywords(self, keywords: List[str]) -> List[str]:
return list(itertools.chain.from_iterable(s.split(" ") for s in keywords))
def normalize_license(self, licenses: str) -> List[SchemaEntry]:
def normalize_license(self, licenses: str) -> List[Dict[str, str]]:
return [{"@id": license} for license in licenses]
......@@ -6,17 +6,17 @@
import ast
import itertools
import re
from typing import Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Union
from swh.indexer.codemeta import CROSSWALK_TABLE, SCHEMA_URI
from .base import Author, Authors, DictMapping, FileEntry, SchemaEntry
from .base import DictMapping, FileEntry
def name_to_person(name: str) -> Author:
def name_to_person(name: str) -> Dict[str, str]:
return {
"@type": SCHEMA_URI + "Person",
SCHEMA_URI + "name": name, # type: ignore
SCHEMA_URI + "name": name,
}
......@@ -103,17 +103,17 @@ class GemspecMapping(DictMapping):
return evaluator(tree.body)
return None
def normalize_homepage(self, s) -> Optional[SchemaEntry]:
def normalize_homepage(self, s) -> Optional[Dict[str, str]]:
if isinstance(s, str):
return {"@id": s}
return None
def normalize_license(self, s) -> Optional[List[SchemaEntry]]:
def normalize_license(self, s) -> Optional[List[Dict[str, str]]]:
if isinstance(s, str):
return [{"@id": "https://spdx.org/licenses/" + s}]
return None
def normalize_licenses(self, licenses) -> Optional[List[SchemaEntry]]:
def normalize_licenses(self, licenses) -> Optional[List[Dict[str, str]]]:
if isinstance(licenses, list):
return [
{"@id": "https://spdx.org/licenses/" + license}
......@@ -122,12 +122,12 @@ class GemspecMapping(DictMapping):
]
return None
def normalize_author(self, author) -> Optional[Authors]:
def normalize_author(self, author) -> Optional[Dict[str, Any]]:
if isinstance(author, str):
return {"@list": [name_to_person(author)]}
return None
def normalize_authors(self, authors) -> Optional[Authors]:
def normalize_authors(self, authors) -> Optional[Dict[str, List[Dict[str, Any]]]]:
if isinstance(authors, list):
return {
"@list": [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment