Skip to content

Commit

Permalink
pylint: various cleanups
Browse files Browse the repository at this point in the history
Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Dec 14, 2024
1 parent 1e4cc38 commit 3ab890f
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 70 deletions.
2 changes: 1 addition & 1 deletion ci/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def matrix_build(shared_libs, ccs, build_types, cmake_bin, cmake_options,
os.path.join(
"build",
"_".join(
map(lambda p: str(p) if p is not None else "", params)
map(str, filter(None, params))
)
)
)
Expand Down
54 changes: 29 additions & 25 deletions doc/templates/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,54 @@
vardir = "."
date_format = "%d-%b-%Y"


# ----------------------------------------------------------------------
# functions
def usage():
print("""Usage: gen.py file.in [...]
Substitute placeholders in input files with content
""")
print("""Usage: gen.py file.in [...]
Substitute placeholders in input files with content
""")


def gen_html(file):
"""Replace variables in the file with their content"""
text = open(file).read()
for var in vars:
vartext = open(f"{vardir}/{var}").read()
text = text.replace(var, vartext)
text = last_modified(text)
return text
"""Replace variables in the file with their content"""
text = open(file, encoding=None).read()
for var in vars:
vartext = open(f"{vardir}/{var}", encoding=None).read()
text = text.replace(var, vartext)
text = last_modified(text)
return text


def last_modified(text):
"""Substitute variable __last_modified__ with the current date"""
date = time.strftime(date_format, time.localtime())
text = text.replace("__last_modified__", date)
return text
"""Substitute variable __last_modified__ with the current date"""
date = time.strftime(date_format, time.localtime())
text = text.replace("__last_modified__", date)
return text


# ----------------------------------------------------------------------
# main
# Check command line arguments
if len(sys.argv) == 1:
usage()
sys.exit()
usage()
sys.exit()

# The input files from the command line
input = sys.argv[1:]

# Get a list of all variables (files in the form __*__) from vardir
# 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
del vars[i]
for i in range(len(vars) - 1, -1, -1):
if re.match("^__.*__$", vars[i]):
continue
del vars[i]
vars.sort()

# Substitute variables in all input files
print(f"Substituting variables {vars}")
for file in input:
print(f"Processing {file}...")
text = gen_html(file)
file = file.replace(".in", "")
open(file, 'w').write(text)
print(f"Processing {file}...")
text = gen_html(file)
file = file.replace(".in", "")
open(file, "w", encoding=None).write(text)
21 changes: 10 additions & 11 deletions fuzz/mkdictionary.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!/usr/bin/env python3

# Utility for generating a fuzzing dictionary for Exiv2.
# See README.md (in this directory) for more information.
Expand All @@ -11,22 +11,21 @@
def escapeChar(c):
if c == '\\':
return '\\\\'
elif c == '"':
if c == '"':
return '\\"'
elif c.isascii() and c.isprintable():
if c.isascii() and c.isprintable():
return c
else:
return f'\\x{ord(c):02X}'
return f'\\x{ord(c):02X}'

def escapeString(str):
return ''.join(map(lambda c: escapeChar(chr(c)), bytes(str, 'utf-8')))
return ''.join(map(escapeChar, bytes(str, 'utf-8')))

if len(sys.argv) < 2:
print("usage: mkdict.py dict.json")
sys.exit(1)

f = open(sys.argv[1], 'r')
dict_json = json.loads(f.read())
tuples = dict_json["#select"]["tuples"]
for r in tuples:
print(f"\"{escapeString(r[0])}\"")
with open(sys.argv[1], 'r', encoding=None) as f:
dict_json = json.loads(f.read())
tuples = dict_json["#select"]["tuples"]
for r in tuples:
print(f"\"{escapeString(r[0])}\"")
7 changes: 3 additions & 4 deletions tests/bash_tests/testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,11 +601,10 @@ def sniff(*files):

except Exception as e:
BT.log.error(e)
raise RuntimeError(f"\n{BT.log.to_str()}")
raise RuntimeError(f"\n{BT.log.to_str()}") from e

finally:
server.stop() # While you're debugging, you can comment this line to keep the server running
pass

BT.reportTest('iotest', out)

Expand Down Expand Up @@ -921,8 +920,8 @@ def png_test(self):
for i in [1,2]:
out += BT.Executer('exiv2 -pS {file}', vars())
out += BT.Executer('exiv2 -pc {file}', vars())
if i ==1:
out+= ''
if i == 1:
out+= ''
out += BT.Executer('exiv2 -pa {file}', vars())
out += BT.Executer('exiv2 -c "changed comment" {file}', vars())

