Skip to content

Commit

Permalink
Lint with black and check lint on github workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Blue committed Oct 29, 2023
1 parent 6263f4a commit f318650
Show file tree
Hide file tree
Showing 25 changed files with 533 additions and 372 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pytest-asyncio pytest-mock mock pytest flake8 hypothesis
python -m pip install pytest-asyncio pytest-mock mock pytest flake8 hypothesis black
python -m pip install .
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
- name: Lint with Black
uses: psf/black@stable
with:
options: "--check --verbose"
src: "."
- name: Test with pytest
run: |
pytest
14 changes: 7 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@


setup(
name="sql_helper",
version="0.1.0",
description="A helper for PostgreSQL ",
author='Blue',
url="https://nqn.blue/",
packages=find_packages(),
install_requires=["aiopg", "discord.py", "python-dateutil"]
name="sql_helper",
version="0.1.0",
description="A helper for PostgreSQL ",
author="Blue",
url="https://nqn.blue/",
packages=find_packages(),
install_requires=["aiopg", "discord.py", "python-dateutil"],
)
9 changes: 7 additions & 2 deletions sql_helper/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@


class _PostgresConnection:
def __init__(self, pool, get_guild: Callable[[int], Optional[Guild]], get_emoji: Callable[[SQLEmoji], Optional[Emoji]], profiler=None):
def __init__(
self,
pool,
get_guild: Callable[[int], Optional[Guild]],
get_emoji: Callable[[SQLEmoji], Optional[Emoji]],
profiler=None,
):
self.pool = pool
self.pool_acq = None
self.conn = None
Expand All @@ -31,4 +37,3 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
self.cur_acq = None
self.cur = None
return rtn

14 changes: 8 additions & 6 deletions sql_helper/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ class PostgresConnection(

class SQLConnection:
def __init__(
self,
pool,
get_guild: Optional[Callable[[int], Optional[Guild]]] = None,
get_emoji: Optional[Callable[[SQLEmoji], Optional[Emoji]]] = None,
profiler=None
self,
pool,
get_guild: Optional[Callable[[int], Optional[Guild]]] = None,
get_emoji: Optional[Callable[[SQLEmoji], Optional[Emoji]]] = None,
profiler=None,
):
self.pool = pool
self._get_guild = get_guild or (lambda id: None)
self._get_emoji = get_emoji or (lambda emoji: None)
self.profiler = profiler

def __call__(self) -> PostgresConnection:
return PostgresConnection(self.pool, self._get_guild, self._get_emoji, self.profiler)
return PostgresConnection(
self.pool, self._get_guild, self._get_emoji, self.profiler
)
14 changes: 13 additions & 1 deletion sql_helper/emoji.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
from collections import namedtuple


SQLEmoji = namedtuple("SQLEmoji", ["emote_id", "emote_hash", "usable", "animated", "emote_sha", "guild_id", "name", "has_roles"])
SQLEmoji = namedtuple(
"SQLEmoji",
[
"emote_id",
"emote_hash",
"usable",
"animated",
"emote_sha",
"guild_id",
"name",
"has_roles",
],
)
EmojiCounts = namedtuple("EmojiCounts", ["static", "animated"])
14 changes: 7 additions & 7 deletions sql_helper/guild_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class SettingsFlags(Flag):


DEFAULTS = (
SettingsFlags.stickers |
SettingsFlags.nitro |
SettingsFlags.replies |
SettingsFlags.pings |
SettingsFlags.user_content |
SettingsFlags.dashboard_posting |
SettingsFlags.phish_detection
SettingsFlags.stickers
| SettingsFlags.nitro
| SettingsFlags.replies
| SettingsFlags.pings
| SettingsFlags.user_content
| SettingsFlags.dashboard_posting
| SettingsFlags.phish_detection
)


Expand Down
10 changes: 4 additions & 6 deletions sql_helper/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

def sql_wrapper(namespace: str):
sql_request = Histogram(
"sql_request_total",
"SQL Requests",
["query"],
namespace=namespace
"sql_request_total", "SQL Requests", ["query"], namespace=namespace
)

def middle(execute):
Expand All @@ -26,9 +23,10 @@ async def execute_wrapper(query, *args, **kwargs):
data={
"query": query,
"params": kwargs.get("parameters"),
"total_time": total_time
}
"total_time": total_time,
},
)

return execute_wrapper

return middle
2 changes: 1 addition & 1 deletion sql_helper/mixins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
from .emoji_hashes import EmojiHashesMixin
from .aliases import AliasesMixin
from .blocked_emojis import BlockedEmojisMixin
from .command_messages import CommandMessagesMixin
from .command_messages import CommandMessagesMixin
46 changes: 26 additions & 20 deletions sql_helper/mixins/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,40 @@
class AliasesMixin(_PostgresConnection):
async def get_user_aliases(self, user_id: int) -> List[PartialEmoji]:
await self.cur.execute(
"SELECT animated, \"name\", emote_id FROM aliases WHERE user_id=%(user_id)s",
parameters={"user_id": user_id}
'SELECT animated, "name", emote_id FROM aliases WHERE user_id=%(user_id)s',
parameters={"user_id": user_id},
)
return await _get_emotes(self.cur)

