Skip to content

Commit

Permalink
Merge pull request #1676 from Gozargah/ruff
Browse files Browse the repository at this point in the history
Migrate code formatting from autopep8 to ruff
  • Loading branch information
ImMohammad20000 authored Feb 22, 2025
2 parents abba81d + 089e74e commit 7990c0e
Show file tree
Hide file tree
Showing 71 changed files with 2,633 additions and 2,943 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ruff-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Code Format Checker
on:
push:
branches:
- '*'
pull_request:
branches:
- '*'

jobs:
ruff:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.12'

- name: Install Ruff
run: |
python -m pip install --upgrade pip
pip install ruff
- name: Run Ruff Format
run: |
ruff check --output-format=github .
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ Backend is built using FastAPI and uses SQLAlchemy as the ORM for database opera
### Python Code Formatting
To maintain consistency in the codebase, we require all code to be formatted using
```bash
autopep8 <file> --max-line-length 120
ruff check .
ruff format .
```

## Frontend
Expand Down
14 changes: 7 additions & 7 deletions Marzban.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
"python.analysis.inlayHints.pytestParameters": true,
"python.analysis.inlayHints.callArgumentNames": "all",
"python.analysis.inlayHints.functionReturnTypes": true,

"[python]": {
"editor.defaultFormatter": "ms-python.autopep8",
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
},
"autopep8.args": [
"--max-line-length",
"120"
],
"ruff.configuration": "pyproject.toml",

"editor.codeActionsOnSave": {
"source.organizeImports": "never"
},
Expand Down Expand Up @@ -85,7 +84,8 @@
"ms-vscode.vscode-typescript-next",
"yoavbls.pretty-ts-errors",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode"
"esbenp.prettier-vscode",
"charliermarsh.ruff"
]
},
}
8 changes: 2 additions & 6 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
redoc_url="/redoc" if DOCS else None,
)

scheduler = BackgroundScheduler(
{"apscheduler.job_defaults.max_instances": 20}, timezone="UTC"
)
scheduler = BackgroundScheduler({"apscheduler.job_defaults.max_instances": 20}, timezone="UTC")
logger = logging.getLogger("uvicorn.error")

