[Commits] (vajda) - reworked copy code with copyFn

commits at osafoundation.org commits at osafoundation.org
Sun Aug 1 04:14:42 PDT 2004


Commit by: vajda
Modified files:
chandler/repository/item/Item.py 1.146 1.147
chandler/repository/item/ItemRef.py 1.83 1.84
chandler/repository/item/PersistentCollections.py 1.17 1.18
chandler/repository/item/Values.py 1.12 1.13
chandler/repository/schema/Cloud.py 1.8 1.9

Log message:
   - reworked copy code with copyFn


ViewCVS links:
http://cvs.osafoundation.org/index.cgi/chandler/repository/item/Item.py.diff?r1=text&tr1=1.146&r2=text&tr2=1.147
http://cvs.osafoundation.org/index.cgi/chandler/repository/item/ItemRef.py.diff?r1=text&tr1=1.83&r2=text&tr2=1.84
http://cvs.osafoundation.org/index.cgi/chandler/repository/item/PersistentCollections.py.diff?r1=text&tr1=1.17&r2=text&tr2=1.18
http://cvs.osafoundation.org/index.cgi/chandler/repository/item/Values.py.diff?r1=text&tr1=1.12&r2=text&tr2=1.13
http://cvs.osafoundation.org/index.cgi/chandler/repository/schema/Cloud.py.diff?r1=text&tr1=1.8&r2=text&tr2=1.9

Index: chandler/repository/schema/Cloud.py
diff -u chandler/repository/schema/Cloud.py:1.8 chandler/repository/schema/Cloud.py:1.9
--- chandler/repository/schema/Cloud.py:1.8	Sat Jul 31 08:41:46 2004
+++ chandler/repository/schema/Cloud.py	Sun Aug  1 04:14:41 2004
@@ -1,6 +1,6 @@
 
-__revision__  = "$Revision: 1.8 $"
-__date__      = "$Date: 2004/07/31 15:41:46 $"
+__revision__  = "$Revision: 1.9 $"
+__date__      = "$Date: 2004/08/01 11:14:41 $"
 __copyright__ = "Copyright (c) 2002 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -110,6 +110,9 @@
               everywhere they occur.
 
             - any other item references are not set on the item copies.
+
+        The copy of the cloud entrypoint, C{item}, is first in the results
+        list.
         
         @param item: the entry point of the cloud.
         @type item: an C{Item} instance
@@ -129,48 +132,30 @@
         if copies is None:
             copies = {}
 
-        copy = item.copy(name, parent, copies, 'remove')
-        results = [copy]
-
-        def copyValue(item, name, value):
-            uuid = value._uuid
+        results = []
+        def copyOther(copy, other, policy):
+            uuid = other._uuid
             if uuid in items:
                 if uuid in copies:
                     return copies[uuid]
                 else:
-                    value = value.copy(None, parent, copies, 'remove')
-                    results.append(value)
-                    return value
+                    other = other.copy(None, parent, copies, 'remove',
+                                       None, copyOther)
+                    results.append(other)
+                    return other
             elif uuid in references:
-                return value
+                return other
             else:
                 return None
 
+        copy = item.copy(name, parent, copies, 'remove', None, copyOther)
+        results.insert(0, copy)
+
         for item in copying:
-            if not item._uuid in copies:
-                copy = item.copy(None, parent, copies, 'remove')
-            else:
-                copy = copies[item._uuid]
+            if item._uuid not in copies:
+                results.append(item.copy(None, parent, copies, 'remove',
+                                         None, copyOther))
                 
-            refs = copy._references
-            for name, value in item.iterAttributeValues(referencesOnly=True):
-                if isinstance(value, Item):
-                    value = copyValue(copy, name, value)
-                    if value is not None and name not in refs:
-                        copy.setAttributeValue(name, value, _attrDict=refs)
-                elif value is None:
-                    copy.setAttributeValue(name, None, _attrDict=refs)
-                else:
-                    if name in refs:
-                        refDict = refs[name]
-                    else:
-                        refDict = refs[name] = copy._refDict(name)
-                        
-                    for v in value:
-                        v = copyValue(copy, name, v)
-                        if v is not None and v not in refDict:
-                            refDict.append(v)
-
         return results
 
     def getAttributeEndpoints(self, attrName, index=0, cloudAlias=None):

