Implemnt _options_factory for OptionsSetting to have dynamic options
This commit is contained in:
parent
b4c762eb4e
commit
19f716ab4c
3 changed files with 35 additions and 17 deletions
|
@ -6,10 +6,6 @@ from src import ModuleManager, utils
|
|||
|
||||
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):
|
||||
def on_load(self):
|
||||
self.exports.add("shorturl", self._shorturl)
|
||||
|
@ -17,6 +13,16 @@ class Module(ModuleManager.BaseModule):
|
|||
|
||||
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):
|
||||
return self.exports.get_one("shorturl-s-%s" % name, None)
|
||||
def _call_shortener(self, shortener_name, url):
|
||||
|
|
|
@ -26,8 +26,10 @@ class Exports(object):
|
|||
self._context_exports[context][setting].append(value)
|
||||
|
||||
def _get_keys(self):
|
||||
return set(list(self._exports.keys())
|
||||
+list(self._context_exports.keys()))
|
||||
keys = list(self._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]:
|
||||
return self._exports.get(setting, []) + sum([
|
||||
|
@ -38,13 +40,12 @@ class Exports(object):
|
|||
values = self.get_all(setting)
|
||||
return values[0] if values else default
|
||||
|
||||
def find_one(self, setting_prefix: str, default: typing.Any=None
|
||||
) -> typing.Optional[typing.Any]:
|
||||
keys = self._get_keys()
|
||||
for key in keys:
|
||||
def find(self, setting_prefix: str) -> typing.List[typing.Any]:
|
||||
found = []
|
||||
for key in self._get_keys():
|
||||
if key.startswith(setting_prefix):
|
||||
return key
|
||||
return default
|
||||
found.append(key)
|
||||
return found
|
||||
|
||||
def purge_context(self, context: str):
|
||||
if context in self._context_exports:
|
||||
|
@ -62,3 +63,5 @@ class ExportsContext(object):
|
|||
def get_one(self, setting: str, default: typing.Any=None
|
||||
) -> typing.Optional[typing.Any]:
|
||||
return self._parent.get_one(setting, default)
|
||||
def find(self, setting_prefix: str) -> typing.List[typing.Any]:
|
||||
return self._parent.find(setting_prefix)
|
||||
|
|
|
@ -230,7 +230,7 @@ def is_main_thread() -> bool:
|
|||
return threading.current_thread() is threading.main_thread()
|
||||
|
||||
class Setting(object):
|
||||
example: str = None
|
||||
example: typing.Optional[str] = None
|
||||
def __init__(self, name: str, help: str=None, example: str=None):
|
||||
self.name = name
|
||||
self.help = help
|
||||
|
@ -269,17 +269,26 @@ class IntSetting(Setting):
|
|||
|
||||
class OptionsSetting(Setting):
|
||||
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_factory = options_factory
|
||||
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:
|
||||
value_lower = value.lower()
|
||||
for option in self._options:
|
||||
for option in self._get_options():
|
||||
if option.lower() == value_lower:
|
||||
return option
|
||||
return None
|
||||
|
||||
def _format_example(self):
|
||||
options = ["'%s'" % option for option in self._options]
|
||||
return "Options: %s" % ", ".join(options)
|
||||
options = self._get_options()
|
||||
options_str = ["'%s'" % option for option in options]
|
||||
return "Options: %s" % ", ".join(options_str)
|
||||
|
|
Loading…
Reference in a new issue