yta-programming-env 0.3.2__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: yta-programming-env
3
- Version: 0.3.2
3
+ Version: 0.4.0
4
4
  Summary: Youtube Autonomous Programming Dependencies Module
5
5
  License-File: LICENSE
6
6
  Author: danialcala94
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "yta-programming-env"
3
- version = "0.3.2"
3
+ version = "0.4.0"
4
4
  description = "Youtube Autonomous Programming Dependencies Module"
5
5
  authors = [
6
6
  {name = "danialcala94",email = "danielalcalavalera@gmail.com"}
@@ -0,0 +1,208 @@
1
+ from yta_programming_path import DevPathHandler
2
+ from yta_validation.parameter import ParameterValidator
3
+ from dotenv import load_dotenv, dotenv_values
4
+ from typing import Union
5
+
6
+ import os
7
+
8
+
9
+ class Environment:
10
+ """
11
+ Class to handle the environment of the project in
12
+ which you are executing this code perfectly.
13
+ """
14
+
15
+
16
+ @staticmethod
17
+ def load_dotenv(
18
+ env_filename: str = '.env',
19
+ do_override: bool = True,
20
+ do_use_project_abspath: bool = True
21
+ ) -> None:
22
+ """
23
+ Load the `env_filename` (by default '.env')
24
+ environment configuration file from the current
25
+ project.
26
+
27
+ The `env_filename` will be read and the variables
28
+ on it will be loaded and ready to be accessed by
29
+ using the methods in this `Environment` class,
30
+ that will use the `os.getenv()`.
31
+
32
+ If the `do_override` is `True`, the previously
33
+ existing configuration variables will be overriden by
34
+ the new values.
35
+ """
36
+ dotenv_path = (
37
+ os.path.join(
38
+ DevPathHandler.get_project_abspath(),
39
+ env_filename
40
+ )
41
+ if do_use_project_abspath else
42
+ env_filename
43
+ )
44
+
45
+ load_dotenv(
46
+ dotenv_path = dotenv_path,
47
+ override = do_override
48
+ )
49
+
50
+ return None
51
+
52
+
53
+ @staticmethod
54
+ def get_dotenv(
55
+ env_filename: str = '.env',
56
+ do_use_project_abspath: bool = True
57
+ ) -> dict:
58
+ """
59
+ Get the variables of the `env_filename` environment
60
+ configuration file as a dict.
61
+
62
+ This method doesn't load the variables in memory, it
63
+ just read the values from the file.
64
+
65
+ It will join the project abspath and the
66
+ `env_filename` if the `do_use_project_abspath` is
67
+ `True`.
68
+ """
69
+ dotenv_path = (
70
+ os.path.join(
71
+ DevPathHandler.get_project_abspath(),
72
+ env_filename
73
+ )
74
+ if do_use_project_abspath else
75
+ env_filename
76
+ )
77
+
78
+ return dotenv_values(
79
+ dotenv_path = dotenv_path
80
+ )
81
+
82
+
83
+ @staticmethod
84
+ def load_variable(
85
+ variable: str,
86
+ default_value: Union[str, bool, float, int, None] = None,
87
+ env_filename: str = '.env',
88
+ do_use_abspath: bool = True
89
+ ) -> Union[str, bool, float, int, None]:
90
+ """
91
+ Get the `variable` from the `env_filename` file and
92
+ load it into the environment variables, using the
93
+ `default_value` if not found.
94
+
95
+ This will load only the `variable` specified, if
96
+ existing, or set the `default_value` instead, and
97
+ will overwrite any previous value.
98
+ """
99
+ environment_variables = Environment.get_dotenv(
100
+ env_filename = env_filename,
101
+ do_use_project_abspath = do_use_abspath
102
+ )
103
+
104
+ value = environment_variables.get(variable, default_value)
105
+
106
+ Environment.set_variable(
107
+ variable = variable,
108
+ value = value
109
+ )
110
+
111
+ return value
112
+
113
+
114
+ # @staticmethod
115
+ # def get_variable_loading(
116
+ # variable: str,
117
+ # default_value: Union[str, bool, float, int, None] = None,
118
+ # env_filename: str = '.env',
119
+ # do_override: bool = True,
120
+ # do_use_project_abspath: bool = True
121
+ # ) -> Union[str, bool, float, int, None]:
122
+ # """
123
+ # Get the `variable` from the `os` environment by
124
+ # loading the dotenv and returning the value of that
125
+ # `variable`, or the `default_value` if not found and
126
+ # provided.
127
+
128
+ # This method will perform an `Environment.load_dotenv()`
129
+ # before accessing to the variable.
130
+ # """
131
+ # Environment.load_dotenv(
132
+ # env_filename = env_filename,
133
+ # do_override = do_override,
134
+ # do_use_project_abspath = do_use_project_abspath
135
+ # )
136
+
137
+ # return Environment.get_variable(variable, default_value)
138
+
139
+
140
+ @staticmethod
141
+ def get_variable_from_dotenv(
142
+ variable: str,
143
+ default_value: Union[str, bool, float, int, None] = None,
144
+ env_filename: str = '.env',
145
+ do_use_abspath: bool = True
146
+ ) -> Union[str, bool, float, int, None]:
147
+ """
148
+ Read the `variable` from the `env_filename` dotenv
149
+ file and return it or the `default_value` if not
150
+ found.
151
+
152
+ This method doesn't load the variables into memory.
153
+ """
154
+ return Environment.get_dotenv(
155
+ env_filename = env_filename,
156
+ do_use_project_abspath = do_use_abspath
157
+ ).get(variable, default_value)
158
+
159
+
160
+ @staticmethod
161
+ def get_variable(
162
+ variable: str,
163
+ default_value: Union[str, bool, float, int, None] = None,
164
+ ) -> Union[str, bool, float, int, None]:
165
+ """
166
+ Get the `variable` from the `os` environment by doing
167
+ a `os.getenv(variable)`, receiving the `default_value`
168
+ if not existing and provided.
169
+ """
170
+ return os.getenv(variable, default_value)
171
+
172
+
173
+ @staticmethod
174
+ def set_variable(
175
+ variable: str,
176
+ value: Union[str, bool, float, int]
177
+ ) -> Union[str, None]:
178
+ """
179
+ Set the value of the `variable` in the environment by
180
+ doing `os.environ[variable] = value`.
181
+
182
+ The environment variables are always strings.
183
+
184
+ This method will return the value the environment
185
+ variable had before setting the new one, if existing.
186
+ """
187
+ previous_value = os.getenv(variable, None)
188
+
189
+ os.environ[variable] = str(value)
190
+
191
+ return previous_value
192
+
193
+
194
+ @staticmethod
195
+ def remove_variable(
196
+ variable: str,
197
+ default_value: Union[str, bool, float, int, None] = None
198
+ ) -> Union[str, None]:
199
+ """
200
+ Unset the `variable` in the environment by doing
201
+ `os.environ.pop[variable]`.
202
+
203
+ This method will return the last value before
204
+ unsetting it, if existing, or the `default_value`
205
+ if provided and not found in the environment
206
+ variables.
207
+ """
208
+ return os.environ.pop(variable, default_value)
@@ -1,84 +0,0 @@
1
- from yta_programming_path import DevPathHandler
2
- from yta_validation.parameter import ParameterValidator
3
- from dotenv import load_dotenv, dotenv_values
4
- from typing import Union
5
-
6
- import os
7
-
8
-
9
- class Environment:
10
- """
11
- Class to handle the environment of the project in
12
- which you are executing this code perfectly.
13
- """
14
-
15
- @staticmethod
16
- def load_current_project_dotenv(
17
- env_filename: str = '.env',
18
- do_override: bool = True
19
- ):
20
- """
21
- Load the `env_filename` (by default '.env')
22
- environment configuration file from the current
23
- project. The current project is the one in which
24
- the code is being executed, not the library in which
25
- it is written.
26
-
27
- The `env_filename` will be read and the variables on
28
- it will be loaded and ready to be accessed.
29
-
30
- If the `do_override` is `True`, the previously
31
- existing configuration variables will be overriden by
32
- the new values.
33
- """
34
- load_dotenv(
35
- dotenv_path = os.path.join(DevPathHandler.get_project_abspath(), env_filename),
36
- override = do_override
37
- )
38
-
39
- @staticmethod
40
- def get_current_project_env_dict(
41
- env_filename: str = '.env'
42
- ) -> dict:
43
- """
44
- Get the variables of the `env_filename` environment
45
- configuration file as a dict.
46
-
47
- This method doesn't load the variables in memory, it
48
- just read the values from the file.
49
- """
50
- return dotenv_values(
51
- dotenv_path = os.path.join(
52
- DevPathHandler.get_project_abspath(),
53
- env_filename
54
- )
55
- )
56
-
57
- @staticmethod
58
- def get_current_project_env(
59
- variable: str,
60
- default_value: Union[str, bool, float, int, None] = None,
61
- env_filename: str = '.env'
62
- ):
63
- """
64
- Load the `env_filename` environment configuration file
65
- and get the value of the `variable`, if existing.
66
-
67
- This method will do a `load_dotenv` call, which will
68
- try to load the `env_filename` in memory and then use
69
- the `os.getenv` method.
70
- """
71
- ParameterValidator.validate_mandatory_string('variable', variable, do_accept_empty = False)
72
-
73
- return Environment.get_current_project_env_dict(env_filename).get(variable, default_value)
74
-
75
- @staticmethod
76
- def get_env(
77
- variable: str,
78
- default_value: Union[str, bool, float, int, None] = None,
79
- ):
80
- """
81
- Get the `variable` from the `os` environment by doing
82
- a `os.getenv(variable)`.
83
- """
84
- return os.getenv(variable, default_value)