-
-
Notifications
You must be signed in to change notification settings - Fork 29
feat: add toml config and arg parsing for poll time #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,11 @@ | ||
| from aw_watcher_input.main import main | ||
| from aw_watcher_input.main import main as _main | ||
| from aw_watcher_input.config import parse_args | ||
|
|
||
| def main() -> None: | ||
| args = parse_args() | ||
|
|
||
| # Start watcher | ||
| _main(args, testing=args.testing) | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import argparse | ||
| import sys | ||
|
|
||
| from aw_core.config import load_config_toml | ||
|
|
||
| default_config = """ | ||
| [aw-watcher-input] | ||
| poll_time = 5 | ||
| [aw-watcher-input-testing] | ||
| poll_time = 1 | ||
| """.strip() | ||
|
|
||
|
|
||
| def load_config(testing: bool): | ||
| section = "aw-watcher-input" + ("-testing" if testing else "") | ||
| return load_config_toml("aw-watcher-input", default_config)[section] | ||
|
|
||
|
|
||
| def parse_args(): | ||
| # get testing in a dirty way, because we need it for the config lookup | ||
| testing = "--testing" in sys.argv | ||
| config = load_config(testing) | ||
|
|
||
| default_poll_time = config["poll_time"] | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| description="A watcher for keyboard and mouse input." | ||
| ) | ||
| parser.add_argument( | ||
| "--testing", dest="testing", action="store_true", help="run in testing mode" | ||
| ) | ||
| parser.add_argument( | ||
| "--poll-time", dest="poll_time", type=float, default=default_poll_time | ||
| ) | ||
| parsed_args = parser.parse_args() | ||
| return parsed_args | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,26 +3,21 @@ | |
| from time import sleep | ||
|
|
||
| import aw_client | ||
| import click | ||
| from aw_core import Event | ||
| from aw_watcher_afk.listeners import KeyboardListener, MouseListener | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.option("--testing", is_flag=True) | ||
| def main(testing: bool): | ||
| def main(args, testing: bool): | ||
|
Comment on lines
-12
to
+11
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't remove click. Rewrite to use it instead. @ellipsis-dev try to do this for us. Edit: Ellipsis won't, but should be trivial to get ChatGPT etc to rewrite this if you're not familiar.
This comment was marked as resolved.
Sorry, something went wrong.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no worries. wasnt sure what was the new and what was the old |
||
| logging.basicConfig(level=logging.INFO) | ||
| logger.info("Starting watcher...") | ||
| logger.info("aw_watcher_input started" + (" in testing mode" if testing else "")) | ||
| client = aw_client.ActivityWatchClient("aw-watcher-input", testing=testing) | ||
| client.connect() | ||
|
|
||
| # Create bucjet | ||
| # Create bucket | ||
| bucket_name = "{}_{}".format(client.client_name, client.client_hostname) | ||
| eventtype = "os.hid.input" | ||
| client.create_bucket(bucket_name, eventtype, queued=False) | ||
| poll_time = 5 | ||
| client.create_bucket(bucket_name, eventtype) | ||
|
|
||
| keyboard = KeyboardListener() | ||
| keyboard.start() | ||
|
|
@@ -35,9 +30,9 @@ def main(testing: bool): | |
| last_run = now | ||
|
|
||
| # we want to ensure that the polling happens with a predictable cadence | ||
| time_to_sleep = poll_time - datetime.now().timestamp() % poll_time | ||
| time_to_sleep = args.poll_time - datetime.now().timestamp() % args.poll_time | ||
| # ensure that the sleep time is between 0 and poll_time (if system time is changed, this might be negative) | ||
| time_to_sleep = max(min(time_to_sleep, poll_time), 0) | ||
| time_to_sleep = max(min(time_to_sleep, args.poll_time), 0) | ||
| sleep(time_to_sleep) | ||
|
|
||
| now = datetime.now(tz=timezone.utc) | ||
|
|
@@ -53,7 +48,7 @@ def main(testing: bool): | |
|
|
||
| pulsetime = 0.0 | ||
| if all(map(lambda v: v == 0, merged_data.values())): | ||
| pulsetime = poll_time + 0.1 | ||
| pulsetime = args.poll_time + 0.1 | ||
| logger.info("No new input") | ||
| else: | ||
| logger.info(f"New input: {e}") | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.