From 7662a7924a48a40b8fb96572153aa3006d605421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=20=D0=93=D0=BB=D0=B0=D0=B7=D1=83?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2?= Date: Thu, 4 Dec 2025 03:06:58 +0300 Subject: [PATCH] fixed flake8 lint errors --- pyserve/__init__.py | 2 +- pyserve/cli.py | 3 ++- pyserve/ctl/_daemon.py | 2 -- pyserve/ctl/_runner.py | 8 +++----- pyserve/ctl/commands/config.py | 1 - pyserve/ctl/commands/down.py | 5 +++-- pyserve/ctl/commands/health.py | 2 +- pyserve/ctl/commands/init.py | 15 +++++++-------- pyserve/ctl/commands/logs.py | 26 ++++++++------------------ pyserve/ctl/commands/scale.py | 8 +++----- pyserve/ctl/commands/service.py | 15 +++++++-------- pyserve/ctl/commands/status.py | 10 ++-------- pyserve/ctl/commands/top.py | 9 +++++---- pyserve/ctl/commands/up.py | 4 ++-- pyserve/ctl/main.py | 30 +++++++++++++++--------------- pyserve/ctl/output/__init__.py | 2 ++ pyserve/ctl/state/__init__.py | 4 +--- 17 files changed, 62 insertions(+), 84 deletions(-) diff --git a/pyserve/__init__.py b/pyserve/__init__.py index 0f0f7ac..2a311af 100644 --- a/pyserve/__init__.py +++ b/pyserve/__init__.py @@ -2,7 +2,7 @@ PyServe - HTTP web server written on Python """ -__version__ = "0.9.10" +__version__ = "0.10.0" __author__ = "Ilya Glazunov" from .asgi_mount import ( diff --git a/pyserve/cli.py b/pyserve/cli.py index f154b93..f71b3e7 100644 --- a/pyserve/cli.py +++ b/pyserve/cli.py @@ -19,7 +19,8 @@ def main() -> None: epilog="For service management (start/stop/restart/logs), use: pyservectl", ) parser.add_argument( - "-c", "--config", + "-c", + "--config", default="config.yaml", help="Path to configuration file (default: config.yaml)", ) diff --git a/pyserve/ctl/_daemon.py b/pyserve/ctl/_daemon.py index 5639d8c..1967536 100644 --- a/pyserve/ctl/_daemon.py +++ b/pyserve/ctl/_daemon.py @@ -88,6 +88,4 @@ def main(): if __name__ == "__main__": - import os - main() diff --git a/pyserve/ctl/_runner.py b/pyserve/ctl/_runner.py index e9451a1..03c1bdf 100644 --- a/pyserve/ctl/_runner.py +++ b/pyserve/ctl/_runner.py @@ -7,16 +7,14 @@ Integrates with ProcessManager for actual process management. import asyncio import os -import signal import sys import time from dataclasses import dataclass, field -from pathlib import Path -from typing import Any, Dict, List, Optional +from typing import Dict, List, Optional from ..config import Config -from ..process_manager import ProcessConfig, ProcessInfo, ProcessManager, ProcessState -from .state import ServiceState, StateManager +from ..process_manager import ProcessConfig, ProcessManager, ProcessState +from .state import StateManager @dataclass diff --git a/pyserve/ctl/commands/config.py b/pyserve/ctl/commands/config.py index e21da9f..3b5a381 100644 --- a/pyserve/ctl/commands/config.py +++ b/pyserve/ctl/commands/config.py @@ -190,7 +190,6 @@ def show_cmd(ctx, config_file: Optional[str], output_format: str, section: Optio console.print(syntax) elif output_format == "table": - from rich.table import Table from rich.tree import Tree def build_tree(data, tree): diff --git a/pyserve/ctl/commands/down.py b/pyserve/ctl/commands/down.py index df5d3cf..f5497de 100644 --- a/pyserve/ctl/commands/down.py +++ b/pyserve/ctl/commands/down.py @@ -5,7 +5,7 @@ pyserve down - Stop all services import signal import time from pathlib import Path -from typing import Optional, cast +from typing import cast import click @@ -56,6 +56,7 @@ def down_cmd( try: import os + # FIXME: Please fix the cast usage here os.kill(cast(int, daemon_pid), signal.SIGTERM) @@ -93,8 +94,8 @@ def down_cmd( console.print("[bold]Stopping services...[/bold]") - from .._runner import ServiceRunner from ...config import Config + from .._runner import ServiceRunner config_path = Path(ctx.config_file) if config_path.exists(): diff --git a/pyserve/ctl/commands/health.py b/pyserve/ctl/commands/health.py index f787f30..1d9a55c 100644 --- a/pyserve/ctl/commands/health.py +++ b/pyserve/ctl/commands/health.py @@ -4,7 +4,6 @@ pyserve health - Check health of services import asyncio from pathlib import Path -from typing import Optional import click @@ -60,6 +59,7 @@ def health_cmd(ctx, services: tuple, timeout: int, output_format: str): return from rich.table import Table + from ..output import format_health table = Table(show_header=True, header_style="bold") diff --git a/pyserve/ctl/commands/init.py b/pyserve/ctl/commands/init.py index dae4a33..90a98c6 100644 --- a/pyserve/ctl/commands/init.py +++ b/pyserve/ctl/commands/init.py @@ -3,7 +3,6 @@ pyserve init - Initialize a new pyserve project """ from pathlib import Path -from typing import Optional import click @@ -119,7 +118,7 @@ extensions: port_range: [9000, 9999] health_check_enabled: true proxy_timeout: 60.0 - + apps: # Example: FastAPI application - name: api @@ -136,7 +135,7 @@ extensions: strip_path: true env: APP_ENV: "production" - + # Example: Flask application (WSGI) # - name: admin # path: /admin @@ -191,7 +190,7 @@ extensions: - path: /api app: myapp.api:app # factory: false # Set to true if app is a factory function - + # Starlette app mounted at /web # - path: /web # app: myapp.web:app @@ -257,13 +256,13 @@ logging: format: type: standard use_colors: false - + # JSON logs for log aggregation - path: ./logs/pyserve.json level: INFO format: type: json - + # Access logs - path: ./logs/access.log level: INFO @@ -278,7 +277,7 @@ extensions: port_range: [9000, 9999] health_check_enabled: true proxy_timeout: 60.0 - + apps: - name: api path: /api @@ -384,7 +383,7 @@ def init_cmd( pyserve init -t full # All features pyserve init -o production.yaml # Custom output file """ - from ..output import console, print_success, print_warning, print_info + from ..output import console, print_info, print_success, print_warning if list_templates: console.print("\n[bold]Available Templates:[/bold]\n") diff --git a/pyserve/ctl/commands/logs.py b/pyserve/ctl/commands/logs.py index 9d563db..06015d2 100644 --- a/pyserve/ctl/commands/logs.py +++ b/pyserve/ctl/commands/logs.py @@ -3,7 +3,6 @@ pyserve logs - View service logs """ import asyncio -import sys import time from pathlib import Path from typing import Optional @@ -81,15 +80,13 @@ def logs_cmd( pyserve logs --tail 50 # Last 50 lines pyserve logs --since "10m" # Logs from last 10 minutes """ - from ..output import console, print_error, print_info + from ..output import print_info from ..state import StateManager state_manager = StateManager(Path(".pyserve"), ctx.project) if services: - log_files = [ - (name, state_manager.get_service_log_file(name)) for name in services - ] + log_files = [(name, state_manager.get_service_log_file(name)) for name in services] else: all_services = state_manager.get_all_services() if not all_services: @@ -100,10 +97,7 @@ def logs_cmd( print_info("No logs available. Start services with 'pyserve up'") return else: - log_files = [ - (name, state_manager.get_service_log_file(name)) - for name in all_services - ] + log_files = [(name, state_manager.get_service_log_file(name)) for name in all_services] existing_logs = [(name, path) for name, path in log_files if path.exists()] @@ -115,9 +109,7 @@ def logs_cmd( until_timestamp = _parse_time(until_time) if until_time else None colors = ["cyan", "green", "yellow", "blue", "magenta"] - service_colors = { - name: colors[i % len(colors)] for i, (name, _) in enumerate(existing_logs) - } + service_colors = {name: colors[i % len(colors)] for i, (name, _) in enumerate(existing_logs)} if follow: asyncio.run( @@ -144,7 +136,7 @@ def logs_cmd( def _parse_time(time_str: str) -> Optional[float]: import re - from datetime import datetime, timedelta + from datetime import datetime # Relative time (e.g., "10m", "1h", "2d") match = re.match(r"^(\d+)([smhd])$", time_str) @@ -182,10 +174,10 @@ def _read_logs( no_color: bool, filter_pattern: Optional[str], ): - from ..output import console - import re + from ..output import console + all_lines = [] for service_name, log_path in log_files: @@ -210,9 +202,7 @@ def _read_logs( try: from datetime import datetime - line_time = datetime.fromisoformat( - timestamp_match.group(1).replace(" ", "T") - ).timestamp() + line_time = datetime.fromisoformat(timestamp_match.group(1).replace(" ", "T")).timestamp() except ValueError: pass diff --git a/pyserve/ctl/commands/scale.py b/pyserve/ctl/commands/scale.py index 607c399..01edc9c 100644 --- a/pyserve/ctl/commands/scale.py +++ b/pyserve/ctl/commands/scale.py @@ -34,10 +34,10 @@ def scale_cmd(ctx, scales: tuple, timeout: int, no_wait: bool): pyserve scale api=4 # Scale api to 4 workers pyserve scale api=4 admin=2 # Scale multiple services """ + from ...config import Config + from .._runner import ServiceRunner from ..output import console, print_error, print_info, print_success from ..state import StateManager - from .._runner import ServiceRunner - from ...config import Config scale_map = {} for scale in scales: @@ -72,9 +72,7 @@ def scale_cmd(ctx, scales: tuple, timeout: int, no_wait: bool): print_info(f"Scaling {service}: {current} → {workers} workers") try: - success = await runner.scale_service( - service, workers, timeout=timeout, wait=not no_wait - ) + success = await runner.scale_service(service, workers, timeout=timeout, wait=not no_wait) if success: print_success(f"Scaled {service} to {workers} workers") else: diff --git a/pyserve/ctl/commands/service.py b/pyserve/ctl/commands/service.py index c4d9f2a..39e1331 100644 --- a/pyserve/ctl/commands/service.py +++ b/pyserve/ctl/commands/service.py @@ -4,7 +4,6 @@ pyserve start/stop/restart - Service management commands import asyncio from pathlib import Path -from typing import List import click @@ -28,10 +27,10 @@ def start_cmd(ctx, services: tuple, timeout: int): pyserve start api # Start api service pyserve start api admin # Start multiple services """ - from ..output import console, print_error, print_info, print_success - from ..state import StateManager - from .._runner import ServiceRunner from ...config import Config + from .._runner import ServiceRunner + from ..output import console, print_error, print_success + from ..state import StateManager config_path = Path(ctx.config_file) if not config_path.exists(): @@ -94,10 +93,10 @@ def stop_cmd(ctx, services: tuple, timeout: int, force: bool): pyserve stop api admin # Stop multiple services pyserve stop api --force # Force stop """ + from ...config import Config + from .._runner import ServiceRunner from ..output import console, print_error, print_success from ..state import StateManager - from .._runner import ServiceRunner - from ...config import Config config_path = Path(ctx.config_file) config = Config.from_yaml(str(config_path)) if config_path.exists() else Config() @@ -150,10 +149,10 @@ def restart_cmd(ctx, services: tuple, timeout: int): pyserve restart api # Restart api service pyserve restart api admin # Restart multiple services """ + from ...config import Config + from .._runner import ServiceRunner from ..output import console, print_error, print_success from ..state import StateManager - from .._runner import ServiceRunner - from ...config import Config config_path = Path(ctx.config_file) if not config_path.exists(): diff --git a/pyserve/ctl/commands/status.py b/pyserve/ctl/commands/status.py index 0ff0ee0..15589f5 100644 --- a/pyserve/ctl/commands/status.py +++ b/pyserve/ctl/commands/status.py @@ -78,17 +78,11 @@ def ps_cmd( all_services = {k: v for k, v in all_services.items() if k in services} if filter_status: - all_services = { - k: v for k, v in all_services.items() if v.state.lower() == filter_status.lower() - } + all_services = {k: v for k, v in all_services.items() if v.state.lower() == filter_status.lower()} if not show_all: # By default, show only running/starting/failed services - all_services = { - k: v - for k, v in all_services.items() - if v.state.lower() in ("running", "starting", "stopping", "failed", "restarting") - } + all_services = {k: v for k, v in all_services.items() if v.state.lower() in ("running", "starting", "stopping", "failed", "restarting")} if not all_services: if daemon_running: diff --git a/pyserve/ctl/commands/top.py b/pyserve/ctl/commands/top.py index a26a850..9a5059c 100644 --- a/pyserve/ctl/commands/top.py +++ b/pyserve/ctl/commands/top.py @@ -65,12 +65,13 @@ async def _run_dashboard( refresh_interval: float, no_color: bool, ): - from rich.live import Live - from rich.table import Table - from rich.panel import Panel from rich.layout import Layout + from rich.live import Live + from rich.panel import Panel + from rich.table import Table from rich.text import Text - from ..output import console, format_uptime, format_bytes + + from ..output import console, format_bytes, format_uptime try: import psutil diff --git a/pyserve/ctl/commands/up.py b/pyserve/ctl/commands/up.py index 771c366..26b0063 100644 --- a/pyserve/ctl/commands/up.py +++ b/pyserve/ctl/commands/up.py @@ -7,7 +7,6 @@ import signal import sys import time from pathlib import Path -from typing import List, Optional import click @@ -78,9 +77,9 @@ def up_cmd( pyserve up --scale api=4 # Scale api to 4 workers pyserve up --wait # Wait for healthy status """ + from .._runner import ServiceRunner from ..output import console, print_error, print_info, print_success, print_warning from ..state import StateManager - from .._runner import ServiceRunner config_path = Path(ctx.config_file) @@ -116,6 +115,7 @@ def up_cmd( try: import os from typing import cast + # FIXME: Please fix the cast usage here os.kill(cast(int, daemon_pid), signal.SIGTERM) time.sleep(2) diff --git a/pyserve/ctl/main.py b/pyserve/ctl/main.py index 1579026..9acdf76 100644 --- a/pyserve/ctl/main.py +++ b/pyserve/ctl/main.py @@ -11,6 +11,21 @@ from typing import Optional import click +from .commands import ( + config_cmd, + down_cmd, + health_cmd, + init_cmd, + logs_cmd, + ps_cmd, + restart_cmd, + scale_cmd, + start_cmd, + stop_cmd, + top_cmd, + up_cmd, +) + from .. import __version__ DEFAULT_CONFIG = "config.yaml" @@ -113,21 +128,6 @@ def cli(ctx, config_file: str, project: Optional[str], verbose: bool, debug: boo click.echo(ctx.get_help()) -from .commands import ( - config_cmd, - down_cmd, - health_cmd, - init_cmd, - logs_cmd, - ps_cmd, - restart_cmd, - scale_cmd, - start_cmd, - stop_cmd, - top_cmd, - up_cmd, -) - cli.add_command(init_cmd, name="init") cli.add_command(config_cmd, name="config") cli.add_command(up_cmd, name="up") diff --git a/pyserve/ctl/output/__init__.py b/pyserve/ctl/output/__init__.py index 72faafb..3b4a2a7 100644 --- a/pyserve/ctl/output/__init__.py +++ b/pyserve/ctl/output/__init__.py @@ -35,9 +35,11 @@ def print_warning(message: str) -> None: def print_success(message: str) -> None: console.print(f"[success] {message}[/success]") + def print_info(message: str) -> None: console.print(f"[info] {message}[/info]") + def create_services_table() -> Table: table = Table( title=None, diff --git a/pyserve/ctl/state/__init__.py b/pyserve/ctl/state/__init__.py index 49f541d..8a923ea 100644 --- a/pyserve/ctl/state/__init__.py +++ b/pyserve/ctl/state/__init__.py @@ -9,9 +9,7 @@ import os import time from dataclasses import asdict, dataclass, field from pathlib import Path -from typing import Any, Dict, List, Optional - -import yaml +from typing import Any, Dict, Optional @dataclass