Skip to content

Commit 8959b4e

Browse files
authored
Merge pull request #83 from 0hsn/feature/variables-http-files-local-variables
Feature/variables http files local variables
2 parents d974850 + c546002 commit 8959b4e

19 files changed

Lines changed: 404 additions & 159 deletions

File tree

‎Pipfile‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ verify_ssl = true
44
name = "pypi"
55

66
[packages]
7-
astroid = "~=2.9.3"
8-
certifi = "~=2021.10.8"
9-
charset-normalizer = "~=2.0.10"
10-
click = "~=8.0.3"
11-
dotmap = "~=1.3.26"
12-
idna = "~=3.3"
13-
isort = "~=5.10.1"
14-
lazy-object-proxy = "~=1.7.1"
15-
mccabe = "~=0.6.1"
16-
platformdirs = "~=2.4.1"
17-
pyyaml = "~=6.0"
18-
requests = "~=2.27.1"
19-
requests-toolbelt = "~=0.9.1"
20-
toml = "~=0.10.2"
21-
typing-extensions = "~=4.0.1"
22-
urllib3 = "~=1.26.8"
23-
wrapt = "~=1.13.3"
7+
astroid = "*"
8+
certifi = "*"
9+
charset-normalizer = "*"
10+
click = "*"
11+
dotmap = "*"
12+
idna = "*"
13+
isort = "*"
14+
lazy-object-proxy = "*"
15+
mccabe = "*"
16+
platformdirs = "*"
17+
pyyaml = "*"
18+
requests = "*"
19+
requests-toolbelt = "*"
20+
toml = "*"
21+
typing-extensions = "*"
22+
urllib3 = "*"
23+
wrapt = "*"
2424
cerberus = "*"
2525

2626
[dev-packages]

‎Pipfile.lock‎

Lines changed: 94 additions & 81 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chk/infrastructure/exception.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
messages = dict(
88
fatal=dict(
9-
V0001='`version` string not found.',
10-
V0002='Invalid file format.',
11-
V0003='`{file_name}` is not a valid YAML.',
12-
V0004='Unsupported document version.',
13-
V0005='Unsupported document.',
14-
V0006='Validation failed.',
15-
V0007='Not a Callable.',
9+
V0001='Document exception: `version` string not found',
10+
V0002='Document exception: Invalid file format',
11+
V0003='Document exception: `{file_name}` is not a valid YAML',
12+
V0004='File exception: Unsupported document version',
13+
V0005='File exception: Unsupported document',
14+
V0006='File exception: Validation failed',
15+
V0007='System exception: Not a Callable',
16+
V0008='System exception: Wrong spec class',
1617
)
1718
)
1819

‎chk/infrastructure/file_loader.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
from chk.infrastructure import exception, mangle
2+
from collections import namedtuple
13
from pathlib import Path
2-
from typing import Dict
4+
from typing import Dict, Tuple
35
from yaml import safe_load
4-
from chk.infrastructure import exception
56

67

78
class ChkFileLoader:
@@ -24,3 +25,10 @@ def is_file_ok(file_name: str) -> bool:
2425

2526
raise SystemExit(exception.err_message('fatal.V0002'))
2627

28+
@staticmethod
29+
def get_mangled_name(file_name: str) -> Tuple[str, str]:
30+
return mangle.filename(file_name), mangle.uniq_sha255(file_name)
31+
32+
33+
# File context that holds file information
34+
FileContext = namedtuple('FileContext', ['filepath', 'filepath_mangled', 'filepath_hash', 'document'])

