bitbot-3.11-fork/src/Config.py

35 lines
1.1 KiB
Python
Raw Normal View History

2019-12-07 10:42:43 +00:00
import collections, configparser, os, typing
2016-03-29 11:56:58 +00:00
class Config(object):
2019-12-07 10:50:50 +00:00
def __init__(self, location: str):
self.location = location
2019-12-07 10:50:50 +00:00
self._config: typing.Dict[str, str] = collections.OrderedDict()
2019-12-07 10:42:43 +00:00
def _parser(self) -> configparser.ConfigParser:
2019-12-07 10:50:50 +00:00
return configparser.ConfigParser()
2016-03-29 11:56:58 +00:00
2018-09-28 15:51:36 +00:00
def load(self):
if os.path.isfile(self.location):
with open(self.location) as config_file:
2019-12-07 10:42:43 +00:00
parser = self._parser()
parser.read_string(config_file.read())
2019-12-07 10:42:43 +00:00
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)
2018-09-28 15:51:36 +00:00
def __getitem__(self, key: str) -> typing.Any:
2018-09-28 15:51:36 +00:00
return self._config[key]
def get(self, key: str, default: typing.Any=None) -> typing.Any:
2018-09-28 15:51:36 +00:00
return self._config.get(key, default)
def __contains__(self, key: str) -> bool:
return key in self._config
2018-09-28 15:51:36 +00:00