FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-tools/voidpp_tools/config_loader.py at master · voidpp/python-tools · GitHub
voidpp
/
python-tools
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-tools
/
voidpp_tools
/
config_loader.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
128 lines (101 loc) · 3.92 KB
Breadcrumbs
python-tools
/
voidpp_tools
/
config_loader.py
Copy path
File metadata and controls
128 lines (101 loc) · 3.92 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import
os
import
abc
from
.
dict_utils
import
recursive_update
class
ConfigLoaderException
(
Exception
):
pass
class
ConfigFileNotFoundException
(
Exception
):
pass
class
ConfigFormatter
(
object
):
__metaclass__
=
abc
.
ABCMeta
@
abc
.
abstractmethod
def
encode
(
self
,
data
):
"""
Args:
data: any serializable python type
Returns:
The encoded string data
"""
pass
@
abc
.
abstractmethod
def
decode
(
self
,
data
):
"""
Args:
data (str): the raw data from the file
Returns:
Dict, list or any serializable python type
"""
pass
class
ConfigLoader
(
object
):
"""
Args:
formatter (ConfigFormatter): config encode/decode interface instance
base_path (str): a custom path which may be contains the config
nested (bool): in case of true, load all the config files found in the sources and merges it
"""
def
__init__
(
self
,
formatter
,
base_path
,
nested
=
False
):
self
.
sources
=
[
os
.
getcwd
(),
os
.
path
.
dirname
(
os
.
path
.
abspath
(
base_path
)),
os
.
path
.
expanduser
(
'~'
),
'/etc'
,
]
self
.
__loaded_config_file
=
None
self
.
__formatter
=
formatter
self
.
__nested
=
nested
@
property
def
filename
(
self
):
return
self
.
__loaded_config_file
def
__search_config_files
(
self
,
filename
):
filenames
=
[]
tries
=
[]
for
source
in
self
.
sources
:
file_path
=
os
.
path
.
join
(
source
,
filename
)
tries
.
append
(
file_path
)
if
not
os
.
path
.
exists
(
file_path
):
continue
filenames
.
append
(
file_path
)
return
filenames
,
tries
def
__load_config_files
(
self
,
filenames
):
data
=
dict
()
for
filename
in
filenames
:
with
open
(
filename
)
as
f
:
recursive_update
(
data
,
self
.
__formatter
.
decode
(
f
.
read
()))
return
data
def
load
(
self
,
filename
,
create
=
None
,
default_conf
=
{}):
"""Load the config file
Args:
filename (str): the filename of the config, without any path
create (str): if the config file not found, and this parameter is not None,
a config file will be create with content of default_conf
default_conf (dict): content of the default config data
Returns:
Return value of the ConfigFormatter.decode or the default_conf value
Raises:
ConfigFileNotFoundException: if the config file not found
"""
filenames
,
tries
=
self
.
__search_config_files
(
filename
)
if
len
(
filenames
):
self
.
__loaded_config_file
=
filenames
if
self
.
__nested
else
filenames
[
0
]
return
self
.
__load_config_files
(
filenames
if
self
.
__nested
else
filenames
[:
1
])
if
create
is
not
None
:
self
.
__loaded_config_file
=
os
.
path
.
join
(
create
,
filename
)
self
.
save
(
default_conf
)
return
default_conf
raise
ConfigFileNotFoundException
(
"Config file not found in: %s"
%
tries
)
def
save
(
self
,
data
):
"""Save the config data
Args:
data: any serializable config data
Raises:
ConfigLoaderException: if the ConfigLoader.load not called, so there is no config file name,
or the data is not serializable or the loader is nested
"""
if
self
.
__nested
:
raise
ConfigLoaderException
(
"Cannot save the config if the 'nested' paramter is True!"
)
if
self
.
__loaded_config_file
is
None
:
raise
ConfigLoaderException
(
"Load not called yet!"
)
try
:
with
open
(
self
.
__loaded_config_file
,
'w'
)
as
f
:
f
.
write
(
self
.
__formatter
.
encode
(
data
))
except
Exception
as
e
:
raise
ConfigLoaderException
(
"Config data is not serializable: %s"
%
e
)
Back
|
FazBrowse Home
|
New Git URL