{"id":20636730,"url":"https://github.com/libcommon/registry-py","last_synced_at":"2026-05-07T05:31:59.185Z","repository":{"id":62575368,"uuid":"220902141","full_name":"libcommon/registry-py","owner":"libcommon","description":"Python library for implementing the registry pattern using metaprogramming.","archived":false,"fork":false,"pushed_at":"2022-12-26T21:31:30.000Z","size":47,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-17T12:47:36.360Z","etag":null,"topics":["library","metaprogramming","python"],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/libcommon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-11T04:39:39.000Z","updated_at":"2020-06-02T21:45:14.000Z","dependencies_parsed_at":"2023-01-31T01:45:35.997Z","dependency_job_id":null,"html_url":"https://github.com/libcommon/registry-py","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libcommon%2Fregistry-py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libcommon%2Fregistry-py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libcommon%2Fregistry-py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libcommon%2Fregistry-py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/libcommon","download_url":"https://codeload.github.com/libcommon/registry-py/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242656036,"owners_count":20164431,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["library","metaprogramming","python"],"created_at":"2024-11-16T15:12:00.378Z","updated_at":"2026-05-07T05:31:54.154Z","avatar_url":"https://github.com/libcommon.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# registry-py\n\n## Overview\n\nMetaprogramming means writing programs that manipulate other programs, such as compilers, interpreters, macros, etc.\nPython enables metaprogramming in a number of ways, one of which is `metaclasses`, which are special classes that can\nhook into child class creation methods, and can enforce invariants on those child classes. The\n[abc.ABCMeta](https://docs.python.org/3/library/abc.html#module-abc) class is one example, and allows for the\ndeclaration of [abstractmethod](https://docs.python.org/3/library/abc.html#abc.abstractmethod)s which must be\nimplemented by child classes. `registry-py` uses metaprogramming to implement the\n[registry pattern](https://github.com/faif/python-patterns/blob/master/patterns/behavioral/registry.py), where a `metaclass`\nstores a map of all child classes. This pattern is particularly useful for auto-discovery or auto-registration, where a\nprogram doesn't doesn't need to know where code is stored to know what's been implemented.\n\n## Installation\n\n### Install from PyPi (preferred method)\n\n```bash\npip install lc-registry\n```\n\n### Install from GitHub with Pip\n\n```bash\npip install git+https://github.com/libcommon/registry-py.git@vx.x.x#egg=lc_registry\n```\n\nwhere `x.x.x` is the version you want to download.\n\n## Install by Manual Download\n\nTo download the source distribution and/or wheel files, navigate to\n`https://github.com/libcommon/registry-py/tree/releases/vx.x.x/dist`, where `x.x.x` is the version you want to install,\nand download either via the UI or with a tool like wget. Then to install run:\n\n```bash\npip install \u003cdownloaded file\u003e\n```\n\nDo _not_ change the name of the file after downloading, as Pip requires a specific naming convention for installation files.\n\n## Dependencies\n\n`registry-py` does not have external dependencies. Only Python versions \u003e= 3.6 are officially supported.\n\n## Getting Started\n\nSuppose you want to implement a plugin system for a command line tool. One approach could be to require that plugin\nfiles are written to a specific folder, say `src/plugins/contrib`, and use\n[importlib.import_module](https://docs.python.org/3.8/library/importlib.html#importlib.import_module) to import modules\nfrom there on program startup. However, plugin files not in the proper directory will not get loaded, and finding\nplugins requires walking a directory tree. Using a registry would alleviate both these issues.\n\n```python\n# plugin_registry.py\nfrom abc import ABCMeta\nfrom typing import Any, ClassVar, Dict, Type\n\nfrom lc_registry import RegistryMetaclassMixin\n\n\nclass PluginRegistry(RegistryMetaclassMixin, ABCMeta):\n    \"\"\"Metaclass with registry.\"\"\"\n    __slots__ = ()\n    _REGISTRY: ClassVar[Dict[str, Type[Any]]] = dict()\n\n    @classmethod\n    def _add_class(cls, name: str, new_cls: Type[Any]) -\u003e None:\n        # All plugins must be named \"*Plugin\"\n        # NOTE: This is just an example what you can do with _add_class\n        if name == \"PluginBase\" or not name.endswith(\"Plugin\"):\n            return None\n        cls._REGISTRY[name] = new_cls\n        return None\n\n    @classmethod\n    def run_plugins(cls):\n        # Use cls._REGISTRY (or cls.registry() method) to\n        # iterate over each plugin and run it.\n\n\nclass PluginBase(metaclass=PluginRegistry):\n    \"\"\"Base class for all plugins. All child classes\n    will be added to the plugin registry once they come\n    into scope (AKA when Python creates the type).\n    \"\"\"\n    __slots__ = ()\n\n    @abstractmethod\n    def run_plugin(self, context: Dict[str, Any]) -\u003e None:\n        \"\"\"Plugin entrypoint.\"\"\"\n        raise NotImplementedError\n\n\n# line_count_plugin.py\nfrom typing import Any, Dict\n\nfrom plugin_registry import PluginBase\n\n\nclass LineCountPlugin(PluginBase):\n    \"\"\"Plugin that counts the number of lines in a file.\"\"\"\n    __slots__ = ()\n\n    def run_plugin(self, context: Dict[str, Any]) -\u003e None:\n        if \"filepath\" in context:\n            filepath = context.get(\"filepath\")\n            line_count = 0\n            with open(filepath) as source_file:\n                for _ in source_file:\n                    line_count += 1\n            print(\"{}\\t{}\".format(filepath, line_count))\n```\n\nNote the doc string for the `PluginBase` class in the example above: \"_... once they come into scope (AKA when\nPython creates the type)_.\" This is **important** - child classes will only be added to the registry if Python\nloads them into the program, meaning if they get imported somewhere.\n\n## Contributing/Suggestions\n\nContributions and suggestions are welcome! To make a feature request, report a bug, or otherwise comment on existing\nfunctionality, please file an issue. For contributions please submit a PR, but make sure to lint, type-check, and test\nyour code before doing so. Thanks in advance!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibcommon%2Fregistry-py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flibcommon%2Fregistry-py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibcommon%2Fregistry-py/lists"}