From 422c3a720aecb6503bdcd6c13d188013c645ba36 Mon Sep 17 00:00:00 2001 From: BelKed <66956532+BelKed@users.noreply.github.com> Date: Mon, 24 Feb 2025 14:03:09 +0100 Subject: [PATCH 1/4] feat: add port option --- aw_notify/main.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/aw_notify/main.py b/aw_notify/main.py index 44dce5a..38721a8 100644 --- a/aw_notify/main.py +++ b/aw_notify/main.py @@ -325,7 +325,8 @@ def init_macos(): @click.pass_context @click.option("-v", "--verbose", is_flag=True, help="Verbose logging.") @click.option("--testing", is_flag=True, help="Enables testing mode.") -def main(ctx, verbose: bool, testing: bool): +@click.option("--port", type=int, default=5600, help="Port to connect to ActivityWatch server.") +def main(ctx, verbose: bool, testing: bool, port: int): setup_logging("aw-notify", testing=testing, verbose=verbose, log_file=True) logging.getLogger("urllib3").setLevel(logging.WARNING) logger.info("Starting...") @@ -334,15 +335,16 @@ def main(ctx, verbose: bool, testing: bool): init_macos() if ctx.invoked_subcommand is None: - ctx.invoke(start, testing=testing) + ctx.invoke(start, testing=testing, port=port) @main.command() @click.option("--testing", is_flag=True, help="Enables testing mode.") -def start(testing=False): +@click.option("--port", type=int, default=5600, help="Port to connect to ActivityWatch server.") +def start(testing=False, port=5600): """Start the notification service.""" global aw, hostname - aw = aw_client.ActivityWatchClient("aw-notify", testing=testing) + aw = aw_client.ActivityWatchClient("aw-notify", testing=testing, port=port) aw.wait_for_start() hostname = aw.get_info().get("hostname", "unknown") From dbb5668f3686b49e894f6796e739a569e95023f0 Mon Sep 17 00:00:00 2001 From: BelKed <66956532+BelKed@users.noreply.github.com> Date: Mon, 24 Feb 2025 14:12:58 +0100 Subject: [PATCH 2/4] fix: use port 5666 when testing --- aw_notify/main.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/aw_notify/main.py b/aw_notify/main.py index 38721a8..d9a1fb0 100644 --- a/aw_notify/main.py +++ b/aw_notify/main.py @@ -325,12 +325,15 @@ def init_macos(): @click.pass_context @click.option("-v", "--verbose", is_flag=True, help="Verbose logging.") @click.option("--testing", is_flag=True, help="Enables testing mode.") -@click.option("--port", type=int, default=5600, help="Port to connect to ActivityWatch server.") +@click.option("--port", type=int, default=None, help="Port to connect to ActivityWatch server.") def main(ctx, verbose: bool, testing: bool, port: int): setup_logging("aw-notify", testing=testing, verbose=verbose, log_file=True) logging.getLogger("urllib3").setLevel(logging.WARNING) logger.info("Starting...") + if port is None: + port = 5666 if testing else 5600 + if sys.platform == "darwin": init_macos() @@ -340,10 +343,14 @@ def main(ctx, verbose: bool, testing: bool, port: int): @main.command() @click.option("--testing", is_flag=True, help="Enables testing mode.") -@click.option("--port", type=int, default=5600, help="Port to connect to ActivityWatch server.") -def start(testing=False, port=5600): +@click.option("--port", type=int, default=None, help="Port to connect to ActivityWatch server.") +def start(testing=False, port=None): """Start the notification service.""" global aw, hostname + + if port is None: + port = 5666 if testing else 5600 + aw = aw_client.ActivityWatchClient("aw-notify", testing=testing, port=port) aw.wait_for_start() hostname = aw.get_info().get("hostname", "unknown") From daa59fe7aee188f8fc3843c1ddc6e5f8d4971631 Mon Sep 17 00:00:00 2001 From: BelKed <66956532+BelKed@users.noreply.github.com> Date: Mon, 24 Feb 2025 14:22:45 +0100 Subject: [PATCH 3/4] refactor: simplify port handling in main and start functions --- aw_notify/main.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/aw_notify/main.py b/aw_notify/main.py index d9a1fb0..56515fe 100644 --- a/aw_notify/main.py +++ b/aw_notify/main.py @@ -326,14 +326,11 @@ def init_macos(): @click.option("-v", "--verbose", is_flag=True, help="Verbose logging.") @click.option("--testing", is_flag=True, help="Enables testing mode.") @click.option("--port", type=int, default=None, help="Port to connect to ActivityWatch server.") -def main(ctx, verbose: bool, testing: bool, port: int): +def main(ctx, verbose: bool, testing: bool, port: Optional[int]): setup_logging("aw-notify", testing=testing, verbose=verbose, log_file=True) logging.getLogger("urllib3").setLevel(logging.WARNING) logger.info("Starting...") - if port is None: - port = 5666 if testing else 5600 - if sys.platform == "darwin": init_macos() @@ -348,9 +345,6 @@ def start(testing=False, port=None): """Start the notification service.""" global aw, hostname - if port is None: - port = 5666 if testing else 5600 - aw = aw_client.ActivityWatchClient("aw-notify", testing=testing, port=port) aw.wait_for_start() hostname = aw.get_info().get("hostname", "unknown") From d8e96c50a95a3946a241bc868cff1dd07be1a995 Mon Sep 17 00:00:00 2001 From: BelKed <66956532+BelKed@users.noreply.github.com> Date: Mon, 24 Feb 2025 14:39:45 +0100 Subject: [PATCH 4/4] feat: add common options for testing and port configuration in CLI --- aw_notify/main.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/aw_notify/main.py b/aw_notify/main.py index 56515fe..4351d82 100644 --- a/aw_notify/main.py +++ b/aw_notify/main.py @@ -321,12 +321,33 @@ def init_macos(): NSBundle.mainBundle.bundleIdentifier = "net.activitywatch.ActivityWatch" +def common_options(func): + """Common click options used across commands.""" + options = [ + click.option("--testing", is_flag=True, help="Enables testing mode."), + click.option( + "--port", + type=int, + default=None, + help="Port to connect to ActivityWatch server (default: 5600, or 5666 for testing).", + ), + ] + for option in reversed(options): + func = option(func) + return func + + @click.group(invoke_without_command=True) @click.pass_context @click.option("-v", "--verbose", is_flag=True, help="Verbose logging.") -@click.option("--testing", is_flag=True, help="Enables testing mode.") -@click.option("--port", type=int, default=None, help="Port to connect to ActivityWatch server.") +@common_options def main(ctx, verbose: bool, testing: bool, port: Optional[int]): + """ + ActivityWatch notification service. + + Sends notifications based on computer usage data from ActivityWatch. + Can connect to a custom ActivityWatch server port (default: 5600, or 5666 for testing). + """ setup_logging("aw-notify", testing=testing, verbose=verbose, log_file=True) logging.getLogger("urllib3").setLevel(logging.WARNING) logger.info("Starting...") @@ -339,8 +360,7 @@ def main(ctx, verbose: bool, testing: bool, port: Optional[int]): @main.command() -@click.option("--testing", is_flag=True, help="Enables testing mode.") -@click.option("--port", type=int, default=None, help="Port to connect to ActivityWatch server.") +@common_options def start(testing=False, port=None): """Start the notification service.""" global aw, hostname