Expand Down
24 changes: 14 additions & 10 deletions tests/bash_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ def diff_bytes(file1, file2, return_str=False):

if return_str:
return '\n'.join([repr(line)[2:-1] for line in output])
else:
return b'\n'.join(output)
return b'\n'.join(output)


def md5sum(filename):
Expand Down Expand Up @@ -365,7 +364,7 @@ def start(self):
if f.status != 200:
raise RuntimeError()
except Exception as e:
raise RuntimeError(f'Failed to run the HTTP server: {e}')
raise RuntimeError(f'Failed to run the HTTP server: {e}') from e
log.info('The HTTP server started')

def stop(self):
Expand Down Expand Up @@ -438,15 +437,21 @@ class Executer:
"""

def __init__(self, cmd: str,
vars_dict=dict(),
vars_dict=None,
cwd=None,
extra_env=dict(),
extra_env=None,
encoding=None,
stdin: (str, bytes) = None,
redirect_stderr_to_stdout=True,
assert_returncode=[0],
compatible_output=True,
decode_output=True):

if vars_dict is None:
vars_dict = {}
if extra_env is None:
extra_env = {}

self.cmd = cmd.format(**vars_dict)
self.cwd = cwd or Config.tmp_dir

Expand Down Expand Up @@ -476,9 +481,9 @@ def __init__(self, cmd: str,
self.args = args.replace('\'', '\"')
else:
self.args = shlex.split(args, posix=os.name == 'posix')

if len(Config.valgrind)>0:
self.args = [ Config.valgrind ] + self.args
self.args = [ Config.valgrind ] + self.args

# Check stdin
if self.stdin:
Expand All @@ -505,7 +510,7 @@ def run(self):
self.subprocess.kill()
output = self.subprocess.communicate()
except Exception as e:
raise RuntimeError(f'Failed to execute {self.args}: {e}')
raise RuntimeError(f'Failed to execute {self.args}: {e}') from 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

Expand Down Expand Up @@ -765,8 +770,7 @@ def verbose_version(verbose=False):
else:
vv[key] = [vv[key]]
if verbose:
for key in vv:
val = vv[key]
for key, val in vv.items():
if isinstance(val, list):
val = f'[ {val[0]} +{len(val) - 1} ]'
print(key.ljust(20), val)
Expand Down
1 change: 0 additions & 1 deletion tests/bugfixes/github/test_CVE_2017_17724.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ class TestFuzzedPoC(metaclass=system_tests.CaseMeta):

def compare_stdout(self, i, command, got_stdout, expected_stdout):
""" We don't care about the stdout, just don't crash """
pass
1 change: 0 additions & 1 deletion tests/bugfixes/github/test_issue_1099.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ class EmptyValueInCommandFile(metaclass=CaseMeta):

def compare_stdout(self, i, command, got_stdout, expected_stdout):
""" We don't care about the stdout, just don't crash """
pass
1 change: 0 additions & 1 deletion tests/bugfixes/github/test_issue_1713.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ class InvalidDateXMP(metaclass=CaseMeta):

def compare_stdout(self, i, command, got_stdout, expected_stdout):
""" We don't care about the stdout, just don't crash """
pass
1 change: 0 additions & 1 deletion tests/bugfixes/github/test_issue_211.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ class TestFuzzedPoC(metaclass=system_tests.CaseMeta):

def compare_stdout(self, i, command, got_stdout, expected_stdout):
""" We don't care about the stdout, just don't crash """
pass
1 change: 0 additions & 1 deletion tests/bugfixes/github/test_issue_ghsa_pvjp_m4f6_q984.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ class MinoltaDivZero(metaclass=CaseMeta):

def compare_stdout(self, i, command, got_stdout, expected_stdout):
""" We don't care about the stdout, just don't crash """
pass
2 changes: 1 addition & 1 deletion tests/bugfixes/redmine/test_issue_751.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def read_xmpfile():
with open(self.xmpname, "r", encoding='utf-8') as xmp:
return xmp.read(-1)

if i == 2 or i == 4:
if i in (2,4):
self.assertMultiLineEqual(self.xmp_packets[i//2 - 1], read_xmpfile())


Expand Down
2 changes: 1 addition & 1 deletion tests/bugfixes/redmine/test_issue_922.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def read_file(filename):
with open(filename, 'r') as f:
with open(filename, 'r', encoding=None) as f:
return f.read()


Expand Down
12 changes: 6 additions & 6 deletions tests/lens_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
log = logging.getLogger(__name__)


LENS_ENTRY_DEFAULT_RE = re.compile('^\{\s*(?P<lens_id>[0-9]+),\s*"(?P<lens_description>.*)"')
LENS_ENTRY_DEFAULT_RE = re.compile(r'^\{\s*(?P<lens_id>\d+),\s*"(?P<lens_description>.*)"')

LENS_META_DEFAULT_RE = re.compile(
(
Expand All @@ -18,9 +18,9 @@
".*?"
# maybe short focal length max aperture and hyphen, surely at least single max aperture e.g.: f/4.5-5.6
# short and tele indicate apertures at the short (focal_length_min) and tele (focal_length_max) position of the lens
"(?:(?:f\/)|T|F)(?:(?P<aperture_max_short>[0-9]+(?:\.[0-9]+)?)-)?(?P<aperture_max_tele>[0-9]+(?:\.[0-9])?)"
r"(?:(?:f\/)|T|F)(?:(?P<aperture_max_short>[0-9]+(?:\.[0-9]+)?)-)?(?P<aperture_max_tele>[0-9]+(?:\.[0-9])?)"
# check if there is a teleconverter pattern e.g. + 1.4x
"(?:.*?\+.*?(?P<tc>[0-9.]+)x)?"
r"(?:.*?\+.*?(?P<tc>[0-9.]+)x)?"
)
)

Expand Down Expand Up @@ -170,7 +170,7 @@ def extract_lenses_from_cpp(filename, start_pattern):
for content of metadata see extract_meta() function.
"""
lenses = []
with open(filename, "r") as f:
with open(filename, "r", encoding=None) as f:
in_lens_array = False