app.add_middleware(
Expand Down Expand Up @@ -52,9 +50,7 @@ def on_startup():
paths = [f"{r.path}/" for r in app.routes]
paths.append("/api/")
if f"/{XRAY_SUBSCRIPTION_PATH}/" in paths:
raise ValueError(
f"you can't use /{XRAY_SUBSCRIPTION_PATH}/ as subscription path it reserved for {app.title}"
)
raise ValueError(f"you can't use /{XRAY_SUBSCRIPTION_PATH}/ as subscription path it reserved for {app.title}")
scheduler.start()


Expand Down
41 changes: 17 additions & 24 deletions app/dashboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,38 @@
from fastapi.staticfiles import StaticFiles

base_dir = Path(__file__).parent
build_dir = base_dir / 'build'
statics_dir = build_dir / 'statics'
build_dir = base_dir / "build"
statics_dir = build_dir / "statics"


def build_api_interface():
proc = subprocess.Popen(
['pnpm', 'run', 'wait-port-gen-api'],
env={**os.environ, 'UVICORN_PORT': str(UVICORN_PORT)},
subprocess.Popen(
["pnpm", "run", "wait-port-gen-api"],
env={**os.environ, "UVICORN_PORT": str(UVICORN_PORT)},
cwd=base_dir,
stdout=subprocess.DEVNULL
stdout=subprocess.DEVNULL,
)


def build():
proc = subprocess.Popen(
['pnpm', 'run', 'build', '--outDir', build_dir, '--assetsDir', 'statics'],
env={**os.environ, 'VITE_BASE_API': VITE_BASE_API},
cwd=base_dir
["pnpm", "run", "build", "--outDir", build_dir, "--assetsDir", "statics"],
env={**os.environ, "VITE_BASE_API": VITE_BASE_API},
cwd=base_dir,
)
proc.wait()
with open(build_dir / 'index.html', 'r') as file:
with open(build_dir / "index.html", "r") as file:
html = file.read()
with open(build_dir / '404.html', 'w') as file:
with open(build_dir / "404.html", "w") as file:
file.write(html)


def run_dev():
build_api_interface()
proc = subprocess.Popen(
['pnpm', 'run', 'dev', '--base', os.path.join(DASHBOARD_PATH, '')],
env={**os.environ, 'VITE_BASE_API': VITE_BASE_API, 'DEBUG': 'false'},
cwd=base_dir
["pnpm", "run", "dev", "--base", os.path.join(DASHBOARD_PATH, "")],
env={**os.environ, "VITE_BASE_API": VITE_BASE_API, "DEBUG": "false"},
cwd=base_dir,
)

atexit.register(proc.terminate)
Expand All @@ -47,16 +48,8 @@ def run_build():
if not build_dir.is_dir():
build()

app.mount(
DASHBOARD_PATH,
StaticFiles(directory=build_dir, html=True),
name="dashboard"
)
app.mount(
'/statics/',
StaticFiles(directory=statics_dir, html=True),
name="statics"
)
app.mount(DASHBOARD_PATH, StaticFiles(directory=build_dir, html=True), name="dashboard")
app.mount("/statics/", StaticFiles(directory=statics_dir, html=True), name="statics")


@app.on_event("startup")
Expand Down
68 changes: 32 additions & 36 deletions app/db/__init__.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session

from .base import Base, SessionLocal, engine # noqa


class GetDB: # Context Manager
def __init__(self):
self.db = SessionLocal()

def __enter__(self):
return self.db

def __exit__(self, exc_type, exc_value, traceback):
if isinstance(exc_value, SQLAlchemyError):
self.db.rollback() # rollback on exception

self.db.close()


def get_db(): # Dependency
with GetDB() as db:
yield db


from .crud import (create_admin, create_notification_reminder, # noqa
create_user, delete_notification_reminder, get_admin,
get_admins, get_jwt_secret_key, get_notification_reminder,
get_or_create_inbound, get_system_usage,
get_tls_certificate, get_user, get_user_by_id, get_users,
get_users_count, remove_admin, remove_user, revoke_user_sub,
set_owner, update_admin, update_user, update_user_status, reset_user_by_next,
update_user_sub, start_user_expire, get_admin_by_id,
get_admin_by_telegram_id)
from .base import Base, GetDB, get_db # noqa


from .crud import (
create_admin,
create_notification_reminder, # noqa
create_user,
delete_notification_reminder,
get_admin,
get_admins,
get_jwt_secret_key,
get_notification_reminder,
get_or_create_inbound,
get_system_usage,
get_tls_certificate,
get_user,
get_user_by_id,
get_users,
get_users_count,
remove_admin,
remove_user,
revoke_user_sub,
set_owner,
update_admin,
update_user,
update_user_status,
reset_user_by_next,
update_user_sub,
start_user_expire,
get_admin_by_id,
get_admin_by_telegram_id,
)

from .models import JWT, System, User # noqa

Expand Down Expand Up @@ -60,18 +60,14 @@ def get_db(): # Dependency
"get_admins",
"get_admin_by_id",
"get_admin_by_telegram_id",

"create_notification_reminder",
"get_notification_reminder",
"delete_notification_reminder",

"GetDB",
"get_db",

"User",
"System",
"JWT",

"Base",
"Session",
]
30 changes: 24 additions & 6 deletions app/db/base.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, DeclarativeBase
from sqlalchemy.exc import SQLAlchemyError

from config import (
SQLALCHEMY_DATABASE_URL,
SQLALCHEMY_POOL_SIZE,
SQLIALCHEMY_MAX_OVERFLOW,
)

IS_SQLITE = SQLALCHEMY_DATABASE_URL.startswith('sqlite')
IS_SQLITE = SQLALCHEMY_DATABASE_URL.startswith("sqlite")

if IS_SQLITE:
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False}
)
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
else:
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
pool_size=SQLALCHEMY_POOL_SIZE,
max_overflow=SQLIALCHEMY_MAX_OVERFLOW,
pool_recycle=3600,
pool_timeout=10
pool_timeout=10,
)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


class Base(DeclarativeBase):
pass


class GetDB: # Context Manager
def __init__(self):
self.db = SessionLocal()

def __enter__(self):
return self.db

def __exit__(self, exc_type, exc_value, traceback):
if isinstance(exc_value, SQLAlchemyError):
self.db.rollback() # rollback on exception

self.db.close()


def get_db(): # Dependency
with GetDB() as db:
yield db
Loading

0 comments on commit 7990c0e

Please sign in to comment.