kisa-utils 0.39.0__py3-none-any.whl → 0.39.2__py3-none-any.whl
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.
- kisa_utils/servers/flask.py +35 -28
- kisa_utils/storage.py +11 -1
- {kisa_utils-0.39.0.dist-info → kisa_utils-0.39.2.dist-info}/METADATA +1 -1
- {kisa_utils-0.39.0.dist-info → kisa_utils-0.39.2.dist-info}/RECORD +6 -6
- {kisa_utils-0.39.0.dist-info → kisa_utils-0.39.2.dist-info}/WHEEL +0 -0
- {kisa_utils-0.39.0.dist-info → kisa_utils-0.39.2.dist-info}/top_level.txt +0 -0
kisa_utils/servers/flask.py
CHANGED
|
@@ -445,8 +445,7 @@ def endpoint(
|
|
|
445
445
|
def decorator(func):
|
|
446
446
|
# in case the decorator is called as `@endpoint` as opposed to `@endpoint(...)`
|
|
447
447
|
nonlocal name, group, methods , security
|
|
448
|
-
|
|
449
|
-
|
|
448
|
+
|
|
450
449
|
if callable(name):
|
|
451
450
|
name, group = '', ''
|
|
452
451
|
|
|
@@ -455,6 +454,8 @@ def endpoint(
|
|
|
455
454
|
if responseHeaders and not (resp := __verifyResponseHeaders(responseHeaders)):
|
|
456
455
|
raise ValueError(resp.log)
|
|
457
456
|
|
|
457
|
+
__DEFAULTS = {}
|
|
458
|
+
|
|
458
459
|
if 1:
|
|
459
460
|
_group = group + '/' if group else ''
|
|
460
461
|
_name = '/' + _group + (name or func.__name__)
|
|
@@ -526,10 +527,15 @@ def endpoint(
|
|
|
526
527
|
raise ValueError(
|
|
527
528
|
f'function `{func.__name__}`: *{key} or **{key} not allowed in function signature')
|
|
528
529
|
|
|
530
|
+
# print(f'<{key}> <{value}> <{value.default}> <{inspect._empty==value.default}>')
|
|
531
|
+
|
|
529
532
|
hint = typeHints.get(key, None)
|
|
530
533
|
if not hint:
|
|
531
534
|
raise TypeError(f"parameter {key} has no type hint")
|
|
532
535
|
|
|
536
|
+
if inspect._empty != value.default:
|
|
537
|
+
__DEFAULTS[key] = value.default
|
|
538
|
+
|
|
533
539
|
# print(typeHints)
|
|
534
540
|
if "headers" in typeHints and "headers" in _WHITE_LISTED_PARAMS:
|
|
535
541
|
del typeHints["headers"]
|
|
@@ -543,7 +549,8 @@ def endpoint(
|
|
|
543
549
|
"handler": handler,
|
|
544
550
|
"expectedStructure": validationStructure,
|
|
545
551
|
"path": __getHandlerRelativePath(handler),
|
|
546
|
-
"docs": handler.__doc__
|
|
552
|
+
"docs": handler.__doc__,
|
|
553
|
+
"defaults": __DEFAULTS,
|
|
547
554
|
}
|
|
548
555
|
setEndpoints(__group, _name, new_entry)
|
|
549
556
|
|
|
@@ -551,7 +558,7 @@ def endpoint(
|
|
|
551
558
|
def w():
|
|
552
559
|
nonlocal handler, new_entry
|
|
553
560
|
|
|
554
|
-
resp = process_request_data(request, new_entry["expectedStructure"])
|
|
561
|
+
resp = process_request_data(request, new_entry["expectedStructure"], defaults=copy.deepcopy(__DEFAULTS))
|
|
555
562
|
if not resp:
|
|
556
563
|
return resp
|
|
557
564
|
|
|
@@ -664,7 +671,7 @@ def _normalize_and_validate_methods(methods: list[str], /) -> list:
|
|
|
664
671
|
return normalized_methods
|
|
665
672
|
|
|
666
673
|
|
|
667
|
-
def process_request_data(request, expected_structure:dict) -> Response:
|
|
674
|
+
def process_request_data(request, expected_structure:dict, *, defaults:dict = {}) -> Response:
|
|
668
675
|
"""
|
|
669
676
|
Process incoming request data.
|
|
670
677
|
|
|
@@ -672,7 +679,8 @@ def process_request_data(request, expected_structure:dict) -> Response:
|
|
|
672
679
|
request: the request to process
|
|
673
680
|
expected_structure(dict): the structure against which to process the request data
|
|
674
681
|
"""
|
|
675
|
-
urlData =
|
|
682
|
+
urlData = defaults
|
|
683
|
+
urlData.update(request.values.to_dict())
|
|
676
684
|
jsonData = {}
|
|
677
685
|
|
|
678
686
|
if request.is_json:
|
|
@@ -864,6 +872,8 @@ def generate_html(data:dict[str, dict[str, Any]]) -> str:
|
|
|
864
872
|
html += "<pre style='background-color: #272822; color: #f8f8f2; padding: 10px; border-radius: 5px; font-size: 0.9em; overflow-x: auto;'>"
|
|
865
873
|
html += "{<br>"
|
|
866
874
|
|
|
875
|
+
__defaults = details['defaults']
|
|
876
|
+
|
|
867
877
|
for key, val in details['expectedStructure'].items():
|
|
868
878
|
# print(key, val, isinstance(val, type))
|
|
869
879
|
expandStructure:bool = False
|
|
@@ -878,7 +888,7 @@ def generate_html(data:dict[str, dict[str, Any]]) -> str:
|
|
|
878
888
|
_type = type(val).__name__ #+ f'\n {val} \n'.replace("'",'').replace('class ','').replace('<','').replace('>','')
|
|
879
889
|
expandStructure = True
|
|
880
890
|
|
|
881
|
-
html += f"<span style='color: #66d9ef;'> '{key}'</span>: <span style='color: #a6e22e;'>{_type}</span
|
|
891
|
+
html += f"<span style='color: #66d9ef;'> '{key}'</span>: <span style='color: #a6e22e;'>{_type}</span>,"
|
|
882
892
|
if expandStructure:
|
|
883
893
|
expanded:str = f'{val}'\
|
|
884
894
|
.replace("'",'')\
|
|
@@ -892,7 +902,19 @@ def generate_html(data:dict[str, dict[str, Any]]) -> str:
|
|
|
892
902
|
indentation = ' '*(len(type(val).__name__)+len(':'))
|
|
893
903
|
expanded = __customPrettyPrint(expanded).replace('\n',f'<br>{indentation}')
|
|
894
904
|
|
|
895
|
-
html += f"<span style='color: #66d9ef;'>{indentation}</span> <span style='color: #fae22e;'>{expanded}</span
|
|
905
|
+
html += f"<br><span style='color: #66d9ef;'>{indentation}</span> <span style='color: #fae22e;'>{expanded}</span>"
|
|
906
|
+
|
|
907
|
+
if key in __defaults:
|
|
908
|
+
trailingComma = False
|
|
909
|
+
if html[-1] in [',']:
|
|
910
|
+
html = html[:-1]
|
|
911
|
+
trailingComma = True
|
|
912
|
+
|
|
913
|
+
html += " <span style='color: #ef66d9; font-weight:bold;'>=</span>"
|
|
914
|
+
html += f" <span style='color: #ffa000; font-weight:bold;'>{__defaults[key]}</span>"
|
|
915
|
+
if trailingComma: html += ','
|
|
916
|
+
|
|
917
|
+
html += "<br>"
|
|
896
918
|
|
|
897
919
|
html += "}</pre></p></div>"
|
|
898
920
|
|
|
@@ -1012,27 +1034,12 @@ def conform_dict(data: Dict[str, Any], structure: Dict[str, Any]) -> Response:
|
|
|
1012
1034
|
continue
|
|
1013
1035
|
|
|
1014
1036
|
# Handle union types (e.g., int | float)
|
|
1037
|
+
if not (_resp := validateWithResponse(value, expected_type)):
|
|
1038
|
+
return Error(f'key `{key}`: {_resp.log}')
|
|
1039
|
+
result[key] = value
|
|
1040
|
+
|
|
1015
1041
|
origin = get_origin(expected_type)
|
|
1016
|
-
|
|
1017
|
-
allowed_types = get_args(expected_type)
|
|
1018
|
-
converted = None
|
|
1019
|
-
|
|
1020
|
-
for t in allowed_types:
|
|
1021
|
-
# print("t======>", t)
|
|
1022
|
-
try:
|
|
1023
|
-
converted = convert_value(value, t)
|
|
1024
|
-
break
|
|
1025
|
-
except (ValueError, TypeError):
|
|
1026
|
-
continue
|
|
1027
|
-
|
|
1028
|
-
if converted is None:
|
|
1029
|
-
return Error(
|
|
1030
|
-
f"Value '{value}' for key '{key}' couldn't be converted to any of {allowed_types}"
|
|
1031
|
-
)
|
|
1032
|
-
result[key] = converted
|
|
1033
|
-
else:
|
|
1034
|
-
result[key] = convert_value(value, expected_type)
|
|
1035
|
-
|
|
1042
|
+
|
|
1036
1043
|
return Ok(result)
|
|
1037
1044
|
|
|
1038
1045
|
def convert_value(value:Any, target_type:type)->Any:
|
kisa_utils/storage.py
CHANGED
|
@@ -254,5 +254,15 @@ class Path:
|
|
|
254
254
|
reply['status'] = True
|
|
255
255
|
return reply
|
|
256
256
|
|
|
257
|
+
@staticmethod
|
|
258
|
+
def getAbsolutePath(path:str) -> str:
|
|
259
|
+
'''
|
|
260
|
+
get absolute path to file
|
|
261
|
+
Args:
|
|
262
|
+
path(str): path, relative or otherwise whose absolute path we need
|
|
263
|
+
'''
|
|
264
|
+
return os.path.abspath(path)
|
|
265
|
+
|
|
266
|
+
|
|
257
267
|
if __name__=='__main__':
|
|
258
|
-
|
|
268
|
+
print(Path.getAbsolutePath('.'))
|
|
@@ -13,17 +13,17 @@ kisa_utils/queues.py,sha256=9QqPtDujw6tbWk7uUiXrsd0rVBTIkzeQw9b45l5Fo3k,6502
|
|
|
13
13
|
kisa_utils/remote.py,sha256=0RDrfC4RUW4m6JLziC0_EXJYqzWp38Rw8NDroJ0MuqI,2149
|
|
14
14
|
kisa_utils/response.py,sha256=FusfDTBn7gOMqt34XkiUf4d1lWn42_c1n_61wJoPKYU,4182
|
|
15
15
|
kisa_utils/standardize.py,sha256=nt-uzHQFoKxGscD_MpDYXw65Teg3724whAqa6Kh_zhE,2231
|
|
16
|
-
kisa_utils/storage.py,sha256=
|
|
16
|
+
kisa_utils/storage.py,sha256=jwQXnGF5PgbBeoOxNzhjnWMsa3gPHs60lnM4U7GTYz8,7961
|
|
17
17
|
kisa_utils/threads.py,sha256=qQqsf64YHMyLpboq5AEXKxYqf3iXUhxiJe6Ymg-vlxI,12840
|
|
18
18
|
kisa_utils/token.py,sha256=Y2qglWYWpmHxoXBh-TH0r1as0uPV5LLqMNcunLvM4vM,7850
|
|
19
19
|
kisa_utils/permissions/__config__.py,sha256=i3ELkOydDnjKx2ozQTxLZdZ8DXSeUncnl2kRxANjFmM,613
|
|
20
20
|
kisa_utils/permissions/__init__.py,sha256=q7LGl26f-MPXkLS6nxBKDotW3xdB8y7pI5S_Oo5fPOw,47976
|
|
21
21
|
kisa_utils/servers/__init__.py,sha256=lPqDyGTrFo0qwPZ2WA9Xtcpc5D8AIU4huqgFx1iZf68,19
|
|
22
|
-
kisa_utils/servers/flask.py,sha256=
|
|
22
|
+
kisa_utils/servers/flask.py,sha256=XZYY1pWnP1mSvaS5Uv8G3EFJV5BJBQtU2gDbO8suvLc,40422
|
|
23
23
|
kisa_utils/structures/__init__.py,sha256=JBU1j3A42jQ62ALKnsS1Hav9YXcYwjDw1wQJtohXPbU,83
|
|
24
24
|
kisa_utils/structures/utils.py,sha256=665rXIapGwFqejizeJwy3DryeskCQOdgP25BCdLkGvk,2898
|
|
25
25
|
kisa_utils/structures/validator.py,sha256=Y4UmB4TH7N-GkK22EV1WOsPWjTeqxVWLTentl1keZD4,4053
|
|
26
|
-
kisa_utils-0.39.
|
|
27
|
-
kisa_utils-0.39.
|
|
28
|
-
kisa_utils-0.39.
|
|
29
|
-
kisa_utils-0.39.
|
|
26
|
+
kisa_utils-0.39.2.dist-info/METADATA,sha256=j2dylGQQjjrHyieDm8Lj64rpK10zKYsYE3CT2LnzwFQ,477
|
|
27
|
+
kisa_utils-0.39.2.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
|
|
28
|
+
kisa_utils-0.39.2.dist-info/top_level.txt,sha256=URxY4sRuqmirOxWtztpVmPoGQdksEMYO6hmYsEDGz2Y,75
|
|
29
|
+
kisa_utils-0.39.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|