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