add ability to save config file
This commit is contained in:
parent
374cc307b1
commit
7274d1bf28
2 changed files with 17 additions and 5 deletions
1
bitbotd
1
bitbotd
|
@ -117,6 +117,7 @@ if args.add_server:
|
|||
|
||||
cache = Cache.Cache()
|
||||
config = Config.Config(args.config)
|
||||
config.load()
|
||||
events = EventManager.EventRoot(log).wrap()
|
||||
exports = Exports.Exports()
|
||||
timers = Timers.Timers(database, events, log)
|
||||
|
|
|
@ -1,17 +1,28 @@
|
|||
import configparser, os, typing
|
||||
import collections, configparser, os, typing
|
||||
|
||||
class Config(object):
|
||||
def __init__(self, location: str):
|
||||
self.location = location
|
||||
self._config = {} # type: typing.Dict[str, str]
|
||||
self.load()
|
||||
self._config = collections.OrderedDict()
|
||||
|
||||
def _parser(self) -> configparser.ConfigParser:
|
||||
return configparser.ConfigParser(dict_type=collections.OrderedDict)
|
||||
|
||||
def load(self):
|
||||
if os.path.isfile(self.location):
|
||||
with open(self.location) as config_file:
|
||||
parser = configparser.ConfigParser()
|
||||
parser = self._parser()
|
||||
parser.read_string(config_file.read())
|
||||
self._config = {k: v for k, v in parser["bot"].items() if v}
|
||||
self._config.clear()
|
||||
for k, v in parser["bot"].items():
|
||||
if v:
|
||||
self._config[k] = v
|
||||
|
||||
def save(self):
|
||||
with open(self.location, "w") as config_file:
|
||||
parser = self._parser()
|
||||
parser["bot"] = self._config.copy()
|
||||
parser.write(config_file)
|
||||
|
||||
def __getitem__(self, key: str) -> typing.Any:
|
||||
return self._config[key]
|
||||
|
|
Loading…
Reference in a new issue