Migrate to ruff

This commit is contained in:
2023-01-08 00:08:00 +01:00
parent 537e28d904
commit 06fcdfd83a
16 changed files with 657 additions and 342 deletions

View File

@@ -6,15 +6,14 @@ repos:
hooks: hooks:
- id: black - id: black
language_version: python3.11 language_version: python3.11
- repo: https://github.com/pycqa/isort
rev: v5.11.3 - repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.213'
hooks: hooks:
- id: isort - id: ruff
- repo: https://github.com/csachs/pyproject-flake8 args: ["--force-exclude"]
rev: v6.0.0.post1
- repo: https://github.com/crate-ci/typos
rev: v1.13.6
hooks: hooks:
- id: pyproject-flake8 - id: typos
additional_dependencies: [
'-e', 'git+https://github.com/pycqa/pyflakes@b37f91a#egg=pyflakes',
'-e', 'git+https://github.com/pycqa/pycodestyle@1063db8#egg=pycodestyle',
]

View File

@@ -1,4 +1,3 @@
from logging.config import fileConfig
import os import os
import sys import sys
@@ -7,15 +6,13 @@ from sqlalchemy.engine import create_engine
from core.db import DATABASE_URL from core.db import DATABASE_URL
myPath = os.path.dirname(os.path.abspath(__file__)) myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, myPath + "/../../") sys.path.insert(0, myPath + "/../../")
config = context.config config = context.config
from app.models import BaseMeta from app.models import BaseMeta # noqa: E402
target_metadata = BaseMeta.metadata target_metadata = BaseMeta.metadata

View File

@@ -1,4 +1,4 @@
from fastapi import Security, HTTPException, status from fastapi import HTTPException, Security, status
from core.auth import default_security from core.auth import default_security
from core.config import env_config from core.config import env_config

View File

@@ -3,7 +3,7 @@ from enum import Enum
import ormar import ormar
from core.db import metadata, database from core.db import database, metadata
class BaseMeta(ormar.ModelMeta): class BaseMeta(ormar.ModelMeta):

View File

@@ -1,5 +1,5 @@
from app.models import UploadBackends from app.models import UploadBackends
from app.services.storages import StoragesContainer, BotStorage, UserStorage from app.services.storages import BotStorage, StoragesContainer, UserStorage
class FileDownloader: class FileDownloader:

View File

@@ -1,9 +1,9 @@
from typing import Optional, Any from typing import Any, Optional
from fastapi import UploadFile from fastapi import UploadFile
from app.models import UploadedFile, UploadBackends from app.models import UploadBackends, UploadedFile
from app.services.storages import StoragesContainer, BotStorage, UserStorage from app.services.storages import BotStorage, StoragesContainer, UserStorage
class Wrapper: class Wrapper:

View File

@@ -1,5 +1,4 @@
import abc from typing import AsyncIterator, Optional, Union
from typing import AsyncIterator, Union, Optional
import telethon.client import telethon.client
import telethon.errors import telethon.errors
@@ -9,7 +8,7 @@ import telethon.tl.types
from core.config import env_config from core.config import env_config
class BaseStorage(abc.ABC): class BaseStorage:
def __init__( def __init__(
self, channel_id: Union[str, int], app_id: int, api_hash: str, session: str self, channel_id: Union[str, int], app_id: int, api_hash: str, session: str
): ):

View File

@@ -1,15 +1,14 @@
from typing import Optional from typing import Optional
from fastapi import File, UploadFile, Depends, Form, APIRouter, HTTPException, status from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile, status
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
from app.depends import check_token from app.depends import check_token
from app.models import UploadedFile as UploadedFileDB from app.models import UploadedFile as UploadedFileDB
from app.serializers import UploadedFile, CreateUploadedFile from app.serializers import CreateUploadedFile, UploadedFile
from app.services.file_downloader import FileDownloader from app.services.file_downloader import FileDownloader
from app.services.file_uploader import FileUploader from app.services.file_uploader import FileUploader
router = APIRouter( router = APIRouter(
prefix="/api/v1/files", dependencies=[Depends(check_token)], tags=["files"] prefix="/api/v1/files", dependencies=[Depends(check_token)], tags=["files"]
) )

View File

@@ -1,15 +1,13 @@
import sentry_sdk
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.responses import ORJSONResponse from fastapi.responses import ORJSONResponse
from prometheus_fastapi_instrumentator import Instrumentator from prometheus_fastapi_instrumentator import Instrumentator
import sentry_sdk
from app.on_start import on_start from app.on_start import on_start
from app.views import router, healthcheck_router from app.views import healthcheck_router, router
from core.config import env_config from core.config import env_config
from core.db import database from core.db import database
sentry_sdk.init( sentry_sdk.init(
env_config.SENTRY_DSN, env_config.SENTRY_DSN,
) )

View File

@@ -1,4 +1,3 @@
from fastapi.security import APIKeyHeader from fastapi.security import APIKeyHeader
default_security = APIKeyHeader(name="Authorization") default_security = APIKeyHeader(name="Authorization")

View File

