def main(dryrun=True): # TODO: Use wordlist instead of argument sensitive_words = ["sensitive"] aw = ActivityWatchClient(testing=True) re_word = r'\b{}\b' buckets = aw.get_buckets() for bid in buckets.keys(): if "window" not in bid: continue print("Checking bucket: {}".format(bid)) events = aw.get_events(bid, limit=-1) old_matches = set() for event in events: for key, val in event.data.items(): if isinstance(val, str): matches = [re.findall(re_word.format(word), val.lower()) for word in sensitive_words] matches = set(sum(matches, [])) if matches: event.data[key] = "REDACTED" if val not in old_matches: print(f"{'(DRYRUN) ' if dryrun else ''} Matches: {matches}, redacting: {val}") old_matches.add(val) if not dryrun: aw.insert_event(bid, event)
def aw_client(): # TODO: Could it be possible to write a sisterclass of ActivityWatchClient # which calls aw_server.api directly? Would it be of use? Would add another # layer of integration tests that are actually more like unit tests. c = ActivityWatchClient("client-test", testing=True) yield c # Delete test buckets after all tests needing the fixture have been run buckets = c.get_buckets() for bucket_id in buckets: if bucket_id.startswith("test-"): c.delete_bucket(bucket_id)
# Usage: # python3 test_continous_events.py aw-watcher-afk-testing_{hostname} # # Example: # python3 test_continous_events.py aw-watcher-afk-testing_erb-laptop-ubuntu import sys from datetime import timedelta from aw_client import ActivityWatchClient client = ActivityWatchClient("aw-watcher-afk-test", testing=True) print(client.get_buckets()) bucket_id = sys.argv[1] events = client.get_events(bucket_id) # For example "aw-watcher-afk-testing_erb-laptop-ubuntu" print("\n\n") last_event = None wrong_events = 0 for event in sorted(events, key=lambda e: e.timestamp): if last_event: # The diff is the gap between the two events, should be zero # In reality it is currently sometimes negative and almost always larger than 1s diff = (event.timestamp - last_event.timestamp) - last_event.duration print("{} at {}".format(event.label, event.timestamp)) print("Duration: {}".format(event.duration)) if not timedelta(seconds=1) > abs(diff):
bucket_etype = "test" # Delete bucket before creating it, and handle error if it doesn't already exist try: client.delete_bucket(bucket_name) except HTTPError as e: pass client.create_bucket(bucket_name, bucket_etype) e1 = create_unique_event() client.insert_event(bucket_name, e1) print("Getting events") events = client.get_events(bucket_name) print("Asserting events") assert events[0]['data']['label'] == e1['data']['label'] print("Getting eventcount") eventcount = client.get_eventcount(bucket_name) assert eventcount == 1 print("Getting bucket") buckets = client.get_buckets() print("Asserting bucket") assert bucket_name in buckets assert bucket_name == buckets[bucket_name]['id'] assert bucket_etype == buckets[bucket_name]['type'] with ActivityWatchClient("yet-another-client", testing=True) as client: assert client.name == "yet-another-client"