Switch more settings to FunctionSettings

This commit is contained in:
jesopo 2019-09-04 14:27:10 +01:00
parent 43fab6777c
commit 203216d94d
6 changed files with 31 additions and 53 deletions

View file

@ -31,8 +31,7 @@ def _format(years, dt):
else: else:
return _format_noyear(dt) return _format_noyear(dt)
class BirthdaySetting(utils.Setting): def _parse_setting(value):
def parse(self, value: str) -> typing.Any:
parsed = _parse(value) parsed = _parse(value)
if parsed: if parsed:
years, parsed = parsed years, parsed = parsed
@ -44,8 +43,8 @@ def _apostrophe(nickname):
return "%s'" % nickname return "%s'" % nickname
return "%s's" % nickname return "%s's" % nickname
@utils.export("set", BirthdaySetting("birthday", "Set your birthday", @utils.export("set", utils.FunctionSetting(_parse_setting, "birthday",
example="1995-09-15")) "Set your birthday", example="1995-09-15"))
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
@utils.hook("received.command.birthday") @utils.hook("received.command.birthday")
def birthday(self, event): def birthday(self, event):

View file

@ -16,33 +16,21 @@ MSGID_TAG = utils.irc.MessageTag("msgid", "draft/msgid")
NON_ALPHANUMERIC = [char for char in string.printable if not char.isalnum()] NON_ALPHANUMERIC = [char for char in string.printable if not char.isalnum()]
def _command_method_validate(s):
if s.upper() in COMMAND_METHODS:
return s.upper()
class BadContextException(Exception): class BadContextException(Exception):
def __init__(self, required_context): def __init__(self, required_context):
self.required_context = required_context self.required_context = required_context
Exception.__init__(self) Exception.__init__(self)
class CommandMethodSetting(utils.Setting): SETTING_COMMANDMETHOD = utils.OptionsSetting(COMMAND_METHODS, COMMAND_METHOD,
example = "NOTICE" "Set the method used to respond to commands")
def parse(self, value: str) -> typing.Any:
upper = value.upper()
if upper in COMMAND_METHODS:
return upper
return None
@utils.export("channelset", utils.Setting("command-prefix", @utils.export("channelset", utils.Setting("command-prefix",
"Set the command prefix used in this channel", example="!")) "Set the command prefix used in this channel", example="!"))
@utils.export("serverset", utils.Setting("command-prefix", @utils.export("serverset", utils.Setting("command-prefix",
"Set the command prefix used on this server", example="!")) "Set the command prefix used on this server", example="!"))
@utils.export("serverset", CommandMethodSetting("command-method", @utils.export("serverset", SETTING_COMMANDMETHOD)
"Set the method used to respond to commands")) @utils.export("channelset", SETTING_COMMANDMETHOD)
@utils.export("channelset", CommandMethodSetting("command-method", @utils.export("botset", SETTING_COMMANDMETHOD)
"Set the method used to respond to commands"))
@utils.export("botset", CommandMethodSetting("command-method",
"Set the method used to respond to commands"))
@utils.export("channelset", utils.BoolSetting("hide-prefix", @utils.export("channelset", utils.BoolSetting("hide-prefix",
"Disable/enable hiding prefix in command reponses")) "Disable/enable hiding prefix in command reponses"))
@utils.export("channelset", utils.BoolSetting("commands", @utils.export("channelset", utils.BoolSetting("commands",
@ -94,7 +82,7 @@ class Module(ModuleManager.BaseModule):
def _command_method(self, target, server): def _command_method(self, target, server):
return target.get_setting(COMMAND_METHOD, return target.get_setting(COMMAND_METHOD,
server.get_setting(COMMAND_METHOD, server.get_setting(COMMAND_METHOD,
self.bot.get_setting(COMMAND_METHOD, "PRIVMSG"))).upper() self.bot.get_setting(COMMAND_METHOD, "PRIVMSG")))
def _find_command_hook(self, server, command, is_channel, args_split): def _find_command_hook(self, server, command, is_channel, args_split):
if not self.has_command(command): if not self.has_command(command):

View file

@ -9,17 +9,16 @@ REGEX_IPv6 = r"(?:(?:[a-f0-9]{1,4}:){2,}|[a-f0-9:]*::)[a-f0-9:]*"
REGEX_IPv4 = r"(?:\d{1,3}\.){3}\d{1,3}" REGEX_IPv4 = r"(?:\d{1,3}\.){3}\d{1,3}"
REGEX_IP = re.compile("%s|%s" % (REGEX_IPv4, REGEX_IPv6), re.I) REGEX_IP = re.compile("%s|%s" % (REGEX_IPv4, REGEX_IPv6), re.I)
class DnsSetting(utils.Setting): def _parse(value):
def parse(self, value: str) -> typing.Any:
if utils.is_ip(value): if utils.is_ip(value):
return value return value
return None return None
@utils.export("botset", utils.BoolSetting("configurable-nameservers", @utils.export("botset", utils.BoolSetting("configurable-nameservers",
"Whether or not users can configure their own nameservers")) "Whether or not users can configure their own nameservers"))
@utils.export("serverset", DnsSetting("dns-nameserver", @utils.export("serverset", utils.FunctionSetting(_parse, "dns-nameserver",
"Set DNS nameserver", example="8.8.8.8")) "Set DNS nameserver", example="8.8.8.8"))
@utils.export("channelset", DnsSetting("dns-nameserver", @utils.export("channelset", utils.FunctionSetting(_parse, "dns-nameserver",
"Set DNS nameserver", example="8.8.8.8")) "Set DNS nameserver", example="8.8.8.8"))
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
@utils.hook("received.command.dns", min_args=1) @utils.hook("received.command.dns", min_args=1)

View file

@ -13,12 +13,11 @@ USERPASS_MECHANISMS = [
"PLAIN" "PLAIN"
] ]
class SaslSetting(utils.Setting): def _parse(value):
def parse(self, value: str) -> typing.Any:
mechanism, _, arguments = value.partition(" ") mechanism, _, arguments = value.partition(" ")
return {"mechanism": mechanism.upper(), "args": arguments} return {"mechanism": mechanism.upper(), "args": arguments}
@utils.export("serverset", SaslSetting("sasl", @utils.export("serverset", utils.FunctionSetting(_parse, "sasl",
"Set the sasl username/password for this server", "Set the sasl username/password for this server",
example="PLAIN BitBot:hunter2")) example="PLAIN BitBot:hunter2"))
@utils.export("serverset", utils.BoolSetting("sasl-hard-fail", @utils.export("serverset", utils.BoolSetting("sasl-hard-fail",

View file

@ -6,16 +6,10 @@ from src import ModuleManager, utils
URL_OPENCAGE = "https://api.opencagedata.com/geocode/v1/json" URL_OPENCAGE = "https://api.opencagedata.com/geocode/v1/json"
class LocationSetting(utils.Setting):
_func = None
def parse(self, value: str) -> typing.Any:
return self._func(value)
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def on_load(self): def on_load(self):
setting = LocationSetting("location", "Set your location", setting = utils.FunctionSetting(self._get_location, "location",
example="London, GB") "Set your location", example="London, GB")
setting._func = self._get_location
self.exports.add("set", setting) self.exports.add("set", setting)
self.exports.add("get-location", self._get_location) self.exports.add("get-location", self._get_location)

View file

@ -8,13 +8,12 @@ TYPES = {
"http": socks.HTTP "http": socks.HTTP
} }
class ProxySetting(utils.Setting): def _parse(value):
def parse(self, value: str) -> typing.Any:
parsed = urllib.parse.urlparse(value) parsed = urllib.parse.urlparse(value)
if parsed.scheme in TYPES and parsed.hostname: if parsed.scheme in TYPES and parsed.hostname:
return value return value
@utils.export("serverset", ProxySetting("proxy", @utils.export("serverset", utils.FunctionSetting(_parse, "proxy",
"Proxy configuration for the current server", "Proxy configuration for the current server",
example="socks5://localhost:9050")) example="socks5://localhost:9050"))
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):