2018-10-30 14:58:48 +00:00
|
|
|
import configparser, os, typing
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
class Config(object):
|
2018-10-30 14:58:48 +00:00
|
|
|
def __init__(self, location: str):
|
2018-09-27 10:07:29 +00:00
|
|
|
self.location = location
|
2018-10-31 15:12:46 +00:00
|
|
|
self._config = {} # type: typing.Dict[str, str]
|
2018-09-28 15:51:36 +00:00
|
|
|
self.load()
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-09-28 15:51:36 +00:00
|
|
|
def load(self):
|
2018-09-27 10:07:29 +00:00
|
|
|
if os.path.isfile(self.location):
|
|
|
|
with open(self.location) as config_file:
|
2018-07-15 23:36:52 +00:00
|
|
|
parser = configparser.ConfigParser()
|
|
|
|
parser.read_string(config_file.read())
|
2018-12-02 08:28:59 +00:00
|
|
|
self._config = {k: v for k, v in parser["bot"].items() if v}
|
2018-09-28 15:51:36 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def __getitem__(self, key: str) -> typing.Any:
|
2018-09-28 15:51:36 +00:00
|
|
|
return self._config[key]
|
2018-10-30 14:58:48 +00:00
|
|
|
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)
|
2018-10-30 14:58:48 +00:00
|
|
|
def __contains__(self, key: str) -> bool:
|
2018-10-06 21:58:59 +00:00
|
|
|
return key in self._config
|
2018-09-28 15:51:36 +00:00
|
|
|
|