Skip to content
This repository has been archived by the owner on Jun 14, 2021. It is now read-only.

Commit

Permalink
Merge branch 'dev' into feature/#151/represent-local-env
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit-dubreuil committed Apr 19, 2021
2 parents c700524 + a5f8cf6 commit 1e4043d
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
4 changes: 2 additions & 2 deletions conf/script/src/test/test_utils/error/test_managed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
4 changes: 2 additions & 2 deletions conf/script/src/test/test_utils/error/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -42,5 +42,5 @@ def dummy(self):
ErrorMetaImplChild()


if utils.cli.main.is_caller_main():
if is_caller_main():
unittest.main()
2 changes: 2 additions & 0 deletions conf/script/src/utils/auto_print.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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):
Expand Down
15 changes: 1 addition & 14 deletions conf/script/src/utils/cli/main.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions conf/script/src/utils/error/managed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions conf/script/src/utils/error/synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions conf/script/src/utils/meta_prog/encapsulation/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .export import *
from .no_export import *
17 changes: 9 additions & 8 deletions conf/script/src/utils/meta_prog/encapsulation/export.py
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
15 changes: 15 additions & 0 deletions conf/script/src/utils/meta_prog/encapsulation/no_export.py
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
1 change: 1 addition & 0 deletions conf/script/src/utils/meta_prog/generics/cls_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions conf/script/src/utils/meta_prog/generics/cls_wrapper.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
3 changes: 3 additions & 0 deletions conf/script/src/utils/meta_prog/introspection/__init__.py
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 conf/script/src/utils/meta_prog/introspection/attr_manip.py
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
35 changes: 35 additions & 0 deletions conf/script/src/utils/meta_prog/introspection/caller.py
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)
22 changes: 22 additions & 0 deletions conf/script/src/utils/meta_prog/introspection/macro.py
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()
4 changes: 2 additions & 2 deletions conf/script/src/utils/more_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 1e4043d

Please sign in to comment.