diff cutils/util/fnmatch.py @ 308:652870b20f9e

treesum: Implement --accept-treesum: trust a treesum-file for a directory checksum
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 08 Mar 2025 04:49:06 +0100
parents 6c212e407524
children f5f54b9c3552
line wrap: on
line diff
--- a/cutils/util/fnmatch.py	Fri Mar 07 14:22:22 2025 +0100
+++ b/cutils/util/fnmatch.py	Sat Mar 08 04:49:06 2025 +0100
@@ -15,6 +15,7 @@
 
 import re
 
+from . import PY2
 from . import glob
 
 
@@ -95,8 +96,6 @@
         "fullpath": fullpath_factory,
     }
 
-    VISIT_DEFAULT = True    # Overall default value for visiting
-
     def __init__(self, matchers):
         super(FnMatcher, self).__init__()
         self._matchers = matchers
@@ -106,6 +105,7 @@
         matchers = []
         if filter_definitions:
             for action, kpattern in filter_definitions:
+                assert action in ("include", "exclude", "accept-treesum")
                 kind, sep, pattern = kpattern.partition(':')
                 if not sep:
                     # use the default
@@ -117,19 +117,36 @@
                 matchers.append((action, kind, factory(pattern), pattern))
         return klass(matchers)
 
-    def shall_visit(self, fn, default=None):
-        visit = default if default is not None else self.VISIT_DEFAULT
+    def shall_visit(self, fn, default=True):
+        visit = default
         for action, kind, matcher, orig_pattern in self._matchers:
-            res = matcher(fn)
-            if res:
+            if matcher(fn):
                 if action == "include":
                     visit = True
-                elif action == "exclude":
+                elif action in ("exclude", "accept-treesum"):
                     visit = False
                 else:
                     raise RuntimeError("unknown action: {}".format(action))
         return visit
 
+    def shall_accept_treesum(self, fn, default=False):
+        accept = default
+        for action, kind, matcher, orig_pattern in self._matchers:
+            if action == "accept-treesum":
+                if matcher(fn):
+                    accept = True
+            elif action in ("include", "exclude"):
+                pass
+            else:
+                raise RuntimeError("unknown action: {}".format(action))
+        return accept
+
     def definitions(self):
         for action, kind, matcher, orig_pattern in self._matchers:
             yield (action, kind, orig_pattern)
+
+    def __bool__(self):
+        return bool(self._matchers)
+
+    if PY2:
+        __nonzero__ = __bool__