#!/usr/bin/env python3 """ Automation Runner Main orchestrator that runs all automation tasks: - File watching - Scheduled Google Sheets sync - Initial conversion """ import json import time import signal import sys import threading import subprocess from pathlib import Path from datetime import datetime # Paths BASE_DIR = Path(__file__).parent.parent CONFIG_FILE = BASE_DIR / "config.json" SHEETS_SYNC_SCRIPT = BASE_DIR / "scripts" / "sheets_sync.py" FILE_WATCHER_SCRIPT = BASE_DIR / "scripts" / "file_watcher.py" CSV_TO_JSON_SCRIPT = BASE_DIR / "scripts" / "csv_to_json.py" class AutomationRunner: """Manages all automation tasks""" def __init__(self): self.config = self.load_config() self.running = True self.watcher_process = None self.sync_thread = None # Set up signal handlers for graceful shutdown signal.signal(signal.SIGINT, self.signal_handler) signal.signal(signal.SIGTERM, self.signal_handler) def signal_handler(self, sig, frame): """Handle shutdown signals""" print("\n\n๐Ÿ›‘ Shutdown signal received...") self.running = False def load_config(self): """Load configuration""" if not CONFIG_FILE.exists(): print(f"โŒ Config file not found: {CONFIG_FILE}") sys.exit(1) with open(CONFIG_FILE, 'r') as f: return json.load(f) def run_initial_conversion(self): """Run initial CSV-to-JSON conversion""" print("๐Ÿ”„ Running initial CSV-to-JSON conversion...") try: subprocess.run([sys.executable, str(CSV_TO_JSON_SCRIPT)], check=True) except subprocess.CalledProcessError: print("โš ๏ธ Initial conversion failed (this is OK if CSV files don't exist yet)") def start_file_watcher(self): """Start the file watcher in a separate process""" if not self.config.get('file_watcher', {}).get('enabled', True): print("โš ๏ธ File watcher disabled in config") return print("๐Ÿ‘€ Starting file watcher...") self.watcher_process = subprocess.Popen( [sys.executable, str(FILE_WATCHER_SCRIPT)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1 ) # Stream output in a separate thread def stream_output(): for line in self.watcher_process.stdout: print(line, end='') threading.Thread(target=stream_output, daemon=True).start() def sync_sheets_once(self): """Run Google Sheets sync once""" print(f"\nโฐ [{datetime.now().strftime('%H:%M:%S')}] Running Google Sheets sync...") try: result = subprocess.run( [sys.executable, str(SHEETS_SYNC_SCRIPT)], capture_output=True, text=True ) print(result.stdout) if result.returncode != 0: print(result.stderr) except Exception as e: print(f"โŒ Sync error: {e}") def start_scheduled_sync(self): """Start scheduled Google Sheets sync""" sheets_config = self.config.get('google_sheets', {}) if not sheets_config.get('enabled', False): print("โš ๏ธ Google Sheets sync disabled in config") return interval_minutes = sheets_config.get('sync_interval_minutes', 5) print(f"โฐ Starting scheduled sync (every {interval_minutes} minutes)...") def sync_loop(): # Run initial sync self.sync_sheets_once() # Continue syncing at intervals while self.running: time.sleep(interval_minutes * 60) if self.running: self.sync_sheets_once() self.sync_thread = threading.Thread(target=sync_loop, daemon=True) self.sync_thread.start() def run(self): """Main run loop""" print("=" * 60) print("๐Ÿš€ Dialect Map Automation System") print("=" * 60) print() # Run initial conversion self.run_initial_conversion() print() # Start file watcher self.start_file_watcher() # Start scheduled sync self.start_scheduled_sync() print() print("=" * 60) print("โœ… All automation tasks started!") print("=" * 60) print("\n๐Ÿ’ก Press Ctrl+C to stop all automation\n") # Keep running until stopped try: while self.running: time.sleep(1) except KeyboardInterrupt: pass # Cleanup print("\n๐Ÿงน Cleaning up...") if self.watcher_process: self.watcher_process.terminate() self.watcher_process.wait() print("โœ… Automation stopped gracefully\n") def main(): """Main entry point""" runner = AutomationRunner() runner.run() if __name__ == "__main__": main()