mirror of
https://github.com/meeb/tubesync.git
synced 2025-06-24 14:06:36 +00:00
Add getenv
to common.utils
This commit is contained in:
parent
3c94e5a0b3
commit
c2653b76a9
@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
import string
|
import string
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from urllib.parse import urlunsplit, urlencode, urlparse
|
from urllib.parse import urlunsplit, urlencode, urlparse
|
||||||
@ -6,6 +7,44 @@ from yt_dlp.utils import LazyList
|
|||||||
from .errors import DatabaseConnectionError
|
from .errors import DatabaseConnectionError
|
||||||
|
|
||||||
|
|
||||||
|
def getenv(key, default=None, /, *, integer=False, string=True):
|
||||||
|
'''
|
||||||
|
Guarantees a returned type from calling `os.getenv`
|
||||||
|
The caller can request the integer type,
|
||||||
|
or use the default string type.
|
||||||
|
'''
|
||||||
|
|
||||||
|
args = dict(key=key, default=default, integer=integer, string=string)
|
||||||
|
supported_types = dict(zip(args.keys(), (
|
||||||
|
(str,), # key
|
||||||
|
(
|
||||||
|
bool,
|
||||||
|
float,
|
||||||
|
int,
|
||||||
|
str,
|
||||||
|
None.__class__,
|
||||||
|
), # default
|
||||||
|
(bool,) * (len(args.keys()) - 2),
|
||||||
|
)))
|
||||||
|
unsupported_type_msg = 'Unsupported type for positional argument, "{}": {}'
|
||||||
|
for k, t in supported_types.items():
|
||||||
|
v = args[k]
|
||||||
|
assert isinstance(v, t), unsupported_type_msg.format(k, type(v))
|
||||||
|
|
||||||
|
d = str(default) if default is not None else None
|
||||||
|
|
||||||
|
# just in case `os` wasn't already imported
|
||||||
|
import os
|
||||||
|
|
||||||
|
r = os.getenv(key, d)
|
||||||
|
if r is None:
|
||||||
|
if string: r = str()
|
||||||
|
if integer: r = int()
|
||||||
|
elif integer:
|
||||||
|
r = int(float(r))
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
def parse_database_connection_string(database_connection_string):
|
def parse_database_connection_string(database_connection_string):
|
||||||
'''
|
'''
|
||||||
Parses a connection string in a URL style format, such as:
|
Parses a connection string in a URL style format, such as:
|
||||||
|
Loading…
Reference in New Issue
Block a user