| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6a601ba commit a2e89c4
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,6 +56,8 @@ def savecache(apicache, json_file): | |||
| 56 | 56 | """ | |
| 57 | 57 | Saves apicache dictionary as json_file, returns dictionary as indented str | |
| 58 | 58 | """ | |
| 59 | + if isinstance(type(apicache), types.NoneType) or apicache is None or apicache is {}: | ||
| 60 | + return "" | ||
| 59 | 61 | apicachestr = json.dumps(apicache, indent=2) | |
| 60 | 62 | with open(json_file, 'w') as cache_file: | |
| 61 | 63 | cache_file.write(apicachestr) | |
@@ -81,8 +83,9 @@ def monkeycache(apis): | |||
| 81 | 83 | """ | |
| 82 | 84 | Feed this a dictionary of api bananas, it spits out processed cache | |
| 83 | 85 | """ | |
| 84 | - if isinstance(type(apis), types.NoneType): | ||
| 86 | + if isinstance(type(apis), types.NoneType) or apis is None: | ||
| 85 | 87 | return {} | |
| 88 | + | ||
| 86 | 89 | responsekey = filter(lambda x: 'response' in x, apis.keys()) | |
| 87 | 90 | ||
| 88 | 91 | if len(responsekey) == 0: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,20 +28,14 @@ | |||
| 28 | 28 | import sys | |
| 29 | 29 | import types | |
| 30 | 30 | ||
| 31 | - from urllib2 import HTTPError, URLError | ||
| 32 | - from httplib import BadStatusLine | ||
| 33 | - | ||
| 31 | + from cachemaker import loadcache, savecache, monkeycache, splitverbsubject | ||
| 34 | 32 | from config import __version__, cache_file | |
| 35 | 33 | from config import read_config, write_config | |
| 36 | - | ||
| 34 | + from prettytable import PrettyTable | ||
| 37 | 35 | from printer import monkeyprint | |
| 38 | 36 | from requester import monkeyrequest | |
| 39 | - from cachemaker import loadcache, savecache, monkeycache | ||
| 40 | - from cachemaker import splitverbsubject | ||
| 41 | - | ||
| 42 | - from prettytable import PrettyTable | ||
| 43 | 37 | except ImportError, e: | |
| 44 | - print "Import error in %s : %s" % (__name__, e) | ||
| 38 | + print("Import error in %s : %s" % (__name__, e)) | ||
| 45 | 39 | import sys | |
| 46 | 40 | sys.exit() | |
| 47 | 41 | ||
@@ -50,11 +44,10 @@ | |||
| 50 | 44 | except ImportError: | |
| 51 | 45 | apicache = {} | |
| 52 | 46 | ||
| 53 | - # Fix autocompletion issue, can be put in .pythonstartup | ||
| 54 | 47 | try: | |
| 55 | 48 | import readline | |
| 56 | 49 | except ImportError, e: | |
| 57 | - print "Module readline not found, autocompletions will fail", e | ||
| 50 | + print("Module readline not found, autocompletions will fail", e) | ||
| 58 | 51 | else: | |
| 59 | 52 | import rlcompleter | |
| 60 | 53 | if 'libedit' in readline.__doc__: | |
@@ -71,8 +64,6 @@ class CloudMonkeyShell(cmd.Cmd, object): | |||
| 71 | 64 | ". Type help or ? to list commands.\n") | |
| 72 | 65 | ruler = "=" | |
| 73 | 66 | cache_file = cache_file | |
| 74 | - ## datastructure {'verb': {cmd': ['api', [params], doc, required=[]]}} | ||
| 75 | - #cache_verbs = apicache | ||
| 76 | 67 | config_options = [] | |
| 77 | 68 | ||
| 78 | 69 | def __init__(self, pname): | |
@@ -91,9 +82,9 @@ def __init__(self, pname): | |||
| 91 | 82 | try: | |
| 92 | 83 | if os.path.exists(self.history_file): | |
| 93 | 84 | readline.read_history_file(self.history_file) | |
| 94 | - atexit.register(readline.write_history_file, self.history_file) | ||
| 95 | - except IOError: | ||
| 96 | - monkeyprint("Error: history support") | ||
| 85 | + except IOError, e: | ||
| 86 | + logger.debug("Error: Unable to read history. " + str(e)) | ||
| 87 | + atexit.register(readline.write_history_file, self.history_file) | ||
| 97 | 88 | ||
| 98 | 89 | def get_attr(self, field): | |
| 99 | 90 | return getattr(self, field) | |
@@ -105,7 +96,7 @@ def emptyline(self): | |||
| 105 | 96 | pass | |
| 106 | 97 | ||
| 107 | 98 | def cmdloop(self, intro=None): | |
| 108 | - print self.intro | ||
| 99 | + print(self.intro) | ||
| 109 | 100 | while True: | |
| 110 | 101 | try: | |
| 111 | 102 | super(CloudMonkeyShell, self).cmdloop(intro="") | |
@@ -118,7 +109,31 @@ def loadcache(self): | |||
| 118 | 109 | self.apicache = loadcache(self.cache_file) | |
| 119 | 110 | else: | |
| 120 | 111 | self.apicache = apicache | |
| 121 | - self.verbs = apicache['verbs'] | ||
| 112 | + if 'verbs' in self.apicache: | ||
| 113 | + self.verbs = self.apicache['verbs'] | ||
| 114 | + | ||
| 115 | + for verb in self.verbs: | ||
| 116 | + def add_grammar(verb): | ||
| 117 | + def grammar_closure(self, args): | ||
| 118 | + if self.pipe_runner("%s %s" % (verb, args)): | ||
| 119 | + return | ||
| 120 | + if ' --help' in args or ' -h' in args: | ||
| 121 | + self.do_help("%s %s" % (verb, args)) | ||
| 122 | + return | ||
| 123 | + try: | ||
| 124 | + args_partition = args.partition(" ") | ||
| 125 | + cmd = self.apicache[verb][args_partition[0]]['name'] | ||
| 126 | + args = args_partition[2] | ||
| 127 | + except KeyError, e: | ||
| 128 | + self.monkeyprint("Error: invalid %s api arg" % verb, e) | ||
| 129 | + return | ||
| 130 | + self.default("%s %s" % (cmd, args)) | ||
| 131 | + return grammar_closure | ||
| 132 | + | ||
| 133 | + grammar_handler = add_grammar(verb) | ||
| 134 | + grammar_handler.__doc__ = "%ss resources" % verb.capitalize() | ||
| 135 | + grammar_handler.__name__ = 'do_' + str(verb) | ||
| 136 | + setattr(self.__class__, grammar_handler.__name__, grammar_handler) | ||
| 122 | 137 | ||
| 123 | 138 | def monkeyprint(self, *args): | |
| 124 | 139 | output = "" | |
@@ -128,12 +143,12 @@ def monkeyprint(self, *args): | |||
| 128 | 143 | continue | |
| 129 | 144 | output += str(arg) | |
| 130 | 145 | except Exception, e: | |
| 131 | - print e | ||
| 146 | + print(e) | ||
| 132 | 147 | ||
| 133 | 148 | if self.color == 'true': | |
| 134 | 149 | monkeyprint(output) | |
| 135 | 150 | else: | |
| 136 | - print output | ||
| 151 | + print(output) | ||
| 137 | 152 | ||
| 138 | 153 | def print_result(self, result, result_filter=None): | |
| 139 | 154 | if result is None or len(result) == 0: | |
@@ -219,7 +234,7 @@ def default(self, args): | |||
| 219 | 234 | next_val = lexp.next() | |
| 220 | 235 | if next_val is None: | |
| 221 | 236 | break | |
| 222 | - args.append(next_val) | ||
| 237 | + args.append(next_val.replace('\x00', '')) | ||
| 223 | 238 | ||
| 224 | 239 | args_dict = dict(map(lambda x: [x.partition("=")[0], | |
| 225 | 240 | x.partition("=")[2]], | |
@@ -230,15 +245,21 @@ def default(self, args): | |||
| 230 | 245 | map(lambda x: x.strip(), | |
| 231 | 246 | args_dict.pop('filter').split(','))) | |
| 232 | 247 | ||
| 233 | - missing_args = filter(lambda x: x not in args_dict.keys(), | ||
| 234 | - self.apicache[verb][subject]['requiredparams']) | ||
| 248 | + missing_args = [] | ||
| 249 | + if verb in self.apicache: | ||
| 250 | + missing_args = filter(lambda x: x not in args_dict.keys(), | ||
| 251 | + self.apicache[verb][subject]['requiredparams']) | ||
| 235 | 252 | ||
| 236 | 253 | if len(missing_args) > 0: | |
| 237 | 254 | self.monkeyprint("Missing arguments: ", ' '.join(missing_args)) | |
| 238 | 255 | return | |
| 239 | 256 | ||
| 240 | - result = self.make_request(apiname, args_dict, | ||
| 241 | - apiname in self.apicache['asyncapis']) | ||
| 257 | + isasync = False | ||
| 258 | + if 'asyncapis' in self.apicache: | ||
| 259 | + isasync = apiname in self.apicache['asyncapis'] | ||
| 260 | + | ||
| 261 | + result = self.make_request(apiname, args_dict, isasync) | ||
| 262 | + | ||
| 242 | 263 | if result is None: | |
| 243 | 264 | return | |
| 244 | 265 | try: | |
@@ -285,6 +306,9 @@ def do_sync(self, args): | |||
| 285 | 306 | """ | |
| 286 | 307 | response = self.make_request("listApis") | |
| 287 | 308 | self.apicache = monkeycache(response) | |
| 309 | + if response is None: | ||
| 310 | + monkeyprint("Failed to sync apis, check your config") | ||
| 311 | + return | ||
| 288 | 312 | savecache(self.apicache, self.cache_file) | |
| 289 | 313 | self.loadcache() | |
| 290 | 314 | ||
@@ -361,9 +385,19 @@ def do_help(self, args): | |||
| 361 | 385 | else: | |
| 362 | 386 | verb = fields[0] | |
| 363 | 387 | subject = fields[2].partition(" ")[0] | |
| 364 | - | ||
| 365 | - if subject in self.cache_verbs[verb]: | ||
| 366 | - self.monkeyprint(self.cache_verbs[verb][subject][2]) | ||
| 388 | + if subject in self.apicache[verb]: | ||
| 389 | + api = self.apicache[verb][subject] | ||
| 390 | + helpdoc = "(%s) %s" % (api['name'], api['description']) | ||
| 391 | + helpdoc = api['description'] | ||
| 392 | + if api['isasync']: | ||
| 393 | + helpdoc += "\nThis API is asynchronous." | ||
| 394 | + required = api['requiredparams'] | ||
| 395 | + if len(required) > 0: | ||
| 396 | + helpdoc += "\nRequired params are %s" % ' '.join(required) | ||
| 397 | + helpdoc += "\nParameters\n" + "=" * 10 | ||
| 398 | + for param in api['params']: | ||
| 399 | + helpdoc += "\n%s = (%s) %s" % (param['name'], param['type'], param['description']) | ||
| 400 | + self.monkeyprint(helpdoc) | ||
| 367 | 401 | else: | |
| 368 | 402 | self.monkeyprint("Error: no such api (%s) on %s" % | |
| 369 | 403 | (subject, verb)) | |
@@ -379,6 +413,12 @@ def complete_help(self, text, line, begidx, endidx): | |||
| 379 | 413 | text = subfields[2] | |
| 380 | 414 | return self.completedefault(text, line, begidx, endidx) | |
| 381 | 415 | ||
| 416 | + def do_EOF(self, args): | ||
| 417 | + """ | ||
| 418 | + Quit on Ctrl+d or EOF | ||
| 419 | + """ | ||
| 420 | + sys.exit() | ||
| 421 | + | ||
| 382 | 422 | def do_exit(self, args): | |
| 383 | 423 | """ | |
| 384 | 424 | Quit CloudMonkey CLI | |
@@ -392,45 +432,8 @@ def do_quit(self, args): | |||
| 392 | 432 | self.monkeyprint("Bye!") | |
| 393 | 433 | return self.do_EOF(args) | |
| 394 | 434 | ||
| 395 | - def do_EOF(self, args): | ||
| 396 | - """ | ||
| 397 | - Quit on Ctrl+d or EOF | ||
| 398 | - """ | ||
| 399 | - sys.exit() | ||
| 400 | - | ||
| 401 | 435 | ||
| 402 | 436 | def main(): | |
| 403 | - verbs = [] | ||
| 404 | - if os.path.exists(cache_file): | ||
| 405 | - verbs = loadcache(cache_file)['verbs'] | ||
| 406 | - elif 'verbs' in apicache: | ||
| 407 | - verbs = apicache['verbs'] | ||
| 408 | - | ||
| 409 | - for verb in verbs: | ||
| 410 | - def add_grammar(verb): | ||
| 411 | - def grammar_closure(self, args): | ||
| 412 | - if self.pipe_runner("%s %s" % (verb, args)): | ||
| 413 | - return | ||
| 414 | - try: | ||
| 415 | - args_partition = args.partition(" ") | ||
| 416 | - api = self.apicache[verb][args_partition[0]] | ||
| 417 | - cmd = api['name'] | ||
| 418 | - helpdoc = api['description'] | ||
| 419 | - args = args_partition[2] | ||
| 420 | - except KeyError, e: | ||
| 421 | - self.monkeyprint("Error: invalid %s api arg" % verb, e) | ||
| 422 | - return | ||
| 423 | - if ' --help' in args or ' -h' in args: | ||
| 424 | - self.monkeyprint(helpdoc) | ||
| 425 | - return | ||
| 426 | - self.default("%s %s" % (cmd, args)) | ||
| 427 | - return grammar_closure | ||
| 428 | - | ||
| 429 | - grammar_handler = add_grammar(verb) | ||
| 430 | - grammar_handler.__doc__ = "%ss resources" % verb.capitalize() | ||
| 431 | - grammar_handler.__name__ = 'do_' + str(verb) | ||
| 432 | - setattr(CloudMonkeyShell, grammar_handler.__name__, grammar_handler) | ||
| 433 | - | ||
| 434 | 437 | shell = CloudMonkeyShell(sys.argv[0]) | |
| 435 | 438 | if len(sys.argv) > 1: | |
| 436 | 439 | shell.onecmd(' '.join(sys.argv[1:])) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -69,10 +69,10 @@ def makelistre(lis): | |||
| 69 | 69 | (r'(?:\b\d+\b(?:-\b\d+|%)?)', Number), | |
| 70 | 70 | (r'^[-=]*\n', Operator.Word), | |
| 71 | 71 | (r'Error', Error), | |
| 72 | - (makelistre(keywords), Keyword), | ||
| 73 | 72 | (makelistre(attributes), Literal), | |
| 74 | 73 | (makelistre(params) + r'( = )(.*)', bygroups(Name, Operator, | |
| 75 | 74 | String)), | |
| 75 | + (makelistre(keywords), Keyword), | ||
| 76 | 76 | (makelistre(params), Name), | |
| 77 | 77 | (r'(^[a-zA-Z]* )(=)', bygroups(Name, Operator)), | |
| 78 | 78 | (r'\S+', Text), | |
| Back | FazBrowse Home | New Git URL |
0 commit comments