Add Ruff formatting and pre-commit hooks

Configure Ruff as the code formatter and linter with pre-commit hooks.
  Applied automatic formatting fixes across the entire codebase including:
  - Import sorting and organization
  - Code style consistency (spacing, line breaks, indentation)
  - String quote normalization
  - Removal of trailing whitespace and unnecessary blank lines
This commit is contained in:
Joel Krauska committed 2025-10-03 20:38:37 -07:00
1 parent aa9922e7fa
commit e25ff22127
15 files changed
+449 -365

No files matched your search

+26 -15
View File
@@ -1,15 +1,15 @@
import argparse
import threading
import subprocess
import logging
import os
import signal
import subprocess
import sys
import threading
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(filename)s:%(lineno)d [pid:%(process)d] %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S"
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger(__name__)
@@ -18,6 +18,7 @@ logger = logging.getLogger(__name__)
running_processes = []
pid_files = []
def cleanup_pid_file(pid_file):
"""Remove a PID file if it exists"""
if os.path.exists(pid_file):
@@ -27,10 +28,11 @@ def cleanup_pid_file(pid_file):
except Exception as e:
logger.error(f"Error removing PID file {pid_file}: {e}")
def signal_handler(sig, frame):
"""Handle Ctrl-C gracefully"""
logger.info("Received interrupt signal (Ctrl-C), shutting down gracefully...")
# Terminate all running processes
for process in running_processes:
if process and process.poll() is None: # Process is still running
@@ -47,14 +49,15 @@ def signal_handler(sig, frame):
process.wait()
except Exception as e:
logger.error(f"Error terminating process PID {process.pid}: {e}")
# Clean up PID files
for pid_file in pid_files:
cleanup_pid_file(pid_file)
logger.info("Shutdown complete")
sys.exit(0)
# Run python in subprocess
def run_script(script_name, pid_file, *args):
process = None
@@ -67,31 +70,34 @@ def run_script(script_name, pid_file, *args):
# Run the subprocess (output goes directly to console for real-time viewing)
process = subprocess.Popen(command)
# Track the process globally
running_processes.append(process)
# Write PID to file
with open(pid_file, 'w') as f:
f.write(str(process.pid))
logger.info(f"Started {script_name} with PID {process.pid}, written to {pid_file}")
# Wait for the process to complete
process.wait()
except Exception as e:
logger.error(f"Error running {script_name}: {e}")
finally:
# Clean up PID file when process exits
cleanup_pid_file(pid_file)
# Parse runtime argument (--config) and start subprocess threads
def main():
# Register signal handler for Ctrl-C
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
parser = argparse.ArgumentParser(description="Helper script to run the database and web frontend in separate threads.")
parser = argparse.ArgumentParser(
description="Helper script to run the database and web frontend in separate threads."
)
# Add --config runtime argument
parser.add_argument('--config', help="Path to the configuration file.", default='config.ini')
@@ -100,16 +106,20 @@ def main():
# PID file paths
db_pid_file = 'meshview-db.pid'
web_pid_file = 'meshview-web.pid'
# Track PID files globally for cleanup
pid_files.append(db_pid_file)
pid_files.append(web_pid_file)
# Database Thread
dbthrd = threading.Thread(target=run_script, args=('startdb.py', db_pid_file, '--config', args.config))
dbthrd = threading.Thread(
target=run_script, args=('startdb.py', db_pid_file, '--config', args.config)
)
# Web server thread
webthrd = threading.Thread(target=run_script, args=('main.py', web_pid_file, '--config', args.config))
webthrd = threading.Thread(
target=run_script, args=('main.py', web_pid_file, '--config', args.config)
)
# Start Meshview subprocess threads
logger.info(f"Starting Meshview with config: {args.config}")
@@ -125,5 +135,6 @@ def main():
# This shouldn't be reached due to signal handler, but just in case
signal_handler(signal.SIGINT, None)
if __name__ == '__main__':
main()