Skip to content
This repository was archived by the owner on Jan 15, 2020. It is now read-only.

Commit fe2fc63

Browse files
committed
Fixed command line tool
1 parent ad3c18a commit fe2fc63

6 files changed

Lines changed: 32 additions & 285 deletions

File tree

cloud-cli/cloudapis/amazon.py

Lines changed: 0 additions & 63 deletions
This file was deleted.

cloud-cli/cloudapis/cloud.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,32 @@ def getText(nodelist):
5858
name = getText(cmd.getElementsByTagName('name')[0].childNodes).strip()
5959
assert name
6060

61-
description = cmd.getElementsByTagName('name')[0].getAttribute("description")
62-
if description: description = '"""%s"""' % description
61+
description = getText(cmd.getElementsByTagName('description')[0].childNodes).strip()
62+
if description:
63+
description = '"""%s"""' % description
6364
else: description = ''
6465
arguments = []
6566
options = []
6667
descriptions = []
6768

68-
for param in cmd.getElementsByTagName('arg'):
69-
argname = getText(param.childNodes).strip()
69+
for param in cmd.getElementsByTagName("request")[0].getElementsByTagName("arg"):
70+
argname = getText(param.getElementsByTagName('name')[0].childNodes).strip()
7071
assert argname
7172

72-
required = param.getAttribute("required").strip()
73+
required = getText(param.getElementsByTagName('required')[0].childNodes).strip()
7374
if required == 'true': required = True
7475
elif required == 'false': required = False
7576
else: raise AssertionError, "Not reached"
7677
if required: arguments.append(argname)
7778
options.append(argname)
7879

79-
description = param.getAttribute("description").strip()
80-
if description: descriptions.append( (argname,description) )
80+
#import ipdb; ipdb.set_trace()
81+
requestDescription = param.getElementsByTagName('description')
82+
if requestDescription:
83+
descriptionParam = getText(requestDescription[0].childNodes)
84+
else:
85+
descriptionParam = ''
86+
if descriptionParam: descriptions.append( (argname,descriptionParam) )
8187

