Source code for AutoArchive._infrastructure.utils.utils
# utils.py
#
# Project: AutoArchive
# License: GNU GPLv3
#
# Copyright (C) 2003 - 2023 Róbert Čerňanský
__all__ = ["Utils"]
import os
import platform
from abc import *
from functools import wraps
from typing import Mapping, TypeVar, Iterable
[docs]
class Utils(metaclass = ABCMeta):
"Various utility methods."
@abstractmethod
def __init__(self):
pass
[docs]
@staticmethod
def uniq(decorated):
"Decorator that filters out duplicate elements from an iterable returned by decorated function."
@wraps(decorated)
def wrapper(*args, **kwargs):
seen = set()
for item in decorated(*args, **kwargs):
if item not in seen:
seen.add(item)
yield item
return wrapper
[docs]
@staticmethod
def isWindows():
return platform.system() == "Windows"
[docs]
@staticmethod
def effectiveAccess(path, mode):
return os.access(path, mode, effective_ids = os.access in os.supports_effective_ids)
__K = TypeVar("__K")
__V = TypeVar("__V")
[docs]
@staticmethod
def getKeysForValue(value: __V, mapping: Mapping[__K, __V]) -> Iterable[__K]:
"""Returns keys of ``mapping`` which has value equal to ``value``"""
return (p[0] for p in filter(lambda pair: pair[1] == value, mapping.items()))