From 8c8f49650247f403b9cfd4aacd5fd0823ab53eb1 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Wed, 4 Dec 2024 20:04:02 -0800 Subject: [PATCH] manual ruff fixes ruff can detect but not fix these. Signed-off-by: Rosen Penev --- doc/templates/gen.py | 13 +++++++------ tests/bash_tests/testcases.py | 26 ++++++++++++-------------- tests/bash_tests/utils.py | 20 ++++++++++---------- tests/lens_tests/utils.py | 2 +- tests/system_tests.py | 2 +- 5 files changed, 31 insertions(+), 32 deletions(-) diff --git a/doc/templates/gen.py b/doc/templates/gen.py index afc390d18a..7e386d0a72 100755 --- a/doc/templates/gen.py +++ b/doc/templates/gen.py @@ -1,4 +1,9 @@ #! /usr/bin/env python +import sys +import os +import re +import time + # ---------------------------------------------------------------------- # Settings vardir = "." @@ -28,11 +33,6 @@ def last_modified(text): # ---------------------------------------------------------------------- # main -import sys -import os -import re -import time - # Check command line arguments if len(sys.argv) == 1: usage() @@ -44,7 +44,8 @@ def last_modified(text): # Get a list of all variables (files in the form __*__) from vardir vars = os.listdir(vardir) for i in range(len(vars)-1, -1, -1): - if re.match("^__.*__$", vars[i]): continue + if re.match("^__.*__$", vars[i]): + continue del vars[i] vars.sort() diff --git a/tests/bash_tests/testcases.py b/tests/bash_tests/testcases.py index 437f900e3e..df69a2e8c0 100644 --- a/tests/bash_tests/testcases.py +++ b/tests/bash_tests/testcases.py @@ -2,9 +2,9 @@ import re import unittest +from importlib.util import find_spec from system_tests import BT - class TestCases(unittest.TestCase): @@ -368,25 +368,25 @@ def exiv2_test(self): out += BT.Executer('exiv2 -u -h') out += '\n\nAdjust -------------------------------------------------------------------' - out += BT.Executer('exiv2 -u -v -a-12:01:01 adjust {images_1_str}', vars(), assert_returncode=[253]) + out += BT.Executer('exiv2 -u -v -a-12:01:01 adjust {}'.format(images_1_str), vars(), assert_returncode=[253]) out += '\nRename -------------------------------------------------------------------' - out += BT.Executer('exiv2 -u -vf rename {images_1_str}', vars(), assert_returncode=[253]) + out += BT.Executer('exiv2 -u -vf rename {}'.format(images_1_str), vars(), assert_returncode=[253]) out += '\nPrint --------------------------------------------------------------------' - out += BT.Executer('exiv2 -u -v print {images_2_str}', vars(), assert_returncode=[253]) + out += BT.Executer('exiv2 -u -v print {}'.format(images_2_str), vars(), assert_returncode=[253]) out += '' - out += BT.Executer('exiv2 -u -v -b -pt print {images_2_str}', vars()) - e = BT.Executer('exiv2 -u -v -b -pt print {images_2_str}', vars(), redirect_stderr_to_stdout=False, decode_output=False) + out += BT.Executer('exiv2 -u -v -b -pt print {}'.format(images_2_str), vars()) + e = BT.Executer('exiv2 -u -v -b -pt print {}'.format(images_2_str), vars(), redirect_stderr_to_stdout=False, decode_output=False) BT.save(e.stdout, 'iii') out += e.stderr.decode() out += '\nExtract Exif data --------------------------------------------------------' - out += BT.Executer('exiv2 -u -vf extract {images_2_str}', vars()) + out += BT.Executer('exiv2 -u -vf extract {}'.format(images_2_str), vars()) out += '\nExtract Thumbnail --------------------------------------------------------' - out += BT.Executer('exiv2 -u -vf -et extract {images_2_str}', vars(), assert_returncode=[253]) - e = BT.Executer('exiv2 -u -v -b -pt print {images_3_str}', vars(), redirect_stderr_to_stdout=False, decode_output=False) + out += BT.Executer('exiv2 -u -vf -et extract {}'.format(images_2_str), vars(), assert_returncode=[253]) + e = BT.Executer('exiv2 -u -v -b -pt print {}'.format(images_3_str), vars(), redirect_stderr_to_stdout=False, decode_output=False) BT.save(e.stdout, 'jjj') out += e.stderr.decode() @@ -1013,9 +1013,7 @@ def preview_test(self): def stdin_test(self): return # temporarily disable # Test driver for stdin - try: - import lxml - except ModuleNotFoundError: + if find_spec('lxml') is None: print('Skipped. Because it misses module lxml. Please install: `pip install lxml`') return @@ -1334,8 +1332,8 @@ def xmpparser_test(self): out += BT.diff(img, img + '-new') xmp = 'xmpsdk.xmp' - BT.save(BT.Executer('xmpparse {xmp}' , vars()).stdout, 't1') - BT.save(BT.Executer('xmpparse {xmp}-new', vars()).stdout, 't2') + BT.save(BT.Executer('xmpparse {}'.format(xmp), vars()).stdout, 't1') + BT.save(BT.Executer('xmpparse {}-new'.format(xmp), vars()).stdout, 't2') out += BT.diff('t1', 't2') out += BT.Executer('xmpsample') diff --git a/tests/bash_tests/utils.py b/tests/bash_tests/utils.py index 7fdc7012ad..e0fb6afe9f 100644 --- a/tests/bash_tests/utils.py +++ b/tests/bash_tests/utils.py @@ -364,8 +364,8 @@ def start(self): with request.urlopen('http://127.0.0.1:{}'.format(self.port), timeout=3) as f: if f.status != 200: raise RuntimeError() - except: - raise RuntimeError('Failed to run the HTTP server') + except Exception as e: + raise RuntimeError('Failed to run the HTTP server: {}'.format(e)) log.info('The HTTP server started') def stop(self): @@ -504,8 +504,8 @@ def run(self): except subprocess.TimeoutExpired: self.subprocess.kill() output = self.subprocess.communicate() - except: - raise RuntimeError('Failed to execute: {}'.format(self.args)) + except Exception as e: + raise RuntimeError('Failed to execute {}: {}'.format(self.args, e)) output = [i or b'' for i in output] output = [i.rstrip(b'\r\n').rstrip(b'\n') for i in output] # Remove the last line break of the output @@ -556,7 +556,7 @@ def __str__(self): def __add__(self, other): if isinstance(other, Executer): other = other.stdout - if other != None: + if other is not None: self.lines.append(str(other)) return self @@ -605,7 +605,7 @@ def copyTest(num, src, good): src_file = os.path.join(Config.data_dir, src) good_file = os.path.join(Config.data_dir, '{}.c{}gd'.format(good, num)) copyTestFile(good, test_file) - Executer('metacopy -a {src_file} {test_file}', vars()) + Executer('metacopy -a {} {}'.format(src_file, test_file), vars()) return diffCheck(good_file, test_file, in_bytes=True) @@ -614,7 +614,7 @@ def iptcTest(num, src, good): src_file = os.path.join(Config.data_dir, src) good_file = os.path.join(Config.data_dir, '{}.i{}gd'.format(good, num)) copyTestFile(good, test_file) - Executer('metacopy -ip {src_file} {test_file}', vars()) + Executer('metacopy -ip {} {}'.format(src_file, test_file), vars()) return diffCheck(good_file, test_file, in_bytes=True) @@ -624,7 +624,7 @@ def printTest(filename): good_file = os.path.join(Config.data_dir, filename + '.ipgd') copyTestFile(filename, test_file) - e = Executer('iptcprint {src_file}', vars(), assert_returncode=None, decode_output=False) + e = Executer('iptcprint {}'.format(src_file), vars(), assert_returncode=None, decode_output=False) stdout = e.stdout.replace(Config.data_dir.replace(os.path.sep, '/').encode(), b'../data') # Ignore the difference of data_dir on Windows save(stdout + b'\n', test_file) @@ -743,8 +743,8 @@ def runTest(cmd,raw=False): print('{} returncode = {}'.format(cmd, p.returncode)) # Split the output by newline out = stdout.decode('utf-8').replace('\r', '').rstrip('\n').split('\n') - except: - print('** {} died **'.format(cmd)) + except Exception as e: + print('** {} died: {} **'.format(cmd, e)) return out diff --git a/tests/lens_tests/utils.py b/tests/lens_tests/utils.py index c7c9a03460..0780dc7bfb 100644 --- a/tests/lens_tests/utils.py +++ b/tests/lens_tests/utils.py @@ -152,7 +152,7 @@ def make_test_cases(lenses): **lens["meta"], "id": lens["id"], "desc": lens["desc"], - "target": " *OR* ".join([l["desc"] for l in lens_group if lens_is_match(lens["meta"], l["meta"])]), + "target": " *OR* ".join([lg["desc"] for lg in lens_group if lens_is_match(lens["meta"], lg["meta"])]), } for lens in lens_group ] diff --git a/tests/system_tests.py b/tests/system_tests.py index d3b00f7913..494ce98dfb 100644 --- a/tests/system_tests.py +++ b/tests/system_tests.py @@ -724,7 +724,7 @@ def _encode(self, data_in, encode_action, get_err): """ result = None for encoding in self.encodings: - if encoding == bytes: + if encoding is bytes: return data_in try: result = encode_action(data_in, encoding)