1
Mainline capture script
david edited this page 2026-03-21 07:19:16 +00:00

===== run_upstream_capture2.py =====

#!/usr/bin/env python3
"""Run upstream/main with figment mode and capture output."""

import sys
import os

# Set environment variables BEFORE importing
os.environ['FIGMENT'] = 'True'
os.environ['FIGMENT_INTERVAL'] = '2'

# Add current directory to path for imports
sys.path.insert(0, '/home/david/src/Mainline')

# Import upstream modules
from engine import config

# Override config directly after import
config.FIGMENT = True
config.FIGMENT_INTERVAL = 2  # Show figment every 2 seconds for testing
config.HEADLINE_LIMIT = 5  # Limit headlines for quick test
config.FRAME_DT = 0.05  # 20 FPS

print(f"[Config] FIGMENT={config.FIGMENT}, INTERVAL={config.FIGMENT_INTERVAL}s", file=sys.stderr)

from engine.scroll import stream
from engine.display import NullDisplay
from engine.fetch import fetch_all
from engine.ntfy import NtfyPoller
from engine.mic import MicMonitor

# Create a capture display
class CaptureDisplay:
    def __init__(self, output_file="/tmp/upstream_output2.txt"):
        self.width = 80
        self.height = 24
        self.output_file = output_file
        self.frame_count = 0
        # Clear output file
        with open(output_file, 'w') as f:
            f.write("=== Upstream Figment Capture ===\n\n")
    
    def init(self, width, height):
        self.width, self.height = width, height
        print(f"[Capture] Display init: {width}x{height}", file=sys.stderr)
    
    def show(self, buffer):
        self.frame_count += 1
        
        # Write frame to file
        with open(self.output_file, 'a') as f:
            f.write(f"\n{'='*60}\n")
            f.write(f"FRAME {self.frame_count}\n")
            f.write(f"{'='*60}\n")
            for line in buffer:
                f.write(line + '\n')
        
        # Print to stderr periodically
        if self.frame_count % 10 == 0:
            print(f"[Capture] Frame {self.frame_count}: {len(buffer)} lines", 
                  file=sys.stderr)
    
    def clear(self):
        pass
    
    def cleanup(self):
        print(f"[Capture] Done. Captured {self.frame_count} frames to {self.output_file}", 
              file=sys.stderr)

print("Starting upstream/main with figment mode...", file=sys.stderr)

# Fetch headlines
print("Fetching headlines...", file=sys.stderr)
items, linked, failed = fetch_all()
print(f"Fetched {len(items)} items ({linked} linked, {failed} failed)", file=sys.stderr)

# Create display
display = CaptureDisplay("/tmp/upstream_output2.txt")

# Create mock ntfy and mic
class MockNtfy:
    def get_active_message(self): return None

class MockMic:
    excess = 0.0
    available = False

ntfy = MockNtfy()
mic = MockMic()

print("Starting stream with figment enabled...", file=sys.stderr)

# Run stream
try:
    stream(items, ntfy, mic, display)
except KeyboardInterrupt:
    print("\n[Capture] Interrupted by user", file=sys.stderr)
except Exception as e:
    print(f"\n[Error] Stream failed: {e}", file=sys.stderr)
    import traceback
    traceback.print_exc()

print(f"\n[Capture] Complete. Output saved to {display.output_file}", file=sys.stderr)