-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotenv_typecast.py
More file actions
204 lines (165 loc) · 6.54 KB
/
dotenv_typecast.py
File metadata and controls
204 lines (165 loc) · 6.54 KB
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
194
195
196
197
198
199
200
201
202
203
204
"""
## Supported types
The following are all type-casting methods of `DotEnv`:
- `env.str`
- `env.bool`
- `env.int`
- `env.float`
- `env.decimal`
- `env.list` (accepts optional `subcast` and `delimiter` keyword arguments)
- `env.json` (casts to a [`JSON object`](https://docs.python.org/3/library/json.html)))
- `env.datetime` (uses fromisoformat() method)
- `env.date` (uses fromisoformat() method)
- `env.time` (uses fromisoformat() method)
- `env.timedelta` (assumes value is an int\float in seconds)
- `env.timestamp` (assumes value is an int\float in seconds)
- `env.url` (recommended to specify the scheme)
- `env.uuid` (assumes value is string of 36 char or 32 without dash; casts to a [`UUID object`](https://docs.python.org/3/library/uuid.html))
- `env.path` (casts to a [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html))
- `env.dict` (not implemented yet; accepts optional `subcast_keys`, `subcast_values` and `delimiter` keyword arguments)
- `env.enum` (not implemented yet; casts to any given enum type specified in `type` keyword argument, accepts optional `ignore_case` keyword argument)
- `env.log_level` (will not be implemented, use string)
"""
import os
import sys
import urllib
from typing import IO, Any, Optional, Union
from dotenv import *
from dotenv.main import *
__version__ = "0.1.6"
__vendor__ = "python.dotenv.typecast"
def read_dotenv(
dotenv_path: Optional[StrPath] = None,
stream: Optional[IO[str]] = None,
verbose: bool = False,
override: bool = False,
interpolate: bool = True,
encoding: Optional[str] = "utf-8",
) -> DotEnv:
"""Parse a .env file and return its content as the DotEnv object.
The returned dict will have `None` values for keys without values in the .env file.
For example, `foo=bar` results in `{"foo": "bar"}` whereas `foo` alone results in
`{"foo": None}`
Parameters:
dotenv_path: Absolute or relative path to the `.env` file.
stream: `StringIO` object with .env's content, used if `dotenv_path` is `None`.
verbose: Whether to output a warning if the `.env` file is missing.
override: Whether to override the system environment variables with the variables
from the `.env` file.
encoding: Encoding to be used to read the file, default `utf-8`.
Returns:
object: DotEnv()
If both `dotenv_path` and `stream` are `None`, `find_dotenv()` is used to find the
.env file.
"""
if dotenv_path is None and stream is None:
dotenv_path = find_dotenv()
dotenv = DotEnv(
dotenv_path=dotenv_path,
stream=stream,
verbose=verbose,
interpolate=interpolate,
override=override,
encoding=encoding,
)
dotenv.set_as_environment_variables()
return dotenv
def from_string(text=Union[str, list, dict]) -> io.StringIO:
if isinstance(text, dict):
text = "\n".join(text.values())
if isinstance(text, list):
text = "\n".join(text)
if not isinstance(text, str):
raise ValueError("incorrect `text` type")
return io.StringIO(text)
def from_string_values(text=Union[str, list, dict]) -> dict:
return dotenv_values(stream=from_string(text))
def getenv(key: str, default: Any = None) -> Any:
return os.getenv(key, default)
def _cast(
self,
value: Any,
typecast: Optional[Union[str, type, None]] = Ellipsis,
*args,
**kwargs,
) -> Any:
""" """
if typecast is Ellipsis:
return value
# if typecast in (None, type(None)): # NoneType:
# return None
if not isinstance(typecast, str):
raise NotImplementedError("Not implemented yet")
typecast = typecast.lower()
globals_builtins = globals()["__builtins__"]
if "none" == typecast:
# fake cast
return None
elif "bool" == typecast:
return (
False
if (str(value).lower() in ("", "0", "false", "n", "no", "non", "none"))
else True
)
elif "path" == typecast:
return pathlib.Path(value)
elif "timedelta" == typecast:
return datetime.timedelta(seconds=float(value))
elif "timestamp" == typecast:
return datetime.datetime.fromtimestamp(float(value))
elif "datetime" == typecast:
return datetime.datetime.fromisoformat(value)
elif "date" == typecast:
return datetime.date.fromisoformat(value)
elif "time" == typecast:
return datetime.time.fromisoformat(value)
elif "json" == typecast:
return json.loads(value)
elif "list" == typecast:
delimiter = kwargs.get("delimiter", ",")
subcast = kwargs.get("subcast", None)
a = map(str.strip, value.split(delimiter))
if subcast:
if isinstance(subcast, str) and subcast in globals_builtins:
subcast = globals_builtins[subcast]
elif isinstance(subcast, type):
pass
else:
raise TypeError(f"subcast type `{subcast}` not found")
a = map(subcast, a)
return list(a)
# elif "dict" == typecast:
# return (value)
elif "url" == typecast:
try:
url_parts = urllib.parse.urlsplit(value, scheme="https")
# print(url_parts)
url = urllib.parse.urlunsplit(
url_parts._replace(
path=urllib.parse.quote_plus(url_parts.path, safe="/"),
fragment=urllib.parse.quote_plus(url_parts.fragment, safe="/"),
)
)
return url
except Exception as exc:
raise TypeError(f"Typecast `{typecast}` error! " + str(exc))
elif typecast in globals_builtins:
return globals_builtins[typecast].__call__(
value or (0 if "int" == typecast else "")
)
else:
raise TypeError()
def _env(self, key: str, default: Any = None) -> Any:
return os.getenv(key, default)
def _getattr(self, name: str) -> Any:
return lambda key, default=None, *args, **kwargs: self.cast(
value=(self.env(key) or default), typecast=name, *args, **kwargs
)
# правильно доработать метод до привычной сигнатуры DotEnv::get(key, default=None)
sys.modules["dotenv"].main.DotEnv.cast = _cast
sys.modules["dotenv"].main.DotEnv.env = sys.modules["dotenv"].main.DotEnv.getenv = _env
sys.modules["dotenv"].main.DotEnv.__getattr__ = _getattr
sys.modules["dotenv"].read_dotenv = read_dotenv
sys.modules["dotenv"].__all__.append("read_dotenv")
sys.modules["dotenv"].__all__.append("getenv")
__all__ = [*sys.modules["dotenv"].__all__, "DotEnv"]