Skip to content

Commit

Permalink
Add Auto Reformat Code #53
Browse files Browse the repository at this point in the history
  • Loading branch information
AliRn76 authored Dec 23, 2023
2 parents f6707ed + 1e079a2 commit 6055f0c
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 14 deletions.
12 changes: 12 additions & 0 deletions docs/docs/configs.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,15 @@ It should be dotted address of your `shutdown` function
this function can be `sync` or `async`

_Example:_ `URLS = 'core.configs.shutdown'`

---
### [AUTO_REFORMAT](https://pantherpy.github.io/auto_reformat)
> <b>Type:</b> `bool` (<b>Default:</b> `False`)
It will reformat your code on every reload (on every change if you run the project with `--reload`)

You may want to write your custom `ruff.toml` in root of your project.

Reference: [https://docs.astral.sh/ruff/formatter/](https://docs.astral.sh/ruff/formatter/)

_Example:_ `AUTO_REFORMAT = True`
6 changes: 3 additions & 3 deletions docs/docs/release_notes.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
### 3.3.0
- Add Auto Reformat Code

### 3.2.4
- Add all() query
- Add tests for `pantherdb`, `load_configs()`, `status.py`, `Panel`, `multipart`, `request headers`
- Refactor `Headers()` class
- Check `uvloop` installation on `Panther init`
- Minor Improvement

### 3.2.1
- Minor Improvement

### 3.2.1
- Move `Startup` to `__call__`

Expand Down
10 changes: 4 additions & 6 deletions example/app/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@
from app.models import User
from app.serializers import (
FileSerializer,
ImageSerializer,
UserInputSerializer,
UserOutputSerializer,
UserUpdateSerializer,
ImageSerializer,
)
from core.permissions import UserPermission

from panther import status
from panther.app import API, GenericAPI
from panther.authentications import JWTAuthentication
from panther.background_tasks import background_tasks, BackgroundTask
from panther.background_tasks import BackgroundTask, background_tasks
from panther.db.connection import redis
from panther.request import Request
from panther.response import HTMLResponse, Response
from panther.throttling import Throttling
from panther.websocket import close_websocket_connection, send_message_to_websocket


logger = logging.getLogger('panther')


Expand Down Expand Up @@ -173,10 +172,9 @@ async def run_background_tasks_api():
async def hello(name: str):
time.sleep(5)
print(f'Done {name}')

task = (
BackgroundTask(hello, 'ali1')
.interval(2)
.every_seconds(2)
BackgroundTask(hello, 'ali1').interval(2).every_seconds(2)
# .on_time(datetime.time(hour=19, minute=18, second=10))
)
background_tasks.add_task(task)
Expand Down
1 change: 1 addition & 0 deletions example/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
async def test(*args, **kwargs):
return Response(data={'detail': 'this is for test'})


urls = {
'none-class/': ReturnNone,
'none/': return_none,
Expand Down
6 changes: 4 additions & 2 deletions example/core/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@


async def startup():
print('inside startup function')
print('Starting Up')


async def shutdown():
print('inside shutdown function')
print('Shutting Down')


STARTUP = 'core.configs.startup'
SHUTDOWN = 'core.configs.shutdown'

AUTO_REFORMAT = True
5 changes: 3 additions & 2 deletions example/single_file_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
async def hello_world_api():
return {'detail': 'Hello World'}


url_routing = {
'/': hello_world_api,
}


def startup():
print("Its startup")
print('Its startup')


def shutdown():
print("Its shutdown")
print('Its shutdown')


app = Panther(__name__, configs=__name__, urls=url_routing, startup=startup, shutdown=shutdown)
2 changes: 1 addition & 1 deletion panther/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from panther.main import Panther # noqa: F401

__version__ = '3.2.5'
__version__ = '3.3.0'


def version():
Expand Down
5 changes: 5 additions & 0 deletions panther/_load_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'load_jwt_config',
'load_startup',
'load_shutdown',
'load_auto_reformat',
'collect_all_models',
'load_urls',
'load_panel_urls',
Expand Down Expand Up @@ -133,6 +134,10 @@ def load_shutdown(configs: dict, /) -> Callable:
return configs.get('SHUTDOWN') and import_class(configs['SHUTDOWN'])


def load_auto_reformat(configs: dict, /) -> bool:
return configs.get('AUTO_REFORMAT', config['auto_reformat'])


def collect_all_models() -> list[dict]:
"""Collecting all models for panel APIs"""
from panther.db.models import Model
Expand Down
2 changes: 2 additions & 0 deletions panther/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Config(TypedDict):
has_ws: bool
startup: Callable
shutdown: Callable
auto_reformat: bool


config: Config = {
Expand All @@ -72,4 +73,5 @@ class Config(TypedDict):
'has_ws': False,
'startup': None,
'shutdown': None,
'auto_reformat': False,
}
11 changes: 11 additions & 0 deletions panther/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import contextlib
import logging
import subprocess
import sys
import types
from collections.abc import Callable
Expand Down Expand Up @@ -44,6 +45,9 @@ def __init__(self, name: str, configs=None, urls: dict | None = None, startup: C
logger.error(clean_traceback_message(e))
sys.exit()

# Reformat Code
self.reformat_code()

# Monitoring
self.monitoring = Monitoring(is_active=config['monitoring'])

Expand Down Expand Up @@ -77,6 +81,7 @@ def load_configs(self) -> None:
config['jwt_config'] = load_jwt_config(self._configs_module)
config['startup'] = load_startup(self._configs_module)
config['shutdown'] = load_shutdown(self._configs_module)
config['auto_reformat'] = load_auto_reformat(self._configs_module)
config['models'] = collect_all_models()

# Initialize Background Tasks
Expand Down Expand Up @@ -113,6 +118,12 @@ def _create_ws_connections_instance(self):
else:
self.ws_redis_connection = None

@classmethod
def reformat_code(cls):
if config['auto_reformat']:
subprocess.run(['ruff', 'format', config['base_dir']])
subprocess.run(['ruff', 'check', '--select', 'I', '--fix', config['base_dir']])

async def __call__(self, scope: dict, receive: Callable, send: Callable) -> None:
"""
1.
Expand Down

0 comments on commit 6055f0c

Please sign in to comment.