Skip to content

Env manipulation

env manipulator

Better live env manipulation than standard python library.

Classes:

Name Description
ENVManipulator

placeholder

ENVManipulator

Source code in app/dcsp/app/functions/env_manipulation.py
 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class ENVManipulator:
    def __init__(
        self,
        env_path: str = c.ENV_PATH_PLACEHOLDERS,
    ) -> None:
        """Initialise the env path

        Initialise the env path

        Args:
            env_path (str): location of the env file. Sets to c.ENV_PATH_PLACEHOLDERS if not
                            sets.
        """
        self.env_path = env_path
        return

    def delete(self, variable_to_delete: str) -> bool:
        """Remove a variable from the env file

        Removes the variable from the env file.

        Args:
            variable_to_delete (str): as name.

        Returns:
            bool: True if was present and deleted, False if was never present.
        """
        variable_set: bool = False
        env_variables = dotenv_values(self.env_path)  # TODO need type
        key: str = ""
        value2: str | None = ""
        key2: str | None = ""

        for key in env_variables:
            if key == variable_to_delete:
                variable_set = True

        if variable_set == True:
            del env_variables[variable_to_delete]
            open(self.env_path, "w").close()

            for (
                key2,
                value2,
            ) in env_variables.items():
                set_key(
                    self.env_path,
                    str(key2),
                    str(value2),
                )

        return variable_set

    def delete_all(self) -> None:
        """Remove all variables from env file

        Removes all the variables from the env file, keeping the file itself
        however.
        """
        open(self.env_path, "w").close()
        return

    def add(self, variable: str, value: str) -> None:
        """Add or change a variable in the env file

        Add or change a variable in the env file

        Args:
            variable (str): name of variable.
            value (str): value to set.

        Returns:
            None
        """
        set_key(self.env_path, variable, value)
        return

    def read(self, key_to_read: str) -> str:
        """Reads a variable from env file

        Reads a variable from an env file

        Args:
            key_to_read (str): the variable to read.

        Returns:
            str: value of the variable. This will return empty string ("")
                 if the variable has not been set.
        """
        dot_values: dict[str, str | None] = dotenv_values(self.env_path)
        return str(dot_values.get(key_to_read) or "")

    def read_all(self) -> dict[str, str]:
        """Reads all variables from env file

        Reads all variable from an env file and returns with keys in alphabetical
        order.

        Returns:
            dict[str, str]: returns all variables in the env file. This will
                            return empty string ("") if the variable has not been
                            set.
        """
        dot_values_raw: dict[str, str | None] = dotenv_values(self.env_path)
        dot_values_clean: dict[str, str] = {}
        key: str = ""
        value: str | None = ""
        sorted_dict: dict[str, str]

        for key, value in dot_values_raw.items():
            dot_values_clean[key] = str(value or "")

        keys_list = list(dot_values_clean.keys())
        keys_list.sort(key=str.lower)
        sorted_dict = {i: dot_values_clean[i] for i in keys_list}
        return sorted_dict

__init__(env_path=c.ENV_PATH_PLACEHOLDERS)

Initialise the env path

Initialise the env path

Parameters:

Name Type Description Default
env_path str

location of the env file. Sets to c.ENV_PATH_PLACEHOLDERS if not sets.

ENV_PATH_PLACEHOLDERS
Source code in app/dcsp/app/functions/env_manipulation.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def __init__(
    self,
    env_path: str = c.ENV_PATH_PLACEHOLDERS,
) -> None:
    """Initialise the env path

    Initialise the env path

    Args:
        env_path (str): location of the env file. Sets to c.ENV_PATH_PLACEHOLDERS if not
                        sets.
    """
    self.env_path = env_path
    return

add(variable, value)

Add or change a variable in the env file

Add or change a variable in the env file

Parameters:

Name Type Description Default
variable str

name of variable.

required
value str

value to set.

required

Returns:

Type Description
None

None

Source code in app/dcsp/app/functions/env_manipulation.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def add(self, variable: str, value: str) -> None:
    """Add or change a variable in the env file

    Add or change a variable in the env file

    Args:
        variable (str): name of variable.
        value (str): value to set.

    Returns:
        None
    """
    set_key(self.env_path, variable, value)
    return

delete(variable_to_delete)

Remove a variable from the env file

Removes the variable from the env file.

Parameters:

Name Type Description Default
variable_to_delete str

as name.

required

Returns:

Name Type Description
bool bool

True if was present and deleted, False if was never present.

Source code in app/dcsp/app/functions/env_manipulation.py
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
def delete(self, variable_to_delete: str) -> bool:
    """Remove a variable from the env file

    Removes the variable from the env file.

    Args:
        variable_to_delete (str): as name.

    Returns:
        bool: True if was present and deleted, False if was never present.
    """
    variable_set: bool = False
    env_variables = dotenv_values(self.env_path)  # TODO need type
    key: str = ""
    value2: str | None = ""
    key2: str | None = ""

    for key in env_variables:
        if key == variable_to_delete:
            variable_set = True

    if variable_set == True:
        del env_variables[variable_to_delete]
        open(self.env_path, "w").close()

        for (
            key2,
            value2,
        ) in env_variables.items():
            set_key(
                self.env_path,
                str(key2),
                str(value2),
            )

    return variable_set

delete_all()

Remove all variables from env file

Removes all the variables from the env file, keeping the file itself however.

Source code in app/dcsp/app/functions/env_manipulation.py
66
67
68
69
70
71
72
73
def delete_all(self) -> None:
    """Remove all variables from env file

    Removes all the variables from the env file, keeping the file itself
    however.
    """
    open(self.env_path, "w").close()
    return

read(key_to_read)

Reads a variable from env file

Reads a variable from an env file

Parameters:

Name Type Description Default
key_to_read str

the variable to read.

required

Returns:

Name Type Description
str str

value of the variable. This will return empty string ("") if the variable has not been set.

Source code in app/dcsp/app/functions/env_manipulation.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def read(self, key_to_read: str) -> str:
    """Reads a variable from env file

    Reads a variable from an env file

    Args:
        key_to_read (str): the variable to read.

    Returns:
        str: value of the variable. This will return empty string ("")
             if the variable has not been set.
    """
    dot_values: dict[str, str | None] = dotenv_values(self.env_path)
    return str(dot_values.get(key_to_read) or "")

read_all()

Reads all variables from env file

Reads all variable from an env file and returns with keys in alphabetical order.

Returns:

Type Description
dict[str, str]

dict[str, str]: returns all variables in the env file. This will return empty string ("") if the variable has not been set.

Source code in app/dcsp/app/functions/env_manipulation.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def read_all(self) -> dict[str, str]:
    """Reads all variables from env file

    Reads all variable from an env file and returns with keys in alphabetical
    order.

    Returns:
        dict[str, str]: returns all variables in the env file. This will
                        return empty string ("") if the variable has not been
                        set.
    """
    dot_values_raw: dict[str, str | None] = dotenv_values(self.env_path)
    dot_values_clean: dict[str, str] = {}
    key: str = ""
    value: str | None = ""
    sorted_dict: dict[str, str]

    for key, value in dot_values_raw.items():
        dot_values_clean[key] = str(value or "")

    keys_list = list(dot_values_clean.keys())
    keys_list.sort(key=str.lower)
    sorted_dict = {i: dot_values_clean[i] for i in keys_list}
    return sorted_dict