click-inject 0.1.0__tar.gz

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.
@@ -0,0 +1,22 @@
1
+ Metadata-Version: 2.3
2
+ Name: click-inject
3
+ Version: 0.1.0
4
+ Summary: A pytest like injection framework for click commands.
5
+ Author: Paul Traylor
6
+ Author-email: Paul Traylor <kungfudiscomonkey@gmail.com>
7
+ Classifier: Development Status :: 3 - Alpha
8
+ Classifier: Programming Language :: Python :: 3.10
9
+ Classifier: Programming Language :: Python :: 3.11
10
+ Classifier: Programming Language :: Python :: 3.12
11
+ Classifier: Programming Language :: Python :: 3.13
12
+ Classifier: Programming Language :: Python :: 3.14
13
+ Classifier: Programming Language :: Python :: 3.15
14
+ Requires-Dist: click
15
+ Requires-Python: >=3.10
16
+ Project-URL: issues, https://codeberg.org/kfdm/click-inject/issues
17
+ Project-URL: source, https://codeberg.org/kfdm/click-inject
18
+ Description-Content-Type: text/markdown
19
+
20
+ # click injection framework
21
+
22
+ A pytest like injection framework for click commands.
@@ -0,0 +1,3 @@
1
+ # click injection framework
2
+
3
+ A pytest like injection framework for click commands.
@@ -0,0 +1,40 @@
1
+ [project]
2
+ name = "click-inject"
3
+ version = "0.1.0"
4
+ description = "A pytest like injection framework for click commands."
5
+ readme = "README.md"
6
+ # https://endoflife.date/python
7
+ requires-python = ">=3.10"
8
+ authors = [
9
+ { name = "Paul Traylor", email = "kungfudiscomonkey@gmail.com" },
10
+ ]
11
+ classifiers = [
12
+ "Development Status :: 3 - Alpha",
13
+ "Programming Language :: Python :: 3.10",
14
+ "Programming Language :: Python :: 3.11",
15
+ "Programming Language :: Python :: 3.12",
16
+ "Programming Language :: Python :: 3.13",
17
+ "Programming Language :: Python :: 3.14",
18
+ "Programming Language :: Python :: 3.15",
19
+ ]
20
+ dependencies = [
21
+ "click",
22
+ ]
23
+
24
+ [project.urls]
25
+ issues = "https://codeberg.org/kfdm/click-inject/issues"
26
+ source = "https://codeberg.org/kfdm/click-inject"
27
+
28
+ [dependency-groups]
29
+ dev = [
30
+ "repo-review-classifiers>=0.1.0",
31
+ "repo-review-endoflife>=0.2.0",
32
+ "repo-review-just>=0.2.0",
33
+ "repo-review-readme>=0.1.1",
34
+ "repo-review[cli]>=1.2.1",
35
+ "validate-pyproject>=0.25",
36
+ ]
37
+
38
+ [build-system]
39
+ requires = ["uv_build>=0.11.32,<0.12.0"]
40
+ build-backend = "uv_build"
File without changes
File without changes
@@ -0,0 +1,65 @@
1
+ import inspect
2
+ import logging
3
+ from functools import wraps
4
+ from types import FunctionType
5
+
6
+ import click
7
+
8
+ logger = logging.getLogger(__name__)
9
+
10
+
11
+ class FixtureRegistry:
12
+ def __init__(self):
13
+ self._providers: dict[str, FunctionType] = {}
14
+
15
+ def register(self, name=None):
16
+ def decorator(fn: FunctionType):
17
+ key = name or fn.__name__
18
+ logger.debug("register %s", fn.__name__)
19
+ self._providers[key] = fn
20
+ return fn
21
+
22
+ return decorator
23
+
24
+ def resolve(self, ctx: click.Context, name, stack=None):
25
+ if name in ctx.obj:
26
+ return ctx.obj[name]
27
+ stack = stack or set()
28
+ if name in stack:
29
+ raise RuntimeError(f"Circular dependency: {name}")
30
+ stack.add(name)
31
+
32
+ provider = self._providers[name]
33
+
34
+ # 🔑 inspect dependencies
35
+ sig = inspect.signature(provider)
36
+ kwargs = {}
37
+
38
+ for param in sig.parameters.values():
39
+ if param.name == "ctx":
40
+ kwargs["ctx"] = ctx
41
+ else:
42
+ kwargs[param.name] = self.resolve(ctx, param.name, stack)
43
+
44
+ value = provider(**kwargs)
45
+ ctx.obj[name] = value
46
+ return value
47
+
48
+ def inject(self, fn):
49
+ sig = inspect.signature(fn)
50
+
51
+ @click.pass_context
52
+ @wraps(fn)
53
+ def wrapper(ctx, *args, **kwargs):
54
+ for name in sig.parameters:
55
+ if name not in kwargs:
56
+ try:
57
+ kwargs[name] = self.resolve(ctx, name)
58
+ except KeyError:
59
+ logger.debug("No fixture for %s found", name)
60
+ return fn(*args, **kwargs)
61
+
62
+ return wrapper
63
+
64
+
65
+ registry = FixtureRegistry()