fixed flake8 lint errors

This commit is contained in:
Илья Глазунов 2025-12-04 03:06:58 +03:00
parent cec6e927a7
commit 7662a7924a
17 changed files with 62 additions and 84 deletions

View File

@ -2,7 +2,7 @@
PyServe - HTTP web server written on Python PyServe - HTTP web server written on Python
""" """
__version__ = "0.9.10" __version__ = "0.10.0"
__author__ = "Ilya Glazunov" __author__ = "Ilya Glazunov"
from .asgi_mount import ( from .asgi_mount import (

View File

@ -19,7 +19,8 @@ def main() -> None:
epilog="For service management (start/stop/restart/logs), use: pyservectl", epilog="For service management (start/stop/restart/logs), use: pyservectl",
) )
parser.add_argument( parser.add_argument(
"-c", "--config", "-c",
"--config",
default="config.yaml", default="config.yaml",
help="Path to configuration file (default: config.yaml)", help="Path to configuration file (default: config.yaml)",
) )

View File

@ -88,6 +88,4 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
import os
main() main()

View File

@ -7,16 +7,14 @@ Integrates with ProcessManager for actual process management.
import asyncio import asyncio
import os import os
import signal
import sys import sys
import time import time
from dataclasses import dataclass, field from dataclasses import dataclass, field
from pathlib import Path from typing import Dict, List, Optional
from typing import Any, Dict, List, Optional
from ..config import Config from ..config import Config
from ..process_manager import ProcessConfig, ProcessInfo, ProcessManager, ProcessState from ..process_manager import ProcessConfig, ProcessManager, ProcessState
from .state import ServiceState, StateManager from .state import StateManager
@dataclass @dataclass

View File

