Exemple #1
0
    def loadcache(self):
        if os.path.exists(self.cache_file):
            self.apicache = loadcache(self.cache_file)
        else:
            self.apicache = apicache
        if 'verbs' in self.apicache:
            self.verbs = self.apicache['verbs']

        for verb in self.verbs:

            def add_grammar(verb):
                def grammar_closure(self, args):
                    if self.pipe_runner("%s %s" % (verb, args)):
                        return
                    if ' --help' in args or ' -h' in args:
                        self.do_help("%s %s" % (verb, args))
                        return
                    try:
                        args_partition = args.partition(" ")
                        cmd = self.apicache[verb][args_partition[0]]['name']
                        args = args_partition[2]
                    except KeyError, e:
                        self.monkeyprint("Error: invalid %s api arg" % verb, e)
                        return
                    self.default("%s %s" % (cmd, args))

                return grammar_closure

            grammar_handler = add_grammar(verb)
            grammar_handler.__doc__ = "%ss resources" % verb.capitalize()
            grammar_handler.__name__ = 'do_' + str(verb)
            setattr(self.__class__, grammar_handler.__name__, grammar_handler)
    def loadcache(self):
        if os.path.exists(self.cache_file):
            self.apicache = loadcache(self.cache_file)
        else:
            self.apicache = apicache
        if 'verbs' in self.apicache:
            self.verbs = self.apicache['verbs']

        for verb in self.verbs:
            def add_grammar(verb):
                def grammar_closure(self, args):
                    if self.pipe_runner("%s %s" % (verb, args)):
                        return
                    if ' --help' in args or ' -h' in args:
                        self.do_help("%s %s" % (verb, args))
                        return
                    try:
                        args_partition = args.partition(" ")
                        cmd = self.apicache[verb][args_partition[0]]['name']
                        args = args_partition[2]
                    except KeyError, e:
                        self.monkeyprint("Error: invalid %s api arg" % verb, e)
                        return
                    self.default("%s %s" % (cmd, args))
                return grammar_closure

            grammar_handler = add_grammar(verb)
            grammar_handler.__doc__ = "%ss resources" % verb.capitalize()
            grammar_handler.__name__ = 'do_' + str(verb)
            setattr(self.__class__, grammar_handler.__name__, grammar_handler)
