Browse Source

v2.6 fix search dir for localenv first load

Josue Gomez 4 months ago
parent
commit
6ad2490438
4 changed files with 26 additions and 12 deletions
  1. BIN
      dist/pyutl-2.6.tar.gz
  2. 1
    1
      pyutl.egg-info/PKG-INFO
  3. 1
    1
      pyutl/__init__.py
  4. 24
    10
      pyutl/localenv.py

BIN
dist/pyutl-2.6.tar.gz View File


+ 1
- 1
pyutl.egg-info/PKG-INFO View File

@@ -1,6 +1,6 @@
1 1
 Metadata-Version: 2.1
2 2
 Name: pyutl
3
-Version: 2.5
3
+Version: 2.6
4 4
 Summary: functions and utilities to recycle code
5 5
 Home-page: https://git.binkfe.com/jesrat/pyutl
6 6
 Author: Josue Gomez <jgomez@jesrat.com>

+ 1
- 1
pyutl/__init__.py View File

@@ -11,7 +11,7 @@ shellExecute can receive a cmd as str or arr example
11 11
 __title__ = 'pyutl'
12 12
 __description__ = 'functions and utilities to recycle code'
13 13
 __url__ = 'https://git.binkfe.com/jesrat/pyutl'
14
-__version__ = '2.5'
14
+__version__ = '2.6'
15 15
 __author__ = 'Josue Gomez <jgomez@jesrat.com>'
16 16
 __email__ = "jgomez@binkfe.com"
17 17
 __maintainer__ = "Josue Gomez"

+ 24
- 10
pyutl/localenv.py View File

@@ -15,19 +15,23 @@ DEFAULT = object()
15 15
 
16 16
 
17 17
 class LocalEnv:
18
+    _BOOLEANS = {'1': True, 'yes': True, 'true': True, 'on': True,
19
+                 '0': False, 'no': False, 'false': False, 'off': False, '': False}
20
+
18 21
     def __init__(self):
19 22
         self.files = []
20 23
         self.data = {}
24
+        self.first_load = False
21 25
 
22 26
     def load(self, file=None):
23 27
         """
24 28
         If no file is defined, the .env file will be searched
25 29
         in invoker module's directory
26 30
         """
27
-        if file is not None:
28
-            self.files.append({'file': file, 'exists': '', 'loaded': False})
29
-        else:
30
-            self.files.append({'file': self._invoker(), 'exists': '', 'loaded': False})
31
+        if file is None:
32
+            file = self._invoker()
33
+
34
+        self.files.append({'file': file, 'exists': '', 'loaded': False})
31 35
 
32 36
         # search all files given and load them
33 37
         for file_dict in self.files:
@@ -45,26 +49,36 @@ class LocalEnv:
45 49
                         self.data[key] = value
46 50
                     file_dict['loaded'] = True
47 51
 
52
+    def _cast(self, cast, data):
53
+        if cast is bool and str(data).lower() not in self._BOOLEANS:
54
+            raise ValueError(f'value can not be parsed as boolean')
55
+        elif cast is bool:
56
+            return self._BOOLEANS[str(data).lower()]
57
+        else:
58
+            return cast(data)
59
+
48 60
     def get(self, key, default=DEFAULT, cast=None):
61
+        if not self.first_load:
62
+            self.load()
63
+            self.first_load = True
64
+
49 65
         try:
50
-            ret_val = self.data[key] if cast is None else cast(self.data[key])
66
+            ret_val = self.data[key] if cast is None else self._cast(cast, self.data[key])
51 67
         except KeyError:
52 68
             if default != DEFAULT:
53
-                ret_val = default if cast is None else cast(default)
69
+                ret_val = default if cast is None else self._cast(cast, default)
54 70
             else:
55 71
                 raise KeyNotFound(f'value not found in files: \n{json.dumps(self.files, indent=4)}')
56 72
         return ret_val
57 73
 
58
-    @staticmethod
59
-    def _invoker():
74
+    def _invoker(self):
60 75
         # tip from:
61 76
         # https://github.com/henriquebastos/python-decouple/blob/master/decouple.py
62 77
         # MAGIC! Get the caller's module path.
63 78
         frame = sys._getframe()
64
-        path = os.path.dirname(frame.f_back.f_back.f_code.co_filename)
79
+        path = os.path.dirname(frame.f_back.f_back.f_back.f_code.co_filename)
65 80
         file = os.path.join(path, '.env')
66 81
         return file
67 82
 
68 83
 
69 84
 localenv = LocalEnv()
70
-localenv.load()