codekingpro/portable-devtools
114k
1# mypy: no-warn-unused-ignores2 3from __future__ import annotations4 5from configparser import ConfigParser6import io7import os8import sys9import typing10from typing import Any11from typing import List12from typing import Optional13from typing import Sequence14from typing import Union15 16if True:17 # zimports hack for too-long names18 from sqlalchemy.util import ( # noqa: F40119 inspect_getfullargspec as inspect_getfullargspec,20 )21 from sqlalchemy.util.compat import ( # noqa: F40122 inspect_formatargspec as inspect_formatargspec,23 )24 25is_posix = os.name == "posix"26 27py311 = sys.version_info >= (3, 11)28py310 = sys.version_info >= (3, 10)29py39 = sys.version_info >= (3, 9)30 31 32# produce a wrapper that allows encoded text to stream33# into a given buffer, but doesn't close it.34# not sure of a more idiomatic approach to this.35class EncodedIO(io.TextIOWrapper):36 def close(self) -> None:37 pass38 39 40if py39:41 from importlib import resources as _resources42 43 importlib_resources = _resources44 from importlib import metadata as _metadata45 46 importlib_metadata = _metadata47 from importlib.metadata import EntryPoint as EntryPoint48else:49 import importlib_resources # type:ignore # noqa50 import importlib_metadata # type:ignore # noqa51 from importlib_metadata import EntryPoint # type:ignore # noqa52 53 54def importlib_metadata_get(group: str) -> Sequence[EntryPoint]:55 ep = importlib_metadata.entry_points()56 if hasattr(ep, "select"):57 return ep.select(group=group)58 else:59 return ep.get(group, ()) # type: ignore60 61 62def formatannotation_fwdref(63 annotation: Any, base_module: Optional[Any] = None64) -> str:65 """vendored from python 3.7"""66 # copied over _formatannotation from sqlalchemy 2.067 68 if isinstance(annotation, str):69 return annotation70 71 if getattr(annotation, "__module__", None) == "typing":72 return repr(annotation).replace("typing.", "").replace("~", "")73 if isinstance(annotation, type):74 if annotation.__module__ in ("builtins", base_module):75 return repr(annotation.__qualname__)76 return annotation.__module__ + "." + annotation.__qualname__77 elif isinstance(annotation, typing.TypeVar):78 return repr(annotation).replace("~", "")79 return repr(annotation).replace("~", "")80 81 82def read_config_parser(83 file_config: ConfigParser,84 file_argument: Sequence[Union[str, os.PathLike[str]]],85) -> List[str]:86 if py310:87 return file_config.read(file_argument, encoding="locale")88 else:89 return file_config.read(file_argument)90 