root / kamaki / cli / commands / __init__.py @ 362adf50
History | View | Annotate | Download (5.9 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.command
|
33 |
|
34 |
from kamaki.cli.logger import get_logger |
35 |
from kamaki.cli.utils import print_json, print_items |
36 |
from kamaki.cli.argument import FlagArgument |
37 |
from kamaki.cli.errors import CLIError |
38 |
from kamaki.clients import Client |
39 |
|
40 |
log = get_logger(__name__) |
41 |
|
42 |
|
43 |
class _command_init(object): |
44 |
|
45 |
def __init__(self, arguments={}, auth_base_or_remote=None): |
46 |
if hasattr(self, 'arguments'): |
47 |
arguments.update(self.arguments)
|
48 |
if isinstance(self, _optional_output_cmd): |
49 |
arguments.update(self.oo_arguments)
|
50 |
if isinstance(self, _optional_json): |
51 |
arguments.update(self.oj_arguments)
|
52 |
self.arguments = dict(arguments) |
53 |
try:
|
54 |
self.config = self['config'] |
55 |
except KeyError: |
56 |
pass
|
57 |
if isinstance(auth_base_or_remote, Client): |
58 |
self.auth_base = auth_base_or_remote
|
59 |
elif not getattr(self, 'auth_base', None): |
60 |
self.remote = auth_base_or_remote
|
61 |
if not self.remote: |
62 |
raise CLIError('CRITICAL: No cloud specified', 3) |
63 |
|
64 |
def _set_log_params(self): |
65 |
try:
|
66 |
self.client.LOG_TOKEN, self.client.LOG_DATA = ( |
67 |
self['config'].get_global('log_token').lower() == 'on', |
68 |
self['config'].get_global('log_data').lower() == 'on') |
69 |
except Exception as e: |
70 |
log.warning('Failed to read custom log settings:'
|
71 |
'%s\n defaults for token and data logging are off' % e)
|
72 |
|
73 |
def _update_max_threads(self): |
74 |
try:
|
75 |
max_threads = int(self['config'].get_global('max_threads')) |
76 |
assert max_threads > 0 |
77 |
self.client.MAX_THREADS = max_threads
|
78 |
except Exception as e: |
79 |
log.warning('Failed to read custom thread settings: '
|
80 |
'%s, use default max threads (%s)' % (
|
81 |
e, self.client.MAX_THREADS))
|
82 |
|
83 |
def _safe_progress_bar(self, msg, arg='progress_bar'): |
84 |
"""Try to get a progress bar, but do not raise errors"""
|
85 |
try:
|
86 |
progress_bar = self.arguments[arg]
|
87 |
gen = progress_bar.get_generator(msg) |
88 |
except Exception: |
89 |
return (None, None) |
90 |
return (progress_bar, gen)
|
91 |
|
92 |
def _safe_progress_bar_finish(self, progress_bar): |
93 |
try:
|
94 |
progress_bar.finish() |
95 |
except Exception: |
96 |
pass
|
97 |
|
98 |
def __getitem__(self, argterm): |
99 |
"""
|
100 |
:param argterm: (str) the name/label of an argument in self.arguments
|
101 |
|
102 |
:returns: the value of the corresponding Argument (not the argument
|
103 |
object)
|
104 |
|
105 |
:raises KeyError: if argterm not in self.arguments of this object
|
106 |
"""
|
107 |
return self.arguments[argterm].value |
108 |
|
109 |
def __setitem__(self, argterm, arg): |
110 |
"""Install an argument as argterm
|
111 |
If argterm points to another argument, the other argument is lost
|
112 |
|
113 |
:param argterm: (str)
|
114 |
|
115 |
:param arg: (Argument)
|
116 |
"""
|
117 |
if not hasattr(self, 'arguments'): |
118 |
self.arguments = {}
|
119 |
self.arguments[argterm] = arg
|
120 |
|
121 |
def get_argument_object(self, argterm): |
122 |
"""
|
123 |
:param argterm: (str) the name/label of an argument in self.arguments
|
124 |
|
125 |
:returns: the arument object
|
126 |
|
127 |
:raises KeyError: if argterm not in self.arguments of this object
|
128 |
"""
|
129 |
return self.arguments[argterm] |
130 |
|
131 |
def get_argument(self, argterm): |
132 |
"""
|
133 |
:param argterm: (str) the name/label of an argument in self.arguments
|
134 |
|
135 |
:returns: the value of the arument object
|
136 |
|
137 |
:raises KeyError: if argterm not in self.arguments of this object
|
138 |
"""
|
139 |
return self[argterm] |
140 |
|
141 |
|
142 |
# feature classes - inherit them to get special features for your commands
|
143 |
|
144 |
|
145 |
class _optional_output_cmd(object): |
146 |
|
147 |
oo_arguments = dict(
|
148 |
with_output=FlagArgument('show response headers', ('--with-output')), |
149 |
json_output=FlagArgument('show headers in json', ('-j', '--json')) |
150 |
) |
151 |
|
152 |
def _optional_output(self, r): |
153 |
if self['json_output']: |
154 |
print_json(r) |
155 |
elif self['with_output']: |
156 |
print_items([r] if isinstance(r, dict) else r) |
157 |
|
158 |
|
159 |
class _optional_json(object): |
160 |
|
161 |
oj_arguments = dict(
|
162 |
json_output=FlagArgument('show headers in json', ('-j', '--json')) |
163 |
) |
164 |
|
165 |
def _print(self, output, print_method=print_items, **print_method_kwargs): |
166 |
if self['json_output']: |
167 |
print_json(output) |
168 |
else:
|
169 |
print_method(output, **print_method_kwargs) |