This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into feature/#151/represent-local-env
- Loading branch information
Showing
18 changed files
with
126 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .export import * | ||
from .no_export import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
conf/script/src/utils/meta_prog/encapsulation/no_export.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .attr_manip import * | ||
from .caller import * | ||
from .macro import * |
24 changes: 24 additions & 0 deletions
24
conf/script/src/utils/meta_prog/introspection/attr_manip.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import * | ||
|
||
from .macro import * | ||
|
||
__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_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): | ||
attr_val = default_val_generator() | ||
setattr(obj, attr, attr_val) | ||
else: | ||
attr_val = getattr(obj, attr) | ||
|
||
return attr_val |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import inspect | ||
from types import FrameType | ||
from typing import Final | ||
|
||
from .macro import * | ||
|
||
__all__ = ['get_nth_caller', 'get_caller', 'is_caller_main'] | ||
|
||
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 _ in stack_indices: | ||
caller_frame = caller_frame.f_back | ||
|
||
return caller_frame | ||
|
||
|
||
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 | ||
|
||
|
||
def is_caller_main() -> bool: | ||
caller = get_caller() | ||
return is_frame_main(frame=caller) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from enum import Enum, auto, unique | ||
from typing import Final | ||
|
||
TAlias_Macro_All = list[str] | ||
|
||
__all__: TAlias_Macro_All = ['Macro', 'TAlias_Macro_All'] | ||
|
||
|
||
class AutoMacroFromName(str, Enum): | ||
|
||
def _generate_next_value_(name: str, start, count, last_values) -> str: | ||
__AFFIX: Final[str] = '__' | ||
return __AFFIX + name.lower() + __AFFIX | ||
|
||
|
||
@unique | ||
class Macro(AutoMacroFromName): | ||
ALL = auto() | ||
DICT = auto() | ||
MAIN = auto() | ||
NAME = auto() | ||
QUALNAME = auto() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters