Skip to content

md_proxy

MdProxy

Proxies the MdObj objects

Makes the MdObj available with PageInfo prefilled.

Source code in mkreports/md/md_proxy.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class MdProxy:
    """
    Proxies the MdObj objects

    Makes the MdObj available with PageInfo prefilled.
    """

    _proxied_classes: Dict[str, Any] = dict()

    def __init__(
        self,
        page_info: PageInfo,
        md_defaults: Optional[Dict[str, Dict[str, Any]]] = None,
    ):
        """
        Initialize the proxy.

        Args:
            page_info (PageInfo): The info of the page for which the proxy works.
            md_defaults (Optional[Dict[str, Dict[str, Any]]): A dictionary mapping the names
                md objects (accessed from the proxy) to default keywords included when
                they are being called.
        """
        self.page_info = page_info
        self.md_defaults = md_defaults if md_defaults is not None else {}

    def __getattr__(self, name):
        # we are not checking if it is included; if not, should raise error
        if name in self._proxied_classes:
            obj = self._proxied_classes[name]
        else:
            raise AttributeError(f"No MdObj of name '{name}' registered")

        # if is a class; try to fix the init method
        if inspect.isclass(obj):
            # check the init method signature
            partial_kwargs = {}
            obj_sig = inspect.signature(obj)
            if "page_info" in obj_sig.parameters:
                partial_kwargs["page_info"] = self.page_info

            # check if there are defaults for the md-object
            if name in self.md_defaults:
                partial_kwargs.update(self.md_defaults[name])

            if len(partial_kwargs) > 0:
                partial_obj = partial(obj, **partial_kwargs)
                update_wrapper(partial_obj, obj)
                partial_obj.__doc__ = obj.__init__.__doc__
                return partial_obj
            else:
                return obj

        else:
            return obj

    @property
    def proxied_classes(self) -> Dict[str, Any]:
        """
        Returns:
            Dict[str, Any]: A dict with the registered items under their name.
        """
        return self._proxied_classes

    def __eq__(self, other):
        if type(self) != type(other):
            return False
        return self.__dict__ == other.__dict__

__init__(page_info, md_defaults=None)

Initialize the proxy.

Parameters:

Name Type Description Default
page_info PageInfo

The info of the page for which the proxy works.

required
md_defaults Optional[Dict[str, Dict[str, Any]]

A dictionary mapping the names md objects (accessed from the proxy) to default keywords included when they are being called.

None
Source code in mkreports/md/md_proxy.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def __init__(
    self,
    page_info: PageInfo,
    md_defaults: Optional[Dict[str, Dict[str, Any]]] = None,
):
    """
    Initialize the proxy.

    Args:
        page_info (PageInfo): The info of the page for which the proxy works.
        md_defaults (Optional[Dict[str, Dict[str, Any]]): A dictionary mapping the names
            md objects (accessed from the proxy) to default keywords included when
            they are being called.
    """
    self.page_info = page_info
    self.md_defaults = md_defaults if md_defaults is not None else {}

proxied_classes()

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: A dict with the registered items under their name.

Source code in mkreports/md/md_proxy.py
72
73
74
75
76
77
78
@property
def proxied_classes(self) -> Dict[str, Any]:
    """
    Returns:
        Dict[str, Any]: A dict with the registered items under their name.
    """
    return self._proxied_classes