aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Numerlor <[email protected]>2020-07-14 03:40:52 +0200
committerGravatar Numerlor <[email protected]>2020-07-14 03:48:29 +0200
commit40d831fb7b5ca7192fb1bdca8be9157f206eb2bc (patch)
tree7933fea16039a47ab307a61c838738ebeb3cfe2a
parentMake the symbol parameter optional. (diff)
Change package name converter to only accept _a-z.
Package names are now directly used for stats, where the lowercase a-z characters and _ are used.
-rw-r--r--bot/cogs/doc.py6
-rw-r--r--bot/converters.py22
2 files changed, 13 insertions, 15 deletions
diff --git a/bot/cogs/doc.py b/bot/cogs/doc.py
index 66c4b4ea8..09bddb02c 100644
--- a/bot/cogs/doc.py
+++ b/bot/cogs/doc.py
@@ -21,7 +21,7 @@ from urllib3.exceptions import ProtocolError
from bot.bot import Bot
from bot.constants import MODERATION_ROLES, RedirectOutput
-from bot.converters import ValidPythonIdentifier, ValidURL
+from bot.converters import PackageName, ValidURL
from bot.decorators import with_role
from bot.pagination import LinePaginator
from bot.utils.messages import wait_for_deletion
@@ -492,7 +492,7 @@ class Doc(commands.Cog):
@docs_group.command(name='setdoc', aliases=('s',))
@with_role(*MODERATION_ROLES)
async def set_command(
- self, ctx: commands.Context, package_name: ValidPythonIdentifier,
+ self, ctx: commands.Context, package_name: PackageName,
base_url: ValidURL, inventory_url: InventoryURL
) -> None:
"""
@@ -525,7 +525,7 @@ class Doc(commands.Cog):
@docs_group.command(name='deletedoc', aliases=('removedoc', 'rm', 'd'))
@with_role(*MODERATION_ROLES)
- async def delete_command(self, ctx: commands.Context, package_name: ValidPythonIdentifier) -> None:
+ async def delete_command(self, ctx: commands.Context, package_name: PackageName) -> None:
"""
Removes the specified package from the database.
diff --git a/bot/converters.py b/bot/converters.py
index 72c46fdf0..fac94e9d0 100644
--- a/bot/converters.py
+++ b/bot/converters.py
@@ -34,22 +34,20 @@ def allowed_strings(*values, preserve_case: bool = False) -> t.Callable[[str], s
return converter
-class ValidPythonIdentifier(Converter):
+class PackageName(Converter):
"""
- A converter that checks whether the given string is a valid Python identifier.
+ A converter that checks whether the given string is a valid package name.
- This is used to have package names that correspond to how you would use the package in your
- code, e.g. `import package`.
-
- Raises `BadArgument` if the argument is not a valid Python identifier, and simply passes through
- the given argument otherwise.
+ Package names are used for stats and are restricted to the a-z and _ characters.
"""
- @staticmethod
- async def convert(ctx: Context, argument: str) -> str:
- """Checks whether the given string is a valid Python identifier."""
- if not argument.isidentifier():
- raise BadArgument(f"`{argument}` is not a valid Python identifier")
+ PACKAGE_NAME_RE = re.compile(r"[^a-z_]")
+
+ @classmethod
+ async def convert(cls, ctx: Context, argument: str) -> str:
+ """Checks whether the given string is a valid package name."""
+ if cls.PACKAGE_NAME_RE.search(argument):
+ raise BadArgument("The provided package name is not valid, please only use the _ and a-z characters.")
return argument