8288
funcparams = ["self"] + [ "%s=None"%o for o in options ]
8389
funcparams = ", ".join(funcparams)
@@ -95,7 +101,7 @@ def %s(%s):
95101
output = self._make_request("%s",parms)
96102
return output
97103
"""%(name,funcparams,description,arguments,name)
98-
104+
99105
namespace = {}
100106
exec code.strip() in namespace
101107

@@ -106,7 +112,8 @@ def %s(%s):
106112
yield (name,func)
107113

108114

109-
for name,meth in load_dynamic_methods(): setattr(CloudAPI,name,meth)
115+
for name,meth in load_dynamic_methods():
116+
setattr(CloudAPI, name, meth)
110117

111118
implementor = CloudAPI
112119

cloud-cli/cloudtool/__init__.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,27 @@
1111

1212
def main(argv=None):
1313

14+
#import ipdb; ipdb.set_trace()
1415
if argv == None:
1516
argv = sys.argv
1617

17-
prelim_args = [ x for x in argv[1:] if not x.startswith('-') ]
18+
prelim_args = [ x for x in argv[0:] if not x.startswith('-') ]
1819
parser = utils.get_parser()
19-
20+
2021
api = __import__("cloudapis")
2122
apis = getattr(api, "implementor")
22-
if len(prelim_args) == 0:
23-
parser.error("you need to specify an API as the first argument\n\nSupported APIs:\n" + "\n".join(utils.get_api_list(apis)))
24-
elif len(prelim_args) == 1:
23+
if len(prelim_args) == 1:
2524
commandlist = utils.get_command_list(apis)
26-
parser.error("you need to specify a command name as the second argument\n\nCommands supported by the %s API:\n"%prelim_args[0] + "\n".join(commandlist))
25+
parser.error("you need to specify a command name as the first argument\n\nCommands supported by the %s API:\n"%prelim_args[0] + "\n".join(commandlist))
2726

2827
command = utils.lookup_command_in_api(apis,prelim_args[1])
2928
if not command: parser.error("command %r not supported by the %s API"%(prelim_args[1],prelim_args[0]))
29+
30+
argv = argv[1:]
31+
if len(argv) == 1:
32+
argv.append("--help")
3033

3134
parser = utils.get_parser(apis.__init__,command)
32-
argv = argv[1:]
3335
opts,args,api_optionsdict,cmd_optionsdict = parser.parse_args(argv)
3436

3537

@@ -38,7 +40,7 @@ def main(argv=None):
3840
except utils.OptParseError,e:
3941
parser.error(str(e))
4042

41-
command = utils.lookup_command_in_api(api,args[1])
43+
command = utils.lookup_command_in_api(api,args[0])
4244

4345
# we now discard the first two arguments as those necessarily are the api and command names
4446
args = args[2:]

cloud-cli/cloudtool/utils.py

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -112,51 +112,18 @@ def get_arguments_and_options(callable):
112112
group = parser.add_option_group("General options")
113113
group.add_option('-v', '--verbose', dest="verbose", help="Print extra output")
114114

115-
# now we need to derive the short options. we initialize the known fixed longopts and shortopts
116-
longopts = [ '--verbose' ]
117-
118-
# we add the known long options, sorted and grouped to guarantee a stable short opt set regardless of order
119-
if api_callable and api_options:
120-
longopts += sorted([ x[0][0] for x in api_options ])
121-
if cmd_callable and cmd_options:
122-
longopts += sorted([ x[0][0] for x in cmd_options ])
123-
124-
# we use this function to derive a suitable short option and remember the already-used short options
125-
def derive_shortopt(longopt,usedopts):
126-
"""longopt begins with a dash"""
127-
shortopt = None
128-
for x in xrange(2,10000):
129-
try: shortopt = "-" + longopt[x]
130-
except IndexError:
131-
shortopt = None
132-
break
133-
if shortopt in usedopts: continue
134-
usedopts.append(shortopt)
135-
break
136-
return shortopt
137-
138-
# now we loop through the long options and assign a suitable short option, saving the short option for later use
139-
long_to_short = {}
140-
alreadyusedshorts = []
141-
for longopt in longopts:
142-
long_to_short[longopt] = derive_shortopt(longopt,alreadyusedshorts)
143-
144115
parser.api_dests = []
145116
if api_callable and api_options:
146117
group = parser.add_option_group("Options for the %s API"%api_name)
147118
for a in api_options:
148-
shortopt = long_to_short[a[0][0]]
149-
if shortopt: group.add_option(shortopt,a[0][0],**a[1])
150-
else: group.add_option(a[0][0],**a[1])
119+
group.add_option(a[0][0],**a[1])
151120
parser.api_dests.append(a[1]["dest"])
152121

153122
parser.cmd_dests = []
154123
if cmd_callable and cmd_options:
155124
group = parser.add_option_group("Options for the %s command"%cmd_name)
156125
for a in cmd_options:
157-
shortopt = long_to_short[a[0][0]]
158-
if shortopt: group.add_option(shortopt,a[0][0],**a[1])
159-
else: group.add_option(a[0][0],**a[1])
126+
group.add_option(a[0][0],**a[1])
160127
parser.cmd_dests.append(a[1]["dest"])
161128

162129
return parser
@@ -178,7 +145,7 @@ def get_command_list(api):
178145
for cmd_name in dir(api):
179146
cmd = getattr(api,cmd_name)
180147
if callable(cmd) and not cmd_name.startswith("_"):
181-
if cmd.__doc__: docstring = cmd.__doc__
182-
else: docstring = ''
183-
cmds.append( " %s %s"%(cmd_name.replace('_','-'),docstring) )
148+
if cmd.__doc__:docstring = cmd.__doc__
149+
else:docstring = ''
150+
cmds.append( " %s" % (cmd_name.replace('_','-')) )
184151
return cmds

0 commit comments

Comments
 (0)