Index: chandler/repository/item/Values.py
diff -u chandler/repository/item/Values.py:1.12 chandler/repository/item/Values.py:1.13
--- chandler/repository/item/Values.py:1.12	Thu Jul 29 07:07:12 2004
+++ chandler/repository/item/Values.py	Sun Aug  1 04:14:40 2004
@@ -1,6 +1,6 @@
 
-__revision__  = "$Revision: 1.12 $"
-__date__      = "$Date: 2004/07/29 14:07:12 $"
+__revision__  = "$Revision: 1.13 $"
+__date__      = "$Date: 2004/08/01 11:14:40 $"
 __copyright__ = "Copyright (c) 2002 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -23,15 +23,13 @@
 
         self._item = item
 
-    def _copy(self, orig, copies, copyPolicy):
+    def _copy(self, orig, copyPolicy, copyFn):
 
         item = self._item
-
         for name, value in orig.iteritems():
-
             if isinstance(value, PersistentCollection):
                 self[name] = value._copy(item, name, value._companion,
-                                         copies, copyPolicy)
+                                         copyPolicy, copyFn)
 
             elif isinstance(value, ItemValue):
                 value = value._copy(item, name)
@@ -42,24 +40,11 @@
                 policy = (copyPolicy or
                           item.getAttributeAspect(name, 'copyPolicy',
                                                   default='copy'))
-                if policy == 'copy':
-                    self[name] = value
+                other = item.find(value.itsUUID)
+                copyOther = copyFn(item, other, policy)
 
-                elif policy == 'cascade':
-                    uuid = value.itsUUID
-                    value = copies.get(uuid, None)
-
-                    if value is None:
-                        value = item.find(uuid)
-
-                        if orig._item.itsParent is item.itsParent:
-                            valueParent = value.itsParent
-                        else:
-                            valueParent = item.itsParent
-                        value = value.copy(None, valueParent, copies,
-                                           copyPolicy)
-                        
-                    self[name] = SingleRef(value.itsUUID)
+                if copyOther is not None:
+                    self[name] = SingleRef(copyOther.itsUUID)
             else:
                 self[name] = value
 
@@ -188,14 +173,12 @@
 
         self._item = item
 
-    def _copy(self, orig, copies, copyPolicy):
+    def _copy(self, orig, copyPolicy, copyFn):
 
         item = self._item
         for name, value in orig.iteritems():
             policy = copyPolicy or item.getAttributeAspect(name, 'copyPolicy')
-            if policy == 'copy' or policy == 'cascade':
-                value._copy(self, orig._item, item, name,
-                            policy, copyPolicy, copies)
+            value._copy(self, orig._item, item, name, policy, copyFn)
 
     def __setitem__(self, key, value, *args):
 

Index: chandler/repository/item/ItemRef.py
diff -u chandler/repository/item/ItemRef.py:1.83 chandler/repository/item/ItemRef.py:1.84
--- chandler/repository/item/ItemRef.py:1.83	Sat Jul 31 08:41:44 2004
+++ chandler/repository/item/ItemRef.py	Sun Aug  1 04:14:40 2004
@@ -1,6 +1,6 @@
 
-__revision__  = "$Revision: 1.83 $"
-__date__      = "$Date: 2004/07/31 15:41:44 $"
+__revision__  = "$Revision: 1.84 $"
+__date__      = "$Date: 2004/08/01 11:14:40 $"
 __copyright__ = "Copyright (c) 2002 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -21,31 +21,14 @@
         self.attach(item, name, other, otherName,
                     otherCard, otherPersist, otherAlias)
 
-    def _copy(self, references, item, copyItem, name,
-              policy, copyPolicy, copies):
+    def _copy(self, references, item, copyItem, name, policy, copyFn):
 
-        # policy: actual copy policy, ie, copyPolicy or aspect value
-        # copyPolicy: optional overriding copy policy to be passed on
+        copyOther = copyFn(copyItem, self.other(item), policy)
 
