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'

View File

@ -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

View File

@ -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"

View File

@ -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