1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-10 15:04:16 +00:00

utils typing

This commit is contained in:
deadc0de6
2024-01-18 22:07:21 +01:00
parent 6dba75ff47
commit a2fba6a906

View File

@@ -24,12 +24,9 @@ from packaging import version
from dotdrop.logger import Logger from dotdrop.logger import Logger
from dotdrop.exceptions import UnmetDependency from dotdrop.exceptions import UnmetDependency
from dotdrop.version import __version__ as VERSION from dotdrop.version import __version__ as VERSION
from dotdrop.action import Action
from dotdrop.dotfile import Dotfile
from dotdrop.options import Options from dotdrop.options import Options
from dotdrop.profile import Profile from typing import Any, Callable, List, \
from ruamel.yaml.comments import CommentedSeq Optional, Tuple
from typing import Any, Callable, List, Optional, Tuple, Union
LOG = Logger() LOG = Logger()
STAR = '*' STAR = '*'
@@ -56,8 +53,8 @@ def run(cmd: List[str], debug: bool=False) -> Tuple[bool, str]:
stderr=subprocess.STDOUT) as proc: stderr=subprocess.STDOUT) as proc:
out, _ = proc.communicate() out, _ = proc.communicate()
ret = proc.returncode ret = proc.returncode
out = out.splitlines(keepends=True) outlines = out.splitlines(keepends=True)
lines = ''.join([x.decode('utf-8', 'replace') for x in out]) lines = ''.join([x.decode('utf-8', 'replace') for x in outlines])
return ret == 0, lines return ret == 0, lines
@@ -349,7 +346,7 @@ def must_ignore(paths: List[str],
def _cp(src: str, def _cp(src: str,
dst: str, dst: str,
ignore_func: Optional[Callable]=None, ignore_func: Optional[Callable[[str], bool]]=None,
debug: bool=False) -> int: debug: bool=False) -> int:
""" """
the copy function for copytree the copy function for copytree
@@ -388,7 +385,7 @@ def copyfile(src: str, dst: str, debug: bool=False) -> bool:
def copytree_with_ign(src: str, def copytree_with_ign(src: str,
dst: str, dst: str,
ignore_func: Optional[Callable]=None, ignore_func: Optional[Callable[[str], bool]]=None,
debug: bool=False) -> int: debug: bool=False) -> int:
""" """
copytree with support for ignore copytree with support for ignore
@@ -426,7 +423,7 @@ def copytree_with_ign(src: str,
def uniq_list(a_list: List[str]) -> List[str]: def uniq_list(a_list: List[str]) -> List[str]:
"""unique elements of a list while preserving order""" """unique elements of a list while preserving order"""
new = [] new: List[str] = []
if not a_list: if not a_list:
return new return new
for elem in a_list: for elem in a_list:
@@ -472,7 +469,7 @@ def ignores_to_absolute(ignores: List[str],
return new return new
def get_module_functions(mod): def get_module_functions(mod): # type: ignore
"""return a list of fonction from a module""" """return a list of fonction from a module"""
funcs = [] funcs = []
for memb in inspect.getmembers(mod): for memb in inspect.getmembers(mod):
@@ -483,7 +480,7 @@ def get_module_functions(mod):
return funcs return funcs
def get_module_from_path(path: str): def get_module_from_path(path: str): # type: ignore
"""get module from path""" """get module from path"""
if not path or not os.path.exists(path): if not path or not os.path.exists(path):
return None return None
@@ -491,10 +488,10 @@ def get_module_from_path(path: str):
# allow any type of files # allow any type of files
importlib.machinery.SOURCE_SUFFIXES.append('') importlib.machinery.SOURCE_SUFFIXES.append('')
# import module # import module
spec = importlib.util.spec_from_file_location(module_name, path) spec = importlib.util.spec_from_file_location(module_name, path) # type: ignore
if not spec: if not spec:
return None return None
mod = importlib.util.module_from_spec(spec) mod = importlib.util.module_from_spec(spec) # type: ignore
if not mod: if not mod:
return None return None
spec.loader.exec_module(mod) spec.loader.exec_module(mod)
@@ -519,7 +516,7 @@ def dependencies_met() -> None:
name = 'python-magic' name = 'python-magic'
err = f'missing python module \"{name}\"' err = f'missing python module \"{name}\"'
try: try:
import magic import magic # type: ignore
assert magic assert magic
if not hasattr(magic, 'from_file'): if not hasattr(magic, 'from_file'):
LOG.warn(err) LOG.warn(err)
@@ -547,7 +544,7 @@ def dependencies_met() -> None:
name = 'ruamel.yaml' name = 'ruamel.yaml'
err = f'missing python module \"{name}\"' err = f'missing python module \"{name}\"'
try: try:
from ruamel.yaml import YAML # noqa # pylint: disable=W0611 from ruamel.yaml import YAML # type: ignore # noqa # pylint: disable=W061
except ImportError as exc: except ImportError as exc:
raise UnmetDependency(err) from exc raise UnmetDependency(err) from exc
@@ -565,7 +562,7 @@ def dependencies_met() -> None:
name = 'tomli_w' name = 'tomli_w'
err = f'missing python module \"{name}\"' err = f'missing python module \"{name}\"'
try: try:
import tomli_w import tomli_w # type: ignore
assert tomli_w assert tomli_w
except ImportError as exc: except ImportError as exc:
raise UnmetDependency(err) from exc raise UnmetDependency(err) from exc
@@ -574,7 +571,7 @@ def dependencies_met() -> None:
name = 'distro' name = 'distro'
err = f'missing python module \"{name}\"' err = f'missing python module \"{name}\"'
try: try:
import distro import distro # type: ignore
assert distro assert distro
except ImportError as exc: except ImportError as exc:
raise UnmetDependency(err) from exc raise UnmetDependency(err) from exc
@@ -632,15 +629,15 @@ def adapt_workers(options: Options,
options.workers = 1 options.workers = 1
def categorize(function: Callable, iterable: List[str]) -> List[str]: def categorize(function: Callable[[str],bool],
iterable: List[str]) -> Tuple[List[str], List[str]]:
""" """
separate an iterable into elements for which separate an iterable into two lists:
function(element) is true for each element and - elements for which function(element) is true for each element
for which function(element) is false for each - elements for which function(element) is false for each element
element
""" """
return (tuple(filter(function, iterable)), return list(filter(function, iterable)), \
tuple(itertools.filterfalse(function, iterable))) list(itertools.filterfalse(function, iterable))
def debug_list(title: str, def debug_list(title: str,
@@ -724,7 +721,7 @@ def is_bin_in_path(command: str) -> bool:
""" """
check binary from command is in path check binary from command is in path
""" """
bpath = "" bpath: Optional[str | None] = ""
if not command: if not command:
return False return False
try: try: