Functions

Remove superfluous "acl_" prefix from function names, the module
is already called "acl".
Replace hardcoded test path with functions to parse a given file.

Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
This commit is contained in:
Georg Pfuetzenreuter 2024-09-16 04:10:02 +02:00
parent d48c773cb4
commit 3f58651401
Signed by: Georg
GPG Key ID: 1ED2F138E7E6FF57
2 changed files with 23 additions and 14 deletions

View File

@ -10,11 +10,6 @@ You may obtain copies of the Licence in any of the official languages at https:/
import posix1e import posix1e
myacl = posix1e.ACL(file='/tmp/testacl')
print(myacl)
myentries = list(myacl)
DEFAULT_ENTRIES = [ DEFAULT_ENTRIES = [
'u::rw-', 'u::rw-',
'g::r--', 'g::r--',
@ -37,12 +32,14 @@ DEFAULT_ENTRYTYPES = [
MAX_PERMBITS = 3 MAX_PERMBITS = 3
def acl_reduce_entries(acl):
def reduce_entries(acl):
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] entries = [entry for entry in entries if entry not in DEFAULT_ENTRIES]
return entries return entries
def acl_parse_permission(strpermission):
def parse_permission(strpermission):
if len(strpermission) != MAX_PERMBITS: if len(strpermission) != MAX_PERMBITS:
return ValueError('Invalid permission') return ValueError('Invalid permission')
@ -65,7 +62,8 @@ def acl_parse_permission(strpermission):
return outmap return outmap
def acl_parse_entry(strentry):
def parse_entry(strentry):
if not strentry: if not strentry:
raise ValueError('Got empty string') raise ValueError('Got empty string')
@ -84,16 +82,29 @@ def acl_parse_entry(strentry):
return { return {
entrytype: { entrytype: {
entryvalue: acl_parse_permission(permissions), entryvalue: parse_permission(permissions),
}, },
} }
def acl_parse_entries(acl):
def parse_entries(acl):
outmap = { outmap = {
group: DEFAULT_PERMISSIONS for group in DEFAULT_ENTRYTYPES group: DEFAULT_PERMISSIONS for group in DEFAULT_ENTRYTYPES
} }
for entry in acl: for entry in acl:
outmap.update(acl_parse_entry(entry)) outmap.update(parse_entry(entry))
return outmap return outmap
def aclfromfile(path):
return posix1e.ACL(file=path)
def entriesfromfile(path):
return reduce_entries(aclfromfile(path))
def parsefromfile(path):
return parse_entries(reduce_entries(aclfromfile(path)))

View File

@ -8,8 +8,6 @@ An English copy of the Licence is shipped in a file called LICENSE along with th
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. 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.
""" """
import posix1e
from pyacl import acl from pyacl import acl
@ -44,5 +42,5 @@ def test_parse_acl(sample_file):
}, },
}, },
} }
have = acl.acl_parse_entries(acl.acl_reduce_entries(posix1e.ACL(file=sample_file))) have = acl.parsefromfile(sample_file)
assert want == have assert want == have