diff options
author | Frederick Muriuki Muriithi | 2022-06-15 09:36:18 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2022-06-15 09:36:18 +0300 |
commit | d2605cb72d7cdbc7d3cc633b94a451c0acd2edbb (patch) | |
tree | 8bf992967a750bf4dc71290b78285f6239823816 /scripts | |
parent | 6760d322637a3d875242a66e9c1a784866d7df1d (diff) | |
download | gn-uploader-d2605cb72d7cdbc7d3cc633b94a451c0acd2edbb.tar.gz |
Fix linting and type errors
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/worker.py | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/scripts/worker.py b/scripts/worker.py index ecdfaa2..c6e989f 100644 --- a/scripts/worker.py +++ b/scripts/worker.py @@ -1,20 +1,23 @@ +"""External worker script""" import os import sys +import traceback from typing import Callable -from zipfile import Path, ZipFile, is_zipfile +from zipfile import ZipFile, is_zipfile import jsonpickle from redis import Redis -from redis.exceptions import ConnectionError +from redis.exceptions import ConnectionError # pylint: disable=[redefined-builtin] -from .qc import cli_argument_parser from quality_control.utils import make_progress_calculator -from quality_control.parsing import ( - take, FileType, strain_names, collect_errors) +from quality_control.parsing import FileType, strain_names, collect_errors +from .qc import cli_argument_parser def make_progress_indicator( - redis_connection: Redis, job_id: str, progress_calc_fn: Callable) -> Callable: + redis_connection: Redis, job_id: str, + progress_calc_fn: Callable) -> Callable: + """Make function that will compute the progress and update redis""" def __indicator__(linenumber, linetext): progress = progress_calc_fn(linenumber, linetext) redis_connection.hset(name=job_id, mapping=progress._asdict()) @@ -24,6 +27,7 @@ def make_progress_indicator( return __indicator__ def cli_args_valid(args): + "Check that the command-line arguments are provided and correct" if not os.path.exists(args.filepath): print(f"The file '{args.filepath}' does not exist.", file=sys.stderr) return None @@ -33,14 +37,15 @@ def cli_args_valid(args): return None try: - conn = Redis.from_url(args.redisurl) - except ConnectionError as ce: + conn = Redis.from_url(args.redisurl) # pylint: disable=[unused-variable] + except ConnectionError as conn_err: # pylint: disable=[unused-variable] print(traceback.format_exc(), file=sys.stderr) return None return args def process_cli_arguments(): + """Setup command-line parser""" parser = cli_argument_parser() parser.prog = "worker" parser.add_argument( @@ -50,12 +55,14 @@ def process_cli_arguments(): return cli_args_valid(parser.parse_args()) def stream_error(redis_conn, job_id, error): + """Update redis with the most current error(s) found""" errors = jsonpickle.decode( redis_conn.hget(job_id, key="errors") or jsonpickle.encode(tuple())) redis_conn.hset( job_id, key="errors", value=jsonpickle.encode(errors + (error,))) def make_user_aborted(redis_conn, job_id): + """Mkae function that checks whether the user aborted the process""" def __aborted__(): user_aborted = bool(int( redis_conn.hget(name=job_id, key="user_aborted") or "0")) @@ -66,10 +73,12 @@ def make_user_aborted(redis_conn, job_id): return __aborted__ def get_zipfile_size(filepath): + "Compute size of given zipfile" with ZipFile(filepath, "r") as zfile: return zfile.infolist()[0].file_size def main(): + "entry point to the script" args = process_cli_arguments() if args is None: print("Quiting due to errors!", file=sys.stderr) |