‎chk/infrastructure/mangle.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
mangle library
3+
"""
4+
5+
6+
def filename(file_name: str) -> str:
7+
"""Mangle filepath to filename string"""
8+
9+
import pathlib
10+
path = pathlib.Path(file_name)
11+
m_file = str(path.resolve())
12+
return m_file.lower().strip('/').replace('/', '-').replace('\\', '_').replace(':', '~')
13+
14+
15+
def uniq_sha255(hashable: str) -> str:
16+
"""Convert a string to hash"""
17+
import hashlib
18+
return hashlib.sha256(hashable.encode('utf-8')).hexdigest()

‎chk/infrastructure/work.py‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@
88
class WorkerContract(ABC):
99
"""Contacts for worker"""
1010

11-
@abstractmethod
12-
def __before_work__(self): pass
13-
1411
@abstractmethod
1512
def __work__(self): pass
1613

17-
@abstractmethod
18-
def __after_work__(self): pass
19-
2014

2115
def handle_worker(worker: WorkerContract):
2216
"""
@@ -26,11 +20,7 @@ def handle_worker(worker: WorkerContract):
2620
:return: bool
2721
"""
2822
# try:
29-
worker.__before_work__()
30-
worker.__work__()
31-
worker.__after_work__()
32-
33-
return True
23+
return worker.__work__()
3424

3525

3626
class ProcessorContract(ABC):

‎chk/modules/http/entities.py‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
from cerberus import Validator
2+
from chk.infrastructure.file_loader import FileContext
23
from chk.infrastructure.work import WorkerContract, ProcessorContract, handle_processor
4+
from chk.modules.http.request_helper import RequestProcessorMixin_PyRequests
35
from chk.modules.http.support import RequestMixin_V072
6+
from chk.modules.variables.support import VariableMixin_V072
47
from chk.modules.version.support import VersionMixin_V072
5-
from chk.modules.http.request_helper import RequestProcessorMixin_PyRequests
68

79

810
class HttpSpec_V072(
911
RequestProcessorMixin_PyRequests,
10-
VersionMixin_V072, RequestMixin_V072,
11-
WorkerContract, ProcessorContract):
12+
VersionMixin_V072,
13+
RequestMixin_V072,
14+
VariableMixin_V072,
15+
WorkerContract,
16+
ProcessorContract,
17+
):
1218

13-
def __init__(self, doc: dict):
14-
self.document = doc
15-
self.validator = Validator()
16-
self.response = None
19+
def __init__(self, file_ctx: FileContext):
20+
self.file_ctx, self.document, self.validator, self.response = file_ctx, file_ctx.document, Validator(), None
1721

18-
def __before_work__(self) -> None:
22+
def __work__(self) -> None:
1923
VersionMixin_V072.version_validated(self)
2024
RequestMixin_V072.request_validated(self)
25+
VariableMixin_V072.variable_validated(self)
2126

22-
def __work__(self) -> None:
23-
self.response = handle_processor(self, self.document)
24-
25-
def __after_work__(self):
26-
pass
27+
ctx_document = VariableMixin_V072.variable_process(self)
28+
self.response = handle_processor(self, ctx_document)

‎chk/modules/http/main.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
main driver
33
"""
4-
from chk.infrastructure.file_loader import ChkFileLoader
4+
from chk.infrastructure.file_loader import ChkFileLoader, FileContext
55
from chk.modules.http.presentation import ResponseToStringFormatter
66
from chk.modules.version.support import SpecificationLoader
77
from chk.infrastructure.work import handle_worker
@@ -11,8 +11,10 @@ def execute(file: str):
1111
"""execute command"""
1212
ChkFileLoader.is_file_ok(file)
1313
document = ChkFileLoader.to_dict(file)
14+
fpath_mangled, fpath_hash = ChkFileLoader.get_mangled_name(file)
1415

15-
http_spec = SpecificationLoader.to_spec_config(document)
16+
file_ctx = FileContext(file, fpath_mangled, fpath_hash, document)
17+
http_spec = SpecificationLoader.to_spec_config(file_ctx)
1618

1719
handle_worker(http_spec)
1820
fmt_str = ResponseToStringFormatter(http_spec.response)

‎chk/modules/http/request_helper.py‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010

1111
class RequestProcessorMixin_PyRequests:
12-
"""
13-
Request class that use python requests
14-
"""
12+
""" Request class that use python requests """
1513

1614
def __process__(self) -> dict:
1715
"""Make external api call"""
@@ -23,6 +21,7 @@ def __process__(self) -> dict:
2321

2422
def __before_process__(self, request_data: dict[str, object]) -> None:
2523
"""Prepare dotmap to dict before making request"""
24+
2625
if not hasattr(self, 'request_args'):
2726
raise SystemExit('ProcessorContract not inherited.')
2827

@@ -33,9 +32,7 @@ def __before_process__(self, request_data: dict[str, object]) -> None:
3332

3433

3534
class HttpRequestArgCompiler:
36-
"""
37-
HttpRequestArgCompiler
38-
"""
35+
""" HttpRequestArgCompiler """
3936

4037
@staticmethod
4138
def add_url_and_method(request_data: DotMap, request_arg: dict) -> None:

‎chk/modules/http/support.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ def request_validated(self) -> dict[str, dict]:
1717
if not self.validator.validate(request_doc, request_schema): # type: ignore
1818
raise SystemExit(err_message('fatal.V0006', extra=self.validator.errors)) # type: ignore
1919
except DocumentError as doc_err:
20-
print('RequestMixin_V072.request_validated')
2120
raise SystemExit(err_message('fatal.V0001', extra=doc_err)) from doc_err
2221
else:
2322
return request_doc # or is a success
@@ -28,6 +27,6 @@ def request_as_dict(self) -> dict[str, dict]:
2827
raise SystemExit(err_message('fatal.V0005'))
2928

3029
try:
31-
return {RequestConfigElements_V072.ROOT: dict(self.document[RequestConfigElements_V072.ROOT])} # type: ignore
32-
except:
33-
raise SystemExit(err_message('fatal.V0005'))
30+
return {key:self.document[key] for key in (RequestConfigElements_V072.ROOT, ) if key in self.document} # type: ignore
31+
except Exception as ex:
32+
raise SystemExit(err_message('fatal.V0005', extra=ex))

0 commit comments

Comments
 (0)