def wrapper(*args, **kwargs): def get_name(): # Returns method name return fun.__name__.replace("_", " ") error = None msg = None try: return fun(*args, **kwargs) except FileNotFoundError: error = errno.ENOENT msg = strerror(error) except KeyError: error = errno.ENODATA msg = strerror(error) except TypeError as exc: error = errno.ENOMSG msg = "%s: %s" % (strerror(errno.ENOMSG), exc.args[0]) except ValueError as exc: error = errno.ENOMSG msg = "%s: %s" % (strerror(errno.ENOMSG), exc.args[0]) finally: if error is not None: raise ValueNotFoundError(get_name(), _PTH, err_no=error, msg=msg)
def _get_stat(): # Look for temp#* files in hwmon folders and associate labels with their inputs. # E.g. temp1_label.read() = "Package id 0" and temp1_input.read() = "4500" # temp2_label.read() = "Core 0" and temp2_input.read() = "4100" # will return {"Core 0": 41000, 'Package id 0': 45000} res = {} err = None for hwmon in listdir(_PARENT): for file in listdir(join(_PARENT, hwmon)): if file.endswith("label"): path_l = join(_PARENT, hwmon, file) with open(path_l, "rb") as file_l: label = file_l.readline()[:-1].decode() path_i = path_l.replace("label", "input") try: with open(path_i, "rb") as file_i: res[label] = int(file_i.readline()[:-1]) except ValueError: err = 42 except FileNotFoundError: err = 2 finally: if err is not None: raise ValueNotFoundError("%s value" % label, path_i, err) return res
def _check_interface(interface: str, stat: dict): if interface not in _interfaces_checked: if interface not in stat.keys(): raise ValueNotFoundError(interface, _PROC_STAT, errno.ENODEV) else: _interfaces_checked.append(interface) return interface
def _check_block(block): valid_block_devices = block_devices() if block not in valid_block_devices: raise ValueNotFoundError(block, "%s%s/" % (_BLOCK_DEV, block), errno.ENODEV, msg="%s not found in %s" % (block, _BLOCK_DEV)) return block
def linux_distribution(): """Returns a tuple with the Linux distribution info (distro id, distro version, version codename) Emulation of the output of the linux_distribution() method (deprecated) of sys.platform. Unlike sys.platform.linux_distribution, info it's got from /etc/os-release """ info = _get_os_release() try: return info["ID"], info["VERSION_ID"], info["VERSION_CODENAME"] except KeyError as ex: key = ex.args[0] value = "distro id" if key == "ID" else "distro version" if key == "VERSION_ID" else "version codename" raise ValueNotFoundError(value, _OS_RELEASE, 61)
def _get_val(*items) -> list: with open(_MEMINFO, "rb") as file: values = [] indices = [] l_items = len(items) for l in file: for i, value in enumerate(items): if l.startswith(value): values.append(int(l.split()[-2])) indices.append(i) if len(values) == l_items: break if len(values) != l_items: nf = [items[k].decode() for k in [j for j in list(range(l_items)) if j not in indices]] raise ValueNotFoundError(" ".join(nf), _MEMINFO, 61) return values
def mounts_info() -> dict: """Returns a dict with mounted partitions and namedtuple with mount point, filesystem and mount options""" from collections import namedtuple with open(_MOUNTS, "r") as file: data = namedtuple("mounts", "mount_point filesystem mount_options") res = {} for line in file.readlines(): ls = line.split() if ls[0].startswith("/dev"): dev = ls[0][5:] # Uncomment and indent 2 lines below to discard non-pure partitions (e.g. loop1) # if dev in partitions(): res[dev] = data(ls[1].replace("\\040", " "), ls[2], " ".join(ls[3:])) if not res: raise ValueNotFoundError("mounted partitions info", _MOUNTS, errno.ENODATA) return res
def lid_state(): """Returns lid state ('Open' or 'Close')""" lid = None error = None try: for folder in listdir(_LID): if folder.startswith("LID"): lid = folder break if lid is not None: with open(join(_LID, lid, "state"), "r") as f: return f.readline().split()[1] else: error: errno.ENODATA except FileNotFoundError: error = errno.ENOENT finally: if error is not None: raise ValueNotFoundError("lid state", _LID, error)
def mounted_partitions() -> dict: """Returns a dict with mounted partitions and mount points""" def get_mounts(): with open(_MOUNTS, "r") as file: res = {} for line in file.readlines(): prt = line.split() if line.startswith("/"): res[prt[0]] = prt[1].replace("\\040", " ") return res mounts = get_mounts() result = { partition: mounts[_DEV + partition] for partition in partitions() if _DEV + partition in mounts.keys() } if not result: raise ValueNotFoundError("partitions", _MOUNTS, errno.ENODATA) return result
def supply_type(): """Returns type of supply ('Battery', 'Mains', 'UPS', etc)""" try: return _get_stat("type", supply="BAT")[0][:-1] except IndexError: raise ValueNotFoundError("supply type", _PTH, 61)
def distro_url() -> str: """Returns distro home url""" try: return _get_os_release()["HOME_URL"] except KeyError: raise ValueNotFoundError("distro url", _OS_RELEASE, 61)
def distro_version() -> str: """Return distro version""" try: return _get_os_release()["VERSION"] except KeyError: raise ValueNotFoundError("distro version", _OS_RELEASE, 61)
def distro_full_name() -> str: """Returns full distro description""" try: return _get_os_release()["PRETTY_NAME"] except KeyError: raise ValueNotFoundError("distro description", _OS_RELEASE, 61)
def distro_name() -> str: """Returns distro short name""" try: return _get_os_release()["NAME"] except KeyError: raise ValueNotFoundError("distro name", _OS_RELEASE, 61)
def _check_interface(interface: str, stat: dict): if interface not in stat.keys(): raise ValueNotFoundError(interface, _STAT_PATH, errno.ENODEV) return interface
def _check_partitions(*partitions_): valid_partitions = partitions() for ptt in partitions_: if ptt not in valid_partitions: raise ValueNotFoundError(ptt, _PARTITIONS, errno.ENODATA) return partitions_