@@ -23,10 +23,13 @@ def _valid_date(s):
2323 raise argparse .ArgumentTypeError (msg )
2424
2525
26+ class _Context :
27+ client : aw_client .ActivityWatchClient
28+
29+
2630@click .group (
2731 help = "CLI utility for aw-client to aid in interacting with the ActivityWatch server"
2832)
29- @click .option ("--testing" , is_flag = True , help = "Set to use testing ports by default" )
3033@click .option (
3134 "--host" ,
3235 default = "127.0.0.1" ,
@@ -37,12 +40,14 @@ def _valid_date(s):
3740 default = 5600 ,
3841 help = "Port to use" ,
3942)
43+ @click .option ("--testing" , is_flag = True , help = "Set to use testing ports by default" )
4044@click .pass_context
4145def main (ctx , testing : bool , host : str , port : int ):
42- ctx .testing = testing
43- ctx .client = aw_client .ActivityWatchClient (
46+ ctx .obj = _Context ()
47+ ctx .obj . client = aw_client .ActivityWatchClient (
4448 host = host ,
4549 port = port if port != 5600 else (5666 if testing else 5600 ),
50+ testing = testing ,
4651 )
4752
4853
@@ -55,13 +60,13 @@ def heartbeat(ctx, bucket_id: str, data: str, pulsetime: int):
5560 now = datetime .now (timezone .utc )
5661 e = Event (duration = 0 , data = json .loads (data ), timestamp = now )
5762 print (e )
58- ctx .client .heartbeat (bucket_id , e , pulsetime )
63+ ctx .obj . client .heartbeat (bucket_id , e , pulsetime )
5964
6065
6166@main .command (help = "List all buckets" )
6267@click .pass_context
6368def buckets (ctx ):
64- buckets = ctx .client .get_buckets ()
69+ buckets = ctx .obj . client .get_buckets ()
6570 print ("Buckets:" )
6671 for bucket in buckets :
6772 print (" - {}" .format (bucket ))
@@ -71,7 +76,7 @@ def buckets(ctx):
7176@click .argument ("bucket_id" )
7277@click .pass_context
7378def events (ctx , bucket_id : str ):
74- events = ctx .client .get_events (bucket_id )
79+ events = ctx .obj . client .get_events (bucket_id )
7580 print ("events:" )
7681 for e in events :
7782 print (
@@ -102,7 +107,7 @@ def query(
102107):
103108 with open (path ) as f :
104109 query = f .read ()
105- result = ctx .client .query (query , [(start , stop )], cache = cache , name = name )
110+ result = ctx .obj . client .query (query , [(start , stop )], cache = cache , name = name )
106111 if _json :
107112 print (json .dumps (result ))
108113 else :
0 commit comments