Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/pubdev/loader.py
# Copyright (C) 2022 The Software Heritage developers | # Copyright (C) 2022 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 | ||||
import json | import json | ||||
from pathlib import Path | from pathlib import Path | ||||
from typing import Any, Dict, Iterator, Optional, Sequence, Tuple | from typing import Any, Dict, Iterator, Optional, Sequence, Tuple | ||||
import attr | import attr | ||||
from packaging.version import parse as parse_version | from packaging.version import parse as parse_version | ||||
import yaml | import yaml | ||||
from swh.loader.package.loader import BasePackageInfo, PackageLoader | from swh.loader.package.loader import BasePackageInfo, PackageLoader | ||||
from swh.loader.package.utils import ( | from swh.loader.package.utils import ( | ||||
EMPTY_AUTHOR, | EMPTY_AUTHOR, | ||||
Person, | Person, | ||||
api_info, | |||||
cached_method, | cached_method, | ||||
get_url_body, | |||||
release_name, | release_name, | ||||
) | ) | ||||
from swh.model.model import ObjectType, Release, Sha1Git, TimestampWithTimezone | from swh.model.model import ObjectType, Release, Sha1Git, TimestampWithTimezone | ||||
from swh.storage.interface import StorageInterface | from swh.storage.interface import StorageInterface | ||||
@attr.s | @attr.s | ||||
class PubDevPackageInfo(BasePackageInfo): | class PubDevPackageInfo(BasePackageInfo): | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | ): | ||||
super().__init__(storage=storage, url=url, **kwargs) | super().__init__(storage=storage, url=url, **kwargs) | ||||
self.url = url | self.url = url | ||||
assert url.startswith(self.PUBDEV_BASE_URL) | assert url.startswith(self.PUBDEV_BASE_URL) | ||||
self.package_info_url = url.replace( | self.package_info_url = url.replace( | ||||
self.PUBDEV_BASE_URL, f"{self.PUBDEV_BASE_URL}api/" | self.PUBDEV_BASE_URL, f"{self.PUBDEV_BASE_URL}api/" | ||||
) | ) | ||||
def _raw_info(self) -> bytes: | def _raw_info(self) -> bytes: | ||||
return api_info(self.package_info_url) | return get_url_body(self.package_info_url) | ||||
@cached_method | @cached_method | ||||
def info(self) -> Dict: | def info(self) -> Dict: | ||||
"""Return the project metadata information (fetched from pub.dev registry)""" | """Return the project metadata information (fetched from pub.dev registry)""" | ||||
# Use strict=False in order to correctly manage case where \n is present in a string | # Use strict=False in order to correctly manage case where \n is present in a string | ||||
info = json.loads(self._raw_info(), strict=False) | info = json.loads(self._raw_info(), strict=False) | ||||
# Arrange versions list as a new dict with `version` as key | # Arrange versions list as a new dict with `version` as key | ||||
versions = {v["version"]: v for v in info["versions"]} | versions = {v["version"]: v for v in info["versions"]} | ||||
▲ Show 20 Lines • Show All 105 Lines • Show Last 20 Lines |