def readItem(self, view, afterLoadHooks): status = self.status withSchema = (status & Item.CORESCHEMA) != 0 isContainer = (status & Item.CONTAINER) != 0 status &= Item.CORESCHEMA kind = self._kind(self.uKind, withSchema, view, afterLoadHooks) parent = self._parent(self.uParent, withSchema, view, afterLoadHooks) cls = self._class(self.moduleName, self.className, withSchema, kind, view, afterLoadHooks) values = Values(None) references = References(None) self._values(values, references, self.uValues, kind, withSchema, view, afterLoadHooks) instance = view._reuseItemInstance(self.uItem) if instance is not None: if cls is not type(instance): instance.__class__ = cls item = self.item = instance status |= item._status & item.PINNED else: item = self.item = cls.__new__(cls) item._fillItem(self.name, parent, kind, uuid=self.uItem, values=values, references=references, afterLoadHooks=afterLoadHooks, version=self.version, status=status, update=False) if isContainer: item._children = view._createChildren(item, False) values._setItem(item) references._setItem(item) for name, value in values.iteritems(): if isinstance(value, ItemValue): value._setOwner(item, name) if kind is not None: afterLoadHooks.append(lambda view: kind._setupClass(cls)) if hasattr(cls, 'onItemLoad'): afterLoadHooks.append(item.onItemLoad) return item
def instantiateItem(self, name, parent, uuid, cls=None, version=0, withInitialValues=False): """ Instantiate an existing item of this kind. This method is intended to help in instantiating an existing item, that is an item in this or another repository for which there already exists a UUID. The item's constructor is not invoked, the item's onItemLoad method is invoked if defined. @param name: The name of the item. It must be unique among the names this item's siblings. C{name} may be C{None}. @type name: a string or C{None} to create an anonymous item. @param parent: The parent of this item. All items require a parent unless they are a repository root in which case C{parent} is a repository view. @type parent: an item or the item's repository view @param uuid: The uuid for the item. @type uuid: L{UUID<chandlerdb.util.uuid.UUID>} @param cls: an optional python class to instantiate the item with, defaults to the class set on this kind. @type cls: a python new style class, that is, a type instance @param version: the optional version of this item instance, zero by default. @type version: integer @param withInitialValues: optionally set the initial values for attributes as specified in this Kind's attribute definitions. @type withInitialValues: boolean """ if cls is None: cls = self.getItemClass() values = Values(None) references = References(None) item = cls.__new__(cls) item._fillItem(name, parent, self, uuid=uuid, version=version, values=values, references=references) values._setItem(item) references._setItem(item) self._setupClass(cls) if withInitialValues: self.getInitialValues(item, values, references) if hasattr(cls, 'onItemLoad'): item.onItemLoad(self.itsView) return item
def itemStart(self, itemHandler, attrs): super(ItemHandler, self).itemStart(itemHandler, attrs) if self.values is None: self.values = Values(None) if self.references is None: self.references = References(None) self.refs = [] self.name = None self.cls = None self.parentRef = None self.isContainer = False self.uuid = UUID(attrs.get('uuid')) self.version = int(attrs.get('version', '0')) self.update = update = attrs.get('update') self.delete = delete = attrs.get('delete') def _find(spec): typeAttr = attrs.get('type', 'path') if typeAttr == 'path': item = self.parent.find(Path(spec)) elif typeAttr == 'uuid': item = self.parent.find(UUID(spec)) else: raise TypeError, typeAttr if item is None: raise NoSuchItemError, (spec, self.version) return item if update is not None: item = _find(update) elif delete is not None: item = _find(delete) else: item = None if item is not None: self.item = item self.cls = type(item) self.version = item._version self.name = item.itsName self.kind = item.itsKind self.uuid = item.itsUUID self.parent = item.itsParent