pax_global_header00006660000000000000000000000064145042236140014513gustar00rootroot0000000000000052 comment=d16194031f31067592f00fdd19ac8e1662a2bb56 catalogue-2.0.10/000077500000000000000000000000001450422361400135375ustar00rootroot00000000000000catalogue-2.0.10/.github/000077500000000000000000000000001450422361400150775ustar00rootroot00000000000000catalogue-2.0.10/.github/workflows/000077500000000000000000000000001450422361400171345ustar00rootroot00000000000000catalogue-2.0.10/.github/workflows/tests.yml000066400000000000000000000034741450422361400210310ustar00rootroot00000000000000name: tests on: push: paths-ignore: - "*.md" pull_request: types: [opened, synchronize, reopened, edited] paths-ignore: - "*.md" env: MODULE_NAME: 'catalogue' RUN_MYPY: 'true' jobs: tests: name: Test if: github.repository_owner == 'explosion' strategy: fail-fast: false matrix: os: [ubuntu-latest, windows-latest, macos-latest] python_version: ["3.8", "3.9", "3.10", "3.11", "3.12.0-rc.2"] runs-on: ${{ matrix.os }} steps: - name: Check out repo uses: actions/checkout@v3 - name: Configure Python version uses: actions/setup-python@v4 with: python-version: ${{ matrix.python_version }} architecture: x64 - name: Build sdist run: | python -m pip install -U build pip setuptools python -m pip install -U -r requirements.txt python -m build --sdist - name: Run mypy shell: bash if: ${{ env.RUN_MYPY == 'true' }} run: | python -m mypy $MODULE_NAME - name: Delete source directory shell: bash run: | rm -rf $MODULE_NAME - name: Uninstall all packages run: | python -m pip freeze > installed.txt python -m pip uninstall -y -r installed.txt - name: Install from sdist shell: bash run: | SDIST=$(python -c "import os;print(os.listdir('./dist')[-1])" 2>&1) pip install dist/$SDIST - name: Test import shell: bash run: | python -c "import $MODULE_NAME" -Werror - name: Install test requirements run: | python -m pip install -U -r requirements.txt - name: Run tests shell: bash run: | python -m pytest --pyargs $MODULE_NAME -Werror catalogue-2.0.10/.gitignore000066400000000000000000000015061450422361400155310ustar00rootroot00000000000000tmp/ .pytest_cache .vscode .mypy_cache .prettierrc .python-version # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python .env/ env/ build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ *.egg-info/ .installed.cfg *.egg # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *,cover .hypothesis/ # Translations *.mo *.pot # Django stuff: *.log # Sphinx documentation docs/_build/ # PyBuilder target/ #Ipython Notebook .ipynb_checkpoints catalogue-2.0.10/LICENSE000066400000000000000000000020611450422361400145430ustar00rootroot00000000000000MIT License Copyright (c) 2019 ExplosionAI GmbH Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. catalogue-2.0.10/MANIFEST.in000066400000000000000000000000201450422361400152650ustar00rootroot00000000000000include LICENSE catalogue-2.0.10/README.md000066400000000000000000000310631450422361400150210ustar00rootroot00000000000000 # catalogue: Super lightweight function registries for your library `catalogue` is a tiny, zero-dependencies library that makes it easy to **add function (or object) registries** to your code. Function registries are helpful when you have objects that need to be both easily serializable and fully customizable. Instead of passing a function into your object, you pass in an identifier name, which the object can use to lookup the function from the registry. This makes the object easy to serialize, because the name is a simple string. If you instead saved the function, you'd have to use Pickle for serialization, which has many drawbacks. [![tests](https://github.com/explosion/catalogue/actions/workflows/tests.yml/badge.svg)](https://github.com/explosion/catalogue/actions/workflows/tests.yml) [![Current Release Version](https://img.shields.io/github/v/release/explosion/catalogue.svg?style=flat-square&include_prereleases&logo=github)](https://github.com/explosion/catalogue/releases) [![pypi Version](https://img.shields.io/pypi/v/catalogue.svg?style=flat-square&logo=pypi&logoColor=white)](https://pypi.org/project/catalogue/) [![conda Version](https://img.shields.io/conda/vn/conda-forge/catalogue.svg?style=flat-square&logo=conda-forge&logoColor=white)](https://anaconda.org/conda-forge/catalogue) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square)](https://github.com/ambv/black) ## ⏳ Installation ```bash pip install catalogue ``` ```bash conda install -c conda-forge catalogue ``` > ⚠️ **Important note:** `catalogue` v2.0+ is only compatible with Python 3.6+. > For Python 2.7+ compatibility, use `catalogue` v1.x. ## 👩‍💻 Usage Let's imagine you're developing a Python package that needs to load data somewhere. You've already implemented some loader functions for the most common data types, but you want to allow the user to easily add their own. Using `catalogue.create` you can create a new registry under the namespace `your_package` → `loaders`. ```python # YOUR PACKAGE import catalogue loaders = catalogue.create("your_package", "loaders") ``` This gives you a `loaders.register` decorator that your users can import and decorate their custom loader functions with. ```python # USER CODE from your_package import loaders @loaders.register("custom_loader") def custom_loader(data): # Load something here... return data ``` The decorated function will be registered automatically and in your package, you'll be able to access all loaders by calling `loaders.get_all`. ```python # YOUR PACKAGE def load_data(data, loader_id): print("All loaders:", loaders.get_all()) # {"custom_loader": } loader = loaders.get(loader_id) return loader(data) ``` The user can now refer to their custom loader using only its string name (`"custom_loader"`) and your application will know what to do and will use their custom function. ```python # USER CODE from your_package import load_data load_data(data, loader_id="custom_loader") ``` ## ❓ FAQ #### But can't the user just pass in the `custom_loader` function directly? Sure, that's the more classic callback approach. Instead of a string ID, `load_data` could also take a function, in which case you wouldn't need a package like this. `catalogue` helps you when you need to produce a serializable record of which functions were passed in. For instance, you might want to write a log message, or save a config to load back your object later. With `catalogue`, your functions can be parameterized by strings, so logging and serialization remains easy – while still giving you full extensibility. #### How do I make sure all of the registration decorators have run? Decorators normally run when modules are imported. Relying on this side-effect can sometimes lead to confusion, especially if there's no other reason the module would be imported. One solution is to use [entry points](https://packaging.python.org/specifications/entry-points/). For instance, in [spaCy](https://spacy.io) we're starting to use function registries to make the pipeline components much more customizable. Let's say one user, Jo, develops a better tagging model using new machine learning research. End-users of Jo's package should be able to write `spacy.load("jo_tagging_model")`. They shouldn't need to remember to write `import jos_tagged_model` first, just to run the function registries as a side-effect. With entry points, the registration happens at install time – so you don't need to rely on the import side-effects. ## 🎛 API ### function `catalogue.create` Create a new registry for a given namespace. Returns a setter function that can be used as a decorator or called with a name and `func` keyword argument. If `entry_points=True` is set, the registry will check for [Python entry points](https://packaging.python.org/tutorials/packaging-projects/#entry-points) advertised for the given namespace, e.g. the entry point group `spacy_architectures` for the namespace `"spacy", "architectures"`, in `Registry.get` and `Registry.get_all`. This allows other packages to auto-register functions. | Argument | Type | Description | | -------------- | ---------- | ---------------------------------------------------------------------------------------------- | | `*namespace` | str | The namespace, e.g. `"spacy"` or `"spacy", "architectures"`. | | `entry_points` | bool | Whether to check for entry points of the given namespace and pre-populate the global registry. | | **RETURNS** | `Registry` | The `Registry` object with methods to register and retrieve functions. | ```python architectures = catalogue.create("spacy", "architectures") # Use as decorator @architectures.register("custom_architecture") def custom_architecture(): pass # Use as regular function architectures.register("custom_architecture", func=custom_architecture) ``` ### class `Registry` The registry object that can be used to register and retrieve functions. It's usually created internally when you call `catalogue.create`. #### method `Registry.__init__` Initialize a new registry. If `entry_points=True` is set, the registry will check for [Python entry points](https://packaging.python.org/tutorials/packaging-projects/#entry-points) advertised for the given namespace, e.g. the entry point group `spacy_architectures` for the namespace `"spacy", "architectures"`, in `Registry.get` and `Registry.get_all`. | Argument | Type | Description | | -------------- | ---------- | -------------------------------------------------------------------------------- | | `namespace` | Tuple[str] | The namespace, e.g. `"spacy"` or `"spacy", "architectures"`. | | `entry_points` | bool | Whether to check for entry points of the given namespace in `get` and `get_all`. | | **RETURNS** | `Registry` | The newly created object. | ```python # User-facing API architectures = catalogue.create("spacy", "architectures") # Internal API architectures = Registry(("spacy", "architectures")) ``` #### method `Registry.__contains__` Check whether a name is in the registry. | Argument | Type | Description | | ----------- | ---- | ------------------------------------ | | `name` | str | The name to check. | | **RETURNS** | bool | Whether the name is in the registry. | ```python architectures = catalogue.create("spacy", "architectures") @architectures.register("custom_architecture") def custom_architecture(): pass assert "custom_architecture" in architectures ``` #### method `Registry.__call__` Register a function in the registry's namespace. Can be used as a decorator or called as a function with the `func` keyword argument supplying the function to register. Delegates to `Registry.register`. #### method `Registry.register` Register a function in the registry's namespace. Can be used as a decorator or called as a function with the `func` keyword argument supplying the function to register. | Argument | Type | Description | | ----------- | -------- | --------------------------------------------------------- | | `name` | str | The name to register under the namespace. | | `func` | Any | Optional function to register (if not used as decorator). | | **RETURNS** | Callable | The decorator that takes one argument, the name. | ```python architectures = catalogue.create("spacy", "architectures") # Use as decorator @architectures.register("custom_architecture") def custom_architecture(): pass # Use as regular function architectures.register("custom_architecture", func=custom_architecture) ``` #### method `Registry.get` Get a function registered in the namespace. | Argument | Type | Description | | ----------- | ---- | ------------------------ | | `name` | str | The name. | | **RETURNS** | Any | The registered function. | ```python custom_architecture = architectures.get("custom_architecture") ``` #### method `Registry.get_all` Get all functions in the registry's namespace. | Argument | Type | Description | | ----------- | -------------- | ---------------------------------------- | | **RETURNS** | Dict[str, Any] | The registered functions, keyed by name. | ```python all_architectures = architectures.get_all() # {"custom_architecture": } ``` #### method `Registry.get_entry_points` Get registered entry points from other packages for this namespace. The name of the entry point group is the namespace joined by `_`. | Argument | Type | Description | | ----------- | -------------- | --------------------------------------- | | **RETURNS** | Dict[str, Any] | The loaded entry points, keyed by name. | ```python architectures = catalogue.create("spacy", "architectures", entry_points=True) # Will get all entry points of the group "spacy_architectures" all_entry_points = architectures.get_entry_points() ``` #### method `Registry.get_entry_point` Check if registered entry point is available for a given name in the namespace and load it. Otherwise, return the default value. | Argument | Type | Description | | ----------- | ---- | ------------------------------------------------ | | `name` | str | Name of entry point to load. | | `default` | Any | The default value to return. Defaults to `None`. | | **RETURNS** | Any | The loaded entry point or the default value. | ```python architectures = catalogue.create("spacy", "architectures", entry_points=True) # Will get entry point "custom_architecture" of the group "spacy_architectures" custom_architecture = architectures.get_entry_point("custom_architecture") ``` #### method `Registry.find` Find the information about a registered function, including the module and path to the file it's defined in, the line number and the docstring, if available. | Argument | Type | Description | | ----------- | -------------------------- | ----------------------------------- | | `name` | str | Name of the registered function. | | **RETURNS** | Dict[str, Union[str, int]] | The information about the function. | ```python import catalogue architectures = catalogue.create("spacy", "architectures", entry_points=True) @architectures("my_architecture") def my_architecture(): """This is an architecture""" pass info = architectures.find("my_architecture") # {'module': 'your_package.architectures', # 'file': '/path/to/your_package/architectures.py', # 'line_no': 5, # 'docstring': 'This is an architecture'} ``` ### function `catalogue.check_exists` Check if a namespace exists. | Argument | Type | Description | | ------------ | ---- | ------------------------------------------------------------ | | `*namespace` | str | The namespace, e.g. `"spacy"` or `"spacy", "architectures"`. | | **RETURNS** | bool | Whether the namespace exists. | catalogue-2.0.10/bin/000077500000000000000000000000001450422361400143075ustar00rootroot00000000000000catalogue-2.0.10/bin/push-tags.sh000077500000000000000000000005371450422361400165660ustar00rootroot00000000000000#!/usr/bin/env bash set -e # Insist repository is clean git diff-index --quiet HEAD git checkout $1 git pull origin $1 git push origin $1 version=$(grep "version = " setup.cfg) version=${version/version = } version=${version/\'/} version=${version/\'/} version=${version/\"/} version=${version/\"/} git tag "v$version" git push origin "v$version"catalogue-2.0.10/catalogue/000077500000000000000000000000001450422361400155035ustar00rootroot00000000000000catalogue-2.0.10/catalogue/__init__.py000066400000000000000000000206641450422361400176240ustar00rootroot00000000000000from typing import Sequence, Any, Dict, Tuple, Callable, Optional, TypeVar, Union from typing import List import inspect try: # Python 3.8 import importlib.metadata as importlib_metadata except ImportError: from . import _importlib_metadata as importlib_metadata # type: ignore # Only ever call this once for performance reasons AVAILABLE_ENTRY_POINTS = importlib_metadata.entry_points() # type: ignore # This is where functions will be registered REGISTRY: Dict[Tuple[str, ...], Any] = {} InFunc = TypeVar("InFunc") def create(*namespace: str, entry_points: bool = False) -> "Registry": """Create a new registry. *namespace (str): The namespace, e.g. "spacy" or "spacy", "architectures". entry_points (bool): Accept registered functions from entry points. RETURNS (Registry): The Registry object. """ if check_exists(*namespace): raise RegistryError(f"Namespace already exists: {namespace}") return Registry(namespace, entry_points=entry_points) class Registry(object): def __init__(self, namespace: Sequence[str], entry_points: bool = False) -> None: """Initialize a new registry. namespace (Sequence[str]): The namespace. entry_points (bool): Whether to also check for entry points. """ self.namespace = namespace self.entry_point_namespace = "_".join(namespace) self.entry_points = entry_points def __contains__(self, name: str) -> bool: """Check whether a name is in the registry. name (str): The name to check. RETURNS (bool): Whether the name is in the registry. """ namespace = tuple(list(self.namespace) + [name]) has_entry_point = self.entry_points and self.get_entry_point(name) return has_entry_point or namespace in REGISTRY def __call__( self, name: str, func: Optional[Any] = None ) -> Callable[[InFunc], InFunc]: """Register a function for a given namespace. Same as Registry.register. name (str): The name to register under the namespace. func (Any): Optional function to register (if not used as decorator). RETURNS (Callable): The decorator. """ return self.register(name, func=func) def register( self, name: str, *, func: Optional[Any] = None ) -> Callable[[InFunc], InFunc]: """Register a function for a given namespace. name (str): The name to register under the namespace. func (Any): Optional function to register (if not used as decorator). RETURNS (Callable): The decorator. """ def do_registration(func): _set(list(self.namespace) + [name], func) return func if func is not None: return do_registration(func) return do_registration def get(self, name: str) -> Any: """Get the registered function for a given name. name (str): The name. RETURNS (Any): The registered function. """ if self.entry_points: from_entry_point = self.get_entry_point(name) if from_entry_point: return from_entry_point namespace = list(self.namespace) + [name] if not check_exists(*namespace): current_namespace = " -> ".join(self.namespace) available = ", ".join(sorted(self.get_all().keys())) or "none" raise RegistryError( f"Cant't find '{name}' in registry {current_namespace}. Available names: {available}" ) return _get(namespace) def get_all(self) -> Dict[str, Any]: """Get a all functions for a given namespace. namespace (Tuple[str]): The namespace to get. RETURNS (Dict[str, Any]): The functions, keyed by name. """ global REGISTRY result = {} if self.entry_points: result.update(self.get_entry_points()) for keys, value in REGISTRY.copy().items(): if len(self.namespace) == len(keys) - 1 and all( self.namespace[i] == keys[i] for i in range(len(self.namespace)) ): result[keys[-1]] = value return result def get_entry_points(self) -> Dict[str, Any]: """Get registered entry points from other packages for this namespace. RETURNS (Dict[str, Any]): Entry points, keyed by name. """ result = {} for entry_point in self._get_entry_points(): result[entry_point.name] = entry_point.load() return result def get_entry_point(self, name: str, default: Optional[Any] = None) -> Any: """Check if registered entry point is available for a given name in the namespace and load it. Otherwise, return the default value. name (str): Name of entry point to load. default (Any): The default value to return. RETURNS (Any): The loaded entry point or the default value. """ for entry_point in self._get_entry_points(): if entry_point.name == name: return entry_point.load() return default def _get_entry_points(self) -> List[importlib_metadata.EntryPoint]: if hasattr(AVAILABLE_ENTRY_POINTS, "select"): return AVAILABLE_ENTRY_POINTS.select(group=self.entry_point_namespace) else: # dict return AVAILABLE_ENTRY_POINTS.get(self.entry_point_namespace, []) def find(self, name: str) -> Dict[str, Optional[Union[str, int]]]: """Find the information about a registered function, including the module and path to the file it's defined in, the line number and the docstring, if available. name (str): Name of the registered function. RETURNS (Dict[str, Optional[Union[str, int]]]): The function info. """ func = self.get(name) module = inspect.getmodule(func) # These calls will fail for Cython modules so we need to work around them line_no: Optional[int] = None file_name: Optional[str] = None try: _, line_no = inspect.getsourcelines(func) file_name = inspect.getfile(func) except (TypeError, ValueError): pass docstring = inspect.getdoc(func) return { "module": module.__name__ if module else None, "file": file_name, "line_no": line_no, "docstring": inspect.cleandoc(docstring) if docstring else None, } def check_exists(*namespace: str) -> bool: """Check if a namespace exists. *namespace (str): The namespace. RETURNS (bool): Whether the namespace exists. """ return namespace in REGISTRY def _get(namespace: Sequence[str]) -> Any: """Get the value for a given namespace. namespace (Sequence[str]): The namespace. RETURNS (Any): The value for the namespace. """ global REGISTRY if not all(isinstance(name, str) for name in namespace): raise ValueError( f"Invalid namespace. Expected tuple of strings, but got: {namespace}" ) namespace = tuple(namespace) if namespace not in REGISTRY: raise RegistryError(f"Can't get namespace {namespace} (not in registry)") return REGISTRY[namespace] def _get_all(namespace: Sequence[str]) -> Dict[Tuple[str, ...], Any]: """Get all matches for a given namespace, e.g. ("a", "b", "c") and ("a", "b") for namespace ("a", "b"). namespace (Sequence[str]): The namespace. RETURNS (Dict[Tuple[str], Any]): All entries for the namespace, keyed by their full namespaces. """ global REGISTRY result = {} for keys, value in REGISTRY.copy().items(): if len(namespace) <= len(keys) and all( namespace[i] == keys[i] for i in range(len(namespace)) ): result[keys] = value return result def _set(namespace: Sequence[str], func: Any) -> None: """Set a value for a given namespace. namespace (Sequence[str]): The namespace. func (Callable): The value to set. """ global REGISTRY REGISTRY[tuple(namespace)] = func def _remove(namespace: Sequence[str]) -> Any: """Remove a value for a given namespace. namespace (Sequence[str]): The namespace. RETURNS (Any): The removed value. """ global REGISTRY namespace = tuple(namespace) if namespace not in REGISTRY: raise RegistryError(f"Can't get namespace {namespace} (not in registry)") removed = REGISTRY[namespace] del REGISTRY[namespace] return removed class RegistryError(ValueError): pass catalogue-2.0.10/catalogue/_importlib_metadata/000077500000000000000000000000001450422361400215035ustar00rootroot00000000000000catalogue-2.0.10/catalogue/_importlib_metadata/LICENSE000066400000000000000000000010731450422361400225110ustar00rootroot00000000000000Copyright 2017-2019 Jason R. Coombs, Barry Warsaw Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. catalogue-2.0.10/catalogue/_importlib_metadata/__init__.py000066400000000000000000000473541450422361400236310ustar00rootroot00000000000000import os import re import abc import csv import sys import zipp import email import pathlib import operator import functools import itertools import posixpath import collections from ._compat import ( NullFinder, PyPy_repr, install, Protocol, ) from configparser import ConfigParser from contextlib import suppress from importlib import import_module from importlib.abc import MetaPathFinder from itertools import starmap from typing import Any, List, Mapping, TypeVar, Union __all__ = [ 'Distribution', 'DistributionFinder', 'PackageNotFoundError', 'distribution', 'distributions', 'entry_points', 'files', 'metadata', 'requires', 'version', ] class PackageNotFoundError(ModuleNotFoundError): """The package was not found.""" def __str__(self): tmpl = "No package metadata was found for {self.name}" return tmpl.format(**locals()) @property def name(self): (name,) = self.args return name class EntryPoint( PyPy_repr, collections.namedtuple('EntryPointBase', 'name value group') ): """An entry point as defined by Python packaging conventions. See `the packaging docs on entry points `_ for more information. """ pattern = re.compile( r'(?P[\w.]+)\s*' r'(:\s*(?P[\w.]+))?\s*' r'(?P\[.*\])?\s*$' ) """ A regular expression describing the syntax for an entry point, which might look like: - module - package.module - package.module:attribute - package.module:object.attribute - package.module:attr [extra1, extra2] Other combinations are possible as well. The expression is lenient about whitespace around the ':', following the attr, and following any extras. """ def load(self): """Load the entry point from its definition. If only a module is indicated by the value, return that module. Otherwise, return the named object. """ match = self.pattern.match(self.value) module = import_module(match.group('module')) attrs = filter(None, (match.group('attr') or '').split('.')) return functools.reduce(getattr, attrs, module) @property def module(self): match = self.pattern.match(self.value) return match.group('module') @property def attr(self): match = self.pattern.match(self.value) return match.group('attr') @property def extras(self): match = self.pattern.match(self.value) return list(re.finditer(r'\w+', match.group('extras') or '')) @classmethod def _from_config(cls, config): return [ cls(name, value, group) for group in config.sections() for name, value in config.items(group) ] @classmethod def _from_text(cls, text): config = ConfigParser(delimiters='=') # case sensitive: https://stackoverflow.com/q/1611799/812183 config.optionxform = str config.read_string(text) return EntryPoint._from_config(config) def __iter__(self): """ Supply iter so one may construct dicts of EntryPoints easily. """ return iter((self.name, self)) def __reduce__(self): return ( self.__class__, (self.name, self.value, self.group), ) class PackagePath(pathlib.PurePosixPath): """A reference to a path in a package""" def read_text(self, encoding='utf-8'): with self.locate().open(encoding=encoding) as stream: return stream.read() def read_binary(self): with self.locate().open('rb') as stream: return stream.read() def locate(self): """Return a path-like object for this path""" return self.dist.locate_file(self) class FileHash: def __init__(self, spec): self.mode, _, self.value = spec.partition('=') def __repr__(self): return ''.format(self.mode, self.value) _T = TypeVar("_T") class PackageMetadata(Protocol): def __len__(self) -> int: ... # pragma: no cover def __contains__(self, item: str) -> bool: ... # pragma: no cover def __getitem__(self, key: str) -> str: ... # pragma: no cover def get_all(self, name: str, failobj: _T = ...) -> Union[List[Any], _T]: """ Return all values associated with a possibly multi-valued key. """ class Distribution: """A Python distribution package.""" @abc.abstractmethod def read_text(self, filename): """Attempt to load metadata file given by the name. :param filename: The name of the file in the distribution info. :return: The text if found, otherwise None. """ @abc.abstractmethod def locate_file(self, path): """ Given a path to a file in this distribution, return a path to it. """ @classmethod def from_name(cls, name): """Return the Distribution for the given package name. :param name: The name of the distribution package to search for. :return: The Distribution instance (or subclass thereof) for the named package, if found. :raises PackageNotFoundError: When the named package's distribution metadata cannot be found. """ for resolver in cls._discover_resolvers(): dists = resolver(DistributionFinder.Context(name=name)) dist = next(iter(dists), None) if dist is not None: return dist else: raise PackageNotFoundError(name) @classmethod def discover(cls, **kwargs): """Return an iterable of Distribution objects for all packages. Pass a ``context`` or pass keyword arguments for constructing a context. :context: A ``DistributionFinder.Context`` object. :return: Iterable of Distribution objects for all packages. """ context = kwargs.pop('context', None) if context and kwargs: raise ValueError("cannot accept context and kwargs") context = context or DistributionFinder.Context(**kwargs) return itertools.chain.from_iterable( resolver(context) for resolver in cls._discover_resolvers() ) @staticmethod def at(path): """Return a Distribution for the indicated metadata path :param path: a string or path-like object :return: a concrete Distribution instance for the path """ return PathDistribution(pathlib.Path(path)) @staticmethod def _discover_resolvers(): """Search the meta_path for resolvers.""" declared = ( getattr(finder, '_catalogue_find_distributions', None) for finder in sys.meta_path ) return filter(None, declared) @classmethod def _local(cls, root='.'): from pep517 import build, meta system = build.compat_system(root) builder = functools.partial( meta.build, source_dir=root, system=system, ) return PathDistribution(zipp.Path(meta.build_as_zip(builder))) @property def metadata(self) -> PackageMetadata: """Return the parsed metadata for this Distribution. The returned object will have keys that name the various bits of metadata. See PEP 566 for details. """ text = ( self.read_text('METADATA') or self.read_text('PKG-INFO') # This last clause is here to support old egg-info files. Its # effect is to just end up using the PathDistribution's self._path # (which points to the egg-info file) attribute unchanged. or self.read_text('') ) return email.message_from_string(text) @property def version(self): """Return the 'Version' metadata for the distribution package.""" return self.metadata['Version'] @property def entry_points(self): return EntryPoint._from_text(self.read_text('entry_points.txt')) @property def files(self): """Files in this distribution. :return: List of PackagePath for this distribution or None Result is `None` if the metadata file that enumerates files (i.e. RECORD for dist-info or SOURCES.txt for egg-info) is missing. Result may be empty if the metadata exists but is empty. """ file_lines = self._read_files_distinfo() or self._read_files_egginfo() def make_file(name, hash=None, size_str=None): result = PackagePath(name) result.hash = FileHash(hash) if hash else None result.size = int(size_str) if size_str else None result.dist = self return result return file_lines and list(starmap(make_file, csv.reader(file_lines))) def _read_files_distinfo(self): """ Read the lines of RECORD """ text = self.read_text('RECORD') return text and text.splitlines() def _read_files_egginfo(self): """ SOURCES.txt might contain literal commas, so wrap each line in quotes. """ text = self.read_text('SOURCES.txt') return text and map('"{}"'.format, text.splitlines()) @property def requires(self): """Generated requirements specified for this Distribution""" reqs = self._read_dist_info_reqs() or self._read_egg_info_reqs() return reqs and list(reqs) def _read_dist_info_reqs(self): return self.metadata.get_all('Requires-Dist') def _read_egg_info_reqs(self): source = self.read_text('requires.txt') return source and self._deps_from_requires_text(source) @classmethod def _deps_from_requires_text(cls, source): section_pairs = cls._read_sections(source.splitlines()) sections = { section: list(map(operator.itemgetter('line'), results)) for section, results in itertools.groupby( section_pairs, operator.itemgetter('section') ) } return cls._convert_egg_info_reqs_to_simple_reqs(sections) @staticmethod def _read_sections(lines): section = None for line in filter(None, lines): section_match = re.match(r'\[(.*)\]$', line) if section_match: section = section_match.group(1) continue yield locals() @staticmethod def _convert_egg_info_reqs_to_simple_reqs(sections): """ Historically, setuptools would solicit and store 'extra' requirements, including those with environment markers, in separate sections. More modern tools expect each dependency to be defined separately, with any relevant extras and environment markers attached directly to that requirement. This method converts the former to the latter. See _test_deps_from_requires_text for an example. """ def make_condition(name): return name and 'extra == "{name}"'.format(name=name) def parse_condition(section): section = section or '' extra, sep, markers = section.partition(':') if extra and markers: markers = '({markers})'.format(markers=markers) conditions = list(filter(None, [markers, make_condition(extra)])) return '; ' + ' and '.join(conditions) if conditions else '' for section, deps in sections.items(): for dep in deps: yield dep + parse_condition(section) class DistributionFinder(MetaPathFinder): """ A MetaPathFinder capable of discovering installed distributions. """ class Context: """ Keyword arguments presented by the caller to ``distributions()`` or ``Distribution.discover()`` to narrow the scope of a search for distributions in all DistributionFinders. Each DistributionFinder may expect any parameters and should attempt to honor the canonical parameters defined below when appropriate. """ name = None """ Specific name for which a distribution finder should match. A name of ``None`` matches all distributions. """ def __init__(self, **kwargs): vars(self).update(kwargs) @property def path(self): """ The path that a distribution finder should search. Typically refers to Python package paths and defaults to ``sys.path``. """ return vars(self).get('path', sys.path) @abc.abstractmethod def _catalogue_find_distributions(self, context=Context()): """ Find distributions. Return an iterable of all Distribution instances capable of loading the metadata for packages matching the ``context``, a DistributionFinder.Context instance. """ class FastPath: """ Micro-optimized class for searching a path for children. """ def __init__(self, root): self.root = str(root) self.base = os.path.basename(self.root).lower() def joinpath(self, child): return pathlib.Path(self.root, child) def children(self): with suppress(Exception): return os.listdir(self.root or '') with suppress(Exception): return self.zip_children() return [] def zip_children(self): zip_path = zipp.Path(self.root) names = zip_path.root.namelist() self.joinpath = zip_path.joinpath return dict.fromkeys(child.split(posixpath.sep, 1)[0] for child in names) def search(self, name): return ( self.joinpath(child) for child in self.children() if name.matches(child, self.base) ) class Prepared: """ A prepared search for metadata on a possibly-named package. """ normalized = None suffixes = '.dist-info', '.egg-info' exact_matches = [''][:0] def __init__(self, name): self.name = name if name is None: return self.normalized = self.normalize(name) self.exact_matches = [self.normalized + suffix for suffix in self.suffixes] @staticmethod def normalize(name): """ PEP 503 normalization plus dashes as underscores. """ return re.sub(r"[-_.]+", "-", name).lower().replace('-', '_') @staticmethod def legacy_normalize(name): """ Normalize the package name as found in the convention in older packaging tools versions and specs. """ return name.lower().replace('-', '_') def matches(self, cand, base): low = cand.lower() pre, ext = os.path.splitext(low) name, sep, rest = pre.partition('-') return ( low in self.exact_matches or ext in self.suffixes and (not self.normalized or name.replace('.', '_') == self.normalized) # legacy case: or self.is_egg(base) and low == 'egg-info' ) def is_egg(self, base): normalized = self.legacy_normalize(self.name or '') prefix = normalized + '-' if normalized else '' versionless_egg_name = normalized + '.egg' if self.name else '' return ( base == versionless_egg_name or base.startswith(prefix) and base.endswith('.egg') ) @install class MetadataPathFinder(NullFinder, DistributionFinder): """A degenerate finder for distribution packages on the file system. This finder supplies only a find_distributions() method for versions of Python that do not have a PathFinder find_distributions(). """ def _catalogue_find_distributions(self, context=DistributionFinder.Context()): """ Find distributions. Return an iterable of all Distribution instances capable of loading the metadata for packages matching ``context.name`` (or all names if ``None`` indicated) along the paths in the list of directories ``context.path``. """ found = self._search_paths(context.name, context.path) return map(PathDistribution, found) @classmethod def _search_paths(cls, name, paths): """Find metadata directories in paths heuristically.""" return itertools.chain.from_iterable( path.search(Prepared(name)) for path in map(FastPath, paths) ) class PathDistribution(Distribution): def __init__(self, path): """Construct a distribution from a path to the metadata directory. :param path: A pathlib.Path or similar object supporting .joinpath(), __div__, .parent, and .read_text(). """ self._path = path def read_text(self, filename): with suppress( FileNotFoundError, IsADirectoryError, KeyError, NotADirectoryError, PermissionError, ): return self._path.joinpath(filename).read_text(encoding='utf-8') read_text.__doc__ = Distribution.read_text.__doc__ def locate_file(self, path): return self._path.parent / path def distribution(distribution_name): """Get the ``Distribution`` instance for the named package. :param distribution_name: The name of the distribution package as a string. :return: A ``Distribution`` instance (or subclass thereof). """ return Distribution.from_name(distribution_name) def distributions(**kwargs): """Get all ``Distribution`` instances in the current environment. :return: An iterable of ``Distribution`` instances. """ return Distribution.discover(**kwargs) def metadata(distribution_name) -> PackageMetadata: """Get the metadata for the named package. :param distribution_name: The name of the distribution package to query. :return: A PackageMetadata containing the parsed metadata. """ return Distribution.from_name(distribution_name).metadata def version(distribution_name): """Get the version string for the named package. :param distribution_name: The name of the distribution package to query. :return: The version string for the package as defined in the package's "Version" metadata key. """ return distribution(distribution_name).version def entry_points(): """Return EntryPoint objects for all installed packages. :return: EntryPoint objects for all installed packages. """ eps = itertools.chain.from_iterable(dist.entry_points for dist in distributions()) by_group = operator.attrgetter('group') ordered = sorted(eps, key=by_group) grouped = itertools.groupby(ordered, by_group) return {group: tuple(eps) for group, eps in grouped} def files(distribution_name): """Return a list of files for the named package. :param distribution_name: The name of the distribution package to query. :return: List of files composing the distribution. """ return distribution(distribution_name).files def requires(distribution_name): """ Return a list of requirements for the named package. :return: An iterator of requirements, suitable for packaging.requirement.Requirement. """ return distribution(distribution_name).requires def packages_distributions() -> Mapping[str, List[str]]: """ Return a mapping of top-level packages to their distributions. >>> pkgs = packages_distributions() >>> all(isinstance(dist, collections.abc.Sequence) for dist in pkgs.values()) True """ pkg_to_dist = collections.defaultdict(list) for dist in distributions(): for pkg in (dist.read_text('top_level.txt') or '').split(): pkg_to_dist[pkg].append(dist.metadata['Name']) return dict(pkg_to_dist) catalogue-2.0.10/catalogue/_importlib_metadata/_compat.py000066400000000000000000000045461450422361400235100ustar00rootroot00000000000000import sys __all__ = ['install', 'NullFinder', 'PyPy_repr', 'Protocol'] try: from typing import Protocol except ImportError: # pragma: no cover """ pytest-mypy complains here because: error: Incompatible import of "Protocol" (imported name has type "typing_extensions._SpecialForm", local name has type "typing._SpecialForm") """ from typing_extensions import Protocol # type: ignore def install(cls): """ Class decorator for installation on sys.meta_path. Adds the backport DistributionFinder to sys.meta_path and attempts to disable the finder functionality of the stdlib DistributionFinder. """ sys.meta_path.append(cls()) disable_stdlib_finder() return cls def disable_stdlib_finder(): """ Give the backport primacy for discovering path-based distributions by monkey-patching the stdlib O_O. See #91 for more background for rationale on this sketchy behavior. """ def matches(finder): return getattr( finder, '__module__', None ) == '_frozen_importlib_external' and hasattr(finder, '_catalogue_find_distributions') for finder in filter(matches, sys.meta_path): # pragma: nocover del finder._catalogue_find_distributions class NullFinder: """ A "Finder" (aka "MetaClassFinder") that never finds any modules, but may find distributions. """ @staticmethod def find_spec(*args, **kwargs): return None # In Python 2, the import system requires finders # to have a find_module() method, but this usage # is deprecated in Python 3 in favor of find_spec(). # For the purposes of this finder (i.e. being present # on sys.meta_path but having no other import # system functionality), the two methods are identical. find_module = find_spec class PyPy_repr: """ Override repr for EntryPoint objects on PyPy to avoid __iter__ access. Ref #97, #102. """ affected = hasattr(sys, 'pypy_version_info') def __compat_repr__(self): # pragma: nocover def make_param(name): value = getattr(self, name) return '{name}={value!r}'.format(**locals()) params = ', '.join(map(make_param, self._fields)) return 'EntryPoint({params})'.format(**locals()) if affected: # pragma: nocover __repr__ = __compat_repr__ del affected catalogue-2.0.10/catalogue/tests/000077500000000000000000000000001450422361400166455ustar00rootroot00000000000000catalogue-2.0.10/catalogue/tests/__init__.py000066400000000000000000000000001450422361400207440ustar00rootroot00000000000000catalogue-2.0.10/catalogue/tests/test_catalogue.py000066400000000000000000000121141450422361400222210ustar00rootroot00000000000000import pytest import sys from pathlib import Path import catalogue @pytest.fixture(autouse=True) def cleanup(): catalogue.REGISTRY = {} yield def test_get_set(): catalogue._set(("a", "b", "c"), "test") assert len(catalogue.REGISTRY) == 1 assert ("a", "b", "c") in catalogue.REGISTRY assert catalogue.check_exists("a", "b", "c") assert catalogue.REGISTRY[("a", "b", "c")] == "test" assert catalogue._get(("a", "b", "c")) == "test" with pytest.raises(catalogue.RegistryError): catalogue._get(("a", "b", "d")) with pytest.raises(catalogue.RegistryError): catalogue._get(("a", "b", "c", "d")) catalogue._set(("x", "y", "z1"), "test1") catalogue._set(("x", "y", "z2"), "test2") assert catalogue._remove(("a", "b", "c")) == "test" catalogue._set(("x", "y2"), "test3") with pytest.raises(catalogue.RegistryError): catalogue._remove(("x", "y")) assert catalogue._remove(("x", "y", "z2")) == "test2" def test_registry_get_set(): test_registry = catalogue.create("test") with pytest.raises(catalogue.RegistryError): test_registry.get("foo") test_registry.register("foo", func=lambda x: x) assert "foo" in test_registry def test_registry_call(): test_registry = catalogue.create("test") test_registry("foo", func=lambda x: x) assert "foo" in test_registry def test_get_all(): catalogue._set(("a", "b", "c"), "test") catalogue._set(("a", "b", "d"), "test") catalogue._set(("a", "b"), "test") catalogue._set(("b", "a"), "test") all_items = catalogue._get_all(("a", "b")) assert len(all_items) == 3 assert ("a", "b", "c") in all_items assert ("a", "b", "d") in all_items assert ("a", "b") in all_items all_items = catalogue._get_all(("a", "b", "c")) assert len(all_items) == 1 assert ("a", "b", "c") in all_items assert len(catalogue._get_all(("a", "b", "c", "d"))) == 0 def test_create_single_namespace(): test_registry = catalogue.create("test") assert catalogue.REGISTRY == {} @test_registry.register("a") def a(): pass def b(): pass test_registry.register("b", func=b) items = test_registry.get_all() assert len(items) == 2 assert items["a"] == a assert items["b"] == b assert catalogue.check_exists("test", "a") assert catalogue.check_exists("test", "b") assert catalogue._get(("test", "a")) == a assert catalogue._get(("test", "b")) == b with pytest.raises(TypeError): # The decorator only accepts one argument @test_registry.register("x", "y") def x(): pass def test_create_multi_namespace(): test_registry = catalogue.create("x", "y") @test_registry.register("z") def z(): pass items = test_registry.get_all() assert len(items) == 1 assert items["z"] == z assert catalogue.check_exists("x", "y", "z") assert catalogue._get(("x", "y", "z")) == z def _check_entry_points(): # Check entry points for test_entry_points_older() and test_entry_points_newer(). assert catalogue.REGISTRY == {} test_registry = catalogue.create("test", "foo", entry_points=True) entry_points = test_registry.get_entry_points() assert "bar" in entry_points assert entry_points["bar"] == catalogue.check_exists assert test_registry.get_entry_point("bar") == catalogue.check_exists assert catalogue.REGISTRY == {} assert test_registry.get("bar") == catalogue.check_exists assert test_registry.get_all() == {"bar": catalogue.check_exists} assert "bar" in test_registry @pytest.mark.skipif( sys.version_info >= (3, 10), reason="Test does not support >=3.10 importlib_metadata API", ) def test_entry_points_older(): # Create a new EntryPoint object by pretending we have a setup.cfg and # use one of catalogue's util functions as the advertised function ep_string = "[options.entry_points]test_foo\n bar = catalogue:check_exists" ep = catalogue.importlib_metadata.EntryPoint._from_text(ep_string) catalogue.AVAILABLE_ENTRY_POINTS["test_foo"] = ep _check_entry_points() @pytest.mark.skipif( sys.version_info < (3, 10) or sys.version_info >= (3, 12), reason="Test only supports python 3.10 and 3.11 importlib_metadata API", ) def test_entry_points_newer(): # Create a new EntryPoint object by pretending we have a setup.cfg and # use one of catalogue's util functions as the advertised function ep = catalogue.importlib_metadata.EntryPoint( "bar", "catalogue:check_exists", "test_foo" ) catalogue.AVAILABLE_ENTRY_POINTS[ "test_foo" ] = catalogue.importlib_metadata.EntryPoints([ep]) _check_entry_points() def test_registry_find(): test_registry = catalogue.create("test_registry_find") name = "a" @test_registry.register(name) def a(): """This is a registered function.""" pass info = test_registry.find(name) assert info["module"] == "catalogue.tests.test_catalogue" assert info["file"] == str(Path(__file__)) assert info["docstring"] == "This is a registered function." assert info["line_no"] catalogue-2.0.10/requirements.txt000066400000000000000000000001631450422361400170230ustar00rootroot00000000000000zipp>=0.5; python_version < "3.8" typing-extensions>=3.6.4; python_version < "3.8" pytest>=4.6.5 mypy>=0.991,<1.1.0catalogue-2.0.10/setup.cfg000066400000000000000000000026151450422361400153640ustar00rootroot00000000000000[metadata] version = 2.0.10 description = Super lightweight function registries for your library url = https://github.com/explosion/catalogue author = Explosion author_email = contact@explosion.ai license = MIT long_description = file: README.md long_description_content_type = text/markdown classifiers = Development Status :: 5 - Production/Stable Environment :: Console Intended Audience :: Developers Intended Audience :: Science/Research License :: OSI Approved :: MIT License Operating System :: POSIX :: Linux Operating System :: MacOS :: MacOS X Operating System :: Microsoft :: Windows Programming Language :: Python :: 3 Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 Programming Language :: Python :: 3.10 Programming Language :: Python :: 3.11 Programming Language :: Python :: 3.12 Topic :: Scientific/Engineering [options] zip_safe = true include_package_data = true python_requires = >=3.6 install_requires = zipp>=0.5; python_version < "3.8" typing-extensions>=3.6.4; python_version < "3.8" [sdist] formats = gztar [flake8] ignore = E203, E266, E501, E731, W503 max-line-length = 80 select = B,C,E,F,W,T4,B9 exclude = .env, .git, __pycache__, [mypy] ignore_missing_imports = True no_implicit_optional = True catalogue-2.0.10/setup.py000066400000000000000000000002311450422361400152450ustar00rootroot00000000000000#!/usr/bin/env python if __name__ == "__main__": from setuptools import setup, find_packages setup(name="catalogue", packages=find_packages())