v2.6 fix search dir for localenv first load
This commit is contained in:
parent
8b150c14ff
commit
6ad2490438
BIN
dist/pyutl-2.6.tar.gz
vendored
Normal file
BIN
dist/pyutl-2.6.tar.gz
vendored
Normal file
Binary file not shown.
@ -1,6 +1,6 @@
|
|||||||
Metadata-Version: 2.1
|
Metadata-Version: 2.1
|
||||||
Name: pyutl
|
Name: pyutl
|
||||||
Version: 2.5
|
Version: 2.6
|
||||||
Summary: functions and utilities to recycle code
|
Summary: functions and utilities to recycle code
|
||||||
Home-page: https://git.binkfe.com/jesrat/pyutl
|
Home-page: https://git.binkfe.com/jesrat/pyutl
|
||||||
Author: Josue Gomez <jgomez@jesrat.com>
|
Author: Josue Gomez <jgomez@jesrat.com>
|
||||||
|
|||||||
@ -11,7 +11,7 @@ shellExecute can receive a cmd as str or arr example
|
|||||||
__title__ = 'pyutl'
|
__title__ = 'pyutl'
|
||||||
__description__ = 'functions and utilities to recycle code'
|
__description__ = 'functions and utilities to recycle code'
|
||||||
__url__ = 'https://git.binkfe.com/jesrat/pyutl'
|
__url__ = 'https://git.binkfe.com/jesrat/pyutl'
|
||||||
__version__ = '2.5'
|
__version__ = '2.6'
|
||||||
__author__ = 'Josue Gomez <jgomez@jesrat.com>'
|
__author__ = 'Josue Gomez <jgomez@jesrat.com>'
|
||||||
__email__ = "jgomez@binkfe.com"
|
__email__ = "jgomez@binkfe.com"
|
||||||
__maintainer__ = "Josue Gomez"
|
__maintainer__ = "Josue Gomez"
|
||||||
|
|||||||
@ -15,19 +15,23 @@ DEFAULT = object()
|
|||||||
|
|
||||||
|
|
||||||
class LocalEnv:
|
class LocalEnv:
|
||||||
|
_BOOLEANS = {'1': True, 'yes': True, 'true': True, 'on': True,
|
||||||
|
'0': False, 'no': False, 'false': False, 'off': False, '': False}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.files = []
|
self.files = []
|
||||||
self.data = {}
|
self.data = {}
|
||||||
|
self.first_load = False
|
||||||
|
|
||||||
def load(self, file=None):
|
def load(self, file=None):
|
||||||
"""
|
"""
|
||||||
If no file is defined, the .env file will be searched
|
If no file is defined, the .env file will be searched
|
||||||
in invoker module's directory
|
in invoker module's directory
|
||||||
"""
|
"""
|
||||||
if file is not None:
|
if file is None:
|
||||||
self.files.append({'file': file, 'exists': '', 'loaded': False})
|
file = self._invoker()
|
||||||
else:
|
|
||||||
self.files.append({'file': self._invoker(), 'exists': '', 'loaded': False})
|
self.files.append({'file': file, 'exists': '', 'loaded': False})
|
||||||
|
|
||||||
# search all files given and load them
|
# search all files given and load them
|
||||||
for file_dict in self.files:
|
for file_dict in self.files:
|
||||||
@ -45,26 +49,36 @@ class LocalEnv:
|
|||||||
self.data[key] = value
|
self.data[key] = value
|
||||||
file_dict['loaded'] = True
|
file_dict['loaded'] = True
|
||||||
|
|
||||||
|
def _cast(self, cast, data):
|
||||||
|
if cast is bool and str(data).lower() not in self._BOOLEANS:
|
||||||
|
raise ValueError(f'value can not be parsed as boolean')
|
||||||
|
elif cast is bool:
|
||||||
|
return self._BOOLEANS[str(data).lower()]
|
||||||
|
else:
|
||||||
|
return cast(data)
|
||||||
|
|
||||||
def get(self, key, default=DEFAULT, cast=None):
|
def get(self, key, default=DEFAULT, cast=None):
|
||||||
|
if not self.first_load:
|
||||||
|
self.load()
|
||||||
|
self.first_load = True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ret_val = self.data[key] if cast is None else cast(self.data[key])
|
ret_val = self.data[key] if cast is None else self._cast(cast, self.data[key])
|
||||||
except KeyError:
|
except KeyError:
|
||||||
if default != DEFAULT:
|
if default != DEFAULT:
|
||||||
ret_val = default if cast is None else cast(default)
|
ret_val = default if cast is None else self._cast(cast, default)
|
||||||
else:
|
else:
|
||||||
raise KeyNotFound(f'value not found in files: \n{json.dumps(self.files, indent=4)}')
|
raise KeyNotFound(f'value not found in files: \n{json.dumps(self.files, indent=4)}')
|
||||||
return ret_val
|
return ret_val
|
||||||
|
|
||||||
@staticmethod
|
def _invoker(self):
|
||||||
def _invoker():
|
|
||||||
# tip from:
|
# tip from:
|
||||||
# https://github.com/henriquebastos/python-decouple/blob/master/decouple.py
|
# https://github.com/henriquebastos/python-decouple/blob/master/decouple.py
|
||||||
# MAGIC! Get the caller's module path.
|
# MAGIC! Get the caller's module path.
|
||||||
frame = sys._getframe()
|
frame = sys._getframe()
|
||||||
path = os.path.dirname(frame.f_back.f_back.f_code.co_filename)
|
path = os.path.dirname(frame.f_back.f_back.f_back.f_code.co_filename)
|
||||||
file = os.path.join(path, '.env')
|
file = os.path.join(path, '.env')
|
||||||
return file
|
return file
|
||||||
|
|
||||||
|
|
||||||
localenv = LocalEnv()
|
localenv = LocalEnv()
|
||||||
localenv.load()
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user