2905 (1, 1 << 10, _(b'%.2f KB')), |
2909 (1, 1 << 10, _(b'%.2f KB')), |
2906 (1, 1, _(b'%.0f bytes')), |
2910 (1, 1, _(b'%.0f bytes')), |
2907 ) |
2911 ) |
2908 |
2912 |
2909 |
2913 |
2910 class transformingwriter: |
2914 class transformingwriter(typelib.BinaryIO_Proxy): |
2911 """Writable file wrapper to transform data by function""" |
2915 """Writable file wrapper to transform data by function""" |
2912 |
2916 |
2913 def __init__(self, fp, encode): |
2917 def __init__(self, fp: BinaryIO, encode: Callable[[bytes], bytes]) -> None: |
2914 self._fp = fp |
2918 self._fp = fp |
2915 self._encode = encode |
2919 self._encode = encode |
2916 |
2920 |
2917 def close(self): |
2921 def close(self) -> None: |
2918 self._fp.close() |
2922 self._fp.close() |
2919 |
2923 |
2920 def flush(self): |
2924 def flush(self) -> None: |
2921 self._fp.flush() |
2925 self._fp.flush() |
2922 |
2926 |
2923 def write(self, data): |
2927 def write(self, data: bytes) -> int: |
2924 return self._fp.write(self._encode(data)) |
2928 return self._fp.write(self._encode(data)) |
2925 |
2929 |
2926 |
2930 |
2927 # Matches a single EOL which can either be a CRLF where repeated CR |
2931 # Matches a single EOL which can either be a CRLF where repeated CR |
2928 # are removed or a LF. We do not care about old Macintosh files, so a |
2932 # are removed or a LF. We do not care about old Macintosh files, so a |
2936 |
2940 |
2937 def tocrlf(s: bytes) -> bytes: |
2941 def tocrlf(s: bytes) -> bytes: |
2938 return _eolre.sub(b'\r\n', s) |
2942 return _eolre.sub(b'\r\n', s) |
2939 |
2943 |
2940 |
2944 |
2941 def _crlfwriter(fp): |
2945 def _crlfwriter(fp: typelib.BinaryIO_Proxy) -> typelib.BinaryIO_Proxy: |
2942 return transformingwriter(fp, tocrlf) |
2946 return transformingwriter(fp, tocrlf) |
2943 |
2947 |
2944 |
2948 |
2945 if pycompat.oslinesep == b'\r\n': |
2949 if pycompat.oslinesep == b'\r\n': |
2946 tonativeeol = tocrlf |
2950 tonativeeol = tocrlf |
2948 nativeeolwriter = _crlfwriter |
2952 nativeeolwriter = _crlfwriter |
2949 else: |
2953 else: |
2950 tonativeeol = pycompat.identity |
2954 tonativeeol = pycompat.identity |
2951 fromnativeeol = pycompat.identity |
2955 fromnativeeol = pycompat.identity |
2952 nativeeolwriter = pycompat.identity |
2956 nativeeolwriter = pycompat.identity |
|
2957 |
|
2958 if typing.TYPE_CHECKING: |
|
2959 # Replace the various overloads that come along with aliasing other methods |
|
2960 # with the narrow definition that we care about in the type checking phase |
|
2961 # only. This ensures that both Windows and POSIX see only the definition |
|
2962 # that is actually available. |
|
2963 |
|
2964 def tonativeeol(s: bytes) -> bytes: |
|
2965 raise NotImplementedError |
|
2966 |
|
2967 def fromnativeeol(s: bytes) -> bytes: |
|
2968 raise NotImplementedError |
|
2969 |
|
2970 def nativeeolwriter(fp: typelib.BinaryIO_Proxy) -> typelib.BinaryIO_Proxy: |
|
2971 raise NotImplementedError |
2953 |
2972 |
2954 |
2973 |
2955 # TODO delete since workaround variant for Python 2 no longer needed. |
2974 # TODO delete since workaround variant for Python 2 no longer needed. |
2956 def iterfile(fp): |
2975 def iterfile(fp): |
2957 return fp |
2976 return fp |