From 38821b682efdb08815d9e6515d797c269518dadc Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Sat, 14 Jun 2025 17:05:59 -0400 Subject: [PATCH] Use logging module instead of print --- awam.desktop => aw-watcher-marvin.desktop | 6 ++--- src/aw-watcher-marvin/__main__.py | 32 ++++++++++++++--------- 2 files changed, 22 insertions(+), 16 deletions(-) rename awam.desktop => aw-watcher-marvin.desktop (67%) diff --git a/awam.desktop b/aw-watcher-marvin.desktop similarity index 67% rename from awam.desktop rename to aw-watcher-marvin.desktop index ad8e31e..8e4429d 100644 --- a/awam.desktop +++ b/aw-watcher-marvin.desktop @@ -2,10 +2,10 @@ Type=Application Name=ActivityWatch Amazing Marvin Watcher Comment=Tracks Amazing Marvin tasks in ActivityWatch -Exec=poetry run awam -Path=/home/felixm/dev/awam +Exec=poetry run aw-watcher-marvin +Path=/home/felixm/dev/aw-watcher-marvin Icon=activitywatch StartupNotify=false NoDisplay=true X-GNOME-Autostart-enabled=true -Categories=System;Monitor; \ No newline at end of file +Categories=System;Monitor; diff --git a/src/aw-watcher-marvin/__main__.py b/src/aw-watcher-marvin/__main__.py index 130bb41..6a487fb 100644 --- a/src/aw-watcher-marvin/__main__.py +++ b/src/aw-watcher-marvin/__main__.py @@ -1,3 +1,4 @@ +import logging import os import sys from datetime import datetime, timezone @@ -22,11 +23,11 @@ def get_tracked_task(api_token: str) -> Optional[Dict[str, Any]]: if response.status_code == 200: return response.json() else: - print(f"No tracked task found (status: {response.status_code})") + logging.debug(f"No tracked task found (status: {response.status_code})") return None except requests.exceptions.RequestException as e: - print(f"Error fetching tracked task: {e}") + logging.error(f"Error fetching tracked task: {e}") return None @@ -38,10 +39,10 @@ def setup_activitywatch_client() -> Tuple[ActivityWatchClient, str]: try: client.create_bucket(bucket_id, event_type=event_type) - print(f"ActivityWatch bucket '{bucket_id}' ready") + logging.info(f"ActivityWatch bucket '{bucket_id}' ready") return client, bucket_id except Exception as e: - print(f"Error setting up ActivityWatch: {e}") + logging.error(f"Error setting up ActivityWatch: {e}") sys.exit(1) @@ -59,22 +60,27 @@ def create_task_event(tracked_task: Dict[str, Any]) -> Event: def main() -> None: + logging.basicConfig( + level=logging.INFO, + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + datefmt="%H:%M:%S", + handlers=[logging.StreamHandler()], + ) + username = os.getlogin() token = keyring.get_password("amazing-marvin-api-token", username) if not token: - print( - "No API token found in keyring. Please store your Amazing Marvin API token:" - ) - print( + logging.error( + f"No API token found in keyring. Please store your Amazing Marvin API token:" f"Run: python -c \"import keyring; keyring.set_password('amazing-marvin-api-token', '{username}', 'YOUR_TOKEN_HERE')\"" ) sys.exit(1) client, bucket_id = setup_activitywatch_client() - print("Starting Amazing Marvin task tracking...") - print("Press Ctrl+C to stop") + logging.info("Starting Amazing Marvin task tracking...") + logging.info("Press Ctrl+C to stop") current_task_id = None heartbeat_interval = 60 @@ -90,7 +96,7 @@ def main() -> None: if task_id != current_task_id: current_task_id = task_id - print(f"Now tracking: {task_title}") + logging.info(f"Now tracking: {task_title}") event = create_task_event(tracked_task) client.heartbeat( @@ -102,12 +108,12 @@ def main() -> None: else: if current_task_id is not None: current_task_id = None - print("No task currently being tracked") + logging.info("No task currently being tracked") sleep(heartbeat_interval) except KeyboardInterrupt: - print("\nStopping Amazing Marvin task tracking...") + logging.info("\nStopping Amazing Marvin task tracking...") sleep(1) # Give time for queued events to be sent