@@ -2,7 +2,6 @@ from typing import Optional
from pydantic import BaseModel, BaseSettings from pydantic import BaseModel, BaseSettings
BotToken = str BotToken = str
TelethonSessionName = str TelethonSessionName = str

View File

@@ -5,7 +5,6 @@ from sqlalchemy import MetaData
from core.config import env_config from core.config import env_config
DATABASE_URL = ( DATABASE_URL = (
f"postgresql://{env_config.POSTGRES_USER}:{quote(env_config.POSTGRES_PASSWORD)}@" f"postgresql://{env_config.POSTGRES_USER}:{quote(env_config.POSTGRES_PASSWORD)}@"
f"{env_config.POSTGRES_HOST}:{env_config.POSTGRES_PORT}/{env_config.POSTGRES_DB}" f"{env_config.POSTGRES_HOST}:{env_config.POSTGRES_PORT}/{env_config.POSTGRES_DB}"

View File

@@ -1,4 +1,3 @@
from core.app import start_app from core.app import start_app
app = start_app() app = start_app()

884
poetry.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -6,25 +6,26 @@ authors = ["Kurbanov Bulat <kurbanovbul@gmail.com>"]
license = "Apache 2.0" license = "Apache 2.0"
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.9" python = "^3.11"
fastapi = "^0.88.0" fastapi = "^0.89.0"
uvicorn = {extras = ["standart"], version = "^0.20.0"} uvicorn = {extras = ["standard"], version = "^0.20.0"}
ormar = {extras = ["postgresql"], version = "^0.12.0"} ormar = {extras = ["postgresql"], version = "^0.12.0"}
alembic = "^1.9.1" alembic = "^1.9.1"
pydantic = {extras = ["dotenv"], version = "^1.10.2"} pydantic = {extras = ["dotenv"], version = "^1.10.2"}
python-multipart = "^0.0.5" python-multipart = "^0.0.5"
httpx = "^0.23.1" httpx = "^0.23.3"
telethon = "^1.26.1" telethon = "^1.26.1"
prometheus-fastapi-instrumentator = "^5.9.1" prometheus-fastapi-instrumentator = "^5.9.1"
uvloop = "^0.17.0" uvloop = "^0.17.0"
gunicorn = "^20.1.0" gunicorn = "^20.1.0"
orjson = "^3.8.3" orjson = "^3.8.4"
sentry-sdk = "^1.12.1" sentry-sdk = "^1.12.1"
greenlet = "^2.0.1" greenlet = "^2.0.1"
[tool.poetry.group.dev.dependencies] [tool.poetry.group.dev.dependencies]
pytest = "^7.2.0" pytest = "^7.2.0"
mypy = "^0.991" mypy = "^0.991"
pre-commit = "^2.21.0"
[build-system] [build-system]
requires = ["poetry-core>=1.0.0"] requires = ["poetry-core>=1.0.0"]
@@ -41,31 +42,38 @@ exclude = '''
)/ )/
''' '''
[tool.flake8] [tool.ruff]
ignore = [ fix = true
# Whitespace before ':' ( https://www.flake8rules.com/rules/E203.html ) target-version = "py311"
"E203" src = ["fastapi_file_server"]
] line-length=88
max-line-length=88 ignore = []
max-complexity = 15 select = ["B", "C", "E", "F", "W", "B9", "I001"]
select = "B,C,E,F,W,T4,B9"
exclude = [ exclude = [
# No need to traverse our git directory # No need to traverse our git directory
".git", ".git",
# There's no value in checking cache directories # There's no value in checking cache directories
"__pycache__", "__pycache__",
# The conf file is mostly autogenerated, ignore it # The conf file is mostly autogenerated, ignore it
"fastapi_file_server/app/alembic/*", "fastapi_file_server/app/alembic",
# The old directory contains Flake8 2.0
] ]
[tool.isort] [tool.ruff.flake8-bugbear]
profile = "black" extend-immutable-calls = ["fastapi.File", "fastapi.Form", "fastapi.Security"]
only_sections = true
force_sort_within_sections = true [tool.ruff.mccabe]
lines_after_imports = 2 max-complexity = 15
lexicographical = true
sections = ["FUTURE", "STDLIB", "BASEFRAMEWORK", "FRAMEWORKEXT", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"] [tool.ruff.isort]
known_baseframework = ["fastapi",] known-first-party = ["core", "app"]
known_frameworkext = ["starlette",]
src_paths = ["fastapi_file_server"] # only_sections = true
# force_sort_within_sections = true
# lines_after_imports = 2
# lexicographical = true
# sections = ["FUTURE", "STDLIB", "BASEFRAMEWORK", "FRAMEWORKEXT", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"]
# known_baseframework = ["fastapi",]
# known_frameworkext = ["starlette",]
[tool.ruff.pyupgrade]
keep-runtime-typing = true

View File

@@ -1,6 +1,5 @@
import httpx import httpx
response = httpx.get("http://localhost:8080/healthcheck") response = httpx.get("http://localhost:8080/healthcheck")
print(f"HEALTHCHECK STATUS: {response.status_code}") print(f"HEALTHCHECK STATUS: {response.status_code}")
exit(0 if response.status_code == 200 else 1) exit(0 if response.status_code == 200 else 1)