WiseAgentsYAMLObject

Bases: YAMLObject

Abstract class to deal with removing the underscores from the keys of the parsed YAML object.

Source code in wiseagents/yaml/wiseagents_yaml_object.py
  8
  9
 10
 11
 12
 13
 14
 15
 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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
class WiseAgentsYAMLObject(yaml.YAMLObject):
    """
        Abstract class to deal with removing the underscores from the keys of the parsed YAML object.
    """

    yaml_loader = WiseAgentsLoader

    def __init__(self):
        enforce_no_abstract_class_instances(self.__class__, WiseAgentsYAMLObject)

    def __setstate__(self, d):
        """
        Sets the state of the object from the parsed representation of the YAML or from Pickle.
        This does conversion of the keys read, which have no leading underscores to the internal representation
        in the class which have leading underscores.

        Note: Subclasses MAY override this method, but need to be aware that the keys in the dictionary will not have
        underscores, and once done they will need to call this method via a super().__setstate__(d) call.

        Note: Rather than overriding __setstate__(), it is preferred to override _validate_and_convert_types(), which
        will be called once conversion has been done, and have the keys in the dictionary with underscores.

        Args:
            d (dict): the parsed representation of the YAML object

        Returns:
            the modified state dictionary of the object we are deserializing
        """
        d = self._convert_yaml_keys_to_members(d)

        self._validate_and_convert_types(d)

        for key, value in d.items():
            setattr(self, key, value)

    def __getstate__(self) -> dict:
        """
        Gets the state of the object for serialization to YAML/Pickle.
        This converts the keys from their internal representation with leading underscores to the external
        representation, which have no leading underscores.

        Note: subclasses overriding this method should call super().__getstate__() to get the state that will
        be serialized to YAML/Pickle, rather than calling self.__dict__ directly.

        Note: when overriding this method and getting the state, e.g. for deleting entries which should not be
        serialized, the entries in the map will have keys in the external form (i.e. no leading underscores).

        Returns:
            the modified state dictionary of the object we are serializing
        """
        d: dict = self.__dict__.copy()
        if d:
            d = self._convert_members_to_yaml_keys(d)
        return d

    @classmethod
    def _convert_yaml_keys_to_members(cls, d: dict) -> dict:
        """
        Translate the dictionary by adding underscores to all the keys.
        The resulting dictionary is used to set the state of the object.

        Args:
            d (dict): the parsed representation of the YAML object

        Returns:
            the modified state dictionary of the object we are deserializing, or the original dictionary if not changes are needed
        """

        copy = {}
        for key, value in d.items():
            # if not key.startswith('_'):
            key = f"_{key}"
            copy[key] = value
        return copy


    @abc.abstractmethod
    def _validate_and_convert_types(self, d: dict) -> dict:
        """
        Validate the dictionary and convert the types of the values.
        Subclasses may override this method to provide custom validation and type conversion.

        Args:
            d (dict): the parsed representation of the YAML object
        """
        return d

    @classmethod
    def _convert_members_to_yaml_keys(cls, d: dict) -> dict:
        """
        Translate the dictionary by removing underscores from all the keys

        Args:
            d (dict): the state dictionary of the object we are serializing
        """

        copy = {}
        for key, value in d.items():
            if key.startswith('_'):
                key = key[1:]
                copy[key] = value
        return copy

__getstate__()

Gets the state of the object for serialization to YAML/Pickle. This converts the keys from their internal representation with leading underscores to the external representation, which have no leading underscores.

Note: subclasses overriding this method should call super().getstate() to get the state that will be serialized to YAML/Pickle, rather than calling self.dict directly.

Note: when overriding this method and getting the state, e.g. for deleting entries which should not be serialized, the entries in the map will have keys in the external form (i.e. no leading underscores).

Returns:
  • dict

    the modified state dictionary of the object we are serializing

Source code in wiseagents/yaml/wiseagents_yaml_object.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def __getstate__(self) -> dict:
    """
    Gets the state of the object for serialization to YAML/Pickle.
    This converts the keys from their internal representation with leading underscores to the external
    representation, which have no leading underscores.

    Note: subclasses overriding this method should call super().__getstate__() to get the state that will
    be serialized to YAML/Pickle, rather than calling self.__dict__ directly.

    Note: when overriding this method and getting the state, e.g. for deleting entries which should not be
    serialized, the entries in the map will have keys in the external form (i.e. no leading underscores).

    Returns:
        the modified state dictionary of the object we are serializing
    """
    d: dict = self.__dict__.copy()
    if d:
        d = self._convert_members_to_yaml_keys(d)
    return d

__setstate__(d)

Sets the state of the object from the parsed representation of the YAML or from Pickle. This does conversion of the keys read, which have no leading underscores to the internal representation in the class which have leading underscores.

Note: Subclasses MAY override this method, but need to be aware that the keys in the dictionary will not have underscores, and once done they will need to call this method via a super().setstate(d) call.

Note: Rather than overriding setstate(), it is preferred to override _validate_and_convert_types(), which will be called once conversion has been done, and have the keys in the dictionary with underscores.

Parameters:
  • d (dict) –

    the parsed representation of the YAML object

Returns:
  • the modified state dictionary of the object we are deserializing

Source code in wiseagents/yaml/wiseagents_yaml_object.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def __setstate__(self, d):
    """
    Sets the state of the object from the parsed representation of the YAML or from Pickle.
    This does conversion of the keys read, which have no leading underscores to the internal representation
    in the class which have leading underscores.

    Note: Subclasses MAY override this method, but need to be aware that the keys in the dictionary will not have
    underscores, and once done they will need to call this method via a super().__setstate__(d) call.

    Note: Rather than overriding __setstate__(), it is preferred to override _validate_and_convert_types(), which
    will be called once conversion has been done, and have the keys in the dictionary with underscores.

    Args:
        d (dict): the parsed representation of the YAML object

    Returns:
        the modified state dictionary of the object we are deserializing
    """
    d = self._convert_yaml_keys_to_members(d)

    self._validate_and_convert_types(d)

    for key, value in d.items():
        setattr(self, key, value)

setup_yaml_for_env_vars()

Configures the YAML Loader to do replacement of environment variables. It will replace YAML value strings such as '${HOST}' and '${PORT:80}' with environment variable lookups. In the first example, '${HOST}', it will replace the string with the value from doing os.getenv("HOST"). If the environment variable 'HOST" is not set, an exception will be thrown. In the second example, '${PORT:80}', we are doing the same but looking up `os.getenv("PORT"). In this case, if the 'PORT' environment variable is not set, it will use the default value shows, which in this case is '80'.

Source code in wiseagents/yaml/wise_yaml_loader.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def setup_yaml_for_env_vars():
    """
    Configures the YAML Loader to do replacement of environment variables.
    It will replace YAML value strings such as '${HOST}' and '${PORT:80}' with environment
    variable lookups.
    In the first example, '${HOST}', it will replace the string with the value from doing
    `os.getenv("HOST")`. If the environment variable 'HOST" is not set, an exception will be thrown.
    In the second example, '${PORT:80}', we are doing the same but looking up `os.getenv("PORT"). In this case,
    if the 'PORT' environment variable is not set, it will use the default value shows, which in this case is '80'.
    """
    yaml.add_implicit_resolver(f"!{_env_var_token}", _env_pattern)
    yaml.add_constructor(f"!{_env_var_token}", _env_constructor)

    yaml.add_implicit_resolver(f"!{_env_var_token}", _env_pattern, Loader=WiseAgentsLoader)
    yaml.add_constructor(f"!{_env_var_token}", _env_constructor, Loader=WiseAgentsLoader)