Implemnt _options_factory for OptionsSetting to have dynamic options

This commit is contained in:
jesopo 2019-06-29 22:26:28 +01:00
parent b4c762eb4e
commit 19f716ab4c
3 changed files with 35 additions and 17 deletions

View file

@ -6,10 +6,6 @@ from src import ModuleManager, utils
URL_BITLYSHORTEN = "https://api-ssl.bitly.com/v3/shorten" URL_BITLYSHORTEN = "https://api-ssl.bitly.com/v3/shorten"
@utils.export("serverset", utils.Setting("url-shortener",
"Set URL shortener service", example="bitly"))
@utils.export("botset", utils.Setting("url-shortener",
"Set URL shortener service", example="bitly"))
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def on_load(self): def on_load(self):
self.exports.add("shorturl", self._shorturl) self.exports.add("shorturl", self._shorturl)
@ -17,6 +13,16 @@ class Module(ModuleManager.BaseModule):
self.exports.add("shorturl-s-bitly", self._bitly) self.exports.add("shorturl-s-bitly", self._bitly)
setting = utils.OptionsSetting("url-shortener",
"Set URL shortener service",
options_factory=self._shorturl_options_factory)
self.exports.add("serverset", setting)
self.exports.add("botset", setting)
def _shorturl_options_factory(self):
shorteners = self.exports.find("shorturl-s-")
return [s.replace("shorturl-s-", "", 1) for s in shorteners]
def _get_shortener(self, name): def _get_shortener(self, name):
return self.exports.get_one("shorturl-s-%s" % name, None) return self.exports.get_one("shorturl-s-%s" % name, None)
def _call_shortener(self, shortener_name, url): def _call_shortener(self, shortener_name, url):

View file

@ -26,8 +26,10 @@ class Exports(object):
self._context_exports[context][setting].append(value) self._context_exports[context][setting].append(value)
def _get_keys(self): def _get_keys(self):
return set(list(self._exports.keys()) keys = list(self._exports.keys())
+list(self._context_exports.keys())) for context in self._context_exports.keys():
keys += list(self._context_exports[context].keys())
return list(set(keys))
def get_all(self, setting: str) -> typing.List[typing.Any]: def get_all(self, setting: str) -> typing.List[typing.Any]:
return self._exports.get(setting, []) + sum([ return self._exports.get(setting, []) + sum([
@ -38,13 +40,12 @@ class Exports(object):
values = self.get_all(setting) values = self.get_all(setting)
return values[0] if values else default return values[0] if values else default
def find_one(self, setting_prefix: str, default: typing.Any=None def find(self, setting_prefix: str) -> typing.List[typing.Any]:
) -> typing.Optional[typing.Any]: found = []
keys = self._get_keys() for key in self._get_keys():
for key in keys:
if key.startswith(setting_prefix): if key.startswith(setting_prefix):
return key found.append(key)
return default return found
def purge_context(self, context: str): def purge_context(self, context: str):
if context in self._context_exports: if context in self._context_exports:
@ -62,3 +63,5 @@ class ExportsContext(object):
def get_one(self, setting: str, default: typing.Any=None def get_one(self, setting: str, default: typing.Any=None
) -> typing.Optional[typing.Any]: ) -> typing.Optional[typing.Any]:
return self._parent.get_one(setting, default) return self._parent.get_one(setting, default)
def find(self, setting_prefix: str) -> typing.List[typing.Any]:
return self._parent.find(setting_prefix)

View file

@ -230,7 +230,7 @@ def is_main_thread() -> bool:
return threading.current_thread() is threading.main_thread() return threading.current_thread() is threading.main_thread()
class Setting(object): class Setting(object):
example: str = None example: typing.Optional[str] = None
def __init__(self, name: str, help: str=None, example: str=None): def __init__(self, name: str, help: str=None, example: str=None):
self.name = name self.name = name
self.help = help self.help = help
@ -269,17 +269,26 @@ class IntSetting(Setting):
class OptionsSetting(Setting): class OptionsSetting(Setting):
def __init__(self, name: str, options: typing.List[str], help: str=None, def __init__(self, name: str, options: typing.List[str], help: str=None,
example: str=None): example: str=None,
options_factory: typing.Callable[[], typing.List[str]]=None):
self._options = options self._options = options
self._options_factory = options_factory
Setting.__init__(self, name, help, example) Setting.__init__(self, name, help, example)
def _get_options(self):
if not self._options_factory == None:
return self._options_factory()
else:
return self._options
def parse(self, value: str) -> typing.Any: def parse(self, value: str) -> typing.Any:
value_lower = value.lower() value_lower = value.lower()
for option in self._options: for option in self._get_options():
if option.lower() == value_lower: if option.lower() == value_lower:
return option return option
return None return None
def _format_example(self): def _format_example(self):
options = ["'%s'" % option for option in self._options] options = self._get_options()
return "Options: %s" % ", ".join(options) options_str = ["'%s'" % option for option in options]
return "Options: %s" % ", ".join(options_str)