Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/npm/loader.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from codecs import BOM_UTF8 | from codecs import BOM_UTF8 | ||||
import json | import json | ||||
import logging | import logging | ||||
import os | import os | ||||
from typing import Any, Dict, Iterator, List, Optional, Sequence, Tuple, Union | from typing import Any, Dict, Iterator, List, Optional, Sequence, Tuple, Union | ||||
from urllib.parse import quote | from urllib.parse import quote | ||||
import attr | import attr | ||||
import chardet | import chardet | ||||
from swh.loader.exception import NotFound | |||||
from swh.loader.package.loader import ( | from swh.loader.package.loader import ( | ||||
BasePackageInfo, | BasePackageInfo, | ||||
PackageLoader, | PackageLoader, | ||||
RawExtrinsicMetadataCore, | RawExtrinsicMetadataCore, | ||||
) | ) | ||||
from swh.loader.package.utils import api_info, cached_method, release_name | from swh.loader.package.utils import api_info, cached_method, release_name | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
MetadataAuthority, | MetadataAuthority, | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | def __init__(self, url: str): | ||||
package_name = url.split("https://www.npmjs.com/package/")[1] | package_name = url.split("https://www.npmjs.com/package/")[1] | ||||
safe_name = quote(package_name, safe="") | safe_name = quote(package_name, safe="") | ||||
self.provider_url = f"https://replicate.npmjs.com/{safe_name}/" | self.provider_url = f"https://replicate.npmjs.com/{safe_name}/" | ||||
self._info: Dict[str, Any] = {} | self._info: Dict[str, Any] = {} | ||||
self._versions = None | self._versions = None | ||||
@cached_method | @cached_method | ||||
def _raw_info(self) -> bytes: | def _raw_info(self) -> bytes: | ||||
try: | |||||
return api_info(self.provider_url) | return api_info(self.provider_url) | ||||
except ValueError as e: | |||||
ardumont: That behavior may need to be pushed to the `api_info` function directly to avoid repetition. | |||||
raise NotFound(e) | |||||
@cached_method | @cached_method | ||||
def info(self) -> Dict: | def info(self) -> Dict: | ||||
"""Return the project metadata information (fetched from npm registry) | """Return the project metadata information (fetched from npm registry) | ||||
""" | """ | ||||
return json.loads(self._raw_info()) | return json.loads(self._raw_info()) | ||||
▲ Show 20 Lines • Show All 227 Lines • Show Last 20 Lines |
That behavior may need to be pushed to the api_info function directly to avoid repetition.