for line in f.readlines():
Expand All @@ -187,15 +187,15 @@ def extract_lenses_from_cpp(filename, start_pattern):
if in_lens_array:
lens_entry = parse_lens_entry(stripped)
if not lens_entry:
log.error(f"Failure parsing lens entry: {stripped}.")
log.error("Failure parsing lens entry: %s.", stripped)
continue

if lens_entry[1] == "n/a":
continue

meta = extract_meta(lens_entry[1])
if not meta:
log.error(f"Failure extracting metadata from lens description: {lens_entry[0]}: {lens_entry[1]}.")
log.error("Failure extracting metadata from lens description: %s: %s.", lens_entry[0], lens_entry[1])
continue

lenses.append({"id": lens_entry[0], "desc": lens_entry[1], "meta": meta})
Expand Down
9 changes: 3 additions & 6 deletions tests/system_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def configure_suite(config_file):
_parameters["timeout"] *= config.getfloat(
"General", "memcheck_timeout_penalty", fallback=20.0
)

# Configure the parameters for bash tests
BT.Config.bin_dir = os.path.abspath(config['ENV']['exiv2_path'])
BT.Config.dyld_library_path = os.path.abspath(config['ENV']['dyld_library_path'])
Expand Down Expand Up @@ -316,7 +316,7 @@ def __init__(self, *files):
"""
if len(files) == 0:
raise ValueError("No files supplied.")
elif len(files) == 1:
if len(files) == 1:
if isinstance(files[0], type):
raise UserWarning(
"Decorator used wrongly, must be called with "
Expand Down Expand Up @@ -370,7 +370,6 @@ def setUp_file_action(self, expanded_file_name):
The default implementation does nothing.
"""
pass

def new_tearDown(self, old_tearDown):
"""
Expand Down Expand Up @@ -460,7 +459,7 @@ def setUp_file_action(self, expanded_file_name):
fname, ext = os.path.splitext(expanded_file_name)
new_name = f"{fname}_copy{ext}"
return shutil.copyfile(expanded_file_name, new_name)

class CopyTmpFiles(FileDecoratorBase):
"""
This class copies files from test/data to test/tmp
Expand Down Expand Up @@ -804,7 +803,6 @@ def post_command_hook(self, i, command):
The default implementation does nothing.
"""
pass

def post_tests_hook(self):
"""
Expand All @@ -816,7 +814,6 @@ def post_tests_hook(self):
The default implementation does nothing.
"""
pass


class CaseMeta(type):
Expand Down

0 comments on commit 3ab890f

Please sign in to comment.