-        if policy == 'copy':
-            references[name] = ItemRef(copyItem, name, self.other(item),
+        if copyOther is not None and name not in references:
+            references[name] = ItemRef(copyItem, name, copyOther,
                                        copyItem._kind.getOtherName(name))
 
-        elif policy == 'cascade':
-            other = self.other(item)
-            copyOther = copies.get(other.itsUUID, None)
-
-            if copyOther is None:
-                if item.itsParent is copyItem.itsParent:
-                    otherParent = other.itsParent
-                else:
-                    otherParent = copyItem.itsParent
-                copyOther = other.copy(None, otherParent, copies, copyPolicy)
-
-            if not name in references:
-                references[name] = ItemRef(copyItem, name, copyOther,
-                                           copyItem._kind.getOtherName(name))
-
     def __repr__(self):
 
         return '<ItemRef: %s - %s>' %(self._item, self._other)
@@ -224,8 +207,7 @@
     def __repr__(self):
         return '<NoneRef>'
 
-    def _copy(self, references, item, copyItem, name,
-              policy, copyPolicy, copies):
+    def _copy(self, references, item, copyItem, name, policy, copyFn):
         return self
 
     def attach(self, item, name, other, otherName,
@@ -496,11 +478,7 @@
         
         super(RefDict, self).__init__()
 
-    def _copy(self, references, item, copyItem, name,
-              policy, copyPolicy, copies):
-
-        # policy: actual copy policy, ie, copyPolicy or aspect value
-        # copyPolicy: optional overriding copy policy to be passed on
+    def _copy(self, references, item, copyItem, name, policy, copyFn):
 
         try:
             refDict = references[name]
@@ -508,29 +486,11 @@
             refDict = copyItem._refDict(name)
             references[name] = refDict
 
-        if policy == 'copy':
-            for key in self.iterkeys():
-                link = self._get(key)
-                other = link._value.other(item)
-                if not other in refDict:
-                    refDict.append(other, link._alias)
-
-        elif policy == 'cascade':
-            for key in self.iterkeys():
-                link = self._get(key)
-                other = link._value.other(item)
-                copyOther = copies.get(other.itsUUID, None)
-
-                if copyOther is None:
-                    if item.itsParent is copyItem.itsParent:
-                        otherParent = other.itsParent
-                    else:
-                        otherParent = copyItem.itsParent
-                    copyOther = other.copy(None, otherParent, copies,
-                                           copyPolicy)
-
-                if not copyOther in refDict:
-                    refDict.append(copyOther, link._alias)
+        for key in self.iterkeys():
+            link = self._get(key)
+            copyOther = copyFn(copyItem, link._value.other(item), policy)
+            if copyOther is not None and copyOther not in refDict:
+                refDict.append(copyOther, link._alias)
 
         return refDict
 

Index: chandler/repository/item/Item.py
diff -u chandler/repository/item/Item.py:1.146 chandler/repository/item/Item.py:1.147
--- chandler/repository/item/Item.py:1.146	Fri Jul 30 04:37:36 2004
+++ chandler/repository/item/Item.py	Sun Aug  1 04:14:40 2004
@@ -1,6 +1,6 @@
 
-__revision__  = "$Revision: 1.146 $"
-__date__      = "$Date: 2004/07/30 11:37:36 $"
+__revision__  = "$Revision: 1.147 $"
+__date__      = "$Date: 2004/08/01 11:14:40 $"
 __copyright__ = "Copyright (c) 2002 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -1282,7 +1282,7 @@
         return False
 
     def copy(self, name=None, parent=None, copies=None,
-             copyPolicy=None, cloudAlias=None):
+             copyPolicy=None, cloudAlias=None, copyFn=None):
         """
         Copy this item.
 
@@ -1338,8 +1338,25 @@
         item._status |= Item.NEW
         copies[self._uuid] = item
 
-        item._values._copy(self._values, copies, copyPolicy)
-        item._references._copy(self._references, copies, copyPolicy)
+        def copyOther(copy, other, policy):
+            if policy == 'copy':
+                return other
+            elif policy == 'cascade':
+                copyOther = copies.get(other.itsUUID, None)
+                if copyOther is None:
+                    if self.itsParent is copy.itsParent:
+                        parent = other.itsParent
+                    else:
+                        parent = copy.itsParent
+                    copyOther = other.copy(None, parent, copies, copyPolicy)
+                return copyOther
+            else:
+                return None
+
+        if copyFn is None:
+            copyFn = copyOther
+        item._values._copy(self._values, copyPolicy, copyFn)
+        item._references._copy(self._references, copyPolicy, copyFn)
 
         if hasattr(cls, 'onItemCopy'):
             item.onItemCopy(self)

Index: chandler/repository/item/PersistentCollections.py
diff -u chandler/repository/item/PersistentCollections.py:1.17 chandler/repository/item/PersistentCollections.py:1.18
--- chandler/repository/item/PersistentCollections.py:1.17	Sat Jul 31 08:41:44 2004
+++ chandler/repository/item/PersistentCollections.py	Sun Aug  1 04:14:40 2004
@@ -1,6 +1,6 @@
 
-__revision__  = "$Revision: 1.17 $"
-__date__      = "$Date: 2004/07/31 15:41:44 $"
+__revision__  = "$Revision: 1.18 $"
+__date__      = "$Date: 2004/08/01 11:14:40 $"
 __copyright__ = "Copyright (c) 2002 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -67,7 +67,7 @@
 
         if isinstance(value, PersistentCollection):
             value = value._copy(self._item, self._attribute, self._companion,
-                                {}, 'copy')
+                                'copy', lambda x, other, z: other)
         elif isinstance(value, list):
             value = PersistentList(self._item, self._attribute,
                                    self._companion, value)
@@ -99,28 +99,6 @@
                 if not self._item.hasValue(self._companion, value):
                     self._item.addValue(self._companion, value)
 
-    def _copyItem(self, value, item, copies, policy, copyPolicy):
-
-        # value: item value to copy
-        # item: item being copied causing item value to be copied
-
-        if policy == 'copy':
-            return value
-
-        if policy == 'cascade':
-            valueCopy = copies.get(value.itsUUID, None)
-
-            if valueCopy is None:
-                if self._item.itsParent is item.itsParent:
-                    valueParent = item.itsParent
-                else:
-                    valueParent = value.itsParent
-                valueCopy = value.copy(None, valueParent, copies, copyPolicy)
-
-            return valueCopy
-
-        return None
-
     def _iterItems(self, items=None):
 
         if self._companion is not None:
@@ -153,7 +131,7 @@
         if initialValues is not None:
             self.extend(initialValues)
 
-    def _copy(self, item, attribute, companion, copies, copyPolicy):
+    def _copy(self, item, attribute, companion, copyPolicy, copyFn):
 
         copy = type(self)(item, attribute, companion)
         policy = copyPolicy or item.getAttributeAspect(attribute, 'copyPolicy',
@@ -161,12 +139,12 @@
 
         for value in self:
             if isinstance(value, repository.item.Item.Item):
-                value = self._copyItem(value, item, copies, policy, copyPolicy)
+                value = copyFn(item, value, policy)
                 if value is not None:
                     copy.append(value)
             elif isinstance(value, PersistentCollection):
                 copy.append(value._copy(item, attribute, companion,
-                                        copies, copyPolicy))
+                                        copyPolicy, copyFn))
             else:
                 copy.append(value)
 
@@ -296,7 +274,7 @@
         if initialValues is not None:
             self.update(initialValues)
 
-    def _copy(self, item, attribute, companion, copies, copyPolicy):
+    def _copy(self, item, attribute, companion, copyPolicy, copyFn):
 
         copy = type(self)(item, attribute, companion)
         policy = copyPolicy or item.getAttributeAspect(attribute, 'copyPolicy',
@@ -304,12 +282,12 @@
         
         for key, value in self.iteritems():
             if isinstance(value, repository.item.Item.Item):
-                value = self._copyItem(value, item, copies, policy, copyPolicy)
+                value = copyFn(item, value, policy)
                 if value is not None:
                     copy[key] = value
             elif isinstance(value, PersistentCollection):
                 copy[key] = value._copy(item, attribute, companion,
-                                        copies, copyPolicy)
+                                        copyPolicy, copyFn)
             else:
                 copy[key] = value
 



More information about the Commits mailing list