2023-04-22 21:02:50 +00:00
|
|
|
from libs.bflb_utils import printf
|
|
|
|
from configparser import ConfigParser
|
2023-04-20 18:45:21 +00:00
|
|
|
|
|
|
|
|
2023-04-22 21:02:50 +00:00
|
|
|
class ConfigObj:
|
|
|
|
cnf_infile = None
|
|
|
|
config = None
|
2023-04-20 18:45:21 +00:00
|
|
|
|
2023-04-22 21:02:50 +00:00
|
|
|
def __init__(self, file):
|
|
|
|
self.cfg_infile = file
|
|
|
|
self.config = ConfigParser()
|
|
|
|
self.config.read([file], 'UTF8')
|
2023-04-20 18:45:21 +00:00
|
|
|
|
2023-04-22 21:02:50 +00:00
|
|
|
def has_option(self, section, key):
|
|
|
|
return self.config.has_option(section, key)
|
2023-04-20 18:45:21 +00:00
|
|
|
|
2023-04-22 21:02:50 +00:00
|
|
|
def get(self, section, key):
|
|
|
|
return self.config.get(section, key)
|
2023-04-20 18:45:21 +00:00
|
|
|
|
2023-04-22 21:02:50 +00:00
|
|
|
def set(self, section, key, value):
|
|
|
|
return self.config.set(section, key, value)
|
2023-04-20 18:45:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BFConfigParser:
|
|
|
|
cfg_infile = None
|
2023-04-22 21:02:50 +00:00
|
|
|
cfg_obj = None
|
2023-04-20 18:45:21 +00:00
|
|
|
|
|
|
|
def __init__(self, file=None):
|
|
|
|
self.cfg_infile = file
|
2023-04-22 21:02:50 +00:00
|
|
|
if file is not None:
|
|
|
|
self.cfg_obj = ConfigObj(self.cfg_infile)
|
2023-04-20 18:45:21 +00:00
|
|
|
|
|
|
|
def read(self, file=None):
|
2023-04-22 21:02:50 +00:00
|
|
|
printf('Reading configuration from file: %s' % file)
|
2023-04-20 18:45:21 +00:00
|
|
|
self.cfg_infile = file
|
2023-04-22 21:02:50 +00:00
|
|
|
if file is not None:
|
|
|
|
self.cfg_obj = ConfigObj(self.cfg_infile)
|
2023-04-20 18:45:21 +00:00
|
|
|
return self.cfg_obj
|
|
|
|
|
2023-04-22 21:02:50 +00:00
|
|
|
def has_option(self, section, key):
|
|
|
|
return self.cfg_obj.has_option(section, key)
|
|
|
|
|
2023-04-20 18:45:21 +00:00
|
|
|
def get(self, section, key):
|
2023-04-22 21:02:50 +00:00
|
|
|
ret = self.cfg_obj.get(section, key)
|
2023-04-20 18:45:21 +00:00
|
|
|
if ret == '""':
|
|
|
|
return ''
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def set(self, section, key, value):
|
2023-04-22 21:02:50 +00:00
|
|
|
self.cfg_obj.set(section, key, str(value))
|
2023-04-20 18:45:21 +00:00
|
|
|
|
2023-04-22 21:02:50 +00:00
|
|
|
def write(self, file, mode):
|
|
|
|
pass # why would the flasher want to write the config file back?
|