diff cutils/util/__init__.py @ 188:2784fdcc99e5

Implement basic parsing of treesum output. Including CRC32 checks.
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 15 Jan 2025 14:41:36 +0100
parents f04d4b1c14b3
children 0f4febf646f5
line wrap: on
line diff
--- a/cutils/util/__init__.py	Tue Jan 14 13:32:25 2025 +0100
+++ b/cutils/util/__init__.py	Wed Jan 15 14:41:36 2025 +0100
@@ -9,6 +9,7 @@
 
 __all__ = ["PY2",
            "PY35",
+           "n", "b", "u",
            "normalize_filename",
            "argv2algo",
            "algotag2algotype",
@@ -17,6 +18,7 @@
            "get_blake2s",
            "default_algotag",
            "fsencode",
+           "interpolate_bytes",
            ]
 
 
@@ -30,6 +32,43 @@
 PY35 = sys.version_info[:2] >= (3, 5)
 
 
+if PY2:
+
+    def n(s, encoding="ascii"):
+        """Convert `s` to the native string implementation"""
+        if isinstance(s, unicode):       # noqa: F821 undefined name 'unicode'
+            return s.encode(encoding)
+        return s
+
+    def b(s, encoding="ascii"):
+        """Convert `s` to bytes"""
+        if isinstance(s, unicode):       # noqa: F821 undefined name 'unicode'
+            return s.encode(encoding)
+        return s
+
+    def u(s, encoding="ascii"):
+        """Convert `s` to a unicode string"""
+        if isinstance(s, str):
+            return s.decode(encoding)
+        return s
+
+else:
+
+    def n(s, encoding="ascii"):
+        """Convert `s` to the native string implementation"""
+        if isinstance(s, (bytes, bytearray)):
+            return s.decode(encoding)
+        return s
+
+    def b(s, encoding="ascii"):
+        """Convert `s` to bytes"""
+        if isinstance(s, str):
+            return s.encode(encoding)
+        return s
+
+    u = n
+
+
 def default_algotag():
     """Determine the "best" default algorithm.