pkn 0.1.2__tar.gz → 0.1.3__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.
- {pkn-0.1.2 → pkn-0.1.3}/PKG-INFO +1 -1
- {pkn-0.1.2 → pkn-0.1.3}/pkn/__init__.py +1 -1
- pkn-0.1.3/pkn/pydantic/__init__.py +3 -0
- pkn-0.1.3/pkn/pydantic/paths.py +41 -0
- pkn-0.1.3/pkn/tests/pydantic/test_paths.py +27 -0
- {pkn-0.1.2 → pkn-0.1.3}/pyproject.toml +2 -2
- pkn-0.1.2/pkn/pydantic/__init__.py +0 -1
- {pkn-0.1.2 → pkn-0.1.3}/.gitignore +0 -0
- {pkn-0.1.2 → pkn-0.1.3}/LICENSE +0 -0
- {pkn-0.1.2 → pkn-0.1.3}/README.md +0 -0
- {pkn-0.1.2 → pkn-0.1.3}/pkn/infra/__init__.py +0 -0
- {pkn-0.1.2 → pkn-0.1.3}/pkn/infra/at.py +0 -0
- {pkn-0.1.2 → pkn-0.1.3}/pkn/infra/common.py +0 -0
- {pkn-0.1.2 → pkn-0.1.3}/pkn/infra/journalctl.py +0 -0
- {pkn-0.1.2 → pkn-0.1.3}/pkn/infra/launchctl.py +0 -0
- {pkn-0.1.2 → pkn-0.1.3}/pkn/infra/reboot.py +0 -0
- {pkn-0.1.2 → pkn-0.1.3}/pkn/infra/systemctl.py +0 -0
- {pkn-0.1.2 → pkn-0.1.3}/pkn/pydantic/generic.py +0 -0
- {pkn-0.1.2 → pkn-0.1.3}/pkn/pydantic/roots.py +0 -0
- {pkn-0.1.2 → pkn-0.1.3}/pkn/tests/pydantic/test_roots.py +0 -0
- {pkn-0.1.2 → pkn-0.1.3}/pkn/tests/test_all.py +0 -0
{pkn-0.1.2 → pkn-0.1.3}/PKG-INFO
RENAMED
@@ -0,0 +1,41 @@
|
|
1
|
+
from importlib import import_module
|
2
|
+
from types import FunctionType, MethodType
|
3
|
+
from typing import Annotated, Optional
|
4
|
+
|
5
|
+
from pydantic import BeforeValidator, PlainSerializer
|
6
|
+
|
7
|
+
__all__ = (
|
8
|
+
"get_import_path",
|
9
|
+
"serialize_path_as_string",
|
10
|
+
"ImportPath",
|
11
|
+
"CallablePath",
|
12
|
+
)
|
13
|
+
|
14
|
+
|
15
|
+
def get_import_path(path: str) -> type:
|
16
|
+
if isinstance(path, type):
|
17
|
+
return path
|
18
|
+
elif isinstance(path, (FunctionType, MethodType)):
|
19
|
+
return path
|
20
|
+
if not isinstance(path, str):
|
21
|
+
raise TypeError(path)
|
22
|
+
module, call = path.rsplit(".", 1)
|
23
|
+
return getattr(import_module(module), call)
|
24
|
+
|
25
|
+
|
26
|
+
def serialize_path_as_string(value: type) -> Optional[str]:
|
27
|
+
if value is None:
|
28
|
+
return None
|
29
|
+
if hasattr(value, "__module__") and hasattr(value, "__qualname__"):
|
30
|
+
return f"{value.__module__}.{value.__qualname__}"
|
31
|
+
if hasattr(value, "__name__"):
|
32
|
+
return f"{value.__module__}.{value.__name__}"
|
33
|
+
if hasattr(value, "__class__") and hasattr(value.__class__, "__name__"):
|
34
|
+
return f"{value.__class__.__module__}.{value.__class__.__name__}"
|
35
|
+
if hasattr(value, "__class__") and hasattr(value.__class__, "__qualname__"):
|
36
|
+
return f"{value.__class__.__module__}.{value.__class__.__qualname__}"
|
37
|
+
raise TypeError(f"Could not derive module and name for {value}")
|
38
|
+
|
39
|
+
|
40
|
+
ImportPath = Annotated[type, BeforeValidator(get_import_path), PlainSerializer(serialize_path_as_string, when_used="json", return_type=str)]
|
41
|
+
CallablePath = Annotated[object, BeforeValidator(get_import_path), PlainSerializer(serialize_path_as_string, when_used="json", return_type=str)]
|
@@ -0,0 +1,27 @@
|
|
1
|
+
from pydantic import BaseModel
|
2
|
+
|
3
|
+
from pkn.pydantic import CallablePath, ImportPath
|
4
|
+
|
5
|
+
|
6
|
+
def foo():
|
7
|
+
return "foo"
|
8
|
+
|
9
|
+
|
10
|
+
class MyType(int): ...
|
11
|
+
|
12
|
+
|
13
|
+
class MyModel(BaseModel):
|
14
|
+
typ: ImportPath
|
15
|
+
foo: CallablePath
|
16
|
+
|
17
|
+
|
18
|
+
def test_get_import_path_inst():
|
19
|
+
m = MyModel(typ=MyType, foo=foo)
|
20
|
+
assert m.typ == MyType
|
21
|
+
assert m.foo == foo
|
22
|
+
|
23
|
+
|
24
|
+
def test_get_import_path_string():
|
25
|
+
m = MyModel(typ="pkn.tests.pydantic.test_paths.MyType", foo="pkn.tests.pydantic.test_paths.foo")
|
26
|
+
assert m.typ(1) == MyType(1)
|
27
|
+
assert m.foo() == foo()
|
@@ -8,7 +8,7 @@ authors = [{name = "the pkn authors", email = "t.paine154@gmail.com"}]
|
|
8
8
|
description = "My Pocket Knife"
|
9
9
|
readme = "README.md"
|
10
10
|
license = { text = "Apache-2.0" }
|
11
|
-
version = "0.1.
|
11
|
+
version = "0.1.3"
|
12
12
|
requires-python = ">=3.9"
|
13
13
|
keywords = []
|
14
14
|
|
@@ -50,7 +50,7 @@ Repository = "https://github.com/1kbgz/pkn"
|
|
50
50
|
Homepage = "https://github.com/1kbgz/pkn"
|
51
51
|
|
52
52
|
[tool.bumpversion]
|
53
|
-
current_version = "0.1.
|
53
|
+
current_version = "0.1.3"
|
54
54
|
commit = true
|
55
55
|
tag = true
|
56
56
|
|
@@ -1 +0,0 @@
|
|
1
|
-
from .roots import *
|
File without changes
|
{pkn-0.1.2 → pkn-0.1.3}/LICENSE
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|