2018-10-30 14:58:48 +00:00
|
|
|
import typing
|
2018-09-02 18:54:45 +00:00
|
|
|
|
|
|
|
class Exports(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._exports = {}
|
|
|
|
self._context_exports = {}
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def new_context(self, context: str) -> "ExportsContext":
|
2018-09-02 18:54:45 +00:00
|
|
|
return ExportsContext(self, context)
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def add(self, setting: str, value: typing.Any):
|
2018-09-02 18:54:45 +00:00
|
|
|
self._add(None, setting, value)
|
2018-10-30 14:58:48 +00:00
|
|
|
def _context_add(self, context: str, setting: str, value: typing.Any):
|
2018-09-02 18:54:45 +00:00
|
|
|
self._add(context, setting, value)
|
2018-10-30 17:49:35 +00:00
|
|
|
def _add(self, context: typing.Optional[str], setting: str,
|
|
|
|
value: typing.Any):
|
2018-09-02 18:54:45 +00:00
|
|
|
if context == None:
|
2018-10-30 16:27:50 +00:00
|
|
|
if not setting in self._exports:
|
2018-09-02 18:54:45 +00:00
|
|
|
self._exports[setting] = []
|
|
|
|
self._exports[setting].append(value)
|
|
|
|
else:
|
|
|
|
if not context in self._context_exports:
|
|
|
|
self._context_exports[context] = {}
|
|
|
|
if not setting in self._context_exports[context]:
|
|
|
|
self._context_exports[context][setting] = []
|
|
|
|
self._context_exports[context][setting].append(value)
|
|
|
|
|
2019-06-25 16:53:00 +00:00
|
|
|
def _get_keys(self):
|
2019-06-29 21:26:28 +00:00
|
|
|
keys = list(self._exports.keys())
|
|
|
|
for context in self._context_exports.keys():
|
|
|
|
keys += list(self._context_exports[context].keys())
|
|
|
|
return list(set(keys))
|
2019-06-25 16:53:00 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_all(self, setting: str) -> typing.List[typing.Any]:
|
2018-09-02 18:54:45 +00:00
|
|
|
return self._exports.get(setting, []) + sum([
|
|
|
|
exports.get(setting, []) for exports in
|
|
|
|
self._context_exports.values()], [])
|
2020-03-08 14:14:00 +00:00
|
|
|
def get(self, setting: str, default: typing.Any=None
|
2019-02-07 13:36:18 +00:00
|
|
|
) -> typing.Optional[typing.Any]:
|
2019-02-07 10:02:27 +00:00
|
|
|
values = self.get_all(setting)
|
2019-04-27 13:49:18 +00:00
|
|
|
return values[0] if values else default
|
2018-09-02 18:54:45 +00:00
|
|
|
|
2019-06-29 21:26:28 +00:00
|
|
|
def find(self, setting_prefix: str) -> typing.List[typing.Any]:
|
|
|
|
found = []
|
|
|
|
for key in self._get_keys():
|
2019-06-25 16:53:00 +00:00
|
|
|
if key.startswith(setting_prefix):
|
2019-06-29 21:26:28 +00:00
|
|
|
found.append(key)
|
|
|
|
return found
|
2019-06-25 16:53:00 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def purge_context(self, context: str):
|
2018-09-02 18:54:45 +00:00
|
|
|
if context in self._context_exports:
|
|
|
|
del self._context_exports[context]
|
2018-10-30 14:58:48 +00:00
|
|
|
|
|
|
|
class ExportsContext(object):
|
|
|
|
def __init__(self, parent: Exports, context: str):
|
|
|
|
self._parent = parent
|
|
|
|
self.context = context
|
|
|
|
|
|
|
|
def add(self, setting: str, value: typing.Any):
|
|
|
|
self._parent._context_add(self.context, setting, value)
|
|
|
|
def get_all(self, setting: str) -> typing.List[typing.Any]:
|
|
|
|
return self._parent.get_all(setting)
|
2020-03-08 14:14:00 +00:00
|
|
|
def get(self, setting: str, default: typing.Any=None
|
2019-02-07 13:36:18 +00:00
|
|
|
) -> typing.Optional[typing.Any]:
|
2020-03-08 14:14:00 +00:00
|
|
|
return self._parent.get(setting, default)
|
2019-06-29 21:26:28 +00:00
|
|
|
def find(self, setting_prefix: str) -> typing.List[typing.Any]:
|
|
|
|
return self._parent.find(setting_prefix)
|