Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-39938: Replace pkg_resources with importlib.resources #35

Merged
merged 5 commits into from
Sep 11, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 66 additions & 8 deletions python/lsst/alert/packet/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,46 @@

import io
import os.path
import pkg_resources
from importlib import resources
from pathlib import PurePath

import fastavro

__all__ = ["get_schema_root", "get_latest_schema_version", "get_schema_path",
"Schema", "get_path_to_latest_schema"]
"Schema", "get_path_to_latest_schema", "get_schema_root_uri",
"get_uri_to_latest_schema", "get_schema_uri"]


def _get_ref(*args):
"""Return the package resource file path object.

Parameters are relative to lsst.alert.packet.
"""
return resources.files("lsst.alert.packet").joinpath(*args)


def _get_uri_str(*args):
"""Return the package resource in the form of a URI.

This URI is suitable for use by `lsst.resources.ResourcePath`
and uses the ``resource`` URI scheme.
"""
return "resource://lsst.alert.packet/" + "/".join(args)


def get_schema_root():
"""Return the root of the directory within which schemas are stored.

This might be a temporary location if a zip distribution file is used.
"""
return pkg_resources.resource_filename(__name__, "schema")
return _get_ref("schema")


def get_schema_root_uri():
"""Return the ``resource`` URI corresponding to the location where
schemas are stored."""
# Add trailing / to indicate that we know this is a directory.
return _get_uri_str("schema") + "/"


def get_latest_schema_version():
Expand All @@ -50,7 +77,8 @@ def get_latest_schema_version():
The minor version number.

"""
val = pkg_resources.resource_string(__name__, "schema/latest.txt")
with _get_ref("schema", "latest.txt").open("rb") as fh:
val = fh.read()
clean = val.strip()
major, minor = clean.split(b".", 1)
return int(major), int(minor)
Expand All @@ -73,11 +101,28 @@ def get_schema_path(major, minor):
Path to the directory containing the schemas.

"""
return _get_ref("schema", str(major), str(minor)).as_posix()

# Note that as_posix() is right here, since pkg_resources
# always uses slash-delimited paths, even on Windows.
path = PurePath(f"schema/{major}/{minor}/")
return pkg_resources.resource_filename(__name__, path.as_posix())

def get_schema_uri(major, minor):
"""Get the URI to a package resource directory housing alert schema
definitions.

Parameters
----------
major : `int`
Major version number for the schema.
minor : `int`
Minor version number for the schema.

Returns
-------
uri : `str`
``resource`` URI to the directory containing the schemas.
"""
# Add trailing / to indicate that we know this URI refers
# to a directory.
return _get_uri_str("schema", str(major), str(minor)) + "/"


def get_path_to_latest_schema():
Expand All @@ -94,6 +139,19 @@ def get_path_to_latest_schema():
return (schema_path / f"lsst.v{major}_{minor}.alert.avsc").as_posix()


def get_uri_to_latest_schema():
"""Get the URI to to the primary file for the latest schema.

Returns
-------
uri : `str`
The ``resource`` URI to the latest schema.
"""
major, minor = get_latest_schema_version()
schema_uri = get_schema_uri(major, minor)
return schema_uri + f"lsst.v{major}_{minor}.alert.avsc"


def resolve_schema_definition(to_resolve, seen_names=None):
"""Fully resolve complex types within a schema definition.

Expand Down