From 1253c421e17db53f4b240aa3743370ccdb3a103b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:02:43 -0400 Subject: [PATCH 01/34] Added the introspection package --- conf/script/src/utils/meta_prog/introspection/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 conf/script/src/utils/meta_prog/introspection/__init__.py diff --git a/conf/script/src/utils/meta_prog/introspection/__init__.py b/conf/script/src/utils/meta_prog/introspection/__init__.py new file mode 100644 index 00000000..e69de29b From 78ed4e216fb927a2d64c827a09a11a3b2463fb7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:14:42 -0400 Subject: [PATCH 02/34] Added get_nth_caller func --- .../src/utils/meta_prog/introspection/caller.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 conf/script/src/utils/meta_prog/introspection/caller.py diff --git a/conf/script/src/utils/meta_prog/introspection/caller.py b/conf/script/src/utils/meta_prog/introspection/caller.py new file mode 100644 index 00000000..beeee2de --- /dev/null +++ b/conf/script/src/utils/meta_prog/introspection/caller.py @@ -0,0 +1,17 @@ +import inspect +from types import FrameType +from typing import Final + +CALLER_MIN_N: Final[int] = 1 + + +def get_nth_caller(n: int = CALLER_MIN_N) -> FrameType: + assert n >= CALLER_MIN_N + + stack_indices = range(CALLER_MIN_N, n) + caller_frame: FrameType = inspect.currentframe() + + for frame_index in stack_indices: + caller_frame = caller_frame.f_back + + return caller_frame From 98ebce4b3fdf57bd420ca4df3048ab1442f21639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:15:09 -0400 Subject: [PATCH 03/34] Refactor --- conf/script/src/utils/meta_prog/introspection/caller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/script/src/utils/meta_prog/introspection/caller.py b/conf/script/src/utils/meta_prog/introspection/caller.py index beeee2de..ddb0d615 100644 --- a/conf/script/src/utils/meta_prog/introspection/caller.py +++ b/conf/script/src/utils/meta_prog/introspection/caller.py @@ -11,7 +11,7 @@ def get_nth_caller(n: int = CALLER_MIN_N) -> FrameType: stack_indices = range(CALLER_MIN_N, n) caller_frame: FrameType = inspect.currentframe() - for frame_index in stack_indices: + for _ in stack_indices: caller_frame = caller_frame.f_back return caller_frame From dec9e30c8a7114907adfdd2d447284cbf5217450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:16:42 -0400 Subject: [PATCH 04/34] Added get_caller func --- conf/script/src/utils/meta_prog/introspection/caller.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/conf/script/src/utils/meta_prog/introspection/caller.py b/conf/script/src/utils/meta_prog/introspection/caller.py index ddb0d615..7c882c2f 100644 --- a/conf/script/src/utils/meta_prog/introspection/caller.py +++ b/conf/script/src/utils/meta_prog/introspection/caller.py @@ -15,3 +15,7 @@ def get_nth_caller(n: int = CALLER_MIN_N) -> FrameType: caller_frame = caller_frame.f_back return caller_frame + + +def get_caller() -> FrameType: + return get_nth_caller(n=CALLER_MIN_N + 1) From 819533adee3a9be88d91c7f8096492957d994966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:18:20 -0400 Subject: [PATCH 05/34] Export caller API --- conf/script/src/utils/meta_prog/introspection/caller.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/conf/script/src/utils/meta_prog/introspection/caller.py b/conf/script/src/utils/meta_prog/introspection/caller.py index 7c882c2f..c7dee7f6 100644 --- a/conf/script/src/utils/meta_prog/introspection/caller.py +++ b/conf/script/src/utils/meta_prog/introspection/caller.py @@ -2,9 +2,12 @@ from types import FrameType from typing import Final +from utils.meta_prog.encapsulation import * + CALLER_MIN_N: Final[int] = 1 +@export def get_nth_caller(n: int = CALLER_MIN_N) -> FrameType: assert n >= CALLER_MIN_N @@ -17,5 +20,6 @@ def get_nth_caller(n: int = CALLER_MIN_N) -> FrameType: return caller_frame +@export def get_caller() -> FrameType: return get_nth_caller(n=CALLER_MIN_N + 1) From d3f3a68e6e97a37e68c8931d5230c6c4b269d37a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:18:56 -0400 Subject: [PATCH 06/34] Expose pkg API --- conf/script/src/utils/meta_prog/introspection/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conf/script/src/utils/meta_prog/introspection/__init__.py b/conf/script/src/utils/meta_prog/introspection/__init__.py index e69de29b..481dec5d 100644 --- a/conf/script/src/utils/meta_prog/introspection/__init__.py +++ b/conf/script/src/utils/meta_prog/introspection/__init__.py @@ -0,0 +1 @@ +from .caller import * From 0fbb6b56b22c3f0e7d404ce66a3885f7dafc481e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:43:06 -0400 Subject: [PATCH 07/34] Added Macro enum --- .../src/utils/meta_prog/introspection/macro.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 conf/script/src/utils/meta_prog/introspection/macro.py diff --git a/conf/script/src/utils/meta_prog/introspection/macro.py b/conf/script/src/utils/meta_prog/introspection/macro.py new file mode 100644 index 00000000..1d279733 --- /dev/null +++ b/conf/script/src/utils/meta_prog/introspection/macro.py @@ -0,0 +1,18 @@ +from enum import Enum, auto, unique +from typing import Final + + +@unique +class AutoMacroFromName(Enum): + __AFFIX: Final[str] = '__' + + def _generate_next_value_(name: str, start, count, last_values) -> str: + affixed_name = AutoMacroFromName.__AFFIX + name + AutoMacroFromName.__AFFIX + return affixed_name + + +@unique +class Macro(AutoMacroFromName): + ALL = auto() + NAME = auto() + QUALNAME = auto() From 0eca664d0278a506e6f5cd99b4363fdcb93ab60f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:45:27 -0400 Subject: [PATCH 08/34] Export Macro --- conf/script/src/utils/meta_prog/introspection/macro.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/conf/script/src/utils/meta_prog/introspection/macro.py b/conf/script/src/utils/meta_prog/introspection/macro.py index 1d279733..08ed12f2 100644 --- a/conf/script/src/utils/meta_prog/introspection/macro.py +++ b/conf/script/src/utils/meta_prog/introspection/macro.py @@ -1,6 +1,8 @@ from enum import Enum, auto, unique from typing import Final +__all__ = ['Macro'] + @unique class AutoMacroFromName(Enum): From 267143254f21f14c87abb2eefebbb01717eda114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:45:49 -0400 Subject: [PATCH 09/34] Export Macro API --- conf/script/src/utils/meta_prog/introspection/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conf/script/src/utils/meta_prog/introspection/__init__.py b/conf/script/src/utils/meta_prog/introspection/__init__.py index 481dec5d..15623ffd 100644 --- a/conf/script/src/utils/meta_prog/introspection/__init__.py +++ b/conf/script/src/utils/meta_prog/introspection/__init__.py @@ -1 +1,2 @@ from .caller import * +from .macro import * From a73872563e50f564771bdbebabe25ca957dd5ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:46:34 -0400 Subject: [PATCH 10/34] Added macro __main__ --- conf/script/src/utils/meta_prog/introspection/macro.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conf/script/src/utils/meta_prog/introspection/macro.py b/conf/script/src/utils/meta_prog/introspection/macro.py index 08ed12f2..865a5440 100644 --- a/conf/script/src/utils/meta_prog/introspection/macro.py +++ b/conf/script/src/utils/meta_prog/introspection/macro.py @@ -16,5 +16,6 @@ def _generate_next_value_(name: str, start, count, last_values) -> str: @unique class Macro(AutoMacroFromName): ALL = auto() + MAIN = auto() NAME = auto() QUALNAME = auto() From 8ece8b1a6adf27b59334eae1319e1f2ed6e3b6f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:52:53 -0400 Subject: [PATCH 11/34] Mixin str --- conf/script/src/utils/meta_prog/introspection/macro.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/script/src/utils/meta_prog/introspection/macro.py b/conf/script/src/utils/meta_prog/introspection/macro.py index 865a5440..b18826a5 100644 --- a/conf/script/src/utils/meta_prog/introspection/macro.py +++ b/conf/script/src/utils/meta_prog/introspection/macro.py @@ -5,7 +5,7 @@ @unique -class AutoMacroFromName(Enum): +class AutoMacroFromName(str, Enum): __AFFIX: Final[str] = '__' def _generate_next_value_(name: str, start, count, last_values) -> str: From df74acd0469327cec34e8ba8bac0a3e519e20c9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:53:42 -0400 Subject: [PATCH 12/34] Removed export --- conf/script/src/utils/meta_prog/introspection/caller.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conf/script/src/utils/meta_prog/introspection/caller.py b/conf/script/src/utils/meta_prog/introspection/caller.py index c7dee7f6..c8977d7e 100644 --- a/conf/script/src/utils/meta_prog/introspection/caller.py +++ b/conf/script/src/utils/meta_prog/introspection/caller.py @@ -4,10 +4,11 @@ from utils.meta_prog.encapsulation import * +__all__ = ['get_nth_caller', 'get_caller', 'is_caller_main'] + CALLER_MIN_N: Final[int] = 1 -@export def get_nth_caller(n: int = CALLER_MIN_N) -> FrameType: assert n >= CALLER_MIN_N @@ -20,6 +21,5 @@ def get_nth_caller(n: int = CALLER_MIN_N) -> FrameType: return caller_frame -@export def get_caller() -> FrameType: return get_nth_caller(n=CALLER_MIN_N + 1) From 54318f12f17bf07b37cb5a4dc39477e23e6ed39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:53:59 -0400 Subject: [PATCH 13/34] Added is_frame_main func --- conf/script/src/utils/meta_prog/introspection/caller.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/conf/script/src/utils/meta_prog/introspection/caller.py b/conf/script/src/utils/meta_prog/introspection/caller.py index c8977d7e..ea3f7159 100644 --- a/conf/script/src/utils/meta_prog/introspection/caller.py +++ b/conf/script/src/utils/meta_prog/introspection/caller.py @@ -2,7 +2,7 @@ from types import FrameType from typing import Final -from utils.meta_prog.encapsulation import * +from .macro import * __all__ = ['get_nth_caller', 'get_caller', 'is_caller_main'] @@ -23,3 +23,8 @@ def get_nth_caller(n: int = CALLER_MIN_N) -> FrameType: def get_caller() -> FrameType: return get_nth_caller(n=CALLER_MIN_N + 1) + + +def is_frame_main(frame: FrameType) -> bool: + frame_script_name = frame.f_locals[Macro.NAME] + return frame_script_name == Macro.MAIN \ No newline at end of file From e88f0a73a4b2f3a71adbbde275648e9da2c1968b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:54:50 -0400 Subject: [PATCH 14/34] Added is_caller_main --- conf/script/src/utils/meta_prog/introspection/caller.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/conf/script/src/utils/meta_prog/introspection/caller.py b/conf/script/src/utils/meta_prog/introspection/caller.py index ea3f7159..764b4d2c 100644 --- a/conf/script/src/utils/meta_prog/introspection/caller.py +++ b/conf/script/src/utils/meta_prog/introspection/caller.py @@ -27,4 +27,9 @@ def get_caller() -> FrameType: def is_frame_main(frame: FrameType) -> bool: frame_script_name = frame.f_locals[Macro.NAME] - return frame_script_name == Macro.MAIN \ No newline at end of file + return frame_script_name == Macro.MAIN + + +def is_caller_main() -> bool: + caller = get_caller() + return is_frame_main(frame=caller) From 42357cb364b262aa81fd2fa5a7396e6c02af0954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 14:56:38 -0400 Subject: [PATCH 15/34] Added TAlias_Macro_All --- conf/script/src/utils/meta_prog/introspection/macro.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/conf/script/src/utils/meta_prog/introspection/macro.py b/conf/script/src/utils/meta_prog/introspection/macro.py index b18826a5..46dbd352 100644 --- a/conf/script/src/utils/meta_prog/introspection/macro.py +++ b/conf/script/src/utils/meta_prog/introspection/macro.py @@ -1,7 +1,9 @@ from enum import Enum, auto, unique from typing import Final -__all__ = ['Macro'] +TAlias_Macro_All = list[str] + +__all__: TAlias_Macro_All = ['Macro'] @unique From 05500c62678dd4efa363dc8598a81a78d6971020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 15:02:47 -0400 Subject: [PATCH 16/34] Extracted main introspect --- .../test_location/test_msvc/test_find_msvc.py | 4 ++-- .../test_msvc/test_fetch_msvc_version.py | 4 ++-- .../src/test/test_utils/error/test_managed.py | 4 ++-- .../script/src/test/test_utils/error/test_meta.py | 4 ++-- conf/script/src/utils/cli/main.py | 15 +-------------- 5 files changed, 9 insertions(+), 22 deletions(-) diff --git a/conf/script/src/test/test_build_system/test_compiler/test_host/test_get_info/test_location/test_msvc/test_find_msvc.py b/conf/script/src/test/test_build_system/test_compiler/test_host/test_get_info/test_location/test_msvc/test_find_msvc.py index ca75806c..c95db8ea 100644 --- a/conf/script/src/test/test_build_system/test_compiler/test_host/test_get_info/test_location/test_msvc/test_find_msvc.py +++ b/conf/script/src/test/test_build_system/test_compiler/test_host/test_get_info/test_location/test_msvc/test_find_msvc.py @@ -5,7 +5,7 @@ from unittest.mock import MagicMock import build_system.cmd.compiler.host.get_info.location.msvc -import utils.cli.main +from utils.meta_prog.introspection import * class TestFindMSVC(unittest.TestCase): @@ -22,5 +22,5 @@ def test_no_arg_not_found(self, mock_vswhere: MagicMock): self.assertIsNone(result) -if utils.cli.main.is_caller_main(): +if is_caller_main(): unittest.main() diff --git a/conf/script/src/test/test_build_system/test_compiler/test_host/test_get_info/test_version/test_msvc/test_fetch_msvc_version.py b/conf/script/src/test/test_build_system/test_compiler/test_host/test_get_info/test_version/test_msvc/test_fetch_msvc_version.py index f7be362f..4cccd0ec 100644 --- a/conf/script/src/test/test_build_system/test_compiler/test_host/test_get_info/test_version/test_msvc/test_fetch_msvc_version.py +++ b/conf/script/src/test/test_build_system/test_compiler/test_host/test_get_info/test_version/test_msvc/test_fetch_msvc_version.py @@ -6,7 +6,7 @@ from unittest.mock import MagicMock import build_system.cmd.compiler.host.get_info.version.msvc -import utils.cli.main +from utils.meta_prog.introspection import * class TestFetchMSVCVersion(unittest.TestCase): @@ -45,5 +45,5 @@ def test_random_arg_return_none(self, mock_location: MagicMock, mock_vswhere: Ma assert return_value is expected_return_value -if utils.cli.main.is_caller_main(): +if is_caller_main(): unittest.main() diff --git a/conf/script/src/test/test_utils/error/test_managed.py b/conf/script/src/test/test_utils/error/test_managed.py index 1794ed28..6d36fffe 100644 --- a/conf/script/src/test/test_utils/error/test_managed.py +++ b/conf/script/src/test/test_utils/error/test_managed.py @@ -2,11 +2,11 @@ import unittest -import utils.cli.main import utils.error.format import utils.error.managed import utils.error.meta import utils.error.status +from utils.meta_prog.introspection import * class TestManage(unittest.TestCase): @@ -127,5 +127,5 @@ def __init__(self): self.assert_decorated_error_type(DecoratedError) -if utils.cli.main.is_caller_main(): +if is_caller_main(): unittest.main() diff --git a/conf/script/src/test/test_utils/error/test_meta.py b/conf/script/src/test/test_utils/error/test_meta.py index ea01d235..df9f3cbe 100644 --- a/conf/script/src/test/test_utils/error/test_meta.py +++ b/conf/script/src/test/test_utils/error/test_meta.py @@ -3,8 +3,8 @@ import abc import unittest -import utils.cli.main import utils.error.meta +from utils.meta_prog.introspection import * class TestErrorMeta(unittest.TestCase): @@ -42,5 +42,5 @@ def dummy(self): ErrorMetaImplChild() -if utils.cli.main.is_caller_main(): +if is_caller_main(): unittest.main() diff --git a/conf/script/src/utils/cli/main.py b/conf/script/src/utils/cli/main.py index 92afa5ec..567249f4 100644 --- a/conf/script/src/utils/cli/main.py +++ b/conf/script/src/utils/cli/main.py @@ -1,12 +1,7 @@ -import inspect -from types import FrameType -from typing import Callable, Final, cast +from typing import Callable import colorama -MACRO___NAME__: Final[str] = '__name__' -MACRO___NAME___MAIN: Final[str] = '__main__' - def init(): colorama.init() @@ -16,14 +11,6 @@ def deinit(): colorama.deinit() -def is_caller_main() -> bool: - # See https://stackoverflow.com/a/57712700/ - caller_frame = cast(FrameType, cast(FrameType, inspect.currentframe()).f_back) - caller_script_name = caller_frame.f_locals[MACRO___NAME__] - - return caller_script_name == MACRO___NAME___MAIN - - def wrap_main(main_func: Callable): init() main_func() From 87c533275626adc9c2d243576e44ec0d70d13ebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 15:10:47 -0400 Subject: [PATCH 17/34] Added empty WIP no_export --- conf/script/src/utils/meta_prog/encapsulation/no_export.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 conf/script/src/utils/meta_prog/encapsulation/no_export.py diff --git a/conf/script/src/utils/meta_prog/encapsulation/no_export.py b/conf/script/src/utils/meta_prog/encapsulation/no_export.py new file mode 100644 index 00000000..e69de29b From fefce0f045fdb630bd832ad7bd0ef98438eff4eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 15:11:02 -0400 Subject: [PATCH 18/34] Export no_export API inside init --- conf/script/src/utils/meta_prog/encapsulation/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conf/script/src/utils/meta_prog/encapsulation/__init__.py b/conf/script/src/utils/meta_prog/encapsulation/__init__.py index 1cc9d89a..0275de28 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/__init__.py +++ b/conf/script/src/utils/meta_prog/encapsulation/__init__.py @@ -1 +1,2 @@ from .export import * +from .no_export import * From eb25fde270068b6935df5d44c1365903d7924b54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 20:24:50 -0400 Subject: [PATCH 19/34] Added TODOs --- conf/script/src/utils/auto_print.py | 2 ++ conf/script/src/utils/error/managed.py | 1 + conf/script/src/utils/error/synthesis.py | 1 + conf/script/src/utils/meta_prog/generics/cls_proxy.py | 1 + conf/script/src/utils/meta_prog/generics/cls_wrapper.py | 1 + 5 files changed, 6 insertions(+) diff --git a/conf/script/src/utils/auto_print.py b/conf/script/src/utils/auto_print.py index 7cb219f6..7b52059d 100644 --- a/conf/script/src/utils/auto_print.py +++ b/conf/script/src/utils/auto_print.py @@ -1,5 +1,6 @@ # Consider using the dataclasses package +# TODO : functools -> wraps ? # From https://stackoverflow.com/a/33800620/2924010 def auto_repr(cls): def __repr__(self): @@ -12,6 +13,7 @@ def __repr__(self): return cls +# TODO : functools -> wraps ? # From https://stackoverflow.com/a/33800620/2924010 def auto_str(cls): def __str__(self): diff --git a/conf/script/src/utils/error/managed.py b/conf/script/src/utils/error/managed.py index f70859e6..c72ceb3d 100644 --- a/conf/script/src/utils/error/managed.py +++ b/conf/script/src/utils/error/managed.py @@ -16,6 +16,7 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) +# TODO : functools -> wraps ? class ManageClass: def __new__(cls, decorated_cls: Optional[type] = None, error_formatter_cls: Type[utils.error.format.BaseFormattedErrorMixin] = utils.error.format.FormattedErrorMixin, diff --git a/conf/script/src/utils/error/synthesis.py b/conf/script/src/utils/error/synthesis.py index 6de0905f..6c6f6637 100644 --- a/conf/script/src/utils/error/synthesis.py +++ b/conf/script/src/utils/error/synthesis.py @@ -3,6 +3,7 @@ import utils.error.cls_def import utils.error.status +# TODO : Remove, deprecated ALL_ERRORS: Final = [ utils.error.cls_def.SuccessWarning, utils.error.cls_def.UnsupportedError, diff --git a/conf/script/src/utils/meta_prog/generics/cls_proxy.py b/conf/script/src/utils/meta_prog/generics/cls_proxy.py index c6bc7fbf..32455e81 100644 --- a/conf/script/src/utils/meta_prog/generics/cls_proxy.py +++ b/conf/script/src/utils/meta_prog/generics/cls_proxy.py @@ -4,6 +4,7 @@ import utils.meta_prog.generics.data +# TODO : functools -> wraps ? class GenericClassProxy(utils.meta_prog.generics.data.GenericsDataMixin, utils.meta_prog.generics.cls_wrapper.GenericClassWrapperMixin): from typing import TypeVar diff --git a/conf/script/src/utils/meta_prog/generics/cls_wrapper.py b/conf/script/src/utils/meta_prog/generics/cls_wrapper.py index b0623f55..77a819f0 100644 --- a/conf/script/src/utils/meta_prog/generics/cls_wrapper.py +++ b/conf/script/src/utils/meta_prog/generics/cls_wrapper.py @@ -1,6 +1,7 @@ import utils.meta_prog.generics.cls_wrapper_data +# TODO : functools -> wraps ? class GenericClassWrapperMixin(utils.meta_prog.generics.cls_wrapper_data.GenericClassWrapperDataMixin): def __init__(self, *args, **kwargs) -> None: From 28e64fe4ab4fc10550a6eb5857970023ac655451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 09:29:19 -0400 Subject: [PATCH 20/34] Added attr_manip --- .../src/utils/meta_prog/introspection/attr_manip.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 conf/script/src/utils/meta_prog/introspection/attr_manip.py diff --git a/conf/script/src/utils/meta_prog/introspection/attr_manip.py b/conf/script/src/utils/meta_prog/introspection/attr_manip.py new file mode 100644 index 00000000..c47256ee --- /dev/null +++ b/conf/script/src/utils/meta_prog/introspection/attr_manip.py @@ -0,0 +1,11 @@ +from typing import Optional + + +def get_or_create_attr(obj, attr: str, default_val: Optional = None): + if not hasattr(obj, attr): + attr_val = default_val + setattr(obj, attr, default_val) + else: + attr_val = getattr(obj, attr) + + return attr_val From 53ad5808561128dee30727f0ba07eafd914a91c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 09:31:58 -0400 Subject: [PATCH 21/34] Fixed naming --- conf/script/src/utils/more_typing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conf/script/src/utils/more_typing.py b/conf/script/src/utils/more_typing.py index b5760be6..72ea2821 100644 --- a/conf/script/src/utils/more_typing.py +++ b/conf/script/src/utils/more_typing.py @@ -2,8 +2,8 @@ import pathlib import typing -T_PathLike_Constraints = (pathlib.Path, os.PathLike, str, bytes) -T_AnyPath_Constraints = (pathlib.Path, os.PathLike, str, bytes, type(None)) +TConstraints_PathLike = (pathlib.Path, os.PathLike, str, bytes) +TConstraints_AnyPath = (pathlib.Path, os.PathLike, str, bytes, type(None)) PathLike = typing.Union[pathlib.Path, os.PathLike, str, bytes] AnyPath = typing.Union[pathlib.Path, os.PathLike, str, bytes, type(None)] From 2d8ce2ac1dd682af0332ef86b1f83abbdfe11d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 09:34:12 -0400 Subject: [PATCH 22/34] Added generics to get_or_create_attr --- .../src/utils/meta_prog/introspection/attr_manip.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/conf/script/src/utils/meta_prog/introspection/attr_manip.py b/conf/script/src/utils/meta_prog/introspection/attr_manip.py index c47256ee..f9a78988 100644 --- a/conf/script/src/utils/meta_prog/introspection/attr_manip.py +++ b/conf/script/src/utils/meta_prog/introspection/attr_manip.py @@ -1,7 +1,11 @@ -from typing import Optional +from typing import * +T_Attr_Val = TypeVar('T_Attr_Val') + + +def get_or_create_attr(obj, attr: str, default_val: Optional[T_Attr_Val] = None) -> Optional[T_Attr_Val]: + attr_val: Optional[T_Attr_Val] -def get_or_create_attr(obj, attr: str, default_val: Optional = None): if not hasattr(obj, attr): attr_val = default_val setattr(obj, attr, default_val) From 31b104e046b4d0942cdc12d9e92f22f5d9ee23a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 09:36:01 -0400 Subject: [PATCH 23/34] Renamed func --- conf/script/src/utils/meta_prog/introspection/attr_manip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/script/src/utils/meta_prog/introspection/attr_manip.py b/conf/script/src/utils/meta_prog/introspection/attr_manip.py index f9a78988..2e0af6ab 100644 --- a/conf/script/src/utils/meta_prog/introspection/attr_manip.py +++ b/conf/script/src/utils/meta_prog/introspection/attr_manip.py @@ -3,7 +3,7 @@ T_Attr_Val = TypeVar('T_Attr_Val') -def get_or_create_attr(obj, attr: str, default_val: Optional[T_Attr_Val] = None) -> Optional[T_Attr_Val]: +def get_or_create_optional_attr(obj, attr: str, default_val: Optional[T_Attr_Val] = None) -> Optional[T_Attr_Val]: attr_val: Optional[T_Attr_Val] if not hasattr(obj, attr): From 1fa99f213415f381d3c10c8e31bcaa0f206ecc4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 09:58:59 -0400 Subject: [PATCH 24/34] Added DICT macro --- conf/script/src/utils/meta_prog/introspection/macro.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conf/script/src/utils/meta_prog/introspection/macro.py b/conf/script/src/utils/meta_prog/introspection/macro.py index 46dbd352..934ccf1d 100644 --- a/conf/script/src/utils/meta_prog/introspection/macro.py +++ b/conf/script/src/utils/meta_prog/introspection/macro.py @@ -18,6 +18,7 @@ def _generate_next_value_(name: str, start, count, last_values) -> str: @unique class Macro(AutoMacroFromName): ALL = auto() + DICT = auto() MAIN = auto() NAME = auto() QUALNAME = auto() From c71b89fbbd0d16bff7a312766df0a320ef1a1146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 10:07:19 -0400 Subject: [PATCH 25/34] Fixed macro inheritance --- conf/script/src/utils/meta_prog/introspection/macro.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/conf/script/src/utils/meta_prog/introspection/macro.py b/conf/script/src/utils/meta_prog/introspection/macro.py index 934ccf1d..a492e2bd 100644 --- a/conf/script/src/utils/meta_prog/introspection/macro.py +++ b/conf/script/src/utils/meta_prog/introspection/macro.py @@ -6,13 +6,11 @@ __all__: TAlias_Macro_All = ['Macro'] -@unique class AutoMacroFromName(str, Enum): - __AFFIX: Final[str] = '__' def _generate_next_value_(name: str, start, count, last_values) -> str: - affixed_name = AutoMacroFromName.__AFFIX + name + AutoMacroFromName.__AFFIX - return affixed_name + __AFFIX: Final[str] = '__' + return __AFFIX + name + __AFFIX @unique From 7285518737df7f86bef22c5d8c266b8ed45a1d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 10:12:44 -0400 Subject: [PATCH 26/34] Bug fix : macro values were uppercased --- conf/script/src/utils/meta_prog/introspection/macro.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/script/src/utils/meta_prog/introspection/macro.py b/conf/script/src/utils/meta_prog/introspection/macro.py index a492e2bd..849f7f4f 100644 --- a/conf/script/src/utils/meta_prog/introspection/macro.py +++ b/conf/script/src/utils/meta_prog/introspection/macro.py @@ -10,7 +10,7 @@ class AutoMacroFromName(str, Enum): def _generate_next_value_(name: str, start, count, last_values) -> str: __AFFIX: Final[str] = '__' - return __AFFIX + name + __AFFIX + return __AFFIX + name.lower() + __AFFIX @unique From b9e7ffd5c345f549970b8679dc73ce219c0197ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 10:13:33 -0400 Subject: [PATCH 27/34] Added assert --- conf/script/src/utils/meta_prog/introspection/attr_manip.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/conf/script/src/utils/meta_prog/introspection/attr_manip.py b/conf/script/src/utils/meta_prog/introspection/attr_manip.py index 2e0af6ab..5bc5f677 100644 --- a/conf/script/src/utils/meta_prog/introspection/attr_manip.py +++ b/conf/script/src/utils/meta_prog/introspection/attr_manip.py @@ -1,4 +1,5 @@ from typing import * +from macro import * T_Attr_Val = TypeVar('T_Attr_Val') @@ -6,9 +7,11 @@ def get_or_create_optional_attr(obj, attr: str, default_val: Optional[T_Attr_Val] = None) -> Optional[T_Attr_Val]: attr_val: Optional[T_Attr_Val] + assert hasattr(obj, Macro.DICT) + if not hasattr(obj, attr): attr_val = default_val - setattr(obj, attr, default_val) + setattr(obj, attr, attr_val) else: attr_val = getattr(obj, attr) From 4a0483f55662b81b875f5fa919173979e296dda5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 10:17:38 -0400 Subject: [PATCH 28/34] Added get_or_create_attr func --- .../meta_prog/introspection/attr_manip.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/conf/script/src/utils/meta_prog/introspection/attr_manip.py b/conf/script/src/utils/meta_prog/introspection/attr_manip.py index 5bc5f677..dc1f3165 100644 --- a/conf/script/src/utils/meta_prog/introspection/attr_manip.py +++ b/conf/script/src/utils/meta_prog/introspection/attr_manip.py @@ -4,6 +4,27 @@ T_Attr_Val = TypeVar('T_Attr_Val') +def get_or_create_attr(obj, attr: str, default_val: Optional[T_Attr_Val] = None) -> T_Attr_Val: + attr_val: T_Attr_Val + + assert hasattr(obj, Macro.DICT) + + if not hasattr(obj, attr): + non_none_default_val: T_Attr_Val + + if default_val is None: + non_none_default_val = T_Attr_Val() + else: + non_none_default_val = default_val + + attr_val = non_none_default_val + setattr(obj, attr, attr_val) + else: + attr_val = getattr(obj, attr) + + return attr_val + + def get_or_create_optional_attr(obj, attr: str, default_val: Optional[T_Attr_Val] = None) -> Optional[T_Attr_Val]: attr_val: Optional[T_Attr_Val] From 86b5e8136ceb193facd8f1262db256f33f65a6ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 10:20:55 -0400 Subject: [PATCH 29/34] Export TAlias --- conf/script/src/utils/meta_prog/introspection/macro.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/script/src/utils/meta_prog/introspection/macro.py b/conf/script/src/utils/meta_prog/introspection/macro.py index 849f7f4f..8c3ee0a1 100644 --- a/conf/script/src/utils/meta_prog/introspection/macro.py +++ b/conf/script/src/utils/meta_prog/introspection/macro.py @@ -3,7 +3,7 @@ TAlias_Macro_All = list[str] -__all__: TAlias_Macro_All = ['Macro'] +__all__: TAlias_Macro_All = ['Macro', 'TAlias_Macro_All'] class AutoMacroFromName(str, Enum): From c31b32e9df2f5c259ae56577f18ef060d1bdd941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 10:21:01 -0400 Subject: [PATCH 30/34] Export funcs --- conf/script/src/utils/meta_prog/introspection/attr_manip.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/conf/script/src/utils/meta_prog/introspection/attr_manip.py b/conf/script/src/utils/meta_prog/introspection/attr_manip.py index dc1f3165..e4974e42 100644 --- a/conf/script/src/utils/meta_prog/introspection/attr_manip.py +++ b/conf/script/src/utils/meta_prog/introspection/attr_manip.py @@ -1,6 +1,9 @@ from typing import * + from macro import * +__all__: TAlias_Macro_All = ['get_or_create_attr', 'get_or_create_optional_attr'] + T_Attr_Val = TypeVar('T_Attr_Val') From 5d82a7297f220498e634ac275ab620dfec5a893e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 10:28:31 -0400 Subject: [PATCH 31/34] Removed now useless func --- .../meta_prog/introspection/attr_manip.py | 30 ++++--------------- 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/conf/script/src/utils/meta_prog/introspection/attr_manip.py b/conf/script/src/utils/meta_prog/introspection/attr_manip.py index e4974e42..6b3818df 100644 --- a/conf/script/src/utils/meta_prog/introspection/attr_manip.py +++ b/conf/script/src/utils/meta_prog/introspection/attr_manip.py @@ -2,39 +2,21 @@ from macro import * -__all__: TAlias_Macro_All = ['get_or_create_attr', 'get_or_create_optional_attr'] +__all__: TAlias_Macro_All = ['get_or_create_attr'] T_Attr_Val = TypeVar('T_Attr_Val') +TAlias_Attr_Val_Generator = Callable[[], T_Attr_Val] -def get_or_create_attr(obj, attr: str, default_val: Optional[T_Attr_Val] = None) -> T_Attr_Val: +def get_or_create_attr(obj, + attr: str, + default_val_generator: TAlias_Attr_Val_Generator = lambda: T_Attr_Val()) -> T_Attr_Val: attr_val: T_Attr_Val assert hasattr(obj, Macro.DICT) if not hasattr(obj, attr): - non_none_default_val: T_Attr_Val - - if default_val is None: - non_none_default_val = T_Attr_Val() - else: - non_none_default_val = default_val - - attr_val = non_none_default_val - setattr(obj, attr, attr_val) - else: - attr_val = getattr(obj, attr) - - return attr_val - - -def get_or_create_optional_attr(obj, attr: str, default_val: Optional[T_Attr_Val] = None) -> Optional[T_Attr_Val]: - attr_val: Optional[T_Attr_Val] - - assert hasattr(obj, Macro.DICT) - - if not hasattr(obj, attr): - attr_val = default_val + attr_val = default_val_generator() setattr(obj, attr, attr_val) else: attr_val = getattr(obj, attr) From 46cc23fabe212359de7c97e6151ab0df8e800224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 10:30:02 -0400 Subject: [PATCH 32/34] Fixed imports --- conf/script/src/utils/meta_prog/introspection/__init__.py | 1 + conf/script/src/utils/meta_prog/introspection/attr_manip.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/conf/script/src/utils/meta_prog/introspection/__init__.py b/conf/script/src/utils/meta_prog/introspection/__init__.py index 15623ffd..f3d1eaeb 100644 --- a/conf/script/src/utils/meta_prog/introspection/__init__.py +++ b/conf/script/src/utils/meta_prog/introspection/__init__.py @@ -1,2 +1,3 @@ +from .attr_manip import * from .caller import * from .macro import * diff --git a/conf/script/src/utils/meta_prog/introspection/attr_manip.py b/conf/script/src/utils/meta_prog/introspection/attr_manip.py index 6b3818df..750c1db3 100644 --- a/conf/script/src/utils/meta_prog/introspection/attr_manip.py +++ b/conf/script/src/utils/meta_prog/introspection/attr_manip.py @@ -1,6 +1,6 @@ from typing import * -from macro import * +from .macro import * __all__: TAlias_Macro_All = ['get_or_create_attr'] From c0e04c55391561f066623c9450868fe69afe96ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 10:34:14 -0400 Subject: [PATCH 33/34] Integrated Macro into `export` script --- .../src/utils/meta_prog/encapsulation/export.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py index d7a72a21..f7f51979 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/export.py @@ -1,21 +1,22 @@ import inspect import typing -__all__ = ['export'] +from utils.meta_prog.introspection import * + +__all__: TAlias_Macro_All = ['export'] def export(func: typing.Callable): - attribute_name_all: typing.Final[str] = '__all__' - module_api: list[str] + module_api: TAlias_Macro_All module = inspect.getmodule(func) - func_api = func.__qualname__ + func_api = getattr(func, Macro.QUALNAME) - if not hasattr(module, attribute_name_all): - module_api = [] - setattr(module, attribute_name_all, module_api) + if not hasattr(module, Macro.ALL): + module_api = TAlias_Macro_All() + setattr(module, Macro.ALL, module_api) else: - module_api = getattr(module, attribute_name_all) + module_api = getattr(module, Macro.ALL) module_api.append(func_api) From e06b7d3c595fe51e2cc83e99422665cf8421a97c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Mon, 19 Apr 2021 10:36:20 -0400 Subject: [PATCH 34/34] Finished `no_export` --- .../utils/meta_prog/encapsulation/no_export.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/conf/script/src/utils/meta_prog/encapsulation/no_export.py b/conf/script/src/utils/meta_prog/encapsulation/no_export.py index e69de29b..75d5961b 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/no_export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/no_export.py @@ -0,0 +1,15 @@ +import inspect +import typing + +from utils.meta_prog.introspection import * + +__all__: TAlias_Macro_All = ['no_export'] + + +def no_export(func: typing.Callable): + module = inspect.getmodule(func) + module_api = TAlias_Macro_All() + + setattr(module, Macro.ALL, module_api) + + return func