-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
environ_utils.py
193 lines (167 loc) · 7.27 KB
/
environ_utils.py
1
2
3
4
5
6
7
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#coding: utf-8
""" Utility programs for environment variables."""
import os
import re
import warnings
from dotenv import load_dotenv
from ._path import DOTENV_PATH
from ._warnings import EnvVariableNotDefinedWarning
from .coloring_utils import toBLUE, toGREEN
TRANSLATION_GUMMY_ENVNAME_PREFIX = "TRANSLATION_GUMMY"
def name2envname(name, service="", prefix=""):
"""Convert name to environment varname.
Args:
name (str) : name.
service (str) : service name. (default= ``""``)
prefix (str) : prefix of the ``name``. (default= ``""``)
Examples:
>>> from gummy.utils import name2envname
>>> name2envname(name="name", service="", prefix="")
'TRANSLATION_GUMMY_NAME'
>>> name2envname(name="name", service="service", prefix="")
'TRANSLATION_GUMMY_SERVICE_NAME'
>>> name2envname(name="name", service="", prefix="prefix")
'TRANSLATION_GUMMY_PREFIX_NAME'
>>> name2envname(name="name", service="service", prefix="prefix")
'TRANSLATION_GUMMY_SERVICE_PREFIX_NAME'
"""
strings = [TRANSLATION_GUMMY_ENVNAME_PREFIX]
for string in [service, prefix]:
if len(string)>0:
strings.append(string)
strings.append(name)
return "_".join(strings).upper()
def where_is_envfile():
"""Get Where the envfile is.
Examples:
>>> from gummy.utils import where_is_envfile
>>> where_is_envfile()
'/Users/iwasakishuto/.gummy/.env'
"""
return DOTENV_PATH
def read_environ(dotenv_path=DOTENV_PATH):
"""Read the environment variables from ``dotenv_path``
Args:
dotenv_path (str) : path/to/.env (default= ``DOTENV_PATH``)
Returns:
dict : the dictionary containing ``dotenv_path``'s environment variables.
Examples:
>>> from gummy.utils import read_environ
>>> read_environ()
{'TRANSLATION_GUMMY_GATEWAY_UTOKYO_USERNAME': 'USERNAME',
'TRANSLATION_GUMMY_GATEWAY_UTOKYO_PASSWORD': 'PASSWORD'}
"""
env_variables = {}
if os.path.exists(dotenv_path):
with open(dotenv_path, mode="r", encoding='utf-8') as f:
for line in f.readlines():
for key,val in re.findall(pattern=r'^(.+?)\s?=\s?"?(.+?)"?$', string=line):
env_variables[key] = val
return env_variables
def write_environ(dotenv_path=DOTENV_PATH, **kwargs):
"""Overwrite or Write the environment variables in ``dotenv_path``
Args:
dotenv_path (str) : path/to/.env (default= ``DOTENV_PATH``)
kwargs (dict) : new environment variables
Examples:
>>> from gummy.utils import read_environ, write_environ
>>> read_environ()
{'TRANSLATION_GUMMY_GATEWAY_UTOKYO_USERNAME': 'USERNAME',
'TRANSLATION_GUMMY_GATEWAY_UTOKYO_PASSWORD': 'PASSWORD'}
>>> write_environ(TRANSLATION_GUMMY_SAMPLE="SAMPLE")
>>> read_environ()
{'TRANSLATION_GUMMY_GATEWAY_UTOKYO_USERNAME': '9890136422',
'TRANSLATION_GUMMY_GATEWAY_UTOKYO_PASSWORD': 'Shuto070389&',
'TRANSLATION_GUMMY_SAMPLE': 'SAMPLE'}
>>> write_environ(TRANSLATION_GUMMY_SAMPLE="SAMPLE_AMENDED")
>>> read_environ()
{'TRANSLATION_GUMMY_GATEWAY_UTOKYO_USERNAME': '9890136422',
'TRANSLATION_GUMMY_GATEWAY_UTOKYO_PASSWORD': 'Shuto070389&',
'TRANSLATION_GUMMY_SAMPLE': 'SAMPLE_AMENDED'}
"""
env_variables = read_environ(dotenv_path)
env_variables.update(kwargs)
with open(DOTENV_PATH, mode="w", encoding='utf-8') as f:
f.writelines([f'{key} = "{val}"\n' for key,val in env_variables.items()])
def show_environ(dotenv_path=DOTENV_PATH):
"""Show environment variables written in ``dotenv_path``.
Args:
dotenv_path (str) : path/to/.env (default= ``DOTENV_PATH``)
Examples:
>>> from gummy.utils import show_environ
>>> show_environ()
* TRANSLATION_GUMMY_GATEWAY_UTOKYO_USERNAME : "9890136422"
* TRANSLATION_GUMMY_GATEWAY_UTOKYO_PASSWORD : "Shuto070389&"
"""
env_variables = read_environ(dotenv_path=dotenv_path)
for key,val in env_variables.items():
print(f'* {toGREEN(key)} : "{toBLUE(val)}"')
def load_environ(dotenv_path=DOTENV_PATH, env_varnames=[], verbose=False):
"""
Load environment variables from ``path`` file, and return whether every
necessary VARNAMES (``env_varnames``) are set.
Args:
dotenv_path (str) : path/to/.env (default= ``DOTENV_PATH``)
env_varnames (list) : new environment variables
verbose (bool) : Whether to print message or not. (default= ``False``)
Returns:
bool : Whether there are environment variables not defined in ``dotenv_path`` but in ``env_varnames``
Examples:
>>> from gummy.utils import load_environ, show_environ
>>> is_ok = load_environ()
>>> is_ok
True
>>> show_environ()
* TRANSLATION_GUMMY_GATEWAY_UTOKYO_USERNAME : "9890136422"
* TRANSLATION_GUMMY_GATEWAY_UTOKYO_PASSWORD : "Shuto070389&"
>>> is_ok = load_environ(env_varnames=["TRANSLATION_GUMMY_NOT_DEFINED"])
TRANSLATION_GUMMY_NOT_DEFINED is not set.
EnvVariableNotDefinedWarning: Please set environment variable in /Users/iwasakishuto/.gummy/.env
>>> is_ok
False
"""
if not os.path.exists(dotenv_path):
return False
load_dotenv(dotenv_path=dotenv_path, verbose=verbose)
omission = False
for env_name in env_varnames:
if os.getenv(env_name) is None:
omission = True
print(f"{toGREEN(env_name)} is not set.")
if omission:
warnings.warn(message=f"Please set environment variable in {toBLUE(dotenv_path)}", category=EnvVariableNotDefinedWarning)
return not omission
def check_environ(required_keynames, required_env_varnames=None, verbose=True, **kwargs):
"""Check whether meet the requirements.
Args:
required_keynames (list) : Required keynames
required_env_varnames (list) : Required environment variables.
verbose (bool) : Whether to print message or not. (default= ``True``)
Examples:
>>> check_environ(
... required_keynames=["hoge"],
... required_env_varnames=["TRANSLATION_GUMMY_HOGE"],
... hoge="hoge"
>>> )
(True, [])
>>> check_environ(
... required_keynames=["hoge"],
... required_env_varnames=["TRANSLATION_GUMMY_HOGE"],
... hoge1="hoge"
>>> )
Please set TRANSLATION_GUMMY_HOGE or give hoge as kwargs.
(False, ['hoge'])
Returns:
tuple (bool, list) :
- ``is_ok`` : Whether meet the requirements.
- ``not_meet_keynames`` : contains keynames you have to supply.
"""
if required_env_varnames is None:
required_env_varnames = [name2envname(name=keyname) for keyname in required_keynames]
not_meet_keynames = []
given_keynames = list(kwargs.keys())
for keyname,env_name in zip(required_keynames, required_env_varnames):
if (keyname not in given_keynames) and (os.getenv(env_name) is None):
not_meet_keynames.append(keyname)
if verbose: print(f"Please set {toGREEN(env_name)} or give {toBLUE(keyname)} as kwargs.")
return len(not_meet_keynames)==0, not_meet_keynames