|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# config.py |
| 3 | +# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors |
| 4 | +# |
| 5 | +# This module is part of GitPython and is released under |
| 6 | +# the BSD License: http://www.opensource.org/licenses/bsd-license.php |
| 7 | +"""utilities to help provide compatibility with python 3""" |
| 8 | +# flake8: noqa |
| 9 | + |
| 10 | +import locale |
| 11 | +import os |
| 12 | +import sys |
| 13 | + |
| 14 | +from gitdb.utils.encoding import ( |
| 15 | + force_bytes, # @UnusedImport |
| 16 | + force_text # @UnusedImport |
| 17 | +) |
| 18 | + |
| 19 | +# typing -------------------------------------------------------------------- |
| 20 | + |
| 21 | +from typing import ( |
| 22 | + Any, |
| 23 | + AnyStr, |
| 24 | + Dict, |
| 25 | + IO, |
| 26 | + Optional, |
| 27 | + Tuple, |
| 28 | + Type, |
| 29 | + Union, |
| 30 | + overload, |
| 31 | +) |
| 32 | +from git.types import TBD |
| 33 | + |
| 34 | +# --------------------------------------------------------------------------- |
| 35 | + |
| 36 | + |
| 37 | +is_win = (os.name == 'nt') # type: bool |
| 38 | +is_posix = (os.name == 'posix') |
| 39 | +is_darwin = (os.name == 'darwin') |
| 40 | +defenc = sys.getfilesystemencoding() |
| 41 | + |
| 42 | + |
| 43 | +@overload |
| 44 | +def safe_decode(s: None) -> None: ... |
| 45 | + |
| 46 | +@overload |
| 47 | +def safe_decode(s: Union[IO[str], AnyStr]) -> str: ... |
| 48 | + |
| 49 | +def safe_decode(s: Union[IO[str], AnyStr, None]) -> Optional[str]: |
| 50 | + """Safely decodes a binary string to unicode""" |
| 51 | + if isinstance(s, str): |
| 52 | + return s |
| 53 | + elif isinstance(s, bytes): |
| 54 | + return s.decode(defenc, 'surrogateescape') |
| 55 | + elif s is None: |
| 56 | + return None |
| 57 | + else: |
| 58 | + raise TypeError('Expected bytes or text, but got %r' % (s,)) |
| 59 | + |
| 60 | + |
| 61 | +@overload |
| 62 | +def safe_encode(s: None) -> None: ... |
| 63 | + |
| 64 | +@overload |
| 65 | +def safe_encode(s: AnyStr) -> bytes: ... |
| 66 | + |
| 67 | +def safe_encode(s: Optional[AnyStr]) -> Optional[bytes]: |
| 68 | + """Safely encodes a binary string to unicode""" |
| 69 | + if isinstance(s, str): |
| 70 | + return s.encode(defenc) |
| 71 | + elif isinstance(s, bytes): |
| 72 | + return s |
| 73 | + elif s is None: |
| 74 | + return None |
| 75 | + else: |
| 76 | + raise TypeError('Expected bytes or text, but got %r' % (s,)) |
| 77 | + |
| 78 | + |
| 79 | +@overload |
| 80 | +def win_encode(s: None) -> None: ... |
| 81 | + |
| 82 | +@overload |
| 83 | +def win_encode(s: AnyStr) -> bytes: ... |
| 84 | + |
| 85 | +def win_encode(s: Optional[AnyStr]) -> Optional[bytes]: |
| 86 | + """Encode unicodes for process arguments on Windows.""" |
| 87 | + if isinstance(s, str): |
| 88 | + return s.encode(locale.getpreferredencoding(False)) |
| 89 | + elif isinstance(s, bytes): |
| 90 | + return s |
| 91 | + elif s is not None: |
| 92 | + raise TypeError('Expected bytes or text, but got %r' % (s,)) |
| 93 | + return None |
| 94 | + |
| 95 | + |
| 96 | +def with_metaclass(meta: Type[Any], *bases: Any) -> TBD: # type: ignore ## mypy cannot understand dynamic class creation |
| 97 | + """copied from https://github.com/Byron/bcore/blob/master/src/python/butility/future.py#L15""" |
| 98 | + |
| 99 | + class metaclass(meta): # type: ignore |
| 100 | + __call__ = type.__call__ |
| 101 | + __init__ = type.__init__ # type: ignore |
| 102 | + |
| 103 | + def __new__(cls, name: str, nbases: Optional[Tuple[int, ...]], d: Dict[str, Any]) -> TBD: |
| 104 | + if nbases is None: |
| 105 | + return type.__new__(cls, name, (), d) |
| 106 | + return meta(name, bases, d) |
| 107 | + |
| 108 | + return metaclass(meta.__name__ + 'Helper', None, {}) # type: ignore |
0 commit comments