+ (in case that it's deleted, it's not removed from the container,
+ but it's not listed either). If the container objects
+ (parent.some_cont[cont]) has an _update attribute, it's set to True
+ when any add, update or delete command is executed.
+ """
+
+ def __init__(self, parent, cont=None, attr=None, cls=None):
+ r"Initialize the object, see the class documentation for details."
+ log.debug(u'ComposedSubHandler(%r, %r, %r, %r)',
+ parent, cont, attr, cls)
+ self.parent = parent
+ if cont is not None:
+ self._comp_subhandler_cont = cont
+ if attr is not None:
+ self._comp_subhandler_attr = attr
+ if cls is not None:
+ self._comp_subhandler_class = cls
+
+ def _cont(self):
+ return getattr(self.parent, self._comp_subhandler_cont)
+
+ def _attr(self, cont, attr=None):
+ if attr is None:
+ return getattr(self._cont()[cont], self._comp_subhandler_attr)
+ setattr(self._cont()[cont], self._comp_subhandler_attr, attr)
+
+ def _vattr(self, cont):
+ if isinstance(self._attr(cont), dict):
+ return dict([(k, i) for (k, i) in self._attr(cont).items()
+ if not hasattr(i, '_delete') or not i._delete])
+ return [i for i in self._attr(cont)
+ if not hasattr(i, '_delete') or not i._delete]
+
+ @handler(u'Add a new item')
+ def add(self, cont, *args, **kwargs):
+ r"add(cont, ...) -> None :: Add an item to the list."
+ log.debug(u'ComposedSubHandler.add(%r, %r, %r)', cont, args, kwargs)
+ if not cont in self._cont():
+ log.debug(u'ComposedSubHandler.add: container not found')
+ raise ContainerNotFoundError(cont)
+ item = self._comp_subhandler_class(*args, **kwargs)
+ if hasattr(item, '_add'):
+ log.debug(u'ComposedSubHandler.add: _add found, setting to True')
+ item._add = True
+ key = item
+ if isinstance(self._attr(cont), dict):
+ key = item.as_tuple()[0]
+ # do we have the same item? then raise an error
+ if key in self._vattr(cont):
+ log.debug(u'ComposedSubHandler.add: allready exists')
+ raise ItemAlreadyExistsError(item)
+ # do we have the same item, but logically deleted? then update flags
+ if key in self._attr(cont):
+ log.debug(u'ComposedSubHandler.add: was deleted, undeleting it')
+ index = key
+ if not isinstance(self._attr(cont), dict):
+ index = self._attr(cont).index(item)
+ if hasattr(item, '_add'):
+ self._attr(cont)[index]._add = False
+ if hasattr(item, '_delete'):
+ self._attr(cont)[index]._delete = False
+ else: # it's *really* new
+ if isinstance(self._attr(cont), dict):
+ self._attr(cont)[key] = item
+ else:
+ self._attr(cont).append(item)
+ if hasattr(self._cont()[cont], '_update'):
+ log.debug(u"ComposedSubHandler.add: container's _update found, "
+ u'setting to True')
+ self._cont()[cont]._update = True
+
+ @handler(u'Update an item')
+ def update(self, cont, index, *args, **kwargs):
+ r"update(cont, index, ...) -> None :: Update an item of the container."
+ # TODO make it right with metaclasses, so the method is not created
+ # unless the update() method really exists.
+ log.debug(u'ComposedSubHandler.update(%r, %r, %r, %r)',
+ cont, index, args, kwargs)
+ if not cont in self._cont():
+ log.debug(u'ComposedSubHandler.add: container not found')
+ raise ContainerNotFoundError(cont)
+ if not isinstance(self._attr(cont), dict):
+ index = int(index) # TODO validation
+ if not hasattr(self._comp_subhandler_class, 'update'):
+ log.debug(u'ComposedSubHandler.update: update() not found, '
+ u"can't really update, raising command not found")
+ raise CommandNotFoundError(('update',))
+ try:
+ item = self._vattr(cont)[index]
+ item.update(*args, **kwargs)
+ if hasattr(item, '_update'):
+ log.debug(u'ComposedSubHandler.update: _update found, '
+ u'setting to True')
+ item._update = True
+ if hasattr(self._cont()[cont], '_update'):
+ log.debug(u"ComposedSubHandler.add: container's _update found, "
+ u'setting to True')
+ self._cont()[cont]._update = True
+ except LookupError:
+ log.debug(u'ComposedSubHandler.update: item not found')
+ raise ItemNotFoundError(index)
+
+ @handler(u'Delete an item')
+ def delete(self, cont, index):
+ r"delete(cont, index) -> None :: Delete an item of the container."
+ log.debug(u'ComposedSubHandler.delete(%r, %r)', cont, index)
+ if not cont in self._cont():
+ log.debug(u'ComposedSubHandler.add: container not found')
+ raise ContainerNotFoundError(cont)
+ if not isinstance(self._attr(cont), dict):
+ index = int(index) # TODO validation
+ try:
+ item = self._vattr(cont)[index]
+ if hasattr(item, '_delete'):
+ log.debug(u'ComposedSubHandler.delete: _delete found, '
+ u'setting to True')
+ item._delete = True
+ else:
+ del self._attr(cont)[index]
+ if hasattr(self._cont()[cont], '_update'):
+ log.debug(u"ComposedSubHandler.add: container's _update found, "
+ u'setting to True')
+ self._cont()[cont]._update = True
+ return item
+ except LookupError:
+ log.debug(u'ComposedSubHandler.delete: item not found')
+ raise ItemNotFoundError(index)
+
+ @handler(u'Remove all items (use with care).')
+ def clear(self, cont):
+ r"clear(cont) -> None :: Delete all items of the container."
+ # FIXME broken really, no item or container _delete attribute is
+ # setted :S
+ log.debug(u'ComposedSubHandler.clear(%r)', cont)
+ if not cont in self._cont():
+ log.debug(u'ComposedSubHandler.add: container not found')
+ raise ContainerNotFoundError(cont)
+ if isinstance(self._attr(cont), dict):
+ self._attr(cont).clear()
+ else:
+ self._attr(cont, list())
+
+ @handler(u'Get information about an item')
+ def get(self, cont, index):
+ r"get(cont, index) -> item :: List all the information of an item."
+ log.debug(u'ComposedSubHandler.get(%r, %r)', cont, index)
+ if not cont in self._cont():
+ log.debug(u'ComposedSubHandler.add: container not found')
+ raise ContainerNotFoundError(cont)
+ if not isinstance(self._attr(cont), dict):
+ index = int(index) # TODO validation
+ try:
+ return self._vattr(cont)[index]
+ except LookupError:
+ log.debug(u'ComposedSubHandler.get: item not found')
+ raise ItemNotFoundError(index)
+
+ @handler(u'Get information about all items')
+ def show(self, cont):
+ r"show(cont) -> list of items :: List all the complete items information."
+ log.debug(u'ComposedSubHandler.show(%r)', cont)
+ if not cont in self._cont():
+ log.debug(u'ComposedSubHandler.add: container not found')
+ raise ContainerNotFoundError(cont)
+ if isinstance(self._attr(cont), dict):
+ return self._attr(cont).values()
+ return self._vattr(cont)
+
+class ListComposedSubHandler(ComposedSubHandler):
+ r"""ListComposedSubHandler(parent) -> ListComposedSubHandler instance.
+
+ ComposedSubHandler holding lists. See ComposedSubHandler documentation
+ for details.
+ """
+
+ @handler(u'Get how many items are in the list')
+ def len(self, cont):
+ r"len(cont) -> int :: Get how many items are in the list."
+ log.debug(u'ListComposedSubHandler.len(%r)', cont)
+ if not cont in self._cont():
+ raise ContainerNotFoundError(cont)
+ return len(self._vattr(cont))
+
+class DictComposedSubHandler(ComposedSubHandler):
+ r"""DictComposedSubHandler(parent) -> DictComposedSubHandler instance.
+
+ ComposedSubHandler holding dicts. See ComposedSubHandler documentation
+ for details.