Mercurial > hgrepos > Python > apps > py-cutils
comparison cutils/util/__init__.py @ 372:bfe1160fbfd3
treesum: Make ERROR outputs more consistent: use native paths where possible
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sun, 13 Apr 2025 14:15:33 +0200 |
| parents | d5c920ace3cb |
| children | 54a6d4534ef4 |
comparison
equal
deleted
inserted
replaced
| 371:29a301ff2501 | 372:bfe1160fbfd3 |
|---|---|
| 12 | 12 |
| 13 __all__ = ["PY2", | 13 __all__ = ["PY2", |
| 14 "PY35", | 14 "PY35", |
| 15 "n", "b", "u", | 15 "n", "b", "u", |
| 16 "normalize_filename", | 16 "normalize_filename", |
| 17 "escape_for_output", | |
| 17 "argv2algo", | 18 "argv2algo", |
| 18 "algotag2algotype", | 19 "algotag2algotype", |
| 19 "algotag2digest_size", | 20 "algotag2digest_size", |
| 20 "get_blake2b", | 21 "get_blake2b", |
| 21 "get_blake2b_256", | 22 "get_blake2b_256", |
| 42 """Convert `s` to the native string implementation""" | 43 """Convert `s` to the native string implementation""" |
| 43 if isinstance(s, unicode): # noqa: F821 undefined name 'unicode' | 44 if isinstance(s, unicode): # noqa: F821 undefined name 'unicode' |
| 44 return s.encode(encoding) | 45 return s.encode(encoding) |
| 45 return s | 46 return s |
| 46 | 47 |
| 47 def b(s, encoding="ascii"): | 48 def b(s, encoding="ascii", errors="strict"): |
| 48 """Convert `s` to bytes""" | 49 """Convert `s` to bytes""" |
| 49 if isinstance(s, unicode): # noqa: F821 undefined name 'unicode' | 50 if isinstance(s, unicode): # noqa: F821 undefined name 'unicode' |
| 50 return s.encode(encoding) | 51 return s.encode(encoding, errors) |
| 51 return s | 52 return s |
| 52 | 53 |
| 53 def u(s, encoding="ascii"): | 54 def u(s, encoding="ascii"): |
| 54 """Convert `s` to a unicode string""" | 55 """Convert `s` to a unicode string""" |
| 55 if isinstance(s, str): | 56 if isinstance(s, str): |
| 62 """Convert `s` to the native string implementation""" | 63 """Convert `s` to the native string implementation""" |
| 63 if isinstance(s, (bytes, bytearray)): | 64 if isinstance(s, (bytes, bytearray)): |
| 64 return s.decode(encoding) | 65 return s.decode(encoding) |
| 65 return s | 66 return s |
| 66 | 67 |
| 67 def b(s, encoding="ascii"): | 68 def b(s, encoding="ascii", errors="strict"): |
| 68 """Convert `s` to bytes""" | 69 """Convert `s` to bytes""" |
| 69 if isinstance(s, str): | 70 if isinstance(s, str): |
| 70 return s.encode(encoding) | 71 return s.encode(encoding, errors) |
| 71 return s | 72 return s |
| 72 | 73 |
| 73 u = n | 74 u = n |
| 75 | |
| 76 | |
| 77 def escape_for_output(what): | |
| 78 """Escape `what` in such a way that the output can be safely written into | |
| 79 a line and/or column-oriented output file | |
| 80 | |
| 81 """ | |
| 82 if isinstance(what, bytes): | |
| 83 return (what.replace(b'\\', b"\\\\") | |
| 84 .replace(b'\n', b"\\x0a") | |
| 85 .replace(b'\r', b"\\x0d") | |
| 86 .replace(b'\t', b"\\x09")) | |
| 87 else: | |
| 88 return (what.replace(u'\\', u"\\\\") | |
| 89 .replace(u'\n', u"\\x0a") | |
| 90 .replace(u'\r', u"\\x0d") | |
| 91 .replace(u'\t', u"\\x09")) | |
| 74 | 92 |
| 75 | 93 |
| 76 def default_algotag(): | 94 def default_algotag(): |
| 77 """Determine the "best" default algorithm. | 95 """Determine the "best" default algorithm. |
| 78 | 96 |
