Skip to content

Classes:

  • IriToFileResolver

    A urllib.request.OpenerDirector that remaps specific URLs to local files.

Functions:

IriToFileResolver

IriToFileResolver(url_map, download=True)

Bases: OpenerDirector

A urllib.request.OpenerDirector that remaps specific URLs to local files.

Parameters:

  • url_map (dict) –

    Mapping from a prefix of a URL to a local location. For example, { "http://example.org/": "foo/bar/" } would remap any urllib open request for any resource under http://example.org/ to a local directory foo/bar/.

  • download (bool, default: True ) –

    If true and the mapped local file does not exist, will attempt to download to the mapped location.

Source code in src/rdf_utils/resolver.py
31
32
33
34
35
36
def __init__(self, url_map: dict, download: bool = True):
    super().__init__()
    self.default_opener = urllib.request.build_opener()
    self.url_map = url_map
    self._download = download
    self._empty_header = EmailMessage()  # header expected by addinfourl

install_resolver

install_resolver(resolver=None, url_map=None, download=True)

Implements default behaviours for resolver installation

Parameters:

  • resolver (Optional[OpenerDirector], default: None ) –

    Resolver to install. If none specified, the default behaviour (using IriToFileResolver) is to download the requested files to the user cache directory using platformdirs. For Linux this should be $HOME/.cache/rdf-utils/.

  • url_map (Optional[dict], default: None ) –

    URL to local path mapping to pass to IriToFileResolver

  • download (bool, default: True ) –

    Download file if true

Note

Only a single opener can be globally installed in urllib. Only the latest installed resolver will be active.

Source code in src/rdf_utils/resolver.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def install_resolver(
    resolver: Optional[urllib.request.OpenerDirector] = None,
    url_map: Optional[dict] = None,
    download: bool = True,
) -> None:
    """Implements default behaviours for resolver installation

    Parameters:
        resolver: Resolver to install. If none specified, the default behaviour
                  (using `IriToFileResolver`) is to download the requested files to the
                  user cache directory using `platformdirs`.
                  For Linux this should be `$HOME/.cache/rdf-utils/`.
        url_map: URL to local path mapping to pass to `IriToFileResolver`
        download: Download file if true

    Note:
        Only a single opener can be globally installed in urllib.
        Only the latest installed resolver will be active.
    """
    if resolver is None:
        if url_map is None:
            url_map = {
                URL_SECORO: join(__PKG_CACHE_ROOT, "secoro"),
                URL_COMP_ROB2B: join(__PKG_CACHE_ROOT, "comp-rob2b"),
            }
        resolver = IriToFileResolver(url_map=url_map, download=download)

    urllib.request.install_opener(resolver)