From 59b2f06566be922fb8a53a0a85e747fb5bdd4162 Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Tue, 24 Sep 2024 01:15:14 +0200 Subject: [PATCH] More style - implement further code style practices for efficiency and simplicity - ensure all files contain a license header to avoid ambiguity - avoid leftover print statements in library functions Signed-off-by: Georg Pfuetzenreuter --- pyacl/__version__.py | 9 +++++++++ pyacl/acl.py | 26 ++++++++++++-------------- ruff.toml | 15 ++++++++++++++- tests/test_pyacl.py | 6 +++--- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/pyacl/__version__.py b/pyacl/__version__.py index 8a81504..f256179 100644 --- a/pyacl/__version__.py +++ b/pyacl/__version__.py @@ -1 +1,10 @@ +""" +Copyright 2024, pyacl contributors + +Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence"). +You may not use this work except in compliance with the Licence. +An English copy of the Licence is shipped in a file called LICENSE along with this applications source code. +You may obtain copies of the Licence in any of the official languages at https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12. +""" + __version__ = '1.0.4' diff --git a/pyacl/acl.py b/pyacl/acl.py index c877097..805f702 100644 --- a/pyacl/acl.py +++ b/pyacl/acl.py @@ -58,8 +58,7 @@ def reduce_entries(acl): Return: List of entries converted to strings """ entries = acl.to_any_text().decode().split() - entries = [entry for entry in entries if entry not in DEFAULT_ENTRIES] - return entries + return [entry for entry in entries if entry not in DEFAULT_ENTRIES] def parse_permission_string(strpermission): @@ -134,7 +133,7 @@ def parse_acl(acl): # noqa PLR0912, FIXME: uncomplexify this Return: Complete ACL map """ permap = { - permission: False for permission in DEFAULT_PERMISSIONS.keys() + permission: False for permission in DEFAULT_PERMISSIONS } outmap = { group: { @@ -166,7 +165,7 @@ def parse_acl(acl): # noqa PLR0912, FIXME: uncomplexify this for tag_high, tag_low in LIBACL_TAGS.items(): if tag_low == tag_type: lowmap = permap.copy() - for permission in lowmap.keys(): + for permission in lowmap: lowmap[permission] = getattr(permset, permission) outtag = tag_high if tag_type == ACL_USER_OBJ: @@ -179,7 +178,7 @@ def parse_acl(acl): # noqa PLR0912, FIXME: uncomplexify this outname = name if outtag not in outmap: outmap[outtag] = {} - if len(outmap[outtag]) == 1 and list(outmap[outtag].keys())[0] is None: + if len(outmap[outtag]) == 1 and next(iter(outmap[outtag].keys())) is None: del outmap[outtag][None] outmap[outtag][outname] = lowmap break @@ -240,7 +239,7 @@ def apply_acl_to_path(acl, path): """ if acl.valid() is not True: return ValueError('ACL is not ready to be applied.') - acl.applyto(path) + return acl.applyto(path) def merge_acls(acl1, acl2): @@ -261,10 +260,8 @@ def merge_acls(acl1, acl2): for existing_entry in acl3: existing_tag_type = existing_entry.tag_type - if tag_type in [ACL_USER, ACL_GROUP, ACL_MASK]: - if tag_type == existing_tag_type: - if tag_type == ACL_MASK or entry.qualifier == existing_entry.qualifier: - acl3.delete_entry(existing_entry) + if tag_type in [ACL_USER, ACL_GROUP, ACL_MASK] and tag_type == existing_tag_type and ( tag_type == ACL_MASK or entry.qualifier == existing_entry.qualifier ): + acl3.delete_entry(existing_entry) acl3.append(entry) @@ -308,11 +305,12 @@ def parse_acl_from_path(path): return parse_acl(read_acl_from_path(path)) +# ignoring T201, debug function is expected to print def debug_dump_acl_entries(acl): for entry in acl: - print(f'tag: {entry.tag_type}', end='') + print(f'tag: {entry.tag_type}', end='') # noqa T201 try: - print(f' qual: {entry.qualifier}') + print(f' qual: {entry.qualifier}') # noqa T201 except TypeError: - print() - print(f'read: {entry.permset.read}') + print() # noqa T201 + print(f'read: {entry.permset.read}') # noqa T201 diff --git a/ruff.toml b/ruff.toml index 2b27633..37d11bf 100644 --- a/ruff.toml +++ b/ruff.toml @@ -2,18 +2,27 @@ # https://docs.astral.sh/ruff/rules/ extend-select = [ "A", # flake8-builtins + "ARG", # flake8-unused-arguments "BLE", # flake8-blind-except "C4", # flake8-comprehensions "COM", # flake8-commas + "CPY001",# flake8-copyright "E", # pycodestyle "E261", # spaces before inline comments "ERA", # eradicate "EXE", # flake8-executable "FBT", # flake8-boolean-trap "I", # isort + "INP", # flake8-no-pep420 "ISC", # flake8-implicit-str-concat + "N", # pep8-naming "PL", # Pylint + "RET", # flake8-return + "RSE", # flake8-raise + "RUF", # Ruff-specific rules "S", # flake8-bandit + "SIM", # flake8-simplify + "T20", # flake8-print "UP", # pyupgrade "W", # pycodestyle "YTT", # flake8-2020 @@ -29,7 +38,11 @@ explicit-preview-rules = true [lint.per-file-ignores] "pyacl/__init__.py" = ["PLC0414"] # allow explicit re-exports / avoid conflict with F401 -"tests/*.py" = ["S101"] # allow "assert" in test suites +"tests/*.py" = [ + "INP001", # tests do not need to be part of a package + "S101", # allow "assert" in test suites + "T201", # lazy printing is ok in tests +] [lint.pydocstyle] convention = "pep257" diff --git a/tests/test_pyacl.py b/tests/test_pyacl.py index fd28b2d..ebca7a8 100644 --- a/tests/test_pyacl.py +++ b/tests/test_pyacl.py @@ -37,20 +37,20 @@ def load_yamls(file1, file2): return out @mark.parametrize('aclin, aclout', load_yaml('matrix.yaml')) -def test_parse_acl_through_string(sample_file_with_acl, aclin, aclout): +def test_parse_acl_through_string(sample_file_with_acl, aclout): have = acl.parse_acl_from_path_via_string(sample_file_with_acl) assert aclout == have @mark.parametrize('aclin, aclout', load_yaml('matrix.yaml')) -def test_parse_acl_native(sample_file_with_acl, aclin, aclout): +def test_parse_acl_native(sample_file_with_acl, aclout): have = acl.parse_acl_from_path(sample_file_with_acl) assert aclout == have @mark.parametrize('mode', ['fresh', 'update']) @mark.parametrize('scenario, data', load_yaml('matrix-apply.yaml')) -def test_build_and_apply_acl(sample_file, mode, scenario, data): +def test_build_and_apply_acl(sample_file, mode, scenario, data): # noqa ARG001, scenario is only used for easier inspection of the dataset built_acl = acl.build_acl(**data['args']) assert len(list(built_acl)) == 5 # noqa PLR2004, this is the expected size of the built ACL assert acl.apply_acl_to_path(built_acl, sample_file) is None