Example #1
0
def subcmd(fn, name=None):
    """
    Decorator to wrap a jsh sub command modelled as a method inside a class.
    
    This decorator is responsible for prefixing the arguments (and options) 
    string received from the wrapped method call with the jsh sub command name 
    and the PREFIX (jsh command name) and invoke the evaluate method on the
    resulting command string.  
    
    """

    def wrapped_fn(cls, handle, *args, **kwargs):
        cmd_list = []

        # Change 'hasattr' later with EAFP
        if hasattr(cls, "PREFIX"):
            # PREFIX should be a list of strings
            cmd_list.extend(getattr(cls, "PREFIX"))
        else:
            try:
                cmd_list.append(cls.__name__)
            except:
                cmd_list.append(cls.__class__.__name__)

        if name:
            cmd_list.append(name)
        else:
            cmd_list.append(fn.__name__)
        cmd_list += tuple(fn(cls, handle, *args, **kwargs))
        return handle.evaluate(*tuple(cmd_list))

    return classmethod(match_signatures(fn, wrapped_fn))
Example #2
0
def jshcmd(fn, name=None):
    """
    Decorator to wrap a python method corresponding to a jsh c++ service.
    
    This decorator is responsible for prefixing the arguments (and options) 
    string received from the wrapped function call with the jsh command name 
    (and any other prefixes if required) and invoke the evaluate method on the
    resulting command string.  
    
    """

    def wrapped_fn(self, *args, **kwargs):
        cmd_list = []

        # Change 'hasattr' later with EAFP
        if hasattr(self.__class__, "PREFIX"):
            cmd_list.append(getattr(self.__class__, "PREFIX"))

        if name:
            cmd_list.append(name)
        else:
            cmd_list.append(fn.__name__)
        cmd_list += tuple(fn(self, *args, **kwargs))
        if isinstance(self, Handle):
            return self.evaluate(*tuple(cmd_list))
        else:
            raise Exception("Error");

    return match_signatures(fn, wrapped_fn)