FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cloudstack/cloud-cli/cloudtool/utils.py at volume-upload · kwanggithub/cloudstack · GitHub
This repository was archived by the owner on Jan 15, 2020. It is now read-only.
kwanggithub
/
cloudstack
Public archive
forked from
apache/cloudstack
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cloudstack
/
cloud-cli
/
cloudtool
/
utils.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
169 lines (133 loc) · 6.05 KB
Breadcrumbs
cloudstack
/
cloud-cli
/
cloudtool
/
utils.py
Copy path
File metadata and controls
169 lines (133 loc) · 6.05 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
'''
Created on Aug 2, 2010
'''
import
sys
import
os
import
inspect
from
optparse
import
OptionParser
,
OptParseError
,
BadOptionError
,
OptionError
,
OptionConflictError
,
OptionValueError
import
cloudapis
as
apis
def
describe
(
name
,
desc
):
def
inner
(
decoratee
):
if
not
hasattr
(
decoratee
,
"descriptions"
):
decoratee
.
descriptions
=
{}
decoratee
.
descriptions
[
name
]
=
desc
return
decoratee
return
inner
def
error
(
msg
):
sys
.
stderr
.
write
(
msg
)
sys
.
stderr
.
write
(
"
\n
"
)
class
MyOptionParser
(
OptionParser
):
def
error
(
self
,
msg
):
error
(
"%s: %s
\n
"
%
(
self
.
get_prog_name
(),
msg
))
self
.
print_usage
(
sys
.
stderr
)
self
.
exit
(
os
.
EX_USAGE
)
def
parse_args
(
self
,
*
args
,
**
kwargs
):
options
,
arguments
=
OptionParser
.
parse_args
(
self
,
*
args
,
**
kwargs
)
def
prune_options
(
options
,
alist
):
"""Given 'options' -- a list of arguments to OptionParser.add_option,
and a set of optparse Values, return a dictionary of only those values
that apply exclusively to 'options'"""
return
dict
( [ (
k
,
getattr
(
options
,
k
))
for
k
in
dir
(
options
)
if
k
in
alist
] )
api_options
=
prune_options
(
options
,
self
.
api_dests
)
cmd_options
=
prune_options
(
options
,
self
.
cmd_dests
)
return
options
,
arguments
,
api_options
,
cmd_options
def
get_parser
(
api_callable
=
None
,
cmd_callable
=
None
):
# this should probably be the __init__ method of myoptionparser
def
getdefaulttag
(
default
):
if
default
is
not
None
:
return
" [Default: %default]"
return
''
def
get_arguments_and_options
(
callable
):
"""Infers and returns arguments and options based on a callable's signature.
Cooperates with decorator @describe"""
try
:
funcargs
=
inspect
.
getargspec
(
callable
).
args
defaults
=
inspect
.
getargspec
(
callable
).
defaults
except
:
funcargs
=
inspect
.
getargspec
(
callable
)[
0
]
defaults
=
inspect
.
getargspec
(
callable
)[
3
]
if
not
defaults
:
defaults
=
[]
args
=
funcargs
[
1
:
len
(
funcargs
)
-
len
(
defaults
)]
# this assumes self, so assumes methods
opts
=
funcargs
[
len
(
funcargs
)
-
len
(
defaults
):]
try
:
descriptions
=
callable
.
descriptions
except
AttributeError
:
descriptions
=
{}
arguments
=
[ (
argname
,
descriptions
.
get
(
argname
,
''
) )
for
argname
in
args
]
options
=
[ [
(
"--%s"
%
argname
.
replace
(
"_"
,
"-"
),),
{
"dest"
:
argname
,
"help"
:
descriptions
.
get
(
argname
,
''
)
+
getdefaulttag
(
default
),
"default"
:
default
,
}
]
for
argname
,
default
in
zip
(
opts
,
defaults
) ]
return
arguments
,
options
basic_usage
=
"usage: %prog [options...] "
api_name
=
"<api>"
cmd_name
=
"<command>"
description
=
"%prog is a command-line tool to access several cloud APIs."
arguments
=
''
argexp
=
""
if
api_callable
:
api_name
=
api_callable
.
__module__
.
split
(
"."
)[
-
1
].
replace
(
"_"
,
"-"
)
api_arguments
,
api_options
=
get_arguments_and_options
(
api_callable
)
assert
len
(
api_arguments
)
is
0
# no mandatory arguments for class initializers
if
cmd_callable
:
cmd_name
=
cmd_callable
.
func_name
.
replace
(
"_"
,
"-"
)
cmd_arguments
,
cmd_options
=
get_arguments_and_options
(
cmd_callable
)
if
cmd_arguments
:
arguments
=
" "
+
" "
.
join
( [
s
[
0
].
upper
()
for
s
in
cmd_arguments
] )
argexp
=
"
\n
\n
Arguments:
\n
"
+
"
\n
"
.
join
(
" %s
\n
%s"
%
(
s
.
upper
(),
u
)
for
s
,
u
in
cmd_arguments
)
description
=
cmd_callable
.
__doc__
api_command
=
"%s %s"
%
(
api_name
,
cmd_name
)
if
description
:
description
=
"
\n
\n
"
+
description
else
:
description
=
''
usage
=
basic_usage
+
api_command
+
arguments
+
description
+
argexp
parser
=
MyOptionParser
(
usage
=
usage
,
add_help_option
=
False
)
parser
.
add_option
(
'--help'
,
action
=
"help"
)
group
=
parser
.
add_option_group
(
"General options"
)
group
.
add_option
(
'-v'
,
'--verbose'
,
dest
=
"verbose"
,
help
=
"Print extra output"
)
parser
.
api_dests
=
[]
if
api_callable
and
api_options
:
group
=
parser
.
add_option_group
(
"Options for the %s API"
%
api_name
)
for
a
in
api_options
:
group
.
add_option
(
a
[
0
][
0
],
**
a
[
1
])
parser
.
api_dests
.
append
(
a
[
1
][
"dest"
])
parser
.
cmd_dests
=
[]
if
cmd_callable
and
cmd_options
:
group
=
parser
.
add_option_group
(
"Options for the %s command"
%
cmd_name
)
for
a
in
cmd_options
:
group
.
add_option
(
a
[
0
][
0
],
**
a
[
1
])
parser
.
cmd_dests
.
append
(
a
[
1
][
"dest"
])
return
parser
def
lookup_command_in_api
(
api
,
command_name
):
command
=
getattr
(
api
,
command_name
.
replace
(
"-"
,
"_"
),
None
)
return
command
def
get_api_list
(
api
):
apilist
=
[]
for
cmd_name
in
dir
(
api
):
cmd
=
getattr
(
api
,
cmd_name
)
if
callable
(
cmd
)
and
not
cmd_name
.
startswith
(
"_"
):
apilist
.
append
(
cmd_name
)
return
apilist
def
get_command_list
(
api
):
cmds
=
[]
for
cmd_name
in
dir
(
api
):
cmd
=
getattr
(
api
,
cmd_name
)
if
callable
(
cmd
)
and
not
cmd_name
.
startswith
(
"_"
):
if
cmd
.
__doc__
:
docstring
=
cmd
.
__doc__
else
:
docstring
=
''
cmds
.
append
(
" %s"
%
(
cmd_name
.
replace
(
'_'
,
'-'
)) )
return
cmds
Back
|
FazBrowse Home
|
New Git URL