X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/cfd04e3240085f95fd7e2fd7f48f57ecf2a5989e..98695f4622c90dc6e23767b37fa4b06b5d1f6ef0:/pymin/service/util.py diff --git a/pymin/service/util.py b/pymin/service/util.py index e8e65a1..478f609 100644 --- a/pymin/service/util.py +++ b/pymin/service/util.py @@ -3,6 +3,8 @@ import subprocess from mako.template import Template from mako.runtime import Context +from formencode.validators import Int +from formencode.schema import Schema from os import path try: import cPickle as pickle @@ -25,6 +27,14 @@ __all__ = ('Error', 'ExecutionError', 'ItemError', 'ItemAlreadyExistsError', 'ListSubHandler', 'ComposedSubHandler', 'ListComposedSubHandler', 'DictComposedSubHandler', 'Device','Address') +class IndexValidator(Schema): + "Trivial schema validator for SubHandler's indexes" + index = Int + def to_python(self, value, state=None): + # we want to return the index directly, the only purpose of this + # validation being a schema is for field error reporting + return super(Schema, self).to_python(dict(index=value), state)['index'] + class Error(HandlerError): r""" Error(message) -> Error instance :: Base ServiceHandler exception class. @@ -877,7 +887,7 @@ class ContainerSubHandler(SubHandler): # TODO make it right with metaclasses, so the method is not created # unless the update() method really exists. if not isinstance(self._attr(), dict): - index = int(index) # TODO validation + index = IndexValidator.to_python(index) if not hasattr(self._cont_subhandler_class, 'update'): log.debug(u'ContainerSubHandler.update: update() not found, ' u"can't really update, raising command not found") @@ -898,7 +908,7 @@ class ContainerSubHandler(SubHandler): r"delete(index) -> None :: Delete an item of the container." log.debug(u'ContainerSubHandler.delete(%r)', index) if not isinstance(self._attr(), dict): - index = int(index) # TODO validation + index = IndexValidator.to_python(index) try: item = self._vattr()[index] if hasattr(item, '_delete'): @@ -927,7 +937,7 @@ class ContainerSubHandler(SubHandler): r"get(index) -> item :: List all the information of an item." log.debug(u'ContainerSubHandler.get(%r)', index) if not isinstance(self._attr(), dict): - index = int(index) # TODO validation + index = IndexValidator.to_python(index) try: return self._vattr()[index] except LookupError: @@ -1085,7 +1095,7 @@ class ComposedSubHandler(SubHandler): log.debug(u'ComposedSubHandler.add: container not found') raise ContainerNotFoundError(cont) if not isinstance(self._attr(cont), dict): - index = int(index) # TODO validation + index = IndexValidator.to_python(index) 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") @@ -1113,7 +1123,7 @@ class ComposedSubHandler(SubHandler): log.debug(u'ComposedSubHandler.add: container not found') raise ContainerNotFoundError(cont) if not isinstance(self._attr(cont), dict): - index = int(index) # TODO validation + index = IndexValidator.to_python(index) try: item = self._vattr(cont)[index] if hasattr(item, '_delete'): @@ -1153,7 +1163,7 @@ class ComposedSubHandler(SubHandler): log.debug(u'ComposedSubHandler.add: container not found') raise ContainerNotFoundError(cont) if not isinstance(self._attr(cont), dict): - index = int(index) # TODO validation + index = IndexValidator.to_python(index) try: return self._vattr(cont)[index] except LookupError: