Skip to content

Commit

Permalink
applied linters, fixed CI
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Piskun <[email protected]>
  • Loading branch information
bigcat88 committed Jul 4, 2024
1 parent ed4b77d commit 4883859
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 63 deletions.
19 changes: 13 additions & 6 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
* text eol=lf

# Denote all files that are truly binary and should not be modified
js/* binary
screenshots/* binary
locales/* binary
translationfiles/* binary
*.png binary
*.heif binary
*.heic binary
*.hif binary
*.avif binary
*.png binary
*.gif binary
*.webp binary
*.tiff binary
*.jpeg binary
*.jpg binary
*.svg binary
*.phar binary

# Files to exclude from GitHub Languages statistics
l10n/* linguist-vendored=true
*.h linguist-vendored=true
*.Dockerfile linguist-vendored=true
33 changes: 0 additions & 33 deletions .github/workflows/analysis-coverage.yml

This file was deleted.

26 changes: 26 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
exclude: '^ex_app/(img|js)/|.*\.phar'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: mixed-line-ending

- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
files: ex_app/lib/

- repo: https://github.com/psf/black
rev: 24.4.0
hooks:
- id: black
files: ex_app/lib/

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.0
hooks:
- id: ruff
52 changes: 28 additions & 24 deletions ex_app/lib/main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import os
import typing
from pathlib import Path
from contextlib import asynccontextmanager
from contextvars import ContextVar
from gettext import translation
from pathlib import Path

import httpx
from starlette.responses import Response, FileResponse
from fastapi import FastAPI, responses, Request, Depends, BackgroundTasks, status
from starlette.middleware.base import BaseHTTPMiddleware

from fastapi import BackgroundTasks, Depends, FastAPI, Request, responses, status
from nc_py_api import NextcloudApp
from nc_py_api.ex_app import (
run_app,
AppAPIAuthMiddleware,
nc_app,
)
from nc_py_api.ex_app import AppAPIAuthMiddleware, nc_app, run_app
from nc_py_api.ex_app.integration_fastapi import fetch_models_task
from contextvars import ContextVar

from gettext import translation
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import FileResponse, Response

LOCALE_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "locale")
current_translator = ContextVar("current_translator")
Expand All @@ -30,14 +24,11 @@ def _(text):

class LocalizationMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
request_lang = request.headers.get('Accept-Language', 'en')
request_lang = request.headers.get("Accept-Language", "en")
print(f"DEBUG: lang={request_lang}")
translator = translation(
os.getenv("APP_ID"), LOCALE_DIR, languages=[request_lang], fallback=True
)
translator = translation(os.getenv("APP_ID"), LOCALE_DIR, languages=[request_lang], fallback=True)
current_translator.set(translator)
response = await call_next(request)
return response
return await call_next(request)


@asynccontextmanager
Expand Down Expand Up @@ -78,12 +69,15 @@ def enabled_callback(enabled: bool, nc: typing.Annotated[NextcloudApp, Depends(n
return responses.JSONResponse(content={"error": enabled_handler(enabled, nc)})


@APP.api_route("/api/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH", "TRACE"])
@APP.api_route(
"/api/{path:path}",
methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH", "TRACE"],
)
async def proxy_backend_requests(request: Request, path: str):
# print(f"proxy_BACKEND_requests: {path} - {request.method}\nCookies: {request.cookies}", flush=True)
async with httpx.AsyncClient() as client:
url = f"http://127.0.0.1:8288/api/{path}"
headers = {key: value for key, value in request.headers.items() if key.lower() not in ("host", 'cookie')}
headers = {key: value for key, value in request.headers.items() if key.lower() not in ("host", "cookie")}
# print(f"proxy_BACKEND_requests: method={request.method}, path={path}, status={response.status_code}")
if request.method == "GET":
response = await client.get(
Expand All @@ -106,12 +100,22 @@ async def proxy_backend_requests(request: Request, path: str):
# )
response_header = dict(response.headers)
response_header.pop("transfer-encoding", None)
return Response(content=response.content, status_code=response.status_code, headers=response_header)
return Response(
content=response.content,
status_code=response.status_code,
headers=response_header,
)


@APP.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH", "TRACE"])
@APP.api_route(
"/{path:path}",
methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH", "TRACE"],
)
async def proxy_requests(_request: Request, path: str):
print(f"proxy_requests: {path} - {_request.method}\nCookies: {_request.cookies}", flush=True)
print(
f"proxy_requests: {path} - {_request.method}\nCookies: {_request.cookies}",
flush=True,
)
if path.startswith("ex_app"):
file_server_path = Path("../../" + path)
elif not path:
Expand Down
13 changes: 13 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[tool.black]
line-length = 120
target-versions = ["py310"]
preview = true

[tool.ruff]
line-length = 120
target-version = "py310"
select = ["A", "B", "C", "E", "F", "G", "I", "S", "SIM", "PIE", "Q", "RET", "RUF", "UP" , "W"]
extend-ignore = ["I001", "RUF100", "D400", "D415"]

[tool.isort]
profile = "black"

0 comments on commit 4883859

Please sign in to comment.