"""Handles caching user and group database info in memory"""
"""Handles caching user and group database info in memory"""
# these lookups are fast, but when looking it up once per file chowned
# to form verbose output, it adds up
def __init__(self, role: str):
self._uid2name = {0: 'root'}
self._gid2name = {0: 'root'}
def getpwnam(self, name: str) -> pwd.struct_passwd:
"""Lookup and cache a struct_passwd by name
KeyError: user not found in /etc/passwd
pwd.struct_passwd: pwd.getpwnam() result
raise KeyError(f"The {name!r} user is missing") from exc
self._uid2name[val.pw_uid] = name
def getgrnam(self, name: str) -> grp.struct_group:
"""Lookup and cache a struct_group by name
KeyError: group not found in /etc/groups
grp.struct_group: grp.getgrnam() result
raise KeyError(f"The {name!r} group is missing") from exc
self._gid2name[val.gr_gid] = name
def uid_label(self, uid: int) -> str:
"""Lookup and cache a user name by UID
str: user name to display, or string of the uid if it was missing
return self._uid2name[uid]
self._uid2name[uid] = str(uid)
return self._uid2name[uid]
self._uid2name[uid] = val.pw_name
self._pwds[val.pw_name] = val
return self._uid2name[uid]
def gid_label(self, gid: int) -> str:
"""Lookup and cache a group name by GID
str: group name to display, or string of the gid if it was missing
return self._gid2name[gid]
self._gid2name[gid] = str(gid)
return self._gid2name[gid]
self._gid2name[gid] = val.gr_name
self._grps[val.gr_name] = val
return self._gid2name[gid]