@ -190,7 +190,6 @@ def show_cmd(ctx, config_file: Optional[str], output_format: str, section: Optio
console.print(syntax) console.print(syntax)
elif output_format == "table": elif output_format == "table":
from rich.table import Table
from rich.tree import Tree from rich.tree import Tree
def build_tree(data, tree): def build_tree(data, tree):

View File

@ -5,7 +5,7 @@ pyserve down - Stop all services
import signal import signal
import time import time
from pathlib import Path from pathlib import Path
from typing import Optional, cast from typing import cast
import click import click
@ -56,6 +56,7 @@ def down_cmd(
try: try:
import os import os
# FIXME: Please fix the cast usage here # FIXME: Please fix the cast usage here
os.kill(cast(int, daemon_pid), signal.SIGTERM) os.kill(cast(int, daemon_pid), signal.SIGTERM)
@ -93,8 +94,8 @@ def down_cmd(
console.print("[bold]Stopping services...[/bold]") console.print("[bold]Stopping services...[/bold]")
from .._runner import ServiceRunner
from ...config import Config from ...config import Config
from .._runner import ServiceRunner
config_path = Path(ctx.config_file) config_path = Path(ctx.config_file)
if config_path.exists(): if config_path.exists():

View File

@ -4,7 +4,6 @@ pyserve health - Check health of services
import asyncio import asyncio
from pathlib import Path from pathlib import Path
from typing import Optional
import click import click
@ -60,6 +59,7 @@ def health_cmd(ctx, services: tuple, timeout: int, output_format: str):
return return
from rich.table import Table from rich.table import Table
from ..output import format_health from ..output import format_health
table = Table(show_header=True, header_style="bold") table = Table(show_header=True, header_style="bold")

View File

@ -3,7 +3,6 @@ pyserve init - Initialize a new pyserve project
""" """
from pathlib import Path from pathlib import Path
from typing import Optional
import click import click
@ -119,7 +118,7 @@ extensions:
port_range: [9000, 9999] port_range: [9000, 9999]
health_check_enabled: true health_check_enabled: true
proxy_timeout: 60.0 proxy_timeout: 60.0
apps: apps:
# Example: FastAPI application # Example: FastAPI application
- name: api - name: api
@ -136,7 +135,7 @@ extensions:
strip_path: true strip_path: true
env: env:
APP_ENV: "production" APP_ENV: "production"
# Example: Flask application (WSGI) # Example: Flask application (WSGI)
# - name: admin # - name: admin
# path: /admin # path: /admin
@ -191,7 +190,7 @@ extensions:
- path: /api - path: /api
app: myapp.api:app app: myapp.api:app
# factory: false # Set to true if app is a factory function # factory: false # Set to true if app is a factory function
# Starlette app mounted at /web # Starlette app mounted at /web
# - path: /web # - path: /web
# app: myapp.web:app # app: myapp.web:app
@ -257,13 +256,13 @@ logging:
format: format:
type: standard type: standard
use_colors: false use_colors: false
# JSON logs for log aggregation # JSON logs for log aggregation
- path: ./logs/pyserve.json - path: ./logs/pyserve.json
level: INFO level: INFO
format: format:
type: json type: json
# Access logs # Access logs
- path: ./logs/access.log - path: ./logs/access.log
level: INFO level: INFO
@ -278,7 +277,7 @@ extensions:
port_range: [9000, 9999] port_range: [9000, 9999]
health_check_enabled: true health_check_enabled: true
proxy_timeout: 60.0 proxy_timeout: 60.0
apps: apps:
- name: api - name: api
path: /api path: /api
@ -384,7 +383,7 @@ def init_cmd(
pyserve init -t full # All features pyserve init -t full # All features
pyserve init -o production.yaml # Custom output file 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: if list_templates:
console.print("\n[bold]Available Templates:[/bold]\n") console.print("\n[bold]Available Templates:[/bold]\n")

View File

@ -3,7 +3,6 @@ pyserve logs - View service logs
""" """
import asyncio import asyncio
import sys
import time import time
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
@ -81,15 +80,13 @@ def logs_cmd(
pyserve logs --tail 50 # Last 50 lines pyserve logs --tail 50 # Last 50 lines
pyserve logs --since "10m" # Logs from last 10 minutes 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 from ..state import StateManager
state_manager = StateManager(Path(".pyserve"), ctx.project) state_manager = StateManager(Path(".pyserve"), ctx.project)
if services: if services:
log_files = [ log_files = [(name, state_manager.get_service_log_file(name)) for name in services]
(name, state_manager.get_service_log_file(name)) for name in services
]
else: else:
all_services = state_manager.get_all_services() all_services = state_manager.get_all_services()
if not all_services: if not all_services:
@ -100,10 +97,7 @@ def logs_cmd(
print_info("No logs available. Start services with 'pyserve up'") print_info("No logs available. Start services with 'pyserve up'")
return return
else: else:
log_files = [ log_files = [(name, state_manager.get_service_log_file(name)) for name in all_services]
(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()] 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 until_timestamp = _parse_time(until_time) if until_time else None
colors = ["cyan", "green", "yellow", "blue", "magenta"] colors = ["cyan", "green", "yellow", "blue", "magenta"]
service_colors = { service_colors = {name: colors[i % len(colors)] for i, (name, _) in enumerate(existing_logs)}
name: colors[i % len(colors)] for i, (name, _) in enumerate(existing_logs)
}
if follow: if follow:
asyncio.run( asyncio.run(
@ -144,7 +136,7 @@ def logs_cmd(
def _parse_time(time_str: str) -> Optional[float]: def _parse_time(time_str: str) -> Optional[float]:
import re import re
from datetime import datetime, timedelta from datetime import datetime
# Relative time (e.g., "10m", "1h", "2d") # Relative time (e.g., "10m", "1h", "2d")
match = re.match(r"^(\d+)([smhd])$", time_str) match = re.match(r"^(\d+)([smhd])$", time_str)
@ -182,10 +174,10 @@ def _read_logs(
no_color: bool, no_color: bool,
filter_pattern: Optional[str], filter_pattern: Optional[str],
): ):
from ..output import console
import re import re
from ..output import console
all_lines = [] all_lines = []
for service_name, log_path in log_files: for service_name, log_path in log_files:
@ -210,9 +202,7 @@ def _read_logs(
try: try:
from datetime import datetime from datetime import datetime
line_time = datetime.fromisoformat( line_time = datetime.fromisoformat(timestamp_match.group(1).replace(" ", "T")).timestamp()
timestamp_match.group(1).replace(" ", "T")
).timestamp()
except ValueError: except ValueError:
pass pass

View File

@ -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 # Scale api to 4 workers
pyserve scale api=4 admin=2 # Scale multiple services 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 ..output import console, print_error, print_info, print_success
from ..state import StateManager from ..state import StateManager
from .._runner import ServiceRunner
from ...config import Config
scale_map = {} scale_map = {}
for scale in scales: 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") print_info(f"Scaling {service}: {current}{workers} workers")
try: try:
success = await runner.scale_service( success = await runner.scale_service(service, workers, timeout=timeout, wait=not no_wait)
service, workers, timeout=timeout, wait=not no_wait
)
if success: if success:
print_success(f"Scaled {service} to {workers} workers") print_success(f"Scaled {service} to {workers} workers")
else: else:

View File

@ -4,7 +4,6 @@ pyserve start/stop/restart - Service management commands
import asyncio import asyncio
from pathlib import Path from pathlib import Path
from typing import List
import click import click
@ -28,10 +27,10 @@ def start_cmd(ctx, services: tuple, timeout: int):
pyserve start api # Start api service pyserve start api # Start api service
pyserve start api admin # Start multiple services 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 ...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) config_path = Path(ctx.config_file)
if not config_path.exists(): 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 admin # Stop multiple services
pyserve stop api --force # Force stop pyserve stop api --force # Force stop
""" """
from ...config import Config
from .._runner import ServiceRunner
from ..output import console, print_error, print_success from ..output import console, print_error, print_success
from ..state import StateManager from ..state import StateManager
from .._runner import ServiceRunner
from ...config import Config
config_path = Path(ctx.config_file) config_path = Path(ctx.config_file)
config = Config.from_yaml(str(config_path)) if config_path.exists() else Config() 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 # Restart api service
pyserve restart api admin # Restart multiple services pyserve restart api admin # Restart multiple services
""" """
from ...config import Config
from .._runner import ServiceRunner
from ..output import console, print_error, print_success from ..output import console, print_error, print_success
from ..state import StateManager from ..state import StateManager
from .._runner import ServiceRunner
from ...config import Config
config_path = Path(ctx.config_file) config_path = Path(ctx.config_file)
if not config_path.exists(): if not config_path.exists():

View File

@ -78,17 +78,11 @@ def ps_cmd(
all_services = {k: v for k, v in all_services.items() if k in services} all_services = {k: v for k, v in all_services.items() if k in services}
if filter_status: if filter_status:
all_services = { all_services = {k: v for k, v in all_services.items() if v.state.lower() == filter_status.lower()}
k: v for k, v in all_services.items() if v.state.lower() == filter_status.lower()
}
if not show_all: if not show_all:
# By default, show only running/starting/failed services # By default, show only running/starting/failed services
all_services = { all_services = {k: v for k, v in all_services.items() if v.state.lower() in ("running", "starting", "stopping", "failed", "restarting")}
k: v
for k, v in all_services.items()
if v.state.lower() in ("running", "starting", "stopping", "failed", "restarting")
}
if not all_services: if not all_services:
if daemon_running: if daemon_running:

View File

@ -65,12 +65,13 @@ async def _run_dashboard(
refresh_interval: float, refresh_interval: float,
no_color: bool, 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.layout import Layout
from rich.live import Live
from rich.panel import Panel
from rich.table import Table
from rich.text import Text from rich.text import Text
from ..output import console, format_uptime, format_bytes
from ..output import console, format_bytes, format_uptime
try: try:
import psutil import psutil

View File

@ -7,7 +7,6 @@ import signal
import sys import sys
import time import time
from pathlib import Path from pathlib import Path
from typing import List, Optional
import click import click
@ -78,9 +77,9 @@ def up_cmd(
pyserve up --scale api=4 # Scale api to 4 workers pyserve up --scale api=4 # Scale api to 4 workers
pyserve up --wait # Wait for healthy status pyserve up --wait # Wait for healthy status
""" """
from .._runner import ServiceRunner
from ..output import console, print_error, print_info, print_success, print_warning from ..output import console, print_error, print_info, print_success, print_warning
from ..state import StateManager from ..state import StateManager
from .._runner import ServiceRunner
config_path = Path(ctx.config_file) config_path = Path(ctx.config_file)
@ -116,6 +115,7 @@ def up_cmd(
try: try:
import os import os
from typing import cast from typing import cast
# FIXME: Please fix the cast usage here # FIXME: Please fix the cast usage here
os.kill(cast(int, daemon_pid), signal.SIGTERM) os.kill(cast(int, daemon_pid), signal.SIGTERM)
time.sleep(2) time.sleep(2)

View File

@ -11,6 +11,21 @@ from typing import Optional
import click 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__ from .. import __version__
DEFAULT_CONFIG = "config.yaml" 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()) 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(init_cmd, name="init")
cli.add_command(config_cmd, name="config") cli.add_command(config_cmd, name="config")
cli.add_command(up_cmd, name="up") cli.add_command(up_cmd, name="up")

View File

@ -35,9 +35,11 @@ def print_warning(message: str) -> None:
def print_success(message: str) -> None: def print_success(message: str) -> None:
console.print(f"[success] {message}[/success]") console.print(f"[success] {message}[/success]")
def print_info(message: str) -> None: def print_info(message: str) -> None:
console.print(f"[info] {message}[/info]") console.print(f"[info] {message}[/info]")
def create_services_table() -> Table: def create_services_table() -> Table:
table = Table( table = Table(
title=None, title=None,

View File

@ -9,9 +9,7 @@ import os
import time import time
from dataclasses import asdict, dataclass, field from dataclasses import asdict, dataclass, field
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Optional from typing import Any, Dict, Optional
import yaml
@dataclass @dataclass