- Add `String` and `Link` validators
- Fix `Series` validator
- Reformat core modules to use validators
pull/1/head
hikariatama 2022-05-19 19:26:03 +00:00
parent 64163b7218
commit 059d531d39
5 changed files with 86 additions and 12 deletions

View File

@ -76,9 +76,24 @@ class APIRatelimiterMod(loader.Module):
def __init__(self):
self.config = loader.ModuleConfig(
loader.ConfigValue("time_sample", 15, lambda: "Time sample DO NOT TOUCH"),
loader.ConfigValue("threshold", 100, lambda: "Threshold DO NOT TOUCH"),
loader.ConfigValue("local_floodwait", 30, lambda: "Local FW DO NOT TOUCH"),
loader.ConfigValue(
"time_sample",
15,
lambda: "Time sample DO NOT TOUCH",
validator=loader.validators.Integer(minimum=1),
),
loader.ConfigValue(
"threshold",
100,
lambda: "Threshold DO NOT TOUCH",
validator=loader.validators.Integer(minimum=10),
),
loader.ConfigValue(
"local_floodwait",
30,
lambda: "Local FW DO NOT TOUCH",
validator=loader.validators.Integer(minimum=10, maximum=3600),
),
)
async def client_ready(self, client, db):

View File

@ -69,9 +69,24 @@ class HelpMod(loader.Module):
def __init__(self):
self.config = loader.ModuleConfig(
loader.ConfigValue("core_emoji", "▪️", lambda: "Core module bullet"),
loader.ConfigValue("hikka_emoji", "🌘", lambda: "Hikka-only module bullet"),
loader.ConfigValue("plain_emoji", "▫️", lambda: "Plain module bullet"),
loader.ConfigValue(
"core_emoji",
"▪️",
lambda: "Core module bullet",
validator=loader.validators.String(length=1),
),
loader.ConfigValue(
"hikka_emoji",
"🌘",
lambda: "Hikka-only module bullet",
validator=loader.validators.String(length=1),
),
loader.ConfigValue(
"plain_emoji",
"▫️",
lambda: "Plain module bullet",
validator=loader.validators.String(length=1),
),
)
async def helphidecmd(self, message: Message):
@ -199,7 +214,9 @@ class HelpMod(loader.Module):
),
)
await utils.answer(message, f"{reply}\n\n{self.strings('not_exact') if not exact else ''}")
await utils.answer(
message, f"{reply}\n\n{self.strings('not_exact') if not exact else ''}"
)
return
count = 0

View File

@ -207,6 +207,7 @@ class LoaderMod(loader.Module):
"MODULES_REPO",
"https://mods.hikariatama.ru/",
lambda: self.strings("repo_config_doc"),
validator=loader.validators.Link(),
),
loader.ConfigValue(
"ADDITIONAL_REPOS",
@ -217,6 +218,7 @@ class LoaderMod(loader.Module):
"https://gitlab.com/CakesTwix/friendly-userbot-modules/-/raw/master/"
),
lambda: self.strings("add_repo_config_doc"),
validator=loader.validators.Series(separator="|"),
),
)
@ -297,9 +299,7 @@ class LoaderMod(loader.Module):
preset = self.get("chosen_preset", None)
if preset != "disable":
possible_mods = (
await self.get_repo_list(preset)
).values()
possible_mods = (await self.get_repo_list(preset)).values()
todo = dict(ChainMap(*possible_mods))
else:
todo = {}

View File

@ -1,5 +1,7 @@
import functools
from typing import Any, Optional, Union
from . import utils
import grapheme
class ValidationError(Exception):
@ -181,9 +183,49 @@ def Choice(possible_values: list, /) -> Validator:
def Series(separator: str = ",") -> Validator:
"""Just a placeholder to let user know about the format of input data for config value"""
return Validator(
lambda value: True,
lambda value: value,
{
"en": f"series of values, separated with «{separator}»",
"ru": f"списком значений, разделенных «{separator}»",
},
)
def _Link(value: Any, /) -> str:
if not utils.check_url(value):
raise ValidationError(f"Passed value ({value}) is not a valid URL")
return value
def Link() -> Validator:
"""Valid url must be specified"""
return Validator(
lambda value: _Link(value),
{
"en": "link",
"ru": "ссылкой",
},
)
def _String(value: Any, /, *, length: int) -> str:
if isinstance(length, int) and len(list(grapheme.graphemes(value))) != length:
raise ValidationError(f"Passed value ({value}) must be a length of {length}")
return value
def String(length: Optional[int] = None) -> Validator:
if length is not None:
doc = {
"en": f"string of length {length}",
"ru": f"строкой из {length} символа(-ов)",
}
else:
doc = {
"en": "string",
"ru": "строкой",
}
return Validator(functools.partial(_String, length=length), doc)

View File

@ -1,2 +1,2 @@
"""Represents current userbot version"""
__version__ = (1, 1, 23)
__version__ = (1, 1, 24)