async def get_user_aliases_after(self, user_id: int, name: str, limit: int) -> List[PartialEmoji]:
async def get_user_aliases_after(
self, user_id: int, name: str, limit: int
) -> List[PartialEmoji]:
await self.cur.execute(
"SELECT animated, \"name\", emote_id FROM aliases WHERE user_id=%(user_id)s and name>%(name)s order by name limit %(limit)s",
parameters={"user_id": user_id, "name": name, "limit": limit}
'SELECT animated, "name", emote_id FROM aliases WHERE user_id=%(user_id)s and name>%(name)s order by name limit %(limit)s',
parameters={"user_id": user_id, "name": name, "limit": limit},
)
return await _get_emotes(self.cur)

async def get_user_alias_name(self, user_id: int, name: str) -> Optional[PartialEmoji]:
async def get_user_alias_name(
self, user_id: int, name: str
) -> Optional[PartialEmoji]:
await self.cur.execute(
"SELECT animated, \"name\", emote_id FROM aliases WHERE user_id=%(user_id)s and lower(\"name\")=%(name)s",
parameters={"user_id": user_id, "name": name.lower()}
'SELECT animated, "name", emote_id FROM aliases WHERE user_id=%(user_id)s and lower("name")=%(name)s',
parameters={"user_id": user_id, "name": name.lower()},
)
emotes = await _get_emotes(self.cur)
if emotes:
return next((emote for emote in emotes if emote.name == name), emotes[0])

async def get_user_alias_name_with_guild(self, user_id: int, name: str, guild_id: int) -> Optional[PartialEmoji]:
async def get_user_alias_name_with_guild(
self, user_id: int, name: str, guild_id: int
) -> Optional[PartialEmoji]:
# Get an alias, but exclude where the current guild has has_roles set on the alias you're trying to use
# Left join here, as if it's not in the main database we still want to be able to use it as it's obviously not blocked
await self.cur.execute(
"SELECT aliases.animated, aliases.\"name\", aliases.emote_id from aliases left join emote_ids on aliases.emote_id=emote_ids.emote_id where user_id=%(user_id)s and lower(aliases.\"name\")=%(name)s and "
'SELECT aliases.animated, aliases."name", aliases.emote_id from aliases left join emote_ids on aliases.emote_id=emote_ids.emote_id where user_id=%(user_id)s and lower(aliases."name")=%(name)s and '
"emote_ids.emote_hash not in (select emote_hash from emote_ids where guild_id=%(guild_id)s and has_roles=true)",
parameters={"user_id": user_id, "name": name.lower(), "guild_id": guild_id}
parameters={"user_id": user_id, "name": name.lower(), "guild_id": guild_id},
)
emotes = await _get_emotes(self.cur)
if emotes:
Expand All @@ -44,15 +50,15 @@ async def get_user_alias_name_with_guild(self, user_id: int, name: str, guild_id
async def count_user_aliases(self, user_id: int) -> int:
await self.cur.execute(
"select count(*) from aliases where user_id=%(user_id)s",
parameters={"user_id": user_id}
parameters={"user_id": user_id},
)
results = await self.cur.fetchall()
return results[0][0]

async def user_has_aliases(self, user_id: int) -> bool:
await self.cur.execute(
"select 1 from aliases where user_id=%(user_id)s limit 1",
parameters={"user_id": user_id}
parameters={"user_id": user_id},
)
return bool(await self.cur.fetchall())

Expand All @@ -69,37 +75,37 @@ async def set_user_aliases(self, user_id: int, aliases: List[PartialEmoji]):

await self.cur.execute(
"INSERT INTO aliases (user_id, emote_id, animated, name) VALUES (%(user_id)s, unnest(%(emote_ids)s), unnest(%(animateds)s), unnest(%(names)s)) "
"ON CONFLICT ON CONSTRAINT aliases_pk DO UPDATE SET emote_id = excluded.emote_id, animated = excluded.animated, \"name\" = excluded.name",
'ON CONFLICT ON CONSTRAINT aliases_pk DO UPDATE SET emote_id = excluded.emote_id, animated = excluded.animated, "name" = excluded.name',
parameters={
"user_id": user_id,
"names": names,
"emote_ids": ids,
"animateds": animateds,
}
},
)

async def set_user_alias(self, user_id: int, alias: PartialEmoji) -> NoReturn:
await self.cur.execute(
"INSERT INTO aliases (user_id, emote_id, animated, name) VALUES (%(user_id)s, %(emote_id)s, %(animated)s, %(name)s) "
"ON CONFLICT ON CONSTRAINT aliases_pk DO UPDATE SET emote_id = excluded.emote_id, animated = excluded.animated, \"name\" = excluded.name",
'ON CONFLICT ON CONSTRAINT aliases_pk DO UPDATE SET emote_id = excluded.emote_id, animated = excluded.animated, "name" = excluded.name',
parameters={
"user_id": user_id,
"name": alias.name,
"emote_id": alias.id,
"animated": alias.animated,
}
},
)

async def delete_user_alias(self, user_id: int, name: str) -> NoReturn:
await self.cur.execute(
"DELETE FROM aliases WHERE user_id=%(user_id)s and \"name\"=%(name)s",
parameters={"user_id": user_id, "name": name}
'DELETE FROM aliases WHERE user_id=%(user_id)s and "name"=%(name)s',
parameters={"user_id": user_id, "name": name},
)

async def delete_all_user_aliases(self, user_id: int) -> NoReturn:
await self.cur.execute(
"DELETE FROM aliases WHERE user_id=%(user_id)s",
parameters={"user_id": user_id}
parameters={"user_id": user_id},
)


Expand Down
Loading

0 comments on commit f318650

Please sign in to comment.