pyanyapi 0.6.0__tar.gz → 1.0.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.
- pyanyapi-1.0.0/.gitignore +49 -0
- pyanyapi-1.0.0/LICENSE +21 -0
- pyanyapi-1.0.0/PKG-INFO +110 -0
- pyanyapi-1.0.0/README.md +70 -0
- pyanyapi-1.0.0/pyproject.toml +107 -0
- pyanyapi-1.0.0/src/pyanyapi/__init__.py +3 -0
- pyanyapi-1.0.0/src/pyanyapi/decorators.py +17 -0
- pyanyapi-1.0.0/src/pyanyapi/exceptions.py +9 -0
- pyanyapi-1.0.0/src/pyanyapi/helpers.py +31 -0
- {pyanyapi-0.6.0 → pyanyapi-1.0.0/src}/pyanyapi/interfaces.py +134 -132
- pyanyapi-1.0.0/src/pyanyapi/parsers.py +185 -0
- pyanyapi-1.0.0/src/pyanyapi/py.typed +0 -0
- pyanyapi-1.0.0/test/__init__.py +0 -0
- pyanyapi-1.0.0/test/conftest.py +81 -0
- pyanyapi-1.0.0/test/test_interfaces.py +7 -0
- pyanyapi-1.0.0/test/test_parsers.py +338 -0
- pyanyapi-1.0.0/test/test_strip.py +65 -0
- pyanyapi-0.6.0/CHANGELOG.md +0 -88
- pyanyapi-0.6.0/MANIFEST.in +0 -6
- pyanyapi-0.6.0/PKG-INFO +0 -28
- pyanyapi-0.6.0/README.rst +0 -532
- pyanyapi-0.6.0/pyanyapi/__init__.py +0 -32
- pyanyapi-0.6.0/pyanyapi/_compat.py +0 -24
- pyanyapi-0.6.0/pyanyapi/decorators.py +0 -17
- pyanyapi-0.6.0/pyanyapi/exceptions.py +0 -11
- pyanyapi-0.6.0/pyanyapi/helpers.py +0 -39
- pyanyapi-0.6.0/pyanyapi/parsers.py +0 -209
- pyanyapi-0.6.0/pyanyapi.egg-info/PKG-INFO +0 -28
- pyanyapi-0.6.0/pyanyapi.egg-info/SOURCES.txt +0 -28
- pyanyapi-0.6.0/pyanyapi.egg-info/dependency_links.txt +0 -1
- pyanyapi-0.6.0/pyanyapi.egg-info/requires.txt +0 -3
- pyanyapi-0.6.0/pyanyapi.egg-info/top_level.txt +0 -1
- pyanyapi-0.6.0/setup.cfg +0 -8
- pyanyapi-0.6.0/setup.py +0 -89
- pyanyapi-0.6.0/tests/__init__$py.class +0 -0
- pyanyapi-0.6.0/tests/__init__.py +0 -1
- pyanyapi-0.6.0/tests/_compat$py.class +0 -0
- pyanyapi-0.6.0/tests/_compat.py +0 -7
- pyanyapi-0.6.0/tests/conftest$py.class +0 -0
- pyanyapi-0.6.0/tests/conftest.py +0 -106
- pyanyapi-0.6.0/tests/test_interfaces.py +0 -8
- pyanyapi-0.6.0/tests/test_parsers$py.class +0 -0
- pyanyapi-0.6.0/tests/test_parsers.py +0 -345
- pyanyapi-0.6.0/tests/test_strip$py.class +0 -0
- pyanyapi-0.6.0/tests/test_strip.py +0 -66
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
*.py[cod]
|
|
2
|
+
|
|
3
|
+
# C extensions
|
|
4
|
+
*.so
|
|
5
|
+
|
|
6
|
+
# Packages
|
|
7
|
+
*.egg
|
|
8
|
+
*.eggs
|
|
9
|
+
*.egg-info
|
|
10
|
+
dist
|
|
11
|
+
build
|
|
12
|
+
eggs
|
|
13
|
+
parts
|
|
14
|
+
bin
|
|
15
|
+
var
|
|
16
|
+
sdist
|
|
17
|
+
develop-eggs
|
|
18
|
+
.installed.cfg
|
|
19
|
+
lib
|
|
20
|
+
lib64
|
|
21
|
+
venv*/
|
|
22
|
+
pyvenv*/
|
|
23
|
+
|
|
24
|
+
# Installer logs
|
|
25
|
+
pip-log.txt
|
|
26
|
+
|
|
27
|
+
# Unit test / coverage reports
|
|
28
|
+
.coverage
|
|
29
|
+
coverage.xml
|
|
30
|
+
junit.xml
|
|
31
|
+
.tox
|
|
32
|
+
.coverage.*
|
|
33
|
+
htmlcov
|
|
34
|
+
|
|
35
|
+
# Translations
|
|
36
|
+
*.mo
|
|
37
|
+
|
|
38
|
+
.idea
|
|
39
|
+
|
|
40
|
+
.DS_Store
|
|
41
|
+
*~
|
|
42
|
+
.*.sw[po]
|
|
43
|
+
.build
|
|
44
|
+
.ve
|
|
45
|
+
.env
|
|
46
|
+
.bootstrap
|
|
47
|
+
*.bak
|
|
48
|
+
site/
|
|
49
|
+
.cache
|
pyanyapi-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Dmitry Dygalo
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
pyanyapi-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pyanyapi
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Tools for convenient interface creation over various types of data in a declarative way.
|
|
5
|
+
Project-URL: Documentation, https://pyanyapi.readthedocs.io/en/stable/
|
|
6
|
+
Project-URL: Changelog, https://github.com/Stranger6667/pyanyapi/blob/master/CHANGELOG.md
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/Stranger6667/pyanyapi/issues
|
|
8
|
+
Project-URL: Funding, https://github.com/sponsors/Stranger6667
|
|
9
|
+
Project-URL: Source Code, https://github.com/Stranger6667/pyanyapi
|
|
10
|
+
Author-email: Dmitry Dygalo <dmitry@dygalo.dev>
|
|
11
|
+
Maintainer-email: Dmitry Dygalo <dmitry@dygalo.dev>
|
|
12
|
+
License-Expression: MIT
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Keywords: API,XPath,csv,interface,json,parsing,regexp,xml,yaml
|
|
15
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
16
|
+
Classifier: Environment :: Console
|
|
17
|
+
Classifier: Intended Audience :: Developers
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
25
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
26
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
27
|
+
Classifier: Topic :: Text Processing :: General
|
|
28
|
+
Classifier: Topic :: Utilities
|
|
29
|
+
Classifier: Typing :: Typed
|
|
30
|
+
Requires-Python: >=3.11
|
|
31
|
+
Requires-Dist: lxml>=4.9.1
|
|
32
|
+
Requires-Dist: pyyaml>=6.0
|
|
33
|
+
Provides-Extra: docs
|
|
34
|
+
Requires-Dist: pymdown-extensions; extra == 'docs'
|
|
35
|
+
Requires-Dist: zensical<0.2,>=0.1.1; extra == 'docs'
|
|
36
|
+
Provides-Extra: tests
|
|
37
|
+
Requires-Dist: coverage[toml]>=7.2; extra == 'tests'
|
|
38
|
+
Requires-Dist: pytest>=8; extra == 'tests'
|
|
39
|
+
Description-Content-Type: text/markdown
|
|
40
|
+
|
|
41
|
+
# PyAnyAPI
|
|
42
|
+
|
|
43
|
+
[](https://github.com/Stranger6667/pyanyapi/actions/workflows/build.yml)
|
|
44
|
+
[](https://codecov.io/github/Stranger6667/pyanyapi?branch=master)
|
|
45
|
+
[](https://pypi.org/project/pyanyapi/)
|
|
46
|
+
[](https://pypi.org/project/pyanyapi/)
|
|
47
|
+
[](https://pyanyapi.readthedocs.io/en/stable/)
|
|
48
|
+
[](https://opensource.org/licenses/MIT)
|
|
49
|
+
|
|
50
|
+
Tools for convenient interface creation over various types of data in a declarative way.
|
|
51
|
+
|
|
52
|
+
Declare the attributes you need as queries, and PyAnyAPI turns the content into an object with those attributes:
|
|
53
|
+
XPath for HTML and XML, `container > key` lookups for JSON and YAML, `row:column` for CSV and regular expressions for text.
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
uv pip install pyanyapi
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from pyanyapi.parsers import HTMLParser
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class SimpleParser(HTMLParser):
|
|
68
|
+
settings = {"header": "string(.//h1/text())"}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
api = SimpleParser().parse("<html><body><h1>Value</h1></body></html>")
|
|
72
|
+
assert api.header == "Value"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
JSON lookups and all results at once:
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from pyanyapi.parsers import JSONParser
|
|
79
|
+
|
|
80
|
+
api = JSONParser({"id": "container > id", "second": "items > 1"}).parse(
|
|
81
|
+
'{"container": {"id": "123"}, "items": ["first", "second"]}'
|
|
82
|
+
)
|
|
83
|
+
assert api.parse_all() == {"id": "123", "second": "second"}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Combine parsers when the content type is unknown upfront, e.g. JSON responses with HTML error pages:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
from pyanyapi.parsers import HTMLParser, JSONParser
|
|
90
|
+
|
|
91
|
+
parser = JSONParser({"status": "status"}) & HTMLParser({"error": "string(//h1)"})
|
|
92
|
+
assert parser.parse('{"status": "ok"}').status == "ok"
|
|
93
|
+
assert parser.parse("<html><body><h1>Server error</h1></body></html>").error == "Server error"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Features
|
|
97
|
+
|
|
98
|
+
- Parsers for HTML, XML, XML Objectify, JSON, YAML, CSV, AJAX (JSON with HTML values), regular expressions and substring checks.
|
|
99
|
+
- Settings inheritance, sub-parsers and "base-children" queries for result lists.
|
|
100
|
+
- Custom properties and methods via `@interface_property` and `@interface_method`.
|
|
101
|
+
- Optional stripping of whitespace in results.
|
|
102
|
+
- Type annotations, with a `py.typed` marker.
|
|
103
|
+
|
|
104
|
+
## Documentation
|
|
105
|
+
|
|
106
|
+
<https://pyanyapi.readthedocs.io/en/stable/>
|
|
107
|
+
|
|
108
|
+
## Python support
|
|
109
|
+
|
|
110
|
+
PyAnyAPI supports CPython 3.11 to 3.15 and PyPy 3.11.
|
pyanyapi-1.0.0/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# PyAnyAPI
|
|
2
|
+
|
|
3
|
+
[](https://github.com/Stranger6667/pyanyapi/actions/workflows/build.yml)
|
|
4
|
+
[](https://codecov.io/github/Stranger6667/pyanyapi?branch=master)
|
|
5
|
+
[](https://pypi.org/project/pyanyapi/)
|
|
6
|
+
[](https://pypi.org/project/pyanyapi/)
|
|
7
|
+
[](https://pyanyapi.readthedocs.io/en/stable/)
|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
|
+
|
|
10
|
+
Tools for convenient interface creation over various types of data in a declarative way.
|
|
11
|
+
|
|
12
|
+
Declare the attributes you need as queries, and PyAnyAPI turns the content into an object with those attributes:
|
|
13
|
+
XPath for HTML and XML, `container > key` lookups for JSON and YAML, `row:column` for CSV and regular expressions for text.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
uv pip install pyanyapi
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from pyanyapi.parsers import HTMLParser
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class SimpleParser(HTMLParser):
|
|
28
|
+
settings = {"header": "string(.//h1/text())"}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
api = SimpleParser().parse("<html><body><h1>Value</h1></body></html>")
|
|
32
|
+
assert api.header == "Value"
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
JSON lookups and all results at once:
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from pyanyapi.parsers import JSONParser
|
|
39
|
+
|
|
40
|
+
api = JSONParser({"id": "container > id", "second": "items > 1"}).parse(
|
|
41
|
+
'{"container": {"id": "123"}, "items": ["first", "second"]}'
|
|
42
|
+
)
|
|
43
|
+
assert api.parse_all() == {"id": "123", "second": "second"}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Combine parsers when the content type is unknown upfront, e.g. JSON responses with HTML error pages:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from pyanyapi.parsers import HTMLParser, JSONParser
|
|
50
|
+
|
|
51
|
+
parser = JSONParser({"status": "status"}) & HTMLParser({"error": "string(//h1)"})
|
|
52
|
+
assert parser.parse('{"status": "ok"}').status == "ok"
|
|
53
|
+
assert parser.parse("<html><body><h1>Server error</h1></body></html>").error == "Server error"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Features
|
|
57
|
+
|
|
58
|
+
- Parsers for HTML, XML, XML Objectify, JSON, YAML, CSV, AJAX (JSON with HTML values), regular expressions and substring checks.
|
|
59
|
+
- Settings inheritance, sub-parsers and "base-children" queries for result lists.
|
|
60
|
+
- Custom properties and methods via `@interface_property` and `@interface_method`.
|
|
61
|
+
- Optional stripping of whitespace in results.
|
|
62
|
+
- Type annotations, with a `py.typed` marker.
|
|
63
|
+
|
|
64
|
+
## Documentation
|
|
65
|
+
|
|
66
|
+
<https://pyanyapi.readthedocs.io/en/stable/>
|
|
67
|
+
|
|
68
|
+
## Python support
|
|
69
|
+
|
|
70
|
+
PyAnyAPI supports CPython 3.11 to 3.15 and PyPy 3.11.
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pyanyapi"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Tools for convenient interface creation over various types of data in a declarative way."
|
|
9
|
+
keywords = ["parsing", "interface", "xml", "json", "csv", "yaml", "API", "XPath", "regexp"]
|
|
10
|
+
authors = [{ name = "Dmitry Dygalo", email = "dmitry@dygalo.dev" }]
|
|
11
|
+
maintainers = [{ name = "Dmitry Dygalo", email = "dmitry@dygalo.dev" }]
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
license = "MIT"
|
|
14
|
+
requires-python = ">=3.11"
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 5 - Production/Stable",
|
|
17
|
+
"Environment :: Console",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Programming Language :: Python :: 3.14",
|
|
25
|
+
"Programming Language :: Python :: 3.15",
|
|
26
|
+
"Programming Language :: Python :: Implementation :: CPython",
|
|
27
|
+
"Programming Language :: Python :: Implementation :: PyPy",
|
|
28
|
+
"Topic :: Text Processing :: General",
|
|
29
|
+
"Topic :: Utilities",
|
|
30
|
+
"Typing :: Typed",
|
|
31
|
+
]
|
|
32
|
+
dependencies = ["lxml>=4.9.1", "PyYAML>=6.0"]
|
|
33
|
+
|
|
34
|
+
[project.optional-dependencies]
|
|
35
|
+
tests = ["coverage[toml]>=7.2", "pytest>=8"]
|
|
36
|
+
docs = ["pymdown-extensions", "zensical>=0.1.1,<0.2"]
|
|
37
|
+
|
|
38
|
+
[project.urls]
|
|
39
|
+
Documentation = "https://pyanyapi.readthedocs.io/en/stable/"
|
|
40
|
+
Changelog = "https://github.com/Stranger6667/pyanyapi/blob/master/CHANGELOG.md"
|
|
41
|
+
"Bug Tracker" = "https://github.com/Stranger6667/pyanyapi/issues"
|
|
42
|
+
Funding = "https://github.com/sponsors/Stranger6667"
|
|
43
|
+
"Source Code" = "https://github.com/Stranger6667/pyanyapi"
|
|
44
|
+
|
|
45
|
+
[tool.hatch.version]
|
|
46
|
+
path = "src/pyanyapi/__init__.py"
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.targets.sdist]
|
|
49
|
+
include = ["/src", "/test"]
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
addopts = ["-ra", "--strict-markers", "--strict-config"]
|
|
53
|
+
xfail_strict = true
|
|
54
|
+
testpaths = ["test"]
|
|
55
|
+
|
|
56
|
+
[tool.coverage.run]
|
|
57
|
+
parallel = true
|
|
58
|
+
branch = true
|
|
59
|
+
source_pkgs = ["pyanyapi"]
|
|
60
|
+
|
|
61
|
+
[tool.coverage.paths]
|
|
62
|
+
source = ["src", ".tox/*/site-packages", ".tox/*/lib/*/site-packages", ".tox/*/Lib/site-packages"]
|
|
63
|
+
|
|
64
|
+
[tool.coverage.report]
|
|
65
|
+
show_missing = true
|
|
66
|
+
precision = 2
|
|
67
|
+
fail_under = 100
|
|
68
|
+
exclude_also = ["raise NotImplementedError"]
|
|
69
|
+
|
|
70
|
+
[tool.ruff]
|
|
71
|
+
line-length = 120
|
|
72
|
+
target-version = "py311"
|
|
73
|
+
|
|
74
|
+
[tool.ruff.lint]
|
|
75
|
+
select = [
|
|
76
|
+
"E", # pycodestyle errors
|
|
77
|
+
"W", # pycodestyle warnings
|
|
78
|
+
"F", # pyflakes
|
|
79
|
+
"C", # flake8-comprehensions
|
|
80
|
+
"B", # flake8-bugbear
|
|
81
|
+
"D", # pydocstyle
|
|
82
|
+
"T201", # flake8-print
|
|
83
|
+
"I", # isort
|
|
84
|
+
"FURB", # refurb
|
|
85
|
+
"PGH003", # blanket-type-ignore
|
|
86
|
+
"UP", # pyupgrade
|
|
87
|
+
"SIM", # flake8-simplify
|
|
88
|
+
]
|
|
89
|
+
ignore = [
|
|
90
|
+
"E501", # Line too long
|
|
91
|
+
"D100", # Missing docstring in public module
|
|
92
|
+
"D101", # Missing docstring in public class
|
|
93
|
+
"D102", # Missing docstring in public method
|
|
94
|
+
"D103", # Missing docstring in public function
|
|
95
|
+
"D104", # Missing docstring in public package
|
|
96
|
+
"D105", # Missing docstring in magic method
|
|
97
|
+
"D106", # Missing docstring in public nested class
|
|
98
|
+
"D107", # Missing docstring in `__init__`
|
|
99
|
+
"D202", # No blank lines allowed after function docstring
|
|
100
|
+
"D203", # One blank line before class
|
|
101
|
+
"D213", # Multiline summary second line
|
|
102
|
+
"D401", # Imperative mood
|
|
103
|
+
"D407", # Missing dashed underline after section
|
|
104
|
+
]
|
|
105
|
+
|
|
106
|
+
[tool.ruff.lint.isort]
|
|
107
|
+
known-first-party = ["pyanyapi"]
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from collections.abc import Callable
|
|
2
|
+
from typing import ParamSpec, TypeVar
|
|
3
|
+
|
|
4
|
+
P = ParamSpec("P")
|
|
5
|
+
R = TypeVar("R")
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def interface_property(method: Callable[P, R]) -> "staticmethod[P, R]":
|
|
9
|
+
"""Marks method to be included in parsing result as property."""
|
|
10
|
+
method._interface_property = True # type: ignore[attr-defined]
|
|
11
|
+
return staticmethod(method)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def interface_method(method: Callable[P, R]) -> "staticmethod[P, R]":
|
|
15
|
+
"""Marks method to be included in parsing result."""
|
|
16
|
+
method._interface_method = True # type: ignore[attr-defined]
|
|
17
|
+
return staticmethod(method)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""Functions to dynamically attach attributes to classes.
|
|
2
|
+
|
|
3
|
+
Most of parsing results are cached because of immutability of input data.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from collections.abc import Callable
|
|
7
|
+
from typing import Any, Generic, TypeVar
|
|
8
|
+
|
|
9
|
+
T = TypeVar("T")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class cached_property(Generic[T]):
|
|
13
|
+
"""Copied from Django."""
|
|
14
|
+
|
|
15
|
+
def __init__(self, func: Callable[[Any], T]) -> None:
|
|
16
|
+
self.func = func
|
|
17
|
+
|
|
18
|
+
def __get__(self, instance: Any, owner: type | None = None) -> T:
|
|
19
|
+
res = instance.__dict__[self.func.__name__] = self.func(instance)
|
|
20
|
+
return res
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def attach_attribute(target: type, name: str, attr: Any) -> None:
|
|
24
|
+
attr.__name__ = name
|
|
25
|
+
attr._attached = True
|
|
26
|
+
setattr(target, name, attr)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def attach_cached_property(target: type, name: str, prop: Callable[[Any], Any]) -> None:
|
|
30
|
+
method = cached_property(prop)
|
|
31
|
+
attach_attribute(target, name, method)
|