From b4c762eb4ea1a83556dac95d6b248e6bcf220843 Mon Sep 17 00:00:00 2001 From: jesopo Date: Sat, 29 Jun 2019 21:33:26 +0100 Subject: [PATCH] Automatically format `example` text for OptionsSetting (showing options) --- modules/channel_op.py | 3 +-- modules/config.py | 6 +++--- modules/permissions/__init__.py | 4 ++-- src/utils/__init__.py | 16 +++++++++++++++- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/modules/channel_op.py b/modules/channel_op.py index 749e7764..4b2dbe15 100644 --- a/modules/channel_op.py +++ b/modules/channel_op.py @@ -20,8 +20,7 @@ class InvalidTimeoutException(Exception): "Set ban format ($n = nick, $u = username, $h = hostname)", example="*!$u@$h")) @utils.export("serverset", utils.OptionsSetting("mute-method", - ["qmode", "insp", "unreal"], "Set this server's method of muting users", - example="qmode")) + ["qmode", "insp", "unreal"], "Set this server's method of muting users")) class Module(ModuleManager.BaseModule): _name = "ChanOp" diff --git a/modules/config.py b/modules/config.py index 8c070e0d..306f2366 100644 --- a/modules/config.py +++ b/modules/config.py @@ -84,7 +84,7 @@ class Module(ModuleManager.BaseModule): if setting_info: value = target.get_setting(require_setting, None) if value == None: - example = setting_info.exaple or "" + example = setting_info.get_example() or "" return "Please set %s, e.g.: %sconfig %s %s %s" % ( require_setting, event["command_prefix"], context[0], require_setting, example) @@ -180,9 +180,9 @@ class Module(ModuleManager.BaseModule): try: result = self._config(export_settings, target, setting, value) except ConfigInvalidValue: - example = export_settings[setting].example + example = export_settings[setting].get_example() if not example == None: - raise utils.EventError("Invalid value. Example: %s" % + raise utils.EventError("Invalid value. %s" % example) else: raise utils.EventError("Invalid value") diff --git a/modules/permissions/__init__.py b/modules/permissions/__init__.py index aae4d500..f1949b9a 100644 --- a/modules/permissions/__init__.py +++ b/modules/permissions/__init__.py @@ -10,8 +10,8 @@ REQUIRES_IDENTIFY_INTERNAL = ("You need to be identified to use that command " "(/msg %s register | /msg %s identify)") @utils.export("serverset", utils.OptionsSetting("identity-mechanism", - ["internal", "ircv3-account"], "Set the identity mechanism for this server", - example="ircv3-account")) + ["internal", "ircv3-account"], + "Set the identity mechanism for this server")) class Module(ModuleManager.BaseModule): @utils.hook("new.user") def new_user(self, event): diff --git a/src/utils/__init__.py b/src/utils/__init__.py index cc3ca689..887e8ab2 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -230,13 +230,23 @@ def is_main_thread() -> bool: return threading.current_thread() is threading.main_thread() class Setting(object): + example: str = None def __init__(self, name: str, help: str=None, example: str=None): self.name = name self.help = help - self.example = example + if not example == None: + self.example = example def parse(self, value: str) -> typing.Any: return value + def get_example(self): + if not self.example == None: + return "Example: %s" % self.example + else: + return self._format_example() + def _format_example(self): + return None + SETTING_TRUE = ["true", "yes", "on", "y"] SETTING_FALSE = ["false", "no", "off", "n"] class BoolSetting(Setting): @@ -269,3 +279,7 @@ class OptionsSetting(Setting): 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)