pjdev-sn-sdk 4.6.4__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.
- pjdev_sn_sdk-4.6.4/.gitignore +165 -0
- pjdev_sn_sdk-4.6.4/LICENSE.txt +9 -0
- pjdev_sn_sdk-4.6.4/PKG-INFO +48 -0
- pjdev_sn_sdk-4.6.4/README.md +21 -0
- pjdev_sn_sdk-4.6.4/pyproject.toml +73 -0
- pjdev_sn_sdk-4.6.4/src/pjdev_sn_sdk/__about__.py +4 -0
- pjdev_sn_sdk-4.6.4/src/pjdev_sn_sdk/__init__.py +3 -0
- pjdev_sn_sdk-4.6.4/src/pjdev_sn_sdk/api_utilities.py +161 -0
- pjdev_sn_sdk-4.6.4/src/pjdev_sn_sdk/config_service.py +19 -0
- pjdev_sn_sdk-4.6.4/src/pjdev_sn_sdk/control_parsing_service.py +98 -0
- pjdev_sn_sdk-4.6.4/src/pjdev_sn_sdk/models.py +123 -0
- pjdev_sn_sdk-4.6.4/src/pjdev_sn_sdk/sn_api_service.py +628 -0
- pjdev_sn_sdk-4.6.4/src/pjdev_sn_sdk/sn_api_utilities.py +188 -0
- pjdev_sn_sdk-4.6.4/test.sh +5 -0
- pjdev_sn_sdk-4.6.4/test_report_pjdev-sn-sdk.txt +31 -0
- pjdev_sn_sdk-4.6.4/tests/__init__.py +3 -0
- pjdev_sn_sdk-4.6.4/tests/tests_for_control_parsing_service.py +77 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
.DS_Store
|
|
2
|
+
.idea
|
|
3
|
+
|
|
4
|
+
!**/.gitkeep
|
|
5
|
+
|
|
6
|
+
# Byte-compiled / optimized / DLL files
|
|
7
|
+
__pycache__/
|
|
8
|
+
*.py[cod]
|
|
9
|
+
*$py.class
|
|
10
|
+
|
|
11
|
+
# C extensions
|
|
12
|
+
*.so
|
|
13
|
+
|
|
14
|
+
# Distribution / packaging
|
|
15
|
+
.Python
|
|
16
|
+
build/
|
|
17
|
+
develop-eggs/
|
|
18
|
+
dist/
|
|
19
|
+
downloads/
|
|
20
|
+
eggs/
|
|
21
|
+
.eggs/
|
|
22
|
+
lib/
|
|
23
|
+
lib64/
|
|
24
|
+
parts/
|
|
25
|
+
sdist/
|
|
26
|
+
var/
|
|
27
|
+
wheels/
|
|
28
|
+
share/python-wheels/
|
|
29
|
+
*.egg-info/
|
|
30
|
+
.installed.cfg
|
|
31
|
+
*.egg
|
|
32
|
+
MANIFEST
|
|
33
|
+
|
|
34
|
+
# PyInstaller
|
|
35
|
+
# Usually these files are written by a python script from a template
|
|
36
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
37
|
+
*.manifest
|
|
38
|
+
*.spec
|
|
39
|
+
|
|
40
|
+
# Installer logs
|
|
41
|
+
pip-log.txt
|
|
42
|
+
pip-delete-this-directory.txt
|
|
43
|
+
|
|
44
|
+
# Unit test / coverage reports
|
|
45
|
+
htmlcov/
|
|
46
|
+
.tox/
|
|
47
|
+
.nox/
|
|
48
|
+
.coverage
|
|
49
|
+
.coverage.*
|
|
50
|
+
.cache
|
|
51
|
+
nosetests.xml
|
|
52
|
+
coverage.xml
|
|
53
|
+
*.cover
|
|
54
|
+
*.py,cover
|
|
55
|
+
.hypothesis/
|
|
56
|
+
.pytest_cache/
|
|
57
|
+
cover/
|
|
58
|
+
|
|
59
|
+
# Translations
|
|
60
|
+
*.mo
|
|
61
|
+
*.pot
|
|
62
|
+
|
|
63
|
+
# Django stuff:
|
|
64
|
+
*.log
|
|
65
|
+
local_settings.py
|
|
66
|
+
db.sqlite3
|
|
67
|
+
db.sqlite3-journal
|
|
68
|
+
|
|
69
|
+
# Flask stuff:
|
|
70
|
+
instance/
|
|
71
|
+
.webassets-cache
|
|
72
|
+
|
|
73
|
+
# Scrapy stuff:
|
|
74
|
+
.scrapy
|
|
75
|
+
|
|
76
|
+
# Sphinx documentation
|
|
77
|
+
docs/_build/
|
|
78
|
+
|
|
79
|
+
# PyBuilder
|
|
80
|
+
.pybuilder/
|
|
81
|
+
target/
|
|
82
|
+
|
|
83
|
+
# Jupyter Notebook
|
|
84
|
+
.ipynb_checkpoints
|
|
85
|
+
|
|
86
|
+
# IPython
|
|
87
|
+
profile_default/
|
|
88
|
+
ipython_config.py
|
|
89
|
+
|
|
90
|
+
# pyenv
|
|
91
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
92
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
93
|
+
# .python-version
|
|
94
|
+
|
|
95
|
+
# pipenv
|
|
96
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
97
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
98
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
99
|
+
# install all needed dependencies.
|
|
100
|
+
#Pipfile.lock
|
|
101
|
+
|
|
102
|
+
# poetry
|
|
103
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
104
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
105
|
+
# commonly ignored for libraries.
|
|
106
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
107
|
+
#poetry.lock
|
|
108
|
+
|
|
109
|
+
# pdm
|
|
110
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
111
|
+
#pdm.lock
|
|
112
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
113
|
+
# in version control.
|
|
114
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
115
|
+
.pdm.toml
|
|
116
|
+
|
|
117
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
118
|
+
__pypackages__/
|
|
119
|
+
|
|
120
|
+
# Celery stuff
|
|
121
|
+
celerybeat-schedule
|
|
122
|
+
celerybeat.pid
|
|
123
|
+
|
|
124
|
+
# SageMath parsed files
|
|
125
|
+
*.sage.py
|
|
126
|
+
|
|
127
|
+
# Environments
|
|
128
|
+
.env
|
|
129
|
+
.venv
|
|
130
|
+
env/
|
|
131
|
+
venv/
|
|
132
|
+
ENV/
|
|
133
|
+
env.bak/
|
|
134
|
+
venv.bak/
|
|
135
|
+
|
|
136
|
+
# Spyder project settings
|
|
137
|
+
.spyderproject
|
|
138
|
+
.spyproject
|
|
139
|
+
|
|
140
|
+
# Rope project settings
|
|
141
|
+
.ropeproject
|
|
142
|
+
|
|
143
|
+
# mkdocs documentation
|
|
144
|
+
/site
|
|
145
|
+
|
|
146
|
+
# mypy
|
|
147
|
+
.mypy_cache/
|
|
148
|
+
.dmypy.json
|
|
149
|
+
dmypy.json
|
|
150
|
+
|
|
151
|
+
# Pyre type checker
|
|
152
|
+
.pyre/
|
|
153
|
+
|
|
154
|
+
# pytype static type analyzer
|
|
155
|
+
.pytype/
|
|
156
|
+
|
|
157
|
+
# Cython debug symbols
|
|
158
|
+
cython_debug/
|
|
159
|
+
|
|
160
|
+
# PyCharm
|
|
161
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
162
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
163
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
164
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
165
|
+
#.idea/
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026-present Chris O'Neill <chris@purplejay.io>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pjdev-sn-sdk
|
|
3
|
+
Version: 4.6.4
|
|
4
|
+
Project-URL: Documentation, https://gitlab.purplejay.io/keystone/python/-/tree/main/pjdev-pydantic/README.md
|
|
5
|
+
Project-URL: Issues, https://gitlab.purplejay.io/keystone/python/-/issues
|
|
6
|
+
Project-URL: Source, https://gitlab.purplejay.io/keystone/python
|
|
7
|
+
Author-email: Purple Jay LLC <developers@purplejay.io>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE.txt
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Programming Language :: Python
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
14
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
15
|
+
Requires-Python: >=3.12
|
|
16
|
+
Requires-Dist: httpx
|
|
17
|
+
Requires-Dist: loguru
|
|
18
|
+
Requires-Dist: pydantic-settings>=2.13.1
|
|
19
|
+
Requires-Dist: pydantic>=2.12.5
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
22
|
+
Provides-Extra: test
|
|
23
|
+
Requires-Dist: coverage; extra == 'test'
|
|
24
|
+
Requires-Dist: pytest; extra == 'test'
|
|
25
|
+
Requires-Dist: pytz; extra == 'test'
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# pjdev-sn-sdk
|
|
29
|
+
|
|
30
|
+
[](https://pypi.org/project/pjdev-sn-sdk)
|
|
31
|
+
[](https://pypi.org/project/pjdev-sn-sdk)
|
|
32
|
+
|
|
33
|
+
-----
|
|
34
|
+
|
|
35
|
+
## Table of Contents
|
|
36
|
+
|
|
37
|
+
- [Installation](#installation)
|
|
38
|
+
- [License](#license)
|
|
39
|
+
|
|
40
|
+
## Installation
|
|
41
|
+
|
|
42
|
+
```console
|
|
43
|
+
pip install pjdev-sn-sdk
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
`pjdev-sn-sdk` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# pjdev-sn-sdk
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/pjdev-sn-sdk)
|
|
4
|
+
[](https://pypi.org/project/pjdev-sn-sdk)
|
|
5
|
+
|
|
6
|
+
-----
|
|
7
|
+
|
|
8
|
+
## Table of Contents
|
|
9
|
+
|
|
10
|
+
- [Installation](#installation)
|
|
11
|
+
- [License](#license)
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```console
|
|
16
|
+
pip install pjdev-sn-sdk
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## License
|
|
20
|
+
|
|
21
|
+
`pjdev-sn-sdk` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pjdev-sn-sdk"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = ''
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
keywords = []
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "Purple Jay LLC", email = "developers@purplejay.io" },
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Programming Language :: Python",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: Implementation :: CPython",
|
|
21
|
+
"Programming Language :: Python :: Implementation :: PyPy",
|
|
22
|
+
]
|
|
23
|
+
dependencies = [
|
|
24
|
+
"httpx",
|
|
25
|
+
"loguru",
|
|
26
|
+
"pydantic-settings>=2.13.1",
|
|
27
|
+
"pydantic>=2.12.5",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
dev = [
|
|
32
|
+
"ruff",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
test = [
|
|
36
|
+
"pytest",
|
|
37
|
+
"coverage",
|
|
38
|
+
"pytz"
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.urls]
|
|
42
|
+
Documentation = "https://gitlab.purplejay.io/keystone/python/-/tree/main/pjdev-pydantic/README.md"
|
|
43
|
+
Issues = "https://gitlab.purplejay.io/keystone/python/-/issues"
|
|
44
|
+
Source = "https://gitlab.purplejay.io/keystone/python"
|
|
45
|
+
|
|
46
|
+
[tool.hatch.version]
|
|
47
|
+
path = "src/pjdev_sn_sdk/__about__.py"
|
|
48
|
+
|
|
49
|
+
[tool.hatch.envs.types]
|
|
50
|
+
extra-dependencies = [
|
|
51
|
+
"mypy>=1.0.0",
|
|
52
|
+
]
|
|
53
|
+
[tool.hatch.envs.types.scripts]
|
|
54
|
+
check = "mypy --install-types --non-interactive {args:src/pjdev_sn_sdk tests}"
|
|
55
|
+
|
|
56
|
+
[tool.coverage.run]
|
|
57
|
+
source_pkgs = ["pjdev_sn_sdk", "tests"]
|
|
58
|
+
branch = true
|
|
59
|
+
parallel = true
|
|
60
|
+
omit = [
|
|
61
|
+
"src/pjdev_sn_sdk/__about__.py",
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
[tool.coverage.paths]
|
|
65
|
+
pjdev_sn_sdk = ["src/pjdev_sn_sdk", "*/pjdev-sn-sdk/src/pjdev_sn_sdk"]
|
|
66
|
+
tests = ["tests", "*/pjdev-sn-sdk/tests"]
|
|
67
|
+
|
|
68
|
+
[tool.coverage.report]
|
|
69
|
+
exclude_lines = [
|
|
70
|
+
"no cov",
|
|
71
|
+
"if __name__ == .__main__.:",
|
|
72
|
+
"if TYPE_CHECKING:",
|
|
73
|
+
]
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import time
|
|
3
|
+
from functools import wraps
|
|
4
|
+
from typing import Optional, List, Any
|
|
5
|
+
|
|
6
|
+
import httpx
|
|
7
|
+
from httpx import HTTPStatusError
|
|
8
|
+
from loguru import logger
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
async def log_server_addr(response):
|
|
12
|
+
network_stream_ext = response.extensions["network_stream"]
|
|
13
|
+
logger.debug(f"Client IP, Port: {network_stream_ext.get_extra_info('client_addr')}")
|
|
14
|
+
logger.debug(f"Server IP, Port: {network_stream_ext.get_extra_info('server_addr')}")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
async def log_request_headers(request):
|
|
18
|
+
header_keys = [k for k in request.headers.keys()]
|
|
19
|
+
logger.debug("Request event hook - Headers: " + " | ".join(header_keys))
|
|
20
|
+
logger.debug(
|
|
21
|
+
f"Request event hook: {request.method} {request.url} - Waiting for response"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
async def log_response_headers(response):
|
|
26
|
+
request = response.request
|
|
27
|
+
header_keys = [k for k in response.headers.keys()]
|
|
28
|
+
logger.debug("Request event hook - Headers: " + " | ".join(header_keys))
|
|
29
|
+
logger.debug(
|
|
30
|
+
f"Response event hook: Content-Type -> {response.headers.get('content-type')}"
|
|
31
|
+
)
|
|
32
|
+
logger.debug(
|
|
33
|
+
f"Response event hook: Content-encoding -> {response.headers.get('content-encoding')}"
|
|
34
|
+
)
|
|
35
|
+
logger.debug(
|
|
36
|
+
f"Response event hook: transfer-encoding -> {response.headers.get('transfer-encoding')}"
|
|
37
|
+
)
|
|
38
|
+
logger.debug(f"Response event hook: Server -> {response.headers.get('server')}")
|
|
39
|
+
logger.debug(
|
|
40
|
+
f"Response event hook: {request.method} {request.url} - Status {response.status_code}"
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def get_error_message(eg: ExceptionGroup) -> str:
|
|
45
|
+
error_messages = []
|
|
46
|
+
for e in eg.exceptions:
|
|
47
|
+
if isinstance(e, httpx.HTTPStatusError):
|
|
48
|
+
error_messages.append(f"{e.response.status_code} -> " + e.response.text)
|
|
49
|
+
else:
|
|
50
|
+
error_messages.append(str(e))
|
|
51
|
+
error = " | ".join(f"{e}" for e in error_messages)
|
|
52
|
+
logger.error(error)
|
|
53
|
+
|
|
54
|
+
return error
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def retry_http(max_attempts: int, delay_seconds: int):
|
|
58
|
+
def decorator(func):
|
|
59
|
+
@wraps(func)
|
|
60
|
+
def wrapper(*args, **kwargs):
|
|
61
|
+
attempts = 0
|
|
62
|
+
while attempts < max_attempts:
|
|
63
|
+
try:
|
|
64
|
+
return func(*args, **kwargs)
|
|
65
|
+
except Exception as e:
|
|
66
|
+
attempts += 1
|
|
67
|
+
logger.warning(e)
|
|
68
|
+
logger.warning(f"Attempt {attempts}/{max_attempts} failed: {e}")
|
|
69
|
+
time.sleep(delay_seconds**attempts)
|
|
70
|
+
raise Exception(f"Failed after {max_attempts} attempts")
|
|
71
|
+
|
|
72
|
+
return wrapper
|
|
73
|
+
|
|
74
|
+
return decorator
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def async_retry_http(
|
|
78
|
+
max_attempts: int,
|
|
79
|
+
delay_seconds: int,
|
|
80
|
+
default_value: Optional[Any] = None,
|
|
81
|
+
status_codes_to_ignore: Optional[List[int]] = None,
|
|
82
|
+
):
|
|
83
|
+
def decorator(func):
|
|
84
|
+
@wraps(func)
|
|
85
|
+
async def wrapper(*args, **kwargs):
|
|
86
|
+
attempts = 0
|
|
87
|
+
exceptions = []
|
|
88
|
+
|
|
89
|
+
while attempts < max_attempts:
|
|
90
|
+
try:
|
|
91
|
+
return await func(*args, **kwargs)
|
|
92
|
+
except HTTPStatusError as e:
|
|
93
|
+
logger.warning(e)
|
|
94
|
+
logger.warning(
|
|
95
|
+
f"{e.response.status_code}: {e.response.reason_phrase}"
|
|
96
|
+
)
|
|
97
|
+
logger.warning(f"{e.response.text}")
|
|
98
|
+
exceptions.append(e)
|
|
99
|
+
if (
|
|
100
|
+
status_codes_to_ignore
|
|
101
|
+
and e.response.status_code in status_codes_to_ignore
|
|
102
|
+
):
|
|
103
|
+
break
|
|
104
|
+
except Exception as e:
|
|
105
|
+
logger.warning("unexpected exception")
|
|
106
|
+
logger.warning(e)
|
|
107
|
+
exceptions.append(e)
|
|
108
|
+
|
|
109
|
+
attempts += 1
|
|
110
|
+
if attempts == max_attempts:
|
|
111
|
+
break
|
|
112
|
+
logger.warning(f"Attempt {attempts}/{max_attempts} failed")
|
|
113
|
+
total_delay = delay_seconds**attempts
|
|
114
|
+
logger.warning(f"Retrying in {total_delay} seconds...")
|
|
115
|
+
await asyncio.sleep(total_delay)
|
|
116
|
+
if default_value is None:
|
|
117
|
+
raise ExceptionGroup(
|
|
118
|
+
f"Failed after {max_attempts} attempts", exceptions
|
|
119
|
+
)
|
|
120
|
+
logger.error(f"Failed after {max_attempts} attempts")
|
|
121
|
+
return default_value
|
|
122
|
+
|
|
123
|
+
return wrapper
|
|
124
|
+
|
|
125
|
+
return decorator
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def record_time(log: bool = False):
|
|
129
|
+
def decorator(func):
|
|
130
|
+
if asyncio.iscoroutinefunction(func):
|
|
131
|
+
|
|
132
|
+
@wraps(func)
|
|
133
|
+
async def wrap_func(*args, **kwargs):
|
|
134
|
+
t1 = time.time()
|
|
135
|
+
result = await func(*args, **kwargs)
|
|
136
|
+
t2 = time.time()
|
|
137
|
+
if log:
|
|
138
|
+
logger.info(
|
|
139
|
+
f"Function {func.__name__!r} executed in {(t2 - t1):.2f}s"
|
|
140
|
+
)
|
|
141
|
+
return result
|
|
142
|
+
|
|
143
|
+
return wrap_func
|
|
144
|
+
|
|
145
|
+
else:
|
|
146
|
+
|
|
147
|
+
@wraps(func)
|
|
148
|
+
def wrap_func(*args, **kwargs):
|
|
149
|
+
t1 = time.time()
|
|
150
|
+
result = func(*args, **kwargs)
|
|
151
|
+
t2 = time.time()
|
|
152
|
+
if log:
|
|
153
|
+
logger.info(
|
|
154
|
+
f"Function {func.__name__!r} executed in {(t2 - t1):.2f}s"
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
return result
|
|
158
|
+
|
|
159
|
+
return wrap_func
|
|
160
|
+
|
|
161
|
+
return decorator
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
from pjdev_sn_sdk.models import Config
|
|
5
|
+
|
|
6
|
+
__ctx = {}
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def get_config() -> Config:
|
|
10
|
+
return __ctx["config"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
async def init(env_path: Optional[Path] = None) -> None:
|
|
14
|
+
Config.model_config.update(env_file=env_path)
|
|
15
|
+
__ctx["config"] = Config()
|
|
16
|
+
|
|
17
|
+
if __ctx["config"].output_path is None:
|
|
18
|
+
__ctx["config"].output_path = env_path / "output"
|
|
19
|
+
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
from loguru import logger
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def extract_control(ap: Optional[str]) -> Optional[str]:
|
|
8
|
+
"""
|
|
9
|
+
Extract the NIST 800-53 control_id from an assessment procedure (ap) value.
|
|
10
|
+
|
|
11
|
+
What it does:
|
|
12
|
+
- Accepts a wide variety of AP formats (case-insensitive).
|
|
13
|
+
- Removes [r5] revision markers (wherever they appear).
|
|
14
|
+
- Parses the control family (e.g., AC) and control number (e.g., 2), removing leading zeros.
|
|
15
|
+
- Captures only the immediate numeric enhancement in parentheses (e.g., (3)).
|
|
16
|
+
- Ignores objective letters like (a), (b), roman numerals, dot suffixes like .01, and other notes.
|
|
17
|
+
|
|
18
|
+
Examples:
|
|
19
|
+
- "AC-2(3)(a)" -> "AC-02(03)"
|
|
20
|
+
- "[r5] ac-02 (03)" -> "AC-02(03)"
|
|
21
|
+
- "PM-16 (01) (b)" -> "PM-16(01)"
|
|
22
|
+
- "RA-5(5)(A)(1)" -> "RA-05(05)"
|
|
23
|
+
- "SC-7 (12).01(a)" -> "SC-07(12)"
|
|
24
|
+
- "AU-3" -> "AU-03"
|
|
25
|
+
- "invalid" -> None
|
|
26
|
+
|
|
27
|
+
:param ap: The assessment procedure string.
|
|
28
|
+
:return: The related control_id (e.g., 'AC-2' or 'AC-2(3)') or None if it can't be parsed.
|
|
29
|
+
"""
|
|
30
|
+
if not ap:
|
|
31
|
+
return None
|
|
32
|
+
|
|
33
|
+
s = ap.strip()
|
|
34
|
+
if not s:
|
|
35
|
+
return None
|
|
36
|
+
|
|
37
|
+
# Remove revision markers like [r5] anywhere in the string (case-insensitive).
|
|
38
|
+
s = re.sub(r"\[\s*r?\s*5\s*\]", "", s, flags=re.IGNORECASE)
|
|
39
|
+
|
|
40
|
+
# Match the base control family and number (e.g., AC-2, CM-06, etc.)
|
|
41
|
+
# NOTE: Use a negative lookahead to avoid requiring a word boundary after the number.
|
|
42
|
+
# This allows inputs like "CA-05b" to match the base "CA-05" while excluding trailing digits.
|
|
43
|
+
base_match = re.search(r"(?i)\b([A-Z]{2})\s*-\s*0*([0-9]{1,3})(?!\d)", s)
|
|
44
|
+
|
|
45
|
+
if not base_match:
|
|
46
|
+
logger.debug(f"Could not find control family and number in AP: {ap}")
|
|
47
|
+
return None
|
|
48
|
+
|
|
49
|
+
family = base_match.group(1).upper()
|
|
50
|
+
number = str(int(base_match.group(2))) # strip any leading zeros by int conversion
|
|
51
|
+
control_id = f"{family}-{'0' if len(number) == 1 else ''}{number}"
|
|
52
|
+
|
|
53
|
+
# Starting right after the base match, capture immediate numeric parentheses like (03) -> (3).
|
|
54
|
+
idx = base_match.end()
|
|
55
|
+
paren_token = re.compile(r"\s*\(\s*([^)]+)\s*\)")
|
|
56
|
+
|
|
57
|
+
while True:
|
|
58
|
+
m = paren_token.match(s, idx)
|
|
59
|
+
if not m:
|
|
60
|
+
break
|
|
61
|
+
|
|
62
|
+
token = m.group(1)
|
|
63
|
+
|
|
64
|
+
# Only accept purely numeric tokens as part of the control identifier.
|
|
65
|
+
# Stop at the first non-numeric token (e.g., letters like (a), roman numerals, etc.).
|
|
66
|
+
if re.fullmatch(r"0*\d+", token):
|
|
67
|
+
sub_number = f"{int(token)}"
|
|
68
|
+
control_id += f"({'0' if len(sub_number) == 1 else ''}{sub_number})" # normalize 0-padded numbers
|
|
69
|
+
idx = m.end()
|
|
70
|
+
# Continue in case there is another numeric enhancement directly chained.
|
|
71
|
+
continue
|
|
72
|
+
else:
|
|
73
|
+
break
|
|
74
|
+
|
|
75
|
+
return control_id
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def strip_leading_zeros_after_dash_or_dot(s: str) -> str:
|
|
79
|
+
return re.sub(r"(?<=[-.(])0+(?=\d)", "", s)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def extract_control_requirement(ap: Optional[str]) -> Optional[str]:
|
|
83
|
+
if not ap:
|
|
84
|
+
return None
|
|
85
|
+
|
|
86
|
+
control_id = extract_control(ap=ap)
|
|
87
|
+
if not control_id:
|
|
88
|
+
return None
|
|
89
|
+
|
|
90
|
+
sn_control_id = strip_leading_zeros_after_dash_or_dot(control_id)
|
|
91
|
+
stripped_ap = strip_leading_zeros_after_dash_or_dot(ap).upper()
|
|
92
|
+
|
|
93
|
+
part = stripped_ap.removeprefix(sn_control_id).removeprefix(".").split(".")[0]
|
|
94
|
+
|
|
95
|
+
if not part:
|
|
96
|
+
return None
|
|
97
|
+
|
|
98
|
+
return f"{sn_control_id}.{part.strip().lower()}"
|