[Kimchi-devel] [PATCH 01/16] Move generate_action_handler() function to Resource() class

Sheldon shaohef at linux.vnet.ibm.com
Tue Dec 24 11:24:07 UTC 2013


On 12/24/2013 02:41 AM, Aline Manera wrote:
> +    def generate_action_handler(self, instance, action_name, action_args=None):

since we don not need decorator from your code.
+ self.stop = self.generate_action_handler(self, 'stop')
seems partial is more better than
def wrapper(*args, **kwargs):

from functools import partial


> +        def wrapper(*args, **kwargs):
> +            validate_method(('POST'))
> +            try:
> +                model_args = list(instance.model_args)
> +                if action_args is not None:
> +                    model_args.extend(parse_request()[key] for key in action_args)
> +                fn = getattr(instance.model, model_fn(instance, action_name))
> +                fn(*model_args)
> +                raise internal_redirect(instance.uri_fmt %
> +                                        tuple(instance.model_args))
> +            except MissingParameter, param:
> +                raise cherrypy.HTTPError(400, "Missing parameter: '%s'" % param)
> +            except InvalidParameter, param:
> +                raise cherrypy.HTTPError(400, "Invalid parameter: '%s'" % param)
> +            except InvalidOperation, msg:
> +                raise cherrypy.HTTPError(400, "Invalid operation: '%s'" % msg)
> +            except OperationFailed, msg:
> +                raise cherrypy.HTTPError(500, "Operation Failed: '%s'" % msg)
> +            except NotFoundError, msg:
> +                raise cherrypy.HTTPError(404, "Not found: '%s'" % msg)
> +
> +        wrapper.__name__ = action_name
> +        wrapper.exposed = True
> +        return wrapper
> +


-- 
Sheldon Feng(冯少合)<shaohef at linux.vnet.ibm.com>
IBM Linux Technology Center




More information about the Kimchi-devel mailing list