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 <mail@georg-pfuetzenreuter.net>
This commit is contained in:
Georg Pfuetzenreuter 2024-09-24 01:15:14 +02:00
parent fd1cf9b337
commit 59b2f06566
Signed by: Georg
GPG Key ID: 1ED2F138E7E6FF57
4 changed files with 38 additions and 18 deletions

View File

@ -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' __version__ = '1.0.4'

View File

@ -58,8 +58,7 @@ def reduce_entries(acl):
Return: List of entries converted to strings Return: List of entries converted to strings
""" """
entries = acl.to_any_text().decode().split() entries = acl.to_any_text().decode().split()
entries = [entry for entry in entries if entry not in DEFAULT_ENTRIES] return [entry for entry in entries if entry not in DEFAULT_ENTRIES]
return entries
def parse_permission_string(strpermission): def parse_permission_string(strpermission):
@ -134,7 +133,7 @@ def parse_acl(acl): # noqa PLR0912, FIXME: uncomplexify this
Return: Complete ACL map Return: Complete ACL map
""" """
permap = { permap = {
permission: False for permission in DEFAULT_PERMISSIONS.keys() permission: False for permission in DEFAULT_PERMISSIONS
} }
outmap = { outmap = {
group: { group: {
@ -166,7 +165,7 @@ def parse_acl(acl): # noqa PLR0912, FIXME: uncomplexify this
for tag_high, tag_low in LIBACL_TAGS.items(): for tag_high, tag_low in LIBACL_TAGS.items():
if tag_low == tag_type: if tag_low == tag_type:
lowmap = permap.copy() lowmap = permap.copy()
for permission in lowmap.keys(): for permission in lowmap:
lowmap[permission] = getattr(permset, permission) lowmap[permission] = getattr(permset, permission)
outtag = tag_high outtag = tag_high
if tag_type == ACL_USER_OBJ: if tag_type == ACL_USER_OBJ:
@ -179,7 +178,7 @@ def parse_acl(acl): # noqa PLR0912, FIXME: uncomplexify this
outname = name outname = name
if outtag not in outmap: if outtag not in outmap:
outmap[outtag] = {} 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] del outmap[outtag][None]
outmap[outtag][outname] = lowmap outmap[outtag][outname] = lowmap
break break
@ -240,7 +239,7 @@ def apply_acl_to_path(acl, path):
""" """
if acl.valid() is not True: if acl.valid() is not True:
return ValueError('ACL is not ready to be applied.') return ValueError('ACL is not ready to be applied.')
acl.applyto(path) return acl.applyto(path)
def merge_acls(acl1, acl2): def merge_acls(acl1, acl2):
@ -261,9 +260,7 @@ def merge_acls(acl1, acl2):
for existing_entry in acl3: for existing_entry in acl3:
existing_tag_type = existing_entry.tag_type existing_tag_type = existing_entry.tag_type
if tag_type in [ACL_USER, ACL_GROUP, ACL_MASK]: 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 ):
if tag_type == existing_tag_type:
if tag_type == ACL_MASK or entry.qualifier == existing_entry.qualifier:
acl3.delete_entry(existing_entry) acl3.delete_entry(existing_entry)
acl3.append(entry) acl3.append(entry)
@ -308,11 +305,12 @@ def parse_acl_from_path(path):
return parse_acl(read_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): def debug_dump_acl_entries(acl):
for entry in acl: for entry in acl:
print(f'tag: {entry.tag_type}', end='') print(f'tag: {entry.tag_type}', end='') # noqa T201
try: try:
print(f' qual: {entry.qualifier}') print(f' qual: {entry.qualifier}') # noqa T201
except TypeError: except TypeError:
print() print() # noqa T201
print(f'read: {entry.permset.read}') print(f'read: {entry.permset.read}') # noqa T201

View File

@ -2,18 +2,27 @@
# https://docs.astral.sh/ruff/rules/ # https://docs.astral.sh/ruff/rules/
extend-select = [ extend-select = [
"A", # flake8-builtins "A", # flake8-builtins
"ARG", # flake8-unused-arguments
"BLE", # flake8-blind-except "BLE", # flake8-blind-except
"C4", # flake8-comprehensions "C4", # flake8-comprehensions
"COM", # flake8-commas "COM", # flake8-commas
"CPY001",# flake8-copyright
"E", # pycodestyle "E", # pycodestyle
"E261", # spaces before inline comments "E261", # spaces before inline comments
"ERA", # eradicate "ERA", # eradicate
"EXE", # flake8-executable "EXE", # flake8-executable
"FBT", # flake8-boolean-trap "FBT", # flake8-boolean-trap
"I", # isort "I", # isort
"INP", # flake8-no-pep420
"ISC", # flake8-implicit-str-concat "ISC", # flake8-implicit-str-concat
"N", # pep8-naming
"PL", # Pylint "PL", # Pylint
"RET", # flake8-return
"RSE", # flake8-raise
"RUF", # Ruff-specific rules
"S", # flake8-bandit "S", # flake8-bandit
"SIM", # flake8-simplify
"T20", # flake8-print
"UP", # pyupgrade "UP", # pyupgrade
"W", # pycodestyle "W", # pycodestyle
"YTT", # flake8-2020 "YTT", # flake8-2020
@ -29,7 +38,11 @@ explicit-preview-rules = true
[lint.per-file-ignores] [lint.per-file-ignores]
"pyacl/__init__.py" = ["PLC0414"] # allow explicit re-exports / avoid conflict with F401 "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] [lint.pydocstyle]
convention = "pep257" convention = "pep257"

View File

@ -37,20 +37,20 @@ def load_yamls(file1, file2):
return out return out
@mark.parametrize('aclin, aclout', load_yaml('matrix.yaml')) @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) have = acl.parse_acl_from_path_via_string(sample_file_with_acl)
assert aclout == have assert aclout == have
@mark.parametrize('aclin, aclout', load_yaml('matrix.yaml')) @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) have = acl.parse_acl_from_path(sample_file_with_acl)
assert aclout == have assert aclout == have
@mark.parametrize('mode', ['fresh', 'update']) @mark.parametrize('mode', ['fresh', 'update'])
@mark.parametrize('scenario, data', load_yaml('matrix-apply.yaml')) @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']) 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 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 assert acl.apply_acl_to_path(built_acl, sample_file) is None