epregistry 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- epregistry/__init__.py +7 -0
- epregistry/epregistry.py +190 -0
- epregistry/py.typed +0 -0
- epregistry-0.0.1.dist-info/METADATA +56 -0
- epregistry-0.0.1.dist-info/RECORD +7 -0
- epregistry-0.0.1.dist-info/WHEEL +4 -0
- epregistry-0.0.1.dist-info/licenses/LICENSE +22 -0
epregistry/__init__.py
ADDED
epregistry/epregistry.py
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"""Module for managing entry points through a registry system."""
|
|
2
|
+
|
|
3
|
+
from collections import defaultdict
|
|
4
|
+
from collections.abc import Callable, Iterator
|
|
5
|
+
from functools import cache
|
|
6
|
+
from importlib.metadata import EntryPoint, entry_points
|
|
7
|
+
from typing import Any, Generic, TypeVar
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
T = TypeVar("T")
|
|
11
|
+
|
|
12
|
+
# Global cache for ALL entry points across all groups
|
|
13
|
+
_entry_point_cache: dict[str, dict[str, EntryPoint]] | None = None
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@cache
|
|
17
|
+
def _initialize_cache() -> dict[str, dict[str, EntryPoint]]:
|
|
18
|
+
"""Initialize the global cache with ALL entry points.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
dict: A nested dictionary mapping group names to their entry points.
|
|
22
|
+
Structure: {group_name: {entry_point_name: entry_point}}
|
|
23
|
+
"""
|
|
24
|
+
all_entry_points: defaultdict[str, dict[str, Any]] = defaultdict(dict)
|
|
25
|
+
for ep in entry_points():
|
|
26
|
+
all_entry_points[ep.group][ep.name] = ep
|
|
27
|
+
return dict(all_entry_points)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class EntryPointRegistry(Generic[T]):
|
|
31
|
+
"""A registry for managing and accessing entry points of a specific group.
|
|
32
|
+
|
|
33
|
+
This class provides a convenient interface to work with entry points from a specified
|
|
34
|
+
group. It handles caching and provides various methods to access / load entry points.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
group: The entry point group to manage.
|
|
38
|
+
|
|
39
|
+
Attributes:
|
|
40
|
+
group: The name of the entry point group being managed.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __init__(self, group: str):
|
|
44
|
+
"""Initialize the registry for a specific entry point group.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
group: The entry point group to manage.
|
|
48
|
+
"""
|
|
49
|
+
self.group = group
|
|
50
|
+
# Ensure the group exists in the cache
|
|
51
|
+
if self.group not in self._get_cache():
|
|
52
|
+
self._get_cache()[self.group] = {}
|
|
53
|
+
|
|
54
|
+
@staticmethod
|
|
55
|
+
def _get_cache() -> dict[str, dict[str, EntryPoint]]:
|
|
56
|
+
"""Get or initialize the global entry point cache.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
dict: The global cache of entry points.
|
|
60
|
+
"""
|
|
61
|
+
global _entry_point_cache
|
|
62
|
+
if _entry_point_cache is None:
|
|
63
|
+
_entry_point_cache = _initialize_cache()
|
|
64
|
+
return _entry_point_cache
|
|
65
|
+
|
|
66
|
+
def get(self, name: str) -> EntryPoint | None:
|
|
67
|
+
"""Get an entry point by name.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
name: The name of the entry point.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
The entry point if found, None otherwise.
|
|
74
|
+
"""
|
|
75
|
+
return self._get_cache()[self.group].get(name)
|
|
76
|
+
|
|
77
|
+
def load(self, name: str) -> T | None:
|
|
78
|
+
"""Load an entry point by name.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
name: The name of the entry point to load.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
The loaded entry point if found, None otherwise.
|
|
85
|
+
"""
|
|
86
|
+
entry_point = self.get(name)
|
|
87
|
+
return entry_point.load() if entry_point is not None else None
|
|
88
|
+
|
|
89
|
+
def __getitem__(self, name: str) -> EntryPoint:
|
|
90
|
+
"""Get an entry point by name.
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
name: The name of the entry point.
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
The requested entry point.
|
|
97
|
+
|
|
98
|
+
Raises:
|
|
99
|
+
KeyError: If the entry point is not found.
|
|
100
|
+
"""
|
|
101
|
+
try:
|
|
102
|
+
return self._get_cache()[self.group][name]
|
|
103
|
+
except KeyError as e:
|
|
104
|
+
msg = f"No entry point named {name!r} found in group {self.group!r}"
|
|
105
|
+
raise KeyError(msg) from e
|
|
106
|
+
|
|
107
|
+
def __iter__(self) -> Iterator[str]:
|
|
108
|
+
"""Iterate over entry point names.
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
Iterator of entry point names.
|
|
112
|
+
"""
|
|
113
|
+
return iter(self._get_cache()[self.group])
|
|
114
|
+
|
|
115
|
+
def __len__(self) -> int:
|
|
116
|
+
"""Get the number of available entry points.
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
The number of entry points in this registry.
|
|
120
|
+
"""
|
|
121
|
+
return len(self._get_cache()[self.group])
|
|
122
|
+
|
|
123
|
+
def __contains__(self, name: str) -> bool:
|
|
124
|
+
"""Check if an entry point name exists.
|
|
125
|
+
|
|
126
|
+
Args:
|
|
127
|
+
name: The name to check.
|
|
128
|
+
|
|
129
|
+
Returns:
|
|
130
|
+
True if the entry point exists, False otherwise.
|
|
131
|
+
"""
|
|
132
|
+
return name in self._get_cache()[self.group]
|
|
133
|
+
|
|
134
|
+
def names(self) -> list[str]:
|
|
135
|
+
"""Get a list of all available entry point names.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
List of entry point names.
|
|
139
|
+
"""
|
|
140
|
+
return list(self._get_cache()[self.group].keys())
|
|
141
|
+
|
|
142
|
+
def get_all(self) -> dict[str, EntryPoint]:
|
|
143
|
+
"""Get all entry points as a dictionary.
|
|
144
|
+
|
|
145
|
+
Returns:
|
|
146
|
+
Dictionary mapping entry point names to entry points.
|
|
147
|
+
"""
|
|
148
|
+
return self._get_cache()[self.group]
|
|
149
|
+
|
|
150
|
+
def load_all(self) -> dict[str, T]:
|
|
151
|
+
"""Load all entry points.
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
Dictionary mapping entry point names to loaded entry points.
|
|
155
|
+
"""
|
|
156
|
+
return {name: ep.load() for name, ep in self.get_all().items()}
|
|
157
|
+
|
|
158
|
+
def get_metadata(self, name: str) -> dict[str, Any]:
|
|
159
|
+
"""Get detailed metadata for an entry point."""
|
|
160
|
+
ep = self.get(name)
|
|
161
|
+
if not ep:
|
|
162
|
+
msg = f"No entry point named '{name}' found in group '{self.group}'"
|
|
163
|
+
raise ValueError(msg)
|
|
164
|
+
return {
|
|
165
|
+
"module": ep.module,
|
|
166
|
+
"attr": ep.attr,
|
|
167
|
+
"dist": ep.dist.metadata["Name"] if ep.dist else None,
|
|
168
|
+
"version": ep.dist.version if ep.dist else None,
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def available_groups() -> list[str]:
|
|
173
|
+
"""Get a list of all available entry point groups.
|
|
174
|
+
|
|
175
|
+
Returns:
|
|
176
|
+
List of entry point group names.
|
|
177
|
+
"""
|
|
178
|
+
return list(EntryPointRegistry._get_cache().keys())
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
if __name__ == "__main__":
|
|
182
|
+
# Create a registry for console scripts
|
|
183
|
+
registry = EntryPointRegistry[Callable]("console_scripts")
|
|
184
|
+
|
|
185
|
+
# Print available console scripts
|
|
186
|
+
print("Available console scripts:")
|
|
187
|
+
for name in registry.names():
|
|
188
|
+
print(f"- {name}")
|
|
189
|
+
|
|
190
|
+
print(f"\nTotal scripts: {len(registry)}")
|
epregistry/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: epregistry
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A registry for entry points (cached and generically typed)
|
|
5
|
+
Project-URL: Documentation, https://phil65.github.io/epregistry/
|
|
6
|
+
Project-URL: Source, https://github.com/phil65/epregistry
|
|
7
|
+
Author-email: Philipp Temminghoff <philipptemminghoff@googlemail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Topic :: Documentation
|
|
19
|
+
Classifier: Topic :: Software Development
|
|
20
|
+
Classifier: Topic :: Utilities
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.12
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# epregistry
|
|
26
|
+
|
|
27
|
+
[](https://pypi.org/project/epregistry/)
|
|
28
|
+
[](https://pypi.org/project/epregistry/)
|
|
29
|
+
[](https://pypi.org/project/epregistry/)
|
|
30
|
+
[](https://pypi.org/project/epregistry/)
|
|
31
|
+
[](https://pypi.org/project/epregistry/)
|
|
32
|
+
[](https://pypi.org/project/epregistry/)
|
|
33
|
+
[](https://pypi.org/project/epregistry/)
|
|
34
|
+
[](https://pypi.org/project/epregistry/)
|
|
35
|
+
[](https://pypi.org/project/epregistry/)
|
|
36
|
+
[](https://github.com/phil65/epregistry/releases)
|
|
37
|
+
[](https://github.com/phil65/epregistry/graphs/contributors)
|
|
38
|
+
[](https://github.com/phil65/epregistry/discussions)
|
|
39
|
+
[](https://github.com/phil65/epregistry/forks)
|
|
40
|
+
[](https://github.com/phil65/epregistry/issues)
|
|
41
|
+
[](https://github.com/phil65/epregistry/pulls)
|
|
42
|
+
[](https://github.com/phil65/epregistry/watchers)
|
|
43
|
+
[](https://github.com/phil65/epregistry/stars)
|
|
44
|
+
[](https://github.com/phil65/epregistry)
|
|
45
|
+
[](https://github.com/phil65/epregistry/commits)
|
|
46
|
+
[](https://github.com/phil65/epregistry/releases)
|
|
47
|
+
[](https://github.com/phil65/epregistry)
|
|
48
|
+
[](https://github.com/phil65/epregistry)
|
|
49
|
+
[](https://github.com/phil65/epregistry)
|
|
50
|
+
[](https://github.com/phil65/epregistry)
|
|
51
|
+
[](https://codecov.io/gh/phil65/epregistry/)
|
|
52
|
+
[](https://github.com/psf/black)
|
|
53
|
+
[](https://pyup.io/repos/github/phil65/epregistry/)
|
|
54
|
+
|
|
55
|
+
[Read the documentation!](https://phil65.github.io/epregistry/)
|
|
56
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
epregistry/__init__.py,sha256=gG75_a_knMe246kLwIDOQnjdPVj128JJuIZ6KWMHVWM,205
|
|
2
|
+
epregistry/epregistry.py,sha256=xsmXkivwvcbs_XTM3piqgLDqboKSeCU0sriHaIN-G10,5889
|
|
3
|
+
epregistry/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
epregistry-0.0.1.dist-info/METADATA,sha256=OUekXMXa55w5ZxdXWzEGMhLT-zSK9cpzYfZwr-kBrCg,4321
|
|
5
|
+
epregistry-0.0.1.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
6
|
+
epregistry-0.0.1.dist-info/licenses/LICENSE,sha256=AteGCH9r177TxxrOFEiOARrastASsf7yW6MQxlAHdwA,1078
|
|
7
|
+
epregistry-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024, Philipp Temminghoff
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|