Skip to content

Commit ddb794d

Browse files
committed
cli: generate verbs on the fly, fix autocompletion whitespace bug
Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
1 parent 1509a21 commit ddb794d

2 files changed

Lines changed: 22 additions & 32 deletions

File tree

tools/cli/cloudmonkey/cloudmonkey.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import logging
2727
import os
2828
import pdb
29+
import re
2930
import shlex
3031
import sys
3132
import time
@@ -38,7 +39,7 @@
3839

3940
from prettytable import PrettyTable
4041
from common import __version__, config_file, config_fields
41-
from common import grammar, precached_verbs
42+
from common import precached_verbs
4243
from marvin.cloudstackConnection import cloudConnection
4344
from marvin.cloudstackException import cloudstackAPIException
4445
from marvin.cloudstackAPI import *
@@ -71,12 +72,12 @@ class CloudMonkeyShell(cmd.Cmd, object):
7172
ruler = "="
7273
config_file = config_file
7374
config_fields = config_fields
74-
grammar = grammar
7575
# datastructure {'verb': {cmd': ['api', [params], doc, required=[]]}}
7676
cache_verbs = precached_verbs
7777

78-
def __init__(self, pname):
78+
def __init__(self, pname, verbs):
7979
self.program_name = pname
80+
self.verbs = verbs
8081
if os.path.exists(self.config_file):
8182
config = self.read_config()
8283
else:
@@ -102,11 +103,9 @@ def __init__(self, pname):
102103
logger.debug("Loaded config fields:\n%s" % self.config_fields)
103104

104105
cmd.Cmd.__init__(self)
105-
# Update config if config_file does not exist
106106
if not os.path.exists(self.config_file):
107107
config = self.write_config()
108108

109-
# Enable history support
110109
try:
111110
if os.path.exists(self.history_file):
112111
readline.read_history_file(self.history_file)
@@ -381,13 +380,13 @@ def default(self, args):
381380

382381
def completedefault(self, text, line, begidx, endidx):
383382
partitions = line.partition(" ")
384-
verb = partitions[0]
385-
rline = partitions[2].partition(" ")
383+
verb = partitions[0].strip()
384+
rline = partitions[2].lstrip().partition(" ")
386385
subject = rline[0]
387386
separator = rline[1]
388-
params = rline[2]
387+
params = rline[2].lstrip()
389388

390-
if verb not in self.grammar:
389+
if verb not in self.verbs:
391390
return []
392391

393392
autocompletions = []
@@ -436,7 +435,7 @@ def do_set(self, args):
436435
args = args.strip().partition(" ")
437436
key, value = (args[0], args[2])
438437
setattr(self, key, value) # keys and attributes should have same names
439-
self.prompt = self.prompt.strip() + " " # prompt fix
438+
self.prompt = self.prompt.strip() + " " # prompt fix
440439
self.write_config()
441440

442441
def complete_set(self, text, line, begidx, endidx):
@@ -513,35 +512,35 @@ def do_EOF(self, args):
513512

514513

515514
def main():
516-
# Create handlers on the fly using closures
517-
self = CloudMonkeyShell
518-
global grammar
519-
for rule in grammar:
520-
def add_grammar(rule):
515+
pattern = re.compile("[A-Z]")
516+
verbs = list(set([x[:pattern.search(x).start()] for x in completions
517+
if pattern.search(x) is not None]))
518+
for verb in verbs:
519+
def add_grammar(verb):
521520
def grammar_closure(self, args):
522-
if self.pipe_runner("%s %s" % (rule, args)):
521+
if self.pipe_runner("%s %s" % (verb, args)):
523522
return
524523
try:
525524
args_partition = args.partition(" ")
526-
res = self.cache_verbs[rule][args_partition[0]]
525+
res = self.cache_verbs[verb][args_partition[0]]
527526
cmd = res[0]
528527
helpdoc = res[2]
529528
args = args_partition[2]
530529
except KeyError, e:
531-
self.print_shell("Error: invalid %s api arg" % rule, e)
530+
self.print_shell("Error: invalid %s api arg" % verb, e)
532531
return
533532
if ' --help' in args or ' -h' in args:
534533
self.print_shell(helpdoc)
535534
return
536535
self.default("%s %s" % (cmd, args))
537536
return grammar_closure
538537

539-
grammar_handler = add_grammar(rule)
540-
grammar_handler.__doc__ = "%ss resources" % rule.capitalize()
541-
grammar_handler.__name__ = 'do_' + rule
542-
setattr(self, grammar_handler.__name__, grammar_handler)
538+
grammar_handler = add_grammar(verb)
539+
grammar_handler.__doc__ = "%ss resources" % verb.capitalize()
540+
grammar_handler.__name__ = 'do_' + verb
541+
setattr(CloudMonkeyShell, grammar_handler.__name__, grammar_handler)
543542

544-
shell = CloudMonkeyShell(sys.argv[0])
543+
shell = CloudMonkeyShell(sys.argv[0], verbs)
545544
if len(sys.argv) > 1:
546545
shell.onecmd(' '.join(sys.argv[1:]))
547546
else:

tools/cli/cloudmonkey/common.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,3 @@
3939
'history_file':
4040
os.path.expanduser('~/.cloudmonkey_history')}
4141

42-
# Add verbs in grammar
43-
grammar = ['create', 'list', 'delete', 'update', 'lock',
44-
'enable', 'activate', 'disable', 'add', 'remove',
45-
'attach', 'detach', 'associate', 'disassociate', 'generate', 'ldap',
46-
'assign', 'authorize', 'change', 'register', 'configure',
47-
'start', 'restart', 'reboot', 'stop', 'reconnect',
48-
'cancel', 'destroy', 'revoke', 'mark', 'reset',
49-
'copy', 'extract', 'migrate', 'restore', 'suspend',
50-
'get', 'query', 'prepare', 'deploy', 'upload']

0 commit comments

Comments
 (0)