Skip to content

Commit

Permalink
chore: add ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
taliraj committed Mar 3, 2024
1 parent 4b3242f commit 47173cd
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 34 deletions.
17 changes: 7 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ repos:
- id: end-of-file-fixer
- id: check-yaml
args: [ '--unsafe' ]
- repo: https://github.com/pycqa/isort
rev: 5.12.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.0
hooks:
- id: isort
name: isort (python)
language_version: '3.11'
args: ["--profile", "black", "--filter-files"]
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
4 changes: 2 additions & 2 deletions django_migrations_tui/management/commands/migrationstui.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def show_list(self, connection, app_names=None):
else:
app_names = sorted(loader.migrated_apps)

migrations = list()
migrations = []
for app_name in app_names:
app_migration = MigrationsList(app_name, 0, list())
app_migration = MigrationsList(app_name, 0, [])
shown = set()
for node in graph.leaf_nodes(app_name):
for plan_node in graph.forwards_plan(node):
Expand Down
10 changes: 7 additions & 3 deletions django_migrations_tui/tui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ def on_migrations_tree_status(self, message: MigrationsTree.Status) -> None:
rich_log = self.query_one(Log)
if message.sql:
if message.message == "BEGIN;":
self.sql_code = message.message # reset
self.sql_code = message.message # reset
elif message.message != "COMMIT;":
self.sql_code = f"{self.sql_code}\n{message.message}"
else:
self.sql_code = f"{self.sql_code}\n{message.message}"
rich_log.write(Syntax(self.sql_code, "sql", word_wrap=True, line_numbers=True))
rich_log.write(
Syntax(self.sql_code, "sql", word_wrap=True, line_numbers=True)
)
else:
rich_log.write(message.message)
rich_log.display = True
Expand Down Expand Up @@ -136,7 +138,9 @@ def check_confirmation(apply) -> None:
tree.run_command(command, sql=True)

if command:
self.push_screen(ConfirmationScreen(command, "Print SQL"), check_confirmation)
self.push_screen(
ConfirmationScreen(command, "Print SQL"), check_confirmation
)

def select_migration(self, migration: str) -> None:
tree = self.query_one(MigrationsTree)
Expand Down
3 changes: 2 additions & 1 deletion django_migrations_tui/tui/confirmation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List

from textual.app import ComposeResult
from textual.containers import Grid
from textual.screen import ModalScreen
Expand All @@ -8,7 +9,7 @@
class ConfirmationScreen(ModalScreen[bool]):
"""Screen with a confirmation dialog"""

def __init__(self, command: List[str], label: str="Migrate"):
def __init__(self, command: List[str], label: str = "Migrate"):
super().__init__()
self.command = f"[bold cyan]{' '.join(command)}"
self.label = label
Expand Down
12 changes: 8 additions & 4 deletions django_migrations_tui/tui/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def migrate_plan(self):
return command

@work(exclusive=True)
async def run_command(self, command, sql: bool=False):
async def run_command(self, command, sql: bool = False):
self.post_success_message(f"Running {' '.join(command)}")

proc = await asyncio.create_subprocess_exec(
Expand Down Expand Up @@ -183,13 +183,18 @@ def fake_migration(self):

def sqlmigrate(self):
selected_item = self.cursor_node
if not selected_item.allow_expand and str(selected_item.label) == " (no migrations)":
if (
not selected_item.allow_expand
and str(selected_item.label) == " (no migrations)"
):
self.post_error_message("Select a migration to print the SQL.")
return None
elif not selected_item.allow_expand:
app_name = str(selected_item.parent.label).split(" (")[0]
migration_name = str(selected_item.label).split("] ")[1]
command = f"python manage.py sqlmigrate {app_name} {migration_name}".split(" ")
command = f"python manage.py sqlmigrate {app_name} {migration_name}".split(
" "
)
return command
else:
self.post_warning_message("Select a migration name to print the SQL.")
Expand Down Expand Up @@ -250,7 +255,6 @@ def revert_migrations(self):
else:
self.post_error_message("Revert not supported in plan format.")


def post_warning_message(self, message: str) -> None:
"""Post a warning message to the log."""
self.post_message(self.Status(f"[bold #ffa62b]{message}"))
Expand Down
23 changes: 23 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,26 @@ build-backend = "setuptools.build_meta"
packages = ["django_migrations_tui"]

[tool.setuptools_scm]

[tool.ruff.lint]
select = [
"ASYNC",
"B",
"C4",
"C90",
"E",
"F",
"G",
"I",
"ISC",
"PERF",
"PLE",
"PLR0",
"W",
"RUF100",
"UP032",
]
[tool.ruff.lint.per-file-ignores]
"test_dj_project/test_show_plan.py" = ["E501"]
"test_dj_project/dj_project/settings.py" = ["E501"]
"django_migrations_tui/management/commands/migrationstui.py" = ["C901", "PLR0912", "PERF401"]
1 change: 1 addition & 0 deletions test_dj_project/dj_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.contrib import admin
from django.urls import path

Expand Down
1 change: 1 addition & 0 deletions test_dj_project/manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
20 changes: 6 additions & 14 deletions test_dj_project/test_show_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
@pytest.mark.django_db
def test_apps_list(migrations_list):
models = apps.get_models()
apps_with_models = set(model._meta.app_label for model in models)
apps_with_migrations = set(migration.app_name for migration in migrations_list)
apps_with_models = {model._meta.app_label for model in models}
apps_with_migrations = {migration.app_name for migration in migrations_list}
assert (
apps_with_models
== apps_with_migrations
Expand All @@ -23,8 +23,7 @@ def test_admin_migrations_list(migrations_list, recorded_migrations):
migration for migration in recorded_migrations if migration[0] == "admin"
]
assert admin_migrations_list.applied_count == len(admin_applied_migrations) == 3
admin_migrations = [migration for migration in admin_migrations_list.migrations]
assert admin_migrations == [
assert admin_migrations_list.migrations == [
" [X] 0001_initial",
" [X] 0002_logentry_remove_auto_add",
" [X] 0003_logentry_add_action_flag_choices",
Expand All @@ -40,8 +39,7 @@ def test_auth_migrations_list(migrations_list, recorded_migrations):
migration for migration in recorded_migrations if migration[0] == "auth"
]
assert auth_migrations_list.applied_count == len(auth_applied_migrations) == 12
auth_migrations = [migration for migration in auth_migrations_list.migrations]
assert auth_migrations == [
assert auth_migrations_list.migrations == [
" [X] 0001_initial",
" [X] 0002_alter_permission_name_max_length",
" [X] 0003_alter_user_email_max_length",
Expand Down Expand Up @@ -72,10 +70,7 @@ def test_contenttypes_migrations_list(migrations_list, recorded_migrations):
== len(contenttypes_applied_migrations)
== 2
)
contenttypes_migrations = [
migration for migration in contenttypes_migrations_list.migrations
]
assert contenttypes_migrations == [
assert contenttypes_migrations_list.migrations == [
" [X] 0001_initial",
" [X] 0002_remove_content_type_name",
]
Expand All @@ -92,7 +87,4 @@ def test_sessions_migrations_list(migrations_list, recorded_migrations):
assert (
sessions_migrations_list.applied_count == len(sessions_applied_migrations) == 1
)
sessions_migrations = [
migration for migration in sessions_migrations_list.migrations
]
assert sessions_migrations == [" [X] 0001_initial"]
assert sessions_migrations_list.migrations == [" [X] 0001_initial"]

0 comments on commit 47173cd

Please sign in to comment.