From f4db5ee94bee6a2b432639e1273dea7180a34933 Mon Sep 17 00:00:00 2001 From: boltgolt Date: Thu, 13 Dec 2018 20:53:18 +0100 Subject: [PATCH] Formatting fixes --- src/cli.py | 32 ++++++++++++++++---------------- src/cli/config.py | 2 +- src/cli/disable.py | 2 +- src/cli/list.py | 2 +- src/cli/remove.py | 4 ++-- src/cli/test.py | 4 +++- src/compare.py | 4 +++- src/ffmpeg_reader.py | 14 ++++++-------- src/pam.py | 5 +++++ 9 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/cli.py b/src/cli.py index 2c5d877..07447cf 100755 --- a/src/cli.py +++ b/src/cli.py @@ -27,37 +27,37 @@ if user == "root" or user is None: # Basic command setup parser = argparse.ArgumentParser(description="Command line interface for Howdy face authentication.", - formatter_class=argparse.RawDescriptionHelpFormatter, - add_help=False, - prog="howdy", - epilog="For support please visit\nhttps://github.com/boltgolt/howdy") + formatter_class=argparse.RawDescriptionHelpFormatter, + add_help=False, + prog="howdy", + epilog="For support please visit\nhttps://github.com/boltgolt/howdy") # Add an argument for the command parser.add_argument("command", - help="The command option to execute, can be one of the following: add, clear, config, disable, list, remove or test.", - metavar="command", - choices=["add", "clear", "config", "disable", "list", "remove", "test"]) + help="The command option to execute, can be one of the following: add, clear, config, disable, list, remove or test.", + metavar="command", + choices=["add", "clear", "config", "disable", "list", "remove", "test"]) # Add an argument for the extra arguments of diable and remove parser.add_argument("argument", - help="Either 0 (enable) or 1 (disable) for the disable command, or the model ID for the remove command.", - nargs="?") + help="Either 0 (enable) or 1 (disable) for the disable command, or the model ID for the remove command.", + nargs="?") # Add the user flag parser.add_argument("-U", "--user", - default=user, - help="Set the user account to use.") + default=user, + help="Set the user account to use.") # Add the -y flag parser.add_argument("-y", - help="Skip all questions.", - action="store_true") + help="Skip all questions.", + action="store_true") # Overwrite the default help message so we can use a uppercase S parser.add_argument("-h", "--help", - action="help", - default=argparse.SUPPRESS, - help="Show this help message and exit.") + action="help", + default=argparse.SUPPRESS, + help="Show this help message and exit.") # If we only have 1 argument we print the help text if len(sys.argv) < 2: diff --git a/src/cli/config.py b/src/cli/config.py index aaa5518..4f6caca 100644 --- a/src/cli/config.py +++ b/src/cli/config.py @@ -17,4 +17,4 @@ elif "EDITOR" in os.environ: editor = os.environ["EDITOR"] # Open the editor as a subprocess and fork it -subprocess.call([editor, os.path.dirname(os.path.realpath(__file__)) + "/../config.ini"]) +subprocess.call([editor, os.path.dirname(os.path.realpath(__file__)) + "/../config.ini"]) diff --git a/src/cli/disable.py b/src/cli/disable.py index 8bc0341..cba8931 100644 --- a/src/cli/disable.py +++ b/src/cli/disable.py @@ -16,7 +16,7 @@ config = configparser.ConfigParser() config.read(config_path) # Check if enough arguments have been passed -if builtins.howdy_args.argument == None: +if builtins.howdy_args.argument is None: print("Please add a 0 (enable) or a 1 (disable) as an argument") sys.exit(1) diff --git a/src/cli/list.py b/src/cli/list.py index 061bf49..08a1e9f 100644 --- a/src/cli/list.py +++ b/src/cli/list.py @@ -8,7 +8,7 @@ import time import builtins # Get the absolute path and the username -path = os.path.dirname(os.path.realpath(__file__)) + "/.." +path = os.path.dirname(os.path.realpath(__file__)) + "/.." user = builtins.howdy_user # Check if the models file has been created yet diff --git a/src/cli/remove.py b/src/cli/remove.py index deef825..45e2036 100644 --- a/src/cli/remove.py +++ b/src/cli/remove.py @@ -7,11 +7,11 @@ import json import builtins # Get the absolute path and the username -path = os.path.dirname(os.path.realpath(__file__)) + "/.." +path = os.path.dirname(os.path.realpath(__file__)) + "/.." user = builtins.howdy_user # Check if enough arguments have been passed -if builtins.howdy_args.argument == None: +if builtins.howdy_args.argument is None: print("Please add the ID of the model you want to remove as an argument") print("You can find the IDs by running:") print("\n\thowdy list\n") diff --git a/src/cli/test.py b/src/cli/test.py index 3faa5b6..4c4effe 100644 --- a/src/cli/test.py +++ b/src/cli/test.py @@ -45,6 +45,7 @@ Press ctrl+C in this terminal to quit Click on the image to enable or disable slow mode """) + def mouse(event, x, y, flags, param): """Handle mouse events""" global slow_mode @@ -53,10 +54,12 @@ def mouse(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: slow_mode = not slow_mode + def print_text(line_number, text): """Print the status text by line number""" cv2.putText(overlay, text, (10, height - 10 - (10 * line_number)), cv2.FONT_HERSHEY_SIMPLEX, .3, (0, 255, 0), 0, cv2.LINE_AA) + # Open the window and attach a a mouse listener cv2.namedWindow("Howdy Test") cv2.setMouseCallback("Howdy Test", mouse) @@ -90,7 +93,6 @@ try: sec = int(time.time()) sec_frames = 0 - # Grab a single frame of video ret, frame = (video_capture.read()) # Make a frame to put overlays in diff --git a/src/compare.py b/src/compare.py index 0b7c283..7d9b939 100644 --- a/src/compare.py +++ b/src/compare.py @@ -18,11 +18,13 @@ import configparser config = configparser.ConfigParser() config.read(os.path.dirname(os.path.abspath(__file__)) + "/config.ini") + def stop(status): """Stop the execution and close video stream""" video_capture.release() sys.exit(status) + # Make sure we were given an username to tast against try: if not isinstance(sys.argv[1], str): @@ -182,7 +184,7 @@ while True: print("Certainty of winning frame: %.3f" % (match * 10, )) # Catch older 3-encoding models - if not match_index in models: + if match_index not in models: match_index = 0 print("Winning model: %d (\"%s\")" % (match_index, models[match_index]["label"])) diff --git a/src/ffmpeg_reader.py b/src/ffmpeg_reader.py index 658b447..da77eae 100755 --- a/src/ffmpeg_reader.py +++ b/src/ffmpeg_reader.py @@ -7,7 +7,6 @@ import re from subprocess import Popen, PIPE from cv2 import CAP_PROP_FRAME_WIDTH from cv2 import CAP_PROP_FRAME_HEIGHT -from cv2 import CAP_PROP_FOURCC try: import ffmpeg @@ -16,6 +15,7 @@ except ImportError: print(" pip3 install ffmpeg-python\n") sys.exit(12) + class ffmpeg_reader: """ This class was created to look as similar to the openCV features used in Howdy as possible for overall code cleanliness. """ @@ -54,16 +54,16 @@ class ffmpeg_reader: return_code = process.poll() # Worst case scenario, err will equal en empty byte string, b'', so probe will get set to [] here. - regex = re.compile('\s\d{3,4}x\d{3,4}') + regex = re.compile(r"\s\d{3,4}x\d{3,4}") probe = regex.findall(str(err.decode("utf-8"))) if not return_code == 1 or len(probe) < 1: # Could not determine the resolution from ffmpeg call. Reverting to ffmpeg.probe() probe = ffmpeg.probe(self.device_path) - height = probe['streams'][0]['height'] - width = probe['streams'][0]['width'] + height = probe["streams"][0]["height"] + width = probe["streams"][0]["width"] else: - (height, width) = [x.strip() for x in probe[0].split('x')] + (height, width) = [x.strip() for x in probe[0].split("x")] # Set height and width from probe if they haven't been set already if height.isdigit() and self.get(CAP_PROP_FRAME_HEIGHT) == 0: @@ -71,7 +71,6 @@ class ffmpeg_reader: if width.isdigit() and self.get(CAP_PROP_FRAME_WIDTH) == 0: self.set(CAP_PROP_FRAME_WIDTH, int(width)) - def record(self, numframes): """ Record a video, saving it to self.video array for processing later """ @@ -86,7 +85,7 @@ class ffmpeg_reader: stream, ret = ( ffmpeg .input(self.device_path, format=self.device_format) - .output('pipe:', format='rawvideo', pix_fmt='rgb24', vframes=numframes) + .output("pipe:", format="rawvideo", pix_fmt="rgb24", vframes=numframes) .run(capture_stdout=True, quiet=True) ) self.video = ( @@ -95,7 +94,6 @@ class ffmpeg_reader: .reshape([-1, self.width, self.height, 3]) ) - def read(self): """ Read a sigle frame from the self.video array. Will record a video if array is empty. """ diff --git a/src/pam.py b/src/pam.py index 2e0dbfa..18f1c12 100644 --- a/src/pam.py +++ b/src/pam.py @@ -12,6 +12,7 @@ import ConfigParser config = ConfigParser.ConfigParser() config.read(os.path.dirname(os.path.abspath(__file__)) + "/config.ini") + def doAuth(pamh): """Starts authentication in a seperate process""" @@ -56,18 +57,22 @@ def doAuth(pamh): pamh.conversation(pamh.Message(pamh.PAM_ERROR_MSG, "Unknown error: " + str(status))) return pamh.PAM_SYSTEM_ERR + def pam_sm_authenticate(pamh, flags, args): """Called by PAM when the user wants to authenticate, in sudo for example""" return doAuth(pamh) + def pam_sm_open_session(pamh, flags, args): """Called when starting a session, such as su""" return doAuth(pamh) + def pam_sm_close_session(pamh, flags, argv): """We don't need to clean anyting up at the end of a session, so returns true""" return pamh.PAM_SUCCESS + def pam_sm_setcred(pamh, flags, argv): """We don't need set any credentials, so returns true""" return pamh.PAM_SUCCESS