Skip to content

Classes:

Functions:

IriToFileResolver

IriToFileResolver(url_map, download=True, quiet=False)

Bases: OpenerDirector

An 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.

  • quiet (bool, default: False ) –

    If False and download is True will print where the file will be downloaded to.

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

install_resolver

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

Implements default behaviours for resolver installation

Parameters:

  • resolver (OpenerDirector | None, 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 (dict | None, default: None ) –

    URL to local path mapping to pass to IriToFileResolver

  • download (bool, default: True ) –

    Download file if true

  • quiet (bool, default: False ) –

    Option for more verbose output, created for printing caching location in IriToFileResolver

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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def install_resolver(
    resolver: urllib.request.OpenerDirector | None = None,
    url_map: dict | None = None,
    download: bool = True,
    quiet: bool = False,
) -> None:
    """Implements default behaviours for resolver installation

    Parameters:
        resolver: Resolver to install. If none specified, the default behaviour
                  (using [`IriToFileResolver`](rdf_utils.resolver.IriToFileResolver)) is to
                  download the requested files to the user cache directory using
                  [`platformdirs`](https://github.com/tox-dev/platformdirs).
                  For Linux this should be `$HOME/.cache/rdf-utils/`.
        url_map: URL to local path mapping to pass to
                 [`IriToFileResolver`](rdf_utils.resolver.IriToFileResolver)
        download: Download file if true
        quiet: Option for more verbose output, created for printing caching location in
               [`IriToFileResolver`](rdf_utils.resolver.IriToFileResolver)

    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, quiet=quiet)

    urllib.request.install_opener(resolver)