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)
|
|
|
|
|
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()], [])
|
2019-02-07 13:36:18 +00:00
|
|
|
def get_one(self, setting: str, default: typing.Any=None
|
|
|
|
) -> typing.Optional[typing.Any]:
|
2019-02-07 10:02:27 +00:00
|
|
|
values = self.get_all(setting)
|
|
|
|
return values[0] if values else None
|
2018-09-02 18:54:45 +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)
|
2019-02-07 13:36:18 +00:00
|
|
|
def get_one(self, setting: str, default: typing.Any=None
|
|
|
|
) -> typing.Optional[typing.Any]:
|
|
|
|
return self._parent.get_one(setting, default)
|