File: config_box.py

package info (click to toggle)
python-dynaconf 3.2.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,900 kB
  • sloc: python: 21,464; sh: 9; makefile: 4
file content (51 lines) | stat: -rw-r--r-- 1,678 bytes parent folder | download
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
#!/usr/bin/env python
_D='getint'
_C='getfloat'
_B='getboolean'
_A=None
from dynaconf.vendor.box.box import Box
class ConfigBox(Box):
	_protected_keys=dir(Box)+['bool','int','float','list',_B,_C,_D]
	def __getattr__(A,item):
		try:return super().__getattr__(item)
		except AttributeError:return super().__getattr__(item.lower())
	def __dir__(A):return super().__dir__()+['bool','int','float','list',_B,_C,_D]
	def bool(D,item,default=_A):
		C=False;B=default;A=item
		try:A=D.__getattr__(A)
		except AttributeError as E:
			if B is not _A:return B
			raise E
		if isinstance(A,(bool,int)):return bool(A)
		if isinstance(A,str)and A.lower()in('n','no','false','f','0'):return C
		return True if A else C
	def int(C,item,default=_A):
		B=default;A=item
		try:A=C.__getattr__(A)
		except AttributeError as D:
			if B is not _A:return B
			raise D
		return int(A)
	def float(C,item,default=_A):
		B=default;A=item
		try:A=C.__getattr__(A)
		except AttributeError as D:
			if B is not _A:return B
			raise D
		return float(A)
	def list(E,item,default=_A,spliter=',',strip=True,mod=_A):
		C=strip;B=default;A=item
		try:A=E.__getattr__(A)
		except AttributeError as F:
			if B is not _A:return B
			raise F
		if C:A=A.lstrip('[').rstrip(']')
		D=[A.strip()if C else A for A in A.split(spliter)]
		if mod:return list(map(mod,D))
		return D
	def getboolean(A,item,default=_A):return A.bool(item,default)
	def getint(A,item,default=_A):return A.int(item,default)
	def getfloat(A,item,default=_A):return A.float(item,default)
	def __repr__(A):return'<ConfigBox: {0}>'.format(str(A.to_dict()))
	def copy(A):return ConfigBox(super().copy())
	def __copy__(A):return ConfigBox(super().copy())