Exemple #3
0
def main():
    verbs = []
    if os.path.exists(cache_file):
        verbs = loadcache(cache_file)['verbs']
    elif 'verbs' in apicache:
        verbs = apicache['verbs']

    for verb in verbs:
        def add_grammar(verb):
            def grammar_closure(self, args):
                if self.pipe_runner("%s %s" % (verb, args)):
                    return
                try:
                    args_partition = args.partition(" ")
                    api = self.apicache[verb][args_partition[0]]
                    cmd = api['name']
                    helpdoc = api['description']
                    args = args_partition[2]
                except KeyError, e:
                    self.monkeyprint("Error: invalid %s api arg" % verb, e)
                    return
                if ' --help' in args or ' -h' in args:
                    self.monkeyprint(helpdoc)
                    return
                self.default("%s %s" % (cmd, args))
            return grammar_closure

        grammar_handler = add_grammar(verb)
        grammar_handler.__doc__ = "%ss resources" % verb.capitalize()
        grammar_handler.__name__ = 'do_' + str(verb)
        setattr(CloudMonkeyShell, grammar_handler.__name__, grammar_handler)
    def loadcache(self):
        if os.path.exists(self.cache_file):
            self.apicache = loadcache(self.cache_file)
        else:
            self.apicache = apicache
        if "verbs" in self.apicache:
            self.verbs = self.apicache["verbs"]

        for verb in self.verbs:

            def add_grammar(verb):
                def grammar_closure(self, args):
                    if not args:
                        return
                    args = args.decode("utf-8")
                    if self.pipe_runner(u"{0} {1}".format(verb, args)):
                        return
                    if " --help" in args or " -h" in args:
                        self.do_help("%s %s" % (verb, args))
                        return
                    try:
                        args_partition = args.partition(" ")
                        cmd = self.apicache[verb][args_partition[0]]["name"]
                        args = args_partition[2]
                    except KeyError, e:
                        self.monkeyprint("Error: invalid %s api arg " % verb, str(e))
                        return
                    self.default(u"{0} {1}".format(cmd, args))

                return grammar_closure

            grammar_handler = add_grammar(verb)
            grammar_handler.__doc__ = "%ss resources" % verb.capitalize()
            grammar_handler.__name__ = "do_" + str(verb)
            setattr(self.__class__, grammar_handler.__name__, grammar_handler)
    def loadcache(self):
        if os.path.exists(self.cache_file):
            self.apicache = loadcache(self.cache_file)
        else:
            self.apicache = apicache
        if 'verbs' in self.apicache:
            self.verbs = self.apicache['verbs']

        for verb in self.verbs:
            handler_name = "do_" + str(verb)
            handler_doc = str("%ss resources" % verb.capitalize())

            if hasattr(self, handler_name) and getattr(
                    self, handler_name).__doc__ == handler_doc:
                continue

            def add_grammar(verb):
                default_handler = None
                if self.default_apis.__contains__(verb):
                    default_handler = getattr(self, handler_name)

                def grammar_closure(self, args):
                    if not args:
                        if default_handler:
                            default_handler(args)
                        return
                    args = args.decode("utf-8")
                    if self.pipe_runner(u"{0} {1}".format(verb, args)):
                        return
                    if ' --help' in args or ' -h' in args:
                        self.do_help("%s %s" % (verb, args))
                        return
                    try:
                        args_partition = args.partition(" ")
                        cmd = self.apicache[verb][args_partition[0]]['name']
                        args = args_partition[2]
                    except KeyError, e:
                        if default_handler:
                            default_handler(args)
                        else:
                            self.monkeyprint(
                                "Error: invalid %s api arg " % verb, str(e))
                        return
                    self.default(u"{0} {1}".format(cmd, args))

                return grammar_closure

            grammar_handler = add_grammar(verb)
            grammar_handler.__doc__ = handler_doc
            grammar_handler.__name__ = handler_name
            setattr(self.__class__, grammar_handler.__name__, grammar_handler)
    def loadcache(self):
        if os.path.exists(self.cache_file):
            self.apicache = loadcache(self.cache_file)
        else:
            self.apicache = apicache
        if 'verbs' in self.apicache:
            self.verbs = self.apicache['verbs']

        for verb in self.verbs:
            handler_name = "do_" + str(verb)
            handler_doc = str("%ss resources" % verb.capitalize())

            if hasattr(self, handler_name) and getattr(self, handler_name).__doc__ == handler_doc:
                continue

            def add_grammar(verb):
                default_handler = None
                if self.default_apis.__contains__(verb):
                    default_handler = getattr(self, handler_name)

                def grammar_closure(self, args):
                    if not args:
                        if default_handler:
                            default_handler(args)
                        return
                    args = args.decode("utf-8")
                    if self.pipe_runner(u"{0} {1}".format(verb, args)):
                        return
                    if ' --help' in args or ' -h' in args:
                        self.do_help("%s %s" % (verb, args))
                        return
                    try:
                        args_partition = args.partition(" ")
                        cmd = self.apicache[verb][args_partition[0]]['name']
                        args = args_partition[2]
                    except KeyError, e:
                        if default_handler:
                            default_handler(args)
                        else:
                            self.monkeyprint("Error: invalid %s api arg " % verb,
                                         str(e))
                        return
                    self.default(u"{0} {1}".format(cmd, args))
                return grammar_closure

            grammar_handler = add_grammar(verb)
            grammar_handler.__doc__ = handler_doc
            grammar_handler.__name__ = handler_name
            setattr(self.__class__, grammar_handler.__name__, grammar_handler)
Exemple #7
0
 def loadcache(self):
     if os.path.exists(self.cache_file):
         self.apicache = loadcache(self.cache_file)
     else:
         self.apicache = apicache
     self.verbs = apicache['verbs']