1- import argparse
21import logging
32import traceback
43import sys
1110from aw_client import ActivityWatchClient
1211
1312from .lib import get_current_window
14- from .config import load_config
13+ from .config import parse_args
1514
1615logger = logging .getLogger (__name__ )
1716
17+ # run with LOG_LEVEL=DEBUG
18+ log_level = os .environ .get ('LOG_LEVEL' )
19+ if log_level :
20+ logger .setLevel (logging .__getattribute__ (log_level .upper ()))
1821
1922def main ():
20- # Read settings from config
21- config = load_config ()
22- args = parse_args (
23- default_poll_time = config .getfloat ("poll_time" ),
24- default_exclude_title = config .getboolean ("exclude_title" ),
25- )
23+ args = parse_args ()
2624
2725 if sys .platform .startswith ("linux" ) and ("DISPLAY" not in os .environ or not os .environ ["DISPLAY" ]):
2826 raise Exception ("DISPLAY environment variable not set" )
@@ -45,43 +43,30 @@ def main():
4543
4644 sleep (1 ) # wait for server to start
4745 with client :
48- heartbeat_loop (client , bucket_id , poll_time = args .poll_time , exclude_title = args .exclude_title )
46+ heartbeat_loop (client , bucket_id , poll_time = args .poll_time , strategy = args . strategy , exclude_title = args .exclude_title )
4947
50-
51- def parse_args (default_poll_time : float , default_exclude_title : bool ):
52- """config contains defaults loaded from the config file"""
53- parser = argparse .ArgumentParser ("A cross platform window watcher for Activitywatch.\n Supported on: Linux (X11), macOS and Windows." )
54- parser .add_argument ("--testing" , dest = "testing" , action = "store_true" )
55- parser .add_argument ("--exclude-title" , dest = "exclude_title" , action = "store_true" , default = default_exclude_title )
56- parser .add_argument ("--verbose" , dest = "verbose" , action = "store_true" )
57- parser .add_argument ("--poll-time" , dest = "poll_time" , type = float , default = default_poll_time )
58- return parser .parse_args ()
59-
60-
61- def heartbeat_loop (client , bucket_id , poll_time , exclude_title = False ):
48+ def heartbeat_loop (client , bucket_id , poll_time , strategy , exclude_title = False ):
6249 while True :
6350 if os .getppid () == 1 :
6451 logger .info ("window-watcher stopped because parent process died" )
6552 break
6653
6754 try :
68- current_window = get_current_window ()
55+ current_window = get_current_window (strategy )
6956 logger .debug (current_window )
7057 except Exception as e :
7158 logger .error ("Exception thrown while trying to get active window: {}" .format (e ))
7259 traceback .print_exc ()
73- current_window = {"appname " : "unknown" , "title" : "unknown" }
60+ current_window = {"app " : "unknown" , "title" : "unknown" }
7461
7562 now = datetime .now (timezone .utc )
7663 if current_window is None :
7764 logger .debug ('Unable to fetch window, trying again on next poll' )
7865 else :
79- # Create current_window event
80- data = {
81- "app" : current_window ["appname" ],
82- "title" : current_window ["title" ] if not exclude_title else "excluded"
83- }
84- current_window_event = Event (timestamp = now , data = data )
66+ if exclude_title :
67+ current_window ["title" ] = "excluded"
68+
69+ current_window_event = Event (timestamp = now , data = current_window )
8570
8671 # Set pulsetime to 1 second more than the poll_time
8772 # This since the loop takes more time than poll_time
0 commit comments