From 4bfb4c3200506da63eb0451d42a300ee400a176c Mon Sep 17 00:00:00 2001 From: jesopo Date: Fri, 30 Aug 2019 14:40:54 +0100 Subject: [PATCH] Add IntRangeSetting, reorder OptionsSetting arg order --- modules/channel_op.py | 4 ++-- modules/ducks.py | 4 ++-- modules/permissions/__init__.py | 5 ++--- modules/shorturl.py | 2 +- src/utils/__init__.py | 19 ++++++++++++++++++- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/modules/channel_op.py b/modules/channel_op.py index b4161f6c..cb4f32e0 100644 --- a/modules/channel_op.py +++ b/modules/channel_op.py @@ -13,8 +13,8 @@ KICK_REASON_SETTING = utils.Setting("default-kick-reason", @utils.export("channelset", utils.Setting("ban-format", "Set ban format ($n = nick, $u = username, $h = hostname)", example="*!$u@$h")) -@utils.export("serverset", utils.OptionsSetting("mute-method", - ["qmode", "insp", "unreal", "none"], +@utils.export("serverset", utils.OptionsSetting( + ["qmode", "insp", "unreal", "none"], "mute-method", "Set this server's method of muting users")) @utils.export("botset", KICK_REASON_SETTING) @utils.export("serverset", KICK_REASON_SETTING) diff --git a/modules/ducks.py b/modules/ducks.py index d3708d08..8fb62ebf 100644 --- a/modules/ducks.py +++ b/modules/ducks.py @@ -11,8 +11,8 @@ DEFAULT_MIN_MESSAGES = 100 @utils.export("channelset", utils.BoolSetting("ducks-enabled", "Whether or not to spawn ducks")) -@utils.export("channelset", utils.IntSetting("ducks-min-messages", - "Minimum messages between ducks spawning", example="50")) +@utils.export("channelset", utils.IntRangeSetting(50, 200, "ducks-min-messages", + "Minimum messages between ducks spawning")) @utils.export("channelset", utils.BoolSetting("ducks-kick", "Whether or not to kick someone talking to non-existent ducks")) class Module(ModuleManager.BaseModule): diff --git a/modules/permissions/__init__.py b/modules/permissions/__init__.py index f1949b9a..ce259319 100644 --- a/modules/permissions/__init__.py +++ b/modules/permissions/__init__.py @@ -9,9 +9,8 @@ REQUIRES_IDENTIFY = "You need to be identified to use that command" 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")) +@utils.export("serverset", utils.OptionsSetting(["internal", "ircv3-account"], + "identity-mechanism", "Set the identity mechanism for this server")) class Module(ModuleManager.BaseModule): @utils.hook("new.user") def new_user(self, event): diff --git a/modules/shorturl.py b/modules/shorturl.py index 77a9a352..e18cdcfd 100644 --- a/modules/shorturl.py +++ b/modules/shorturl.py @@ -13,7 +13,7 @@ class Module(ModuleManager.BaseModule): self.exports.add("shorturl-s-bitly", self._bitly) - setting = utils.OptionsSetting("url-shortener", [], + setting = utils.OptionsSetting([], "url-shortener", "Set URL shortener service", options_factory=self._shorturl_options_factory) self.exports.add("channelset", setting) diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 69d74fa2..2202a3de 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -300,8 +300,25 @@ class IntSetting(Setting): return int(stripped) return None +class IntRangeSetting(IntSetting): + example = None + def __init__(self, n_min: int, n_max: int, name: str, help: str=None, + example: str=None): + self._n_min = n_min + self._n_max = n_max + Setting.__init__(self, name, help, example) + + def parse(self, value: str) -> typing.Any: + out = IntSetting.parse(self, value) + if not out == None and self._n_min <= out <= self._n_max: + return out + return None + + def _format_example(self): + return "Must be between %d and %d" % (self._n_min, self._n_max) + class OptionsSetting(Setting): - def __init__(self, name: str, options: typing.List[str], help: str=None, + def __init__(self, options: typing.List[str], name: str, help: str=None, example: str=None, options_factory: typing.Callable[[], typing.List[str]]=None): self._options = options