appsettings2 0.4.1__tar.gz → 1.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: appsettings2
3
- Version: 0.4.1
3
+ Version: 1.0.2
4
4
  Summary: A python library that unifies configuration sources into a Configuration object that can be bound to complex types, or accessed directly for configuration data.
5
5
  License: MIT
6
6
  Author: Shaun Wilson
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "appsettings2"
3
- version = "0.4.1"
3
+ version = "1.0.2"
4
4
  description = "A python library that unifies configuration sources into a Configuration object that can be bound to complex types, or accessed directly for configuration data."
5
5
  authors = ["Shaun Wilson <mrshaunwilson@msn.com>"]
6
6
  license = "MIT"
@@ -11,6 +11,7 @@ classifiers = [
11
11
  "Programming Language :: Python :: 3.13"
12
12
  ]
13
13
  exclude = [
14
+ "sys",
14
15
  "tests"
15
16
  ]
16
17
  packages = [
@@ -31,7 +31,7 @@ class Configuration:
31
31
  self.__keys.pop(key)
32
32
 
33
33
  def __getitem__(self, key:str) -> any:
34
- parts = key.replace('__', '.').replace(':', '.').split('.')
34
+ parts = key.replace(':', '__').split('__')
35
35
  o = self
36
36
  for part in parts:
37
37
  if o == self:
@@ -60,7 +60,18 @@ class Configuration:
60
60
  continue
61
61
  ahint = targetTypeHints.get(aname)
62
62
  rval = source.get(aname)
63
- if isinstance(rval, Configuration):
63
+ if rval == None:
64
+ setattr(target, aname, None)
65
+ elif ahint is float:
66
+ v = float(rval)
67
+ setattr(target, aname, v)
68
+ elif ahint is int:
69
+ v = int(rval)
70
+ setattr(target, aname, v)
71
+ elif ahint is str:
72
+ v = str(rval)
73
+ setattr(target, aname, v)
74
+ elif isinstance(rval, Configuration):
64
75
  if hasattr(ahint, '__origin__') and issubclass(ahint.__origin__, dict):
65
76
  lval = rval.toDictionary()
66
77
  setattr(target, aname, lval)
@@ -69,29 +80,17 @@ class Configuration:
69
80
  lval = ahint()
70
81
  setattr(target, aname, lval)
71
82
  self.__recursiveBind(lval, rval)
72
- elif rval != None:
73
- if ahint is float:
74
- v = float(rval)
75
- setattr(target, aname, v)
76
- elif ahint is int:
77
- v = int(rval)
78
- setattr(target, aname, v)
79
- elif ahint is str:
80
- v = str(rval)
81
- setattr(target, aname, v)
82
- else:
83
- if hasattr(ahint, '__origin__') and issubclass(ahint.__origin__, list):
84
- elementType = ahint.__args__[0]
85
- l = ahint()
86
- for e in rval:
87
- v = self.__recursiveBindType(elementType, e)
88
- l.append(v)
89
- setattr(target, aname, l)
90
- else:
91
- # naive behavior
92
- setattr(target, aname, rval)
93
83
  else:
94
- setattr(target, aname, None)
84
+ if hasattr(ahint, '__origin__') and issubclass(ahint.__origin__, list):
85
+ elementType = ahint.__args__[0]
86
+ l = ahint()
87
+ for e in rval:
88
+ v = self.__recursiveBindType(elementType, e)
89
+ l.append(v)
90
+ setattr(target, aname, l)
91
+ else:
92
+ # naive behavior
93
+ setattr(target, aname, rval)
95
94
  return target
96
95
 
97
96
  def __recursiveBindType(self, elementType:type, source:any) -> any:
@@ -111,9 +110,7 @@ class Configuration:
111
110
 
112
111
  def __scrub_key(self, key:str) -> str:
113
112
  """Scrubs a key for use as an attribute/identifier according to the Python lexer/standard."""
114
- key = key\
115
- .replace('__', '.')\
116
- .replace(':', '.')
113
+ key = key.replace(':', '__').replace('.', '_')
117
114
  return key if None == self.__key_scrub_re else \
118
115
  self.__key_scrub_re.sub(
119
116
  self.__scrub_uc,
@@ -155,9 +152,19 @@ class Configuration:
155
152
  t = self.__keys.popitem()
156
153
  delattr(self, t[1])
157
154
 
155
+ @staticmethod
156
+ def fromDictionary(source:dict, *, normalize:bool = False, scrubkeys:bool = False) -> 'Configuration':
157
+ config:Configuration = Configuration(normalize=normalize, scrubkeys=scrubkeys)
158
+ for kvp in source.items():
159
+ v = kvp[1]
160
+ if issubclass(type(v), dict):
161
+ v = Configuration.fromDictionary(v, normalize=normalize, scrubkeys=scrubkeys)
162
+ config.set(kvp[0], v)
163
+ return config
164
+
158
165
  def get(self, key:str, default:any = None) -> any:
159
166
  """Gets the configuration element associated with the specified key."""
160
- parts = key.replace('__', '.').replace(':', '.').split('.')
167
+ parts = key.replace(':', '__').split('__')
161
168
  o = self
162
169
  for part in parts:
163
170
  if o == self:
@@ -192,7 +199,7 @@ class Configuration:
192
199
  """Sets the configuration element for the specified key."""
193
200
  if self.__normalize:
194
201
  key = key.upper()
195
- parts = key.replace('__', '.').replace(':', '.').split('.')
202
+ parts = key.replace(':', '__').split('__')
196
203
  o = self
197
204
  for i in range(len(parts) - 1):
198
205
  if o == self:
@@ -211,6 +218,17 @@ class Configuration:
211
218
  o = c
212
219
  else:
213
220
  o = o.get(parts[i])
221
+ vtype = type(value)
222
+ if issubclass(vtype, dict):
223
+ value = Configuration.fromDictionary(value, normalize=self.__normalize, scrubkeys=self.__key_scrub_re != None)
224
+ elif issubclass(vtype, list):
225
+ l = []
226
+ for e in value:
227
+ if issubclass(type(e), dict):
228
+ l.append(Configuration.fromDictionary(e, normalize=self.__normalize, scrubkeys=self.__key_scrub_re != None))
229
+ else:
230
+ l.append(e)
231
+ value = l
214
232
  key = parts[-1]
215
233
  if o == self:
216
234
  k = self.__keys.get(key.upper())
@@ -229,6 +247,14 @@ class Configuration:
229
247
  v = getattr(self, self.__scrub_key(k))
230
248
  if isinstance(v, Configuration):
231
249
  result[k] = v.toDictionary()
250
+ elif issubclass(type(v), list):
251
+ tmp = []
252
+ for e in v:
253
+ if isinstance(e, Configuration):
254
+ tmp.append(e.toDictionary())
255
+ else:
256
+ tmp.append(e)
257
+ result[k] = tmp
232
258
  else:
233
259
  result[k] = v
234
260
  return result
@@ -6,4 +6,4 @@ from .Configuration import Configuration
6
6
  from .ConfigurationBuilder import ConfigurationBuilder
7
7
  from .ConfigurationException import ConfigurationException
8
8
 
9
- __version__ = '0.4.1'
9
+ __version__ = '1.0.2'
File without changes
File without changes