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,21 +31,20 @@ def _format(years, dt):
else:
return _format_noyear(dt)
class BirthdaySetting(utils.Setting):
def parse(self, value: str) -> typing.Any:
parsed = _parse(value)
if parsed:
years, parsed = parsed
return _format(years, parsed)
return None
def _parse_setting(value):
parsed = _parse(value)
if parsed:
years, parsed = parsed
return _format(years, parsed)
return None
def _apostrophe(nickname):
if nickname[-1].lower() == "s":
return "%s'" % nickname
return "%s's" % nickname
@utils.export("set", BirthdaySetting("birthday", "Set your birthday",
example="1995-09-15"))
@utils.export("set", utils.FunctionSetting(_parse_setting, "birthday",
"Set your birthday", example="1995-09-15"))
class Module(ModuleManager.BaseModule):
@utils.hook("received.command.birthday")
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()]
def _command_method_validate(s):
if s.upper() in COMMAND_METHODS:
return s.upper()
class BadContextException(Exception):
def __init__(self, required_context):
self.required_context = required_context
Exception.__init__(self)
class CommandMethodSetting(utils.Setting):
example = "NOTICE"
def parse(self, value: str) -> typing.Any:
upper = value.upper()
if upper in COMMAND_METHODS:
return upper
return None
SETTING_COMMANDMETHOD = utils.OptionsSetting(COMMAND_METHODS, COMMAND_METHOD,
"Set the method used to respond to commands")
@utils.export("channelset", utils.Setting("command-prefix",
"Set the command prefix used in this channel", example="!"))
@utils.export("serverset", utils.Setting("command-prefix",
"Set the command prefix used on this server", example="!"))
@utils.export("serverset", CommandMethodSetting("command-method",
"Set the method used to respond to commands"))
@utils.export("channelset", CommandMethodSetting("command-method",
"Set the method used to respond to commands"))
@utils.export("botset", CommandMethodSetting("command-method",
"Set the method used to respond to commands"))
@utils.export("serverset", SETTING_COMMANDMETHOD)
@utils.export("channelset", SETTING_COMMANDMETHOD)
@utils.export("botset", SETTING_COMMANDMETHOD)
@utils.export("channelset", utils.BoolSetting("hide-prefix",
"Disable/enable hiding prefix in command reponses"))
@utils.export("channelset", utils.BoolSetting("commands",
@ -94,7 +82,7 @@ class Module(ModuleManager.BaseModule):
def _command_method(self, target, server):
return target.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):
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_IP = re.compile("%s|%s" % (REGEX_IPv4, REGEX_IPv6), re.I)
class DnsSetting(utils.Setting):
def parse(self, value: str) -> typing.Any:
if utils.is_ip(value):
return value
return None
def _parse(value):
if utils.is_ip(value):
return value
return None
@utils.export("botset", utils.BoolSetting("configurable-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"))
@utils.export("channelset", DnsSetting("dns-nameserver",
@utils.export("channelset", utils.FunctionSetting(_parse, "dns-nameserver",
"Set DNS nameserver", example="8.8.8.8"))
class Module(ModuleManager.BaseModule):
@utils.hook("received.command.dns", min_args=1)

View file

@ -13,12 +13,11 @@ USERPASS_MECHANISMS = [
"PLAIN"
]
class SaslSetting(utils.Setting):
def parse(self, value: str) -> typing.Any:
mechanism, _, arguments = value.partition(" ")
return {"mechanism": mechanism.upper(), "args": arguments}
def _parse(value):
mechanism, _, arguments = value.partition(" ")
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",
example="PLAIN BitBot:hunter2"))
@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"
class LocationSetting(utils.Setting):
_func = None
def parse(self, value: str) -> typing.Any:
return self._func(value)
class Module(ModuleManager.BaseModule):
def on_load(self):
setting = LocationSetting("location", "Set your location",
example="London, GB")
setting._func = self._get_location
setting = utils.FunctionSetting(self._get_location, "location",
"Set your location", example="London, GB")
self.exports.add("set", setting)
self.exports.add("get-location", self._get_location)

View file

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