[Zope3-checkins] CVS: Zope3/src/zope/app/fssync - committer.py:1.8

Guido van Rossum guido@python.org
Tue, 3 Jun 2003 13:09:23 -0400


Update of /cvs-repository/Zope3/src/zope/app/fssync
In directory cvs.zope.org:/tmp/cvs-serv20741/zope/app/fssync

Modified Files:
	committer.py 
Log Message:
Refactored the Committer class, separating it out (again!) into a
Checker class and a Committer class.  The Committer class no longer
modifies the filesystem; that's toFS()'s responsibility (also due for
a rewrite).  The unit tests for Committer are much more sensible now.


=== Zope3/src/zope/app/fssync/committer.py 1.7 => 1.8 === (441/541 lines abridged)
--- Zope3/src/zope/app/fssync/committer.py:1.7	Mon Jun  2 15:48:48 2003
+++ Zope3/src/zope/app/fssync/committer.py	Tue Jun  3 13:08:52 2003
@@ -42,10 +42,10 @@
 class SynchronizationError(Exception):
     pass
 
-class Committer(object):
-    """Commit changes from the filesystem to the object database.
+class Checker(object):
+    """Check that the filesystem is consistent with the object database.
 
-    The filesystem's originals should be consistent with the object database.
+    The public API consists of __init__(), check() and errors() only.
     """
 
     def __init__(self, metadata=None):
@@ -55,16 +55,8 @@
         self.metadata = metadata
         self.conflicts = []
 
-    def report_conflict(self, fspath, ignore_conflicts=False):
-        """Helper to report a conflict.
-
-        Conflicts can be retrieved by calling get_errors().
-        """
-        if not ignore_conflicts and fspath not in self.conflicts:
-            self.conflicts.append(fspath)
-
-    def get_errors(self):
-        """Get a list of errors (conflicts).
+    def errors(self):
+        """Return a list of errors (conflicts).
 
         The return value is a list of filesystem pathnames for which
         a conflict exists.  A conflict usually refers to a file that
@@ -77,15 +69,14 @@
         """
         return self.conflicts
 
-    def synch(self, container, name, fspath, ignore_conflicts=False):
-        """Synchronize an object or object tree from the filesystem.
+    def check(self, container, name, fspath):
+        """Compare an object or object tree from the filesystem.
 
-        If the originals on the filesystem is not uptodate, errors are
-        reported by calling report_conflict(), but no exception is
-        raised unless something unexpected is wrong.
+        If the originals on the filesystem are not uptodate, errors
+        are reported by calling conflict().
 

[-=- -=- -=- 441 lines omitted -=- -=- -=-]

+                obj = factory(name, None, data)
+                obj = removeAllProxies(obj)
+            else:
+                # Oh well, assume the file is an xml pickle
+                obj = load_file(fspath)
+
+    set_item(container, name, obj, replace)
+
+def set_item(container, name, obj, replace=False):
+    """Helper to set an item in a container or mapping."""
+    if IContainer.isImplementedBy(container):
+        if not replace:
+            publish(container, ObjectCreatedEvent(obj))
+        container = getAdapter(container, IZopeContainer)
+        if replace:
+            del container[name]
+        newname = container.setObject(name, obj)
+        if newname != name:
+            raise SynchronizationError(
+                "Container generated new name for %s (new name %s)" %
+                (name, newname))
+    else:
+        # Not a container, must be a mapping
+        # (This is used for extras and annotations)
+        container[name] = obj
+
+def delete_item(container, name):
+    """Helper to delete an item from a container or mapping."""
+    if IContainer.isImplementedBy(container):
+        container = getAdapter(container, IZopeContainer)
+    del container[name]
+
+def load_file(fspath):
+    """Helper to load an xml pickle from a file."""
+    return loads(read_file(fspath, "r"))
+
+def read_file(fspath, mode="rb"):
+    """Helper to read the data from a file."""
+    assert mode in ("r", "rb")
+    f = open(fspath, mode)
+    try:
+        data = f.read()
+    finally:
+        f.close()
+    return data
+
+def get_adapter(obj):
+    """Helper to get the special fssync adapter."""
+    syncService = getService(obj, 'FSRegistryService')
+    return syncService.getSynchronizer(obj)