Skip to content

Commit

Permalink
chore(misc): format Python code (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba authored Dec 31, 2024
1 parent a32d8ee commit 1d11961
Show file tree
Hide file tree
Showing 48 changed files with 662 additions and 687 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/check-on-pr-console.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ jobs:
version: 24.10.0
src: console

- name: Lint Using isort
uses: isort/isort-action@v1
with:
isort-version: 5.13.2
configuration: --profile=black

permissions:
contents: read
pull-requests: read
Expand Down Expand Up @@ -74,4 +80,3 @@ jobs:
context: ./console
push: false
platforms: linux/${{ matrix.platform }}

12 changes: 9 additions & 3 deletions .github/workflows/check-on-pr-language.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ on:
paths:
- "language/**"
- "!language/README.md"

jobs:
lint:
runs-on: ubuntu-latest
Expand All @@ -38,6 +38,12 @@ jobs:
max-line-length: "120"
exclude: ".git,.venv,.dockerignore,.pdm-python,Dockerfile"

- name: Lint Using isort
uses: isort/isort-action@v1
with:
isort-version: 5.13.2
configuration: --profile=black

permissions:
contents: read
pull-requests: read
Expand All @@ -48,7 +54,7 @@ jobs:
needs: lint
strategy:
matrix:
platform: [ "arm64", "amd64" ]
platform: ["arm64", "amd64"]
steps:
- name: Checkout Repository
uses: actions/checkout@v4
Expand All @@ -66,4 +72,4 @@ jobs:
with:
context: ./language
push: false
platforms: linux/${{ matrix.platform }}
platforms: linux/${{ matrix.platform }}
10 changes: 8 additions & 2 deletions console/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ poetry run python -m api.uvi --reload
Lint code:

```shell
flake8 .
poetry run flake8 .
```

Format code:

```shell
black .
poetry run black .
```

Sort imports:

```shell
poetry run isort .
```
20 changes: 8 additions & 12 deletions console/api/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,21 @@
# AGPL-3.0-only in the root of this repository.

from .generic import exists
from .group import fetch_group, fetch_groups, fetch_group_count
from .group import fetch_group, fetch_group_count, fetch_groups
from .organization import (
fetch_organization,
fetch_organizations,
fetch_organization_count,
fetch_organization_groups,
fetch_organization_users,
fetch_organization_workspaces,
fetch_organization_groups,
fetch_organization_count,
fetch_organizations,
)
from .overview import fetch_version
from .user import (
fetch_user_organizations,
fetch_user_count,
fetch_user_groups,
fetch_user_organizations,
fetch_user_workspaces,
fetch_user_count,
)
from .workspace import (
fetch_workspace,
fetch_workspaces,
fetch_workspace_count,
)
from .overview import fetch_version
from .userpermission import grant_user_permission, revoke_user_permission
from .workspace import fetch_workspace, fetch_workspace_count, fetch_workspaces
4 changes: 1 addition & 3 deletions console/api/database/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

def exists(curs: psycopg.cursor, tablename: str, _id: str) -> bool:
try:
return curs.execute(
f"SELECT EXISTS(SELECT 1 FROM \"{tablename}\" WHERE id = '{_id}');"
).fetchone()["exists"]
return curs.execute(f"SELECT EXISTS(SELECT 1 FROM \"{tablename}\" WHERE id = '{_id}');").fetchone()["exists"]
except Exception as e:
raise e
4 changes: 3 additions & 1 deletion console/api/database/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
# by the GNU Affero General Public License v3.0 only, included in the file
# AGPL-3.0-only in the root of this repository.

from typing import Tuple, Iterable, Dict
from typing import Dict, Iterable, Tuple

from psycopg import DatabaseError

from ..dependencies import conn
from ..errors import EmptyDataException, NotFoundException
from .generic import exists
Expand Down
22 changes: 8 additions & 14 deletions console/api/database/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
# by the GNU Affero General Public License v3.0 only, included in the file
# AGPL-3.0-only in the root of this repository.

from typing import Dict, Tuple, Iterable
from typing import Dict, Iterable, Tuple

from psycopg import DatabaseError
from . import exists

from ..dependencies import conn
from ..errors import EmptyDataException, NotFoundException
from . import exists


def fetch_organization(organization_id: str) -> Dict:
try:
with conn.cursor() as curs:
if not exists(curs=curs, tablename="organization", _id=organization_id):
raise NotFoundException(
message=f"Organization with id={organization_id} does not exist!"
)
raise NotFoundException(message=f"Organization with id={organization_id} does not exist!")
return curs.execute(
f"""
SELECT id, name, create_time as "createTime", update_time as "updateTime"
Expand Down Expand Up @@ -61,9 +61,7 @@ def fetch_organization_count() -> Dict:
raise error


def fetch_organization_users(
organization_id: str, page=1, size=10
) -> Tuple[Iterable[Dict], int]:
def fetch_organization_users(organization_id: str, page=1, size=10) -> Tuple[Iterable[Dict], int]:
try:
with conn.cursor() as curs:
data = curs.execute(
Expand Down Expand Up @@ -94,9 +92,7 @@ def fetch_organization_users(
raise error


def fetch_organization_workspaces(
organization_id: str, page=1, size=10
) -> Tuple[Iterable[Dict], int]:
def fetch_organization_workspaces(organization_id: str, page=1, size=10) -> Tuple[Iterable[Dict], int]:
try:
with conn.cursor() as curs:
data = curs.execute(
Expand Down Expand Up @@ -124,9 +120,7 @@ def fetch_organization_workspaces(
raise error


def fetch_organization_groups(
organization_id: str, page=1, size=10
) -> Tuple[Iterable[Dict], int]:
def fetch_organization_groups(organization_id: str, page=1, size=10) -> Tuple[Iterable[Dict], int]:
try:
with conn.cursor() as curs:
data = curs.execute(
Expand Down
1 change: 1 addition & 0 deletions console/api/database/overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# AGPL-3.0-only in the root of this repository.

from psycopg import DatabaseError

from ..dependencies import conn


Expand Down
22 changes: 8 additions & 14 deletions console/api/database/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@
# by the GNU Affero General Public License v3.0 only, included in the file
# AGPL-3.0-only in the root of this repository.

from typing import Tuple, Iterable, Dict
from typing import Dict, Iterable, Tuple

from psycopg import DatabaseError
from . import exists

from ..dependencies import conn
from ..errors import EmptyDataException, NotFoundException
from . import exists


def fetch_user_organizations(
user_id: str, page=1, size=10
) -> Tuple[Iterable[Dict], int]:
def fetch_user_organizations(user_id: str, page=1, size=10) -> Tuple[Iterable[Dict], int]:
try:
with conn.cursor() as curs:
if not exists(curs=curs, tablename="user", _id=user_id):
raise NotFoundException(
message=f"User with id={user_id} does not exist!"
)
raise NotFoundException(message=f"User with id={user_id} does not exist!")
data = curs.execute(
f"""
SELECT u.id, u."permission", u.create_time as "createTime", o.id as "organizationId",
Expand Down Expand Up @@ -63,9 +61,7 @@ def fetch_user_workspaces(user_id: str, page=1, size=10) -> Tuple[Iterable[Dict]
try:
with conn.cursor() as curs:
if not exists(curs=curs, tablename="user", _id=user_id):
raise NotFoundException(
message=f"User with id={user_id} does not exist!"
)
raise NotFoundException(message=f"User with id={user_id} does not exist!")
data = curs.execute(
f"""
SELECT u.id, u.permission, u.create_time as "createTime", w.id as "workspaceId",
Expand Down Expand Up @@ -98,9 +94,7 @@ def fetch_user_groups(user_id: str, page=1, size=10) -> Tuple[Iterable[Dict], in
try:
with conn.cursor() as curs:
if not exists(curs=curs, tablename="user", _id=user_id):
raise NotFoundException(
message=f"User with id={user_id} does not exist!"
)
raise NotFoundException(message=f"User with id={user_id} does not exist!")
data = curs.execute(
f"""
SELECT u.id, u.permission, u.create_time as "createTime", g.id as "groupId", g."name" as "groupName"
Expand Down
1 change: 1 addition & 0 deletions console/api/database/userpermission.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# AGPL-3.0-only in the root of this repository.

from psycopg import DatabaseError

from api.dependencies import conn, new_id, new_timestamp


Expand Down
10 changes: 5 additions & 5 deletions console/api/database/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
# by the GNU Affero General Public License v3.0 only, included in the file
# AGPL-3.0-only in the root of this repository.

from typing import Dict, Tuple, Iterable
from typing import Dict, Iterable, Tuple

from psycopg import DatabaseError
from . import exists

from ..dependencies import conn
from ..errors import EmptyDataException, NotFoundException
from . import exists


def fetch_workspace(_id: str) -> Dict:
try:
with conn.cursor() as curs:
if not exists(curs=curs, tablename="workspace", _id=_id):
raise NotFoundException(
message=f"Workspace with id={_id} does not exist!"
)
raise NotFoundException(message=f"Workspace with id={_id} does not exist!")
data = curs.execute(
f"""
SELECT w.id, w.name, w.organization_id, o.name as "organizationName",
Expand Down
6 changes: 3 additions & 3 deletions console/api/dependencies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
# by the GNU Affero General Public License v3.0 only, included in the file
# AGPL-3.0-only in the root of this repository.

from .settings import settings
from .meilisearch import meilisearch_client
from .database import conn
from .utils import parse_sql_update_query, camel_to_snake, new_id, new_timestamp
from .jwt import JWTBearer
from .meilisearch import meilisearch_client
from .redis import redis_conn
from .settings import settings
from .utils import camel_to_snake, new_id, new_timestamp, parse_sql_update_query
1 change: 1 addition & 0 deletions console/api/dependencies/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from psycopg import connect
from psycopg.rows import dict_row

from . import settings

conn = connect(
Expand Down
9 changes: 4 additions & 5 deletions console/api/dependencies/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

import jwt
from fastapi import Request
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from . import settings
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer

from ..errors import GenericForbiddenException
from . import settings


class JWTBearer(HTTPBearer):
Expand All @@ -21,9 +22,7 @@ def __init__(self, auto_error: bool = True):

async def __call__(self, request: Request):
jwt.api_jws.PyJWS.header_typ = False
credentials: HTTPAuthorizationCredentials = await super(
JWTBearer, self
).__call__(request)
credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request)
if credentials:
if not credentials.scheme == "Bearer":
raise GenericForbiddenException(detail="Invalid authentication scheme.")
Expand Down
1 change: 1 addition & 0 deletions console/api/dependencies/meilisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# AGPL-3.0-only in the root of this repository.

from meilisearch import Client

from . import settings

meilisearch_client = Client(settings.SEARCH_URL)
1 change: 1 addition & 0 deletions console/api/dependencies/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# AGPL-3.0-only in the root of this repository.

from redis.asyncio import Redis

from . import settings

redis_conn = Redis().from_url(url=settings.REDIS_URL)
1 change: 1 addition & 0 deletions console/api/dependencies/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# AGPL-3.0-only in the root of this repository.

from typing import Optional

from pydantic_settings import BaseSettings


Expand Down
9 changes: 4 additions & 5 deletions console/api/dependencies/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
# by the GNU Affero General Public License v3.0 only, included in the file
# AGPL-3.0-only in the root of this repository.
import sys
import time
import uuid
from datetime import datetime, timezone
from functools import reduce
import uuid
import time

from sqids import Sqids


Expand All @@ -26,9 +27,7 @@ def parse_sql_update_query(tablename: str, data: dict):
if k != "id" and isinstance(v, datetime)
)
data_fields = ", ".join(
f"{camel_to_snake(k)} = '{v}'"
for k, v in data.items()
if k != "id" and not isinstance(v, datetime)
f"{camel_to_snake(k)} = '{v}'" for k, v in data.items() if k != "id" and not isinstance(v, datetime)
)
return (
f'UPDATE "{tablename}" SET {data_fields} WHERE id = \'{data["id"]}\';'
Expand Down
6 changes: 3 additions & 3 deletions console/api/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
# AGPL-3.0-only in the root of this repository.

from .api_errors import (
NotFoundError,
ForbiddenError,
GenericForbiddenException,
NoContentError,
NotFoundError,
ServiceUnavailableError,
UnknownApiError,
ForbiddenError,
GenericForbiddenException,
)
from .database_errors import EmptyDataException, NotFoundException
from .error_codes import errors
Loading

0 comments on commit 1d11961

Please sign in to comment.