Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F69028630
readconf.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Jun 29, 23:15
Size
2 KB
Mime Type
text/x-python
Expires
Mon, Jul 1, 23:15 (2 d)
Engine
blob
Format
Raw Data
Handle
18661895
Attached To
R11599 sausage-binary
readconf.py
View Options
'''
Configuration file module
'''
import
configparser
import
errno
import
os
from
base64
import
b64decode
,
binascii
conf_path
=
"/etc/sausage/sausage.cfg"
class
SectionMissing
(
Exception
):
def
__init__
(
self
,
section
):
self
.
message
=
f
"Configuration file have to contain {section} section"
def
__str__
(
self
):
return
self
.
message
class
ReadConf
(
object
):
def
__init__
(
self
,
cost
=
False
)
->
None
:
self
.
options
=
{}
self
.
cfg_parser
=
configparser
.
ConfigParser
()
if
len
(
self
.
cfg_parser
.
read
(
conf_path
))
!=
1
:
raise
FileNotFoundError
(
errno
.
ENOENT
,
os
.
strerror
(
errno
.
ENOENT
),
conf_path
)
if
"default"
not
in
self
.
cfg_parser
:
raise
(
SectionMissing
(
"default"
))
ReadConf
.
cluster
=
self
.
cfg_parser
[
"default"
]
.
get
(
"cluster"
,
"cluster"
)
ReadConf
.
clusters
=
[
i
.
replace
(
" "
,
""
)
for
i
in
self
.
cfg_parser
[
"default"
]
.
get
(
"clusters"
,
""
)
.
split
(
","
)
]
ReadConf
.
debug
=
self
.
cfg_parser
[
"default"
]
.
getboolean
(
"log_devel"
)
if
not
cost
:
ReadConf
.
hosts
=
self
.
cfg_parser
[
"server"
]
.
get
(
"urls"
,
""
)
.
split
(
","
)
ReadConf
.
indexes
=
self
.
cfg_parser
[
"server"
]
.
get
(
"index"
)
ReadConf
.
fields
=
dict
(
self
.
cfg_parser
[
"fields"
])
ReadConf
.
apikey
=
tuple
(
map
(
str
,
self
.
cfg_parser
[
"server"
]
.
get
(
"apikey"
)
.
split
(
","
))
)
try
:
ReadConf
.
apikey
=
tuple
(
bytes
.
decode
(
b64decode
(
str
.
encode
(
self
.
cfg_parser
[
"server"
]
.
get
(
"apikey"
)
)
)
)
.
split
(
','
))
print
(
ReadConf
.
apikey
)
except
(
UnicodeDecodeError
,
binascii
.
Error
)
as
_except
:
raise
ValueError
(
f
"ApiKey format not valid {_except}"
)
if
len
(
ReadConf
.
apikey
)
!=
2
:
ReadConf
.
apikey
=
tuple
((
None
,
None
))
ReadConf
.
billinggrp
=
self
.
cfg_parser
[
"default"
]
.
get
(
"billing_group"
,
"root"
)
Event Timeline
Log In to Comment