From 753180b4d128fc1d113f6a9ae89432e68ef42ada Mon Sep 17 00:00:00 2001 From: Lukáš Růžička Date: Jul 30 2020 18:13:05 +0000 Subject: Merge autococonut. --- diff --git a/autococonut.py b/autococonut.py index d018ee2..c77b7d1 100755 --- a/autococonut.py +++ b/autococonut.py @@ -5,7 +5,7 @@ AutoCoconut - a recording tool to map your workflow. This is the main script for the application. -Created by Lukáš Růžička (lruzicka@redhat.com). Licensed under GPLv3. +Created by Lukáš Růžička (lruzicka@redhat.com). Licensed under GPLv3. """ import argparse @@ -13,7 +13,6 @@ import event_handler import interpreter import json import os -import signal import sys import time @@ -30,7 +29,7 @@ class Parser(): self.parser.add_argument("-s","--stopkey", default="f10", help="The dedicated key to stop listening to events.") self.parser.add_argument("-r","--resolution", default=None, help="To which screen resolution to switch before starting listening to the events.") self.parser.add_argument("-p","--pause", default="1", help="How long in seconds to wait before screenshot is taken.") - self.parser.add_argument("-t", "--printkey", default="f9", help="The dedicated key to print extra screenshots.") + self.parser.add_argument("-t", "--printkey", default="f9", help="The dedicated key to print extra screenshots.") self.parser.add_argument("-o","--output", default="md", help="The form of the output (md, html, openqa, raw, json).") self.parser.add_argument("-f","--file", default=None,help="Store the output into a file instead of the screen.") self.parser.add_argument("-c","--caption", default="AutoCoconut - workflow report",help="The title of the output document.") @@ -42,18 +41,12 @@ class Parser(): args = self.parser.parse_args() return args -def signal_handler(signum, frame): - pass - def main(): """ The main method for the AutoCoconut script. """ # Invoke the CLI Parser args_cli = Parser() cli = args_cli.return_arguments() - # Fix handling the Ctrl_C SIGINT - signal.signal(signal.SIGINT, signal_handler) - # Use CLI arguments to specify the app run. stopkey = cli.stopkey pause = float(cli.pause) @@ -63,8 +56,12 @@ def main(): caption = cli.caption # Start the collector - # Get the raw_report from the collector collector = event_handler.Collector(stopkey, pause) + try: + collector.start() + # Get the raw_report from the collector + except KeyboardInterrupt: + pass raw = collector.return_report() final_result = None # If we only want the raw output from the event handler. diff --git a/event_handler.py b/event_handler.py index c0a0839..036efe9 100644 --- a/event_handler.py +++ b/event_handler.py @@ -23,19 +23,19 @@ class Handler: """ The Handler class processes the mouse and keyboard events, interprets them, creates a dictionary of events and returns it for further analysis. """ def __init__(self, stop_key="f10", time_offset=1): - """ Innitiates the variables. + """ Innitiates the variables. - The ${stop_key} defines a special key that will terminate the listeners + The ${stop_key} defines a special key that will terminate the listeners and finish the script. The ${time_offset} defines the divergence of the "corrected" screenshot from the event timestamp. It should be a positive, non-zero value.""" # The $stop_key switches on the recording and quits the script. self.stop_key = stop_key - # If self.started is False no events are recorded. After pressing the + # If self.started is False no events are recorded. After pressing the # stop key, this will change to true and the script will record events. self.started = False # The $time_offset sets the delay with which a "corrected" needle is - # taken. By default it is 1 second. + # taken. By default it is 1 second. self.time_offset = time_offset # All the captured events are stored in the $self.events which is returned # at the end. @@ -82,7 +82,7 @@ class Handler: ] # When scrolling the mouse, each single step is sent from pynput. - # This would case the script to take a high number of screenshots, + # This would case the script to take a high number of screenshots, # therefore we only take a screenshot after a certain number # of scroll steps. This is controlled by these variables. self.scrollstep = 0 @@ -114,7 +114,7 @@ class Handler: Note, that the horizontal direction is not available on the standard mouse.""" shot = self.scrollscreen cshot = None - # For scrolls, take a screenshot after each 10 steps. + # For scrolls, take a screenshot after each 10 steps. if self.scrollstep == 0 or self.scrollstep > 10: camera.save_screenshot(timestamp) shot = self.name_shot(timestamp) @@ -135,14 +135,14 @@ class Handler: return report def record_mouse_click(self, point_x, point_y, button, timestamp, release=False): - """ Records the mouse click of the 'button' at the given position 'x' and 'y'. + """ Records the mouse click of the 'button' at the given position 'x' and 'y'. It takes the $button, $point_x, $point_y from the event collector, including the $timestamp and $release to recognize whether the button was pressed or - released in this event. + released in this event. The corrected screenshot, in this case, is taken prior (not after) the timestamp - screenshot, because often, when the mouse hovers over a gui element, this one + screenshot, because often, when the mouse hovers over a gui element, this one changes, highlights and this would cause problems with OpenQA recognition.""" shot = cshot = None button = str(button).split('.')[1] @@ -154,7 +154,7 @@ class Handler: # or shapes, when the mouse cursor hovers over the button. # Therefore, we will take a screenshot that precedes the # actual action instead. - offset = self.time_offset * -1 + offset = self.time_offset * -1 camera.save_screenshot(timestamp, offset) shot = self.name_shot(timestamp) cshot = self.name_shot(timestamp + offset) @@ -174,7 +174,7 @@ class Handler: def record_mouse_move(self, point_x, point_y, timestamp): """ Records the mouse moves. - Takes the $point_x and $point_y coordinates where the mouse has moved + Takes the $point_x and $point_y coordinates where the mouse has moved and the $timestamp of the event. """ # Moving the mouse is not so much important, but we need it to interpret # some of the "superevents", such as dragging the mouse. Therefore, @@ -196,7 +196,7 @@ class Handler: if not release: action = 'press' # We do not want to take screenshots for alphanumeric keys, because - # that would require a lot of screenshots to be taken as, when typing, + # that would require a lot of screenshots to be taken as, when typing, # many keys are used without any effects on the screen. We only # take a screenshots for special keys, modifiers, and the tab key. if key in self.specials or key in self.modifiers or key == "tab": @@ -254,6 +254,8 @@ class Collector: self.interrupt = False # Start the key and mouse listeners to listen to events. Events will be # dropped until $self.powered becomes True. + + def start(self): with MouseListener(on_move=self.on_mouse_move, on_click=self.on_mouse_click, on_scroll=self.on_mouse_scroll) as self.mlistener, \ @@ -275,7 +277,7 @@ class Collector: return key_name def return_report(self, beautiful=False): - """ Returns the final report dictionary. + """ Returns the final report dictionary. If $beautiful is set, the json will be formatted.""" raw = self.handler.events