summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
e797591)
Now the method to access to the container attribute is named _attr() (and
_vattr) instead of _cont() (and _vcont). The method accept now an optional
'attr' argument to act as a setter.
class ItemNotFoundError(ItemError):
r"""
class ItemNotFoundError(ItemError):
r"""
- ItemNotFoundError(key) -> ItemNotFoundError instance
+ ItemNotFoundError(key) -> ItemNotFoundError instance.
This exception is raised when trying to operate on an item that doesn't
exists.
This exception is raised when trying to operate on an item that doesn't
exists.
if cls is not None:
self._cont_subhandler_class = cls
if cls is not None:
self._cont_subhandler_class = cls
- def _cont(self):
- return getattr(self.parent, self._cont_subhandler_attr)
+ def _attr(self, attr=None):
+ if attr is None:
+ return getattr(self.parent, self._cont_subhandler_attr)
+ setattr(self.parent, self._cont_subhandler_attr, attr)
- def _vcont(self):
- if isinstance(self._cont(), dict):
- return dict([(k, i) for (k, i) in self._cont().items()
+ def _vattr(self):
+ if isinstance(self._attr(), dict):
+ return dict([(k, i) for (k, i) in self._attr().items()
if not hasattr(i, '_delete') or not i._delete])
if not hasattr(i, '_delete') or not i._delete])
- return [i for i in self._cont()
+ return [i for i in self._attr()
if not hasattr(i, '_delete') or not i._delete]
@handler(u'Add a new item')
if not hasattr(i, '_delete') or not i._delete]
@handler(u'Add a new item')
if hasattr(item, '_add'):
item._add = True
key = item
if hasattr(item, '_add'):
item._add = True
key = item
- if isinstance(self._cont(), dict):
+ if isinstance(self._attr(), dict):
key = item.as_tuple()[0]
# do we have the same item? then raise an error
key = item.as_tuple()[0]
# do we have the same item? then raise an error
- if key in self._vcont():
+ if key in self._vattr():
raise ItemAlreadyExistsError(item)
# do we have the same item, but logically deleted? then update flags
raise ItemAlreadyExistsError(item)
# do we have the same item, but logically deleted? then update flags
- if key in self._cont():
+ if key in self._attr():
- if not isinstance(self._cont(), dict):
- index = self._cont().index(item)
+ if not isinstance(self._attr(), dict):
+ index = self._attr().index(item)
if hasattr(item, '_add'):
if hasattr(item, '_add'):
- self._cont()[index]._add = False
+ self._attr()[index]._add = False
if hasattr(item, '_delete'):
if hasattr(item, '_delete'):
- self._cont()[index]._delete = False
+ self._attr()[index]._delete = False
else: # it's *really* new
else: # it's *really* new
- if isinstance(self._cont(), dict):
- self._cont()[key] = item
+ if isinstance(self._attr(), dict):
+ self._attr()[key] = item
- self._cont().append(item)
+ self._attr().append(item)
@handler(u'Update an item')
def update(self, index, *args, **kwargs):
@handler(u'Update an item')
def update(self, index, *args, **kwargs):
# TODO make it right with metaclasses, so the method is not created
# unless the update() method really exists.
# TODO check if the modified item is the same of an existing one
# TODO make it right with metaclasses, so the method is not created
# unless the update() method really exists.
# TODO check if the modified item is the same of an existing one
- if not isinstance(self._cont(), dict):
+ if not isinstance(self._attr(), dict):
index = int(index) # TODO validation
if not hasattr(self._cont_subhandler_class, 'update'):
raise CommandNotFoundError(('update',))
try:
index = int(index) # TODO validation
if not hasattr(self._cont_subhandler_class, 'update'):
raise CommandNotFoundError(('update',))
try:
- item = self._vcont()[index]
+ item = self._vattr()[index]
item.update(*args, **kwargs)
if hasattr(item, '_update'):
item._update = True
item.update(*args, **kwargs)
if hasattr(item, '_update'):
item._update = True
@handler(u'Delete an item')
def delete(self, index):
r"delete(index) -> None :: Delete an item of the container."
@handler(u'Delete an item')
def delete(self, index):
r"delete(index) -> None :: Delete an item of the container."
- if not isinstance(self._cont(), dict):
+ if not isinstance(self._attr(), dict):
index = int(index) # TODO validation
try:
index = int(index) # TODO validation
try:
- item = self._vcont()[index]
+ item = self._vattr()[index]
if hasattr(item, '_delete'):
item._delete = True
else:
if hasattr(item, '_delete'):
item._delete = True
else:
- del self._cont()[index]
+ del self._attr()[index]
return item
except IndexError:
raise ItemNotFoundError(index)
return item
except IndexError:
raise ItemNotFoundError(index)
@handler(u'Get information about an item')
def get(self, index):
r"get(index) -> item :: List all the information of an item."
@handler(u'Get information about an item')
def get(self, index):
r"get(index) -> item :: List all the information of an item."
- if not isinstance(self._cont(), dict):
+ if not isinstance(self._attr(), dict):
index = int(index) # TODO validation
try:
index = int(index) # TODO validation
try:
- return self._vcont()[index]
+ return self._vattr()[index]
except IndexError:
raise ItemNotFoundError(index)
@handler(u'Get information about all items')
def show(self):
r"show() -> list of items :: List all the complete items information."
except IndexError:
raise ItemNotFoundError(index)
@handler(u'Get information about all items')
def show(self):
r"show() -> list of items :: List all the complete items information."
- if isinstance(self._cont(), dict):
- return self._cont().values()
- return self._vcont()
+ if isinstance(self._attr(), dict):
+ return self._attr().values()
+ return self._vattr()
class ListSubHandler(ContainerSubHandler):
r"""ListSubHandler(parent) -> ListSubHandler instance.
class ListSubHandler(ContainerSubHandler):
r"""ListSubHandler(parent) -> ListSubHandler instance.
@handler(u'Get how many items are in the list')
def len(self):
r"len() -> int :: Get how many items are in the list."
@handler(u'Get how many items are in the list')
def len(self):
r"len() -> int :: Get how many items are in the list."
- return len(self._vcont())
+ return len(self._vattr())
class DictSubHandler(ContainerSubHandler):
r"""DictSubHandler(parent) -> DictSubHandler instance.
class DictSubHandler(ContainerSubHandler):
r"""DictSubHandler(parent) -> DictSubHandler instance.
@handler(u'List all the items by key')
def list(self):
r"list() -> tuple :: List all the item keys."
@handler(u'List all the items by key')
def list(self):
r"list() -> tuple :: List all the item keys."
- return self._cont().keys()
+ return self._attr().keys()
if __name__ == '__main__':
if __name__ == '__main__':