|
|
@@ -5,70 +5,70 @@ class Tag:
|
|
|
# Internals
|
|
|
_encoding = 'utf-8'
|
|
|
|
|
|
- def __init__(self, logLevel=logging.WARN):
|
|
|
+ def __init__(self, logLevel: int = logging.WARN) -> None:
|
|
|
self.logger = logging.getLogger(__name__)
|
|
|
self.logger.setLevel(logLevel)
|
|
|
|
|
|
- def get_int(self, path, attr):
|
|
|
+ def get_int(self, path: pathlib.Path, attr: str) -> int | None:
|
|
|
ret = self.get_bytes(path, attr)
|
|
|
if ret:
|
|
|
try:
|
|
|
- ret = int.from_bytes(ret)
|
|
|
+ return int.from_bytes(ret)
|
|
|
except Exception:
|
|
|
self.logger.error(f"Unable to decode int ({path}): {attr} => {ret}")
|
|
|
- return ret
|
|
|
+ return None
|
|
|
|
|
|
- def get_str(self, path, attr):
|
|
|
+ def get_str(self, path: pathlib.Path, attr: str) -> str | None:
|
|
|
ret = self.get_bytes(path, attr)
|
|
|
if ret:
|
|
|
try:
|
|
|
- ret = ret.decode(self._encoding)
|
|
|
+ return ret.decode(self._encoding)
|
|
|
except Exception:
|
|
|
self.logger.error(f"Unable to decode str ({path}): {attr} => {ret}")
|
|
|
- return ret
|
|
|
+ return None
|
|
|
|
|
|
- def get(self, path, attr):
|
|
|
+ def get(self, path: pathlib.Path, attr: str) -> str | None:
|
|
|
return self.get_str(path, attr)
|
|
|
|
|
|
- def get_bytes(self, path, attr):
|
|
|
+ def get_bytes(self, path: pathlib.Path, attr: str) -> bytes | None:
|
|
|
value = None
|
|
|
try:
|
|
|
if attr in xattr.listxattr(path):
|
|
|
value = xattr.getxattr(path, attr)
|
|
|
self.logger.debug(f"Got tag ({path}): {attr} => {value}")
|
|
|
- except BaseException as e:
|
|
|
+ except Exception as e:
|
|
|
self.logger.error(f"Get error ({path}:{attr}): {e}")
|
|
|
return value
|
|
|
|
|
|
- def set_bytes(self, path, attr, value):
|
|
|
+ def set_bytes(self, path: pathlib.Path, attr: str, value: bytes) -> bool:
|
|
|
try:
|
|
|
xattr.setxattr(path, attr, value)
|
|
|
self.logger.debug(f"Wrote tag ({path}): {attr} => {value}")
|
|
|
return True
|
|
|
- except BaseException as e:
|
|
|
+ except Exception as e:
|
|
|
self.logger.error(f"Set error ({path}:{attr}): {e}")
|
|
|
return False
|
|
|
|
|
|
- def set_int(self, path, attr, value):
|
|
|
+ def set_int(self, path: pathlib.Path, attr: str, value: int) -> bool:
|
|
|
try:
|
|
|
bval = value.to_bytes()
|
|
|
return self.set_bytes(path, attr, bval)
|
|
|
- except:
|
|
|
+ except Exception:
|
|
|
self.logger.error(f"Unable to encode int: {value}")
|
|
|
return False
|
|
|
|
|
|
- def set_str(self, path, attr, value):
|
|
|
+ def set_str(self, path: pathlib.Path, attr: str, value: str) -> bool:
|
|
|
try:
|
|
|
bval = value.encode(self._encoding)
|
|
|
return self.set_bytes(path, attr, bval)
|
|
|
- except:
|
|
|
+ except Exception:
|
|
|
self.logger.error(f"Unable to encode str: {value}")
|
|
|
return False
|
|
|
|
|
|
- def set(self, path, attr, value):
|
|
|
+ def set(self, path: pathlib.Path, attr: str, value: bytes | int | str) -> bool:
|
|
|
if isinstance(value, bytes):
|
|
|
return self.set_bytes(path, attr, value)
|
|
|
- elif hasattr(value, 'to_bytes'):
|
|
|
+ elif isinstance(value, int):
|
|
|
return self.set_int(path, attr, value)
|
|
|
elif isinstance(value, str):
|
|
|
return self.set_str(path, attr, value)
|