Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / __init__.py @ 64a3c0de

History | View | Annotate | Download (9.8 kB)

1
# Copyright 2011-2013 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, filter_dicts_by_dict
36
from kamaki.cli.argument import FlagArgument, ValueArgument
37

    
38
log = get_logger(__name__)
39

    
40

    
41
def DontRaiseKeyError(foo):
42
    def wrap(*args, **kwargs):
43
        try:
44
            return foo(*args, **kwargs)
45
        except KeyError:
46
            return None
47
    return wrap
48

    
49

    
50
def addLogSettings(foo):
51
    def wrap(self, *args, **kwargs):
52
        try:
53
            return foo(self, *args, **kwargs)
54
        finally:
55
            self._set_log_params()
56
            self._update_max_threads
57
    return wrap
58

    
59

    
60
class _command_init(object):
61

    
62
    def __init__(self, arguments={}, auth_base=None, cloud=None):
63
        if hasattr(self, 'arguments'):
64
            arguments.update(self.arguments)
65
        if isinstance(self, _optional_output_cmd):
66
            arguments.update(self.oo_arguments)
67
        if isinstance(self, _optional_json):
68
            arguments.update(self.oj_arguments)
69
        if isinstance(self, _name_filter):
70
            arguments.update(self.nf_arguments)
71
        if isinstance(self, _id_filter):
72
            arguments.update(self.if_arguments)
73
        try:
74
            arguments.update(self.wait_arguments)
75
        except AttributeError:
76
            pass
77
        self.arguments = dict(arguments)
78
        try:
79
            self.config = self['config']
80
        except KeyError:
81
            pass
82
        self.auth_base = auth_base or getattr(self, 'auth_base', None)
83
        self.cloud = cloud or getattr(self, 'cloud', None)
84

    
85
    @DontRaiseKeyError
86
    def _custom_url(self, service):
87
        return self.config.get_cloud(self.cloud, '%s_url' % service)
88

    
89
    @DontRaiseKeyError
90
    def _custom_token(self, service):
91
        return self.config.get_cloud(self.cloud, '%s_token' % service)
92

    
93
    @DontRaiseKeyError
94
    def _custom_type(self, service):
95
        return self.config.get_cloud(self.cloud, '%s_type' % service)
96

    
97
    @DontRaiseKeyError
98
    def _custom_version(self, service):
99
        return self.config.get_cloud(self.cloud, '%s_version' % service)
100

    
101
    def _uuids2usernames(self, uuids):
102
        return self.auth_base.post_user_catalogs(uuids).json['uuid_catalog']
103

    
104
    def _usernames2uuids(self, username):
105
        return self.auth_base.post_user_catalogs(
106
            displaynames=username).json['displayname_catalog']
107

    
108
    def _uuid2username(self, uuid):
109
        return self._uuids2usernames([uuid]).get(uuid, None)
110

    
111
    def _username2uuid(self, username):
112
        return self._usernames2uuids([username]).get(username, None)
113

    
114
    def _set_log_params(self):
115
        try:
116
            self.client.LOG_TOKEN = (
117
                self['config'].get_global('log_token').lower() == 'on')
118
        except Exception as e:
119
            log.debug('Failed to read custom log_token setting:'
120
                '%s\n default for log_token is off' % e)
121
        try:
122
            self.client.LOG_DATA = (
123
                self['config'].get_global('log_data').lower() == 'on')
124
        except Exception as e:
125
            log.debug('Failed to read custom log_data setting:'
126
                '%s\n default for log_data is off' % e)
127
        try:
128
            self.client.LOG_PID = (
129
                self['config'].get_global('log_pid').lower() == 'on')
130
        except Exception as e:
131
            log.debug('Failed to read custom log_pid setting:'
132
                '%s\n default for log_pid is off' % e)
133

    
134
    def _update_max_threads(self):
135
        if getattr(self, 'client', None):
136
            max_threads = int(self['config'].get_global('max_threads'))
137
            assert max_threads > 0
138
            self.client.MAX_THREADS = max_threads
139

    
140
    def _safe_progress_bar(self, msg, arg='progress_bar'):
141
        """Try to get a progress bar, but do not raise errors"""
142
        try:
143
            progress_bar = self.arguments[arg]
144
            gen = progress_bar.get_generator(msg)
145
        except Exception:
146
            return (None, None)
147
        return (progress_bar, gen)
148

    
149
    def _safe_progress_bar_finish(self, progress_bar):
150
        try:
151
            progress_bar.finish()
152
        except Exception:
153
            pass
154

    
155
    def __getitem__(self, argterm):
156
        """
157
        :param argterm: (str) the name/label of an argument in self.arguments
158

159
        :returns: the value of the corresponding Argument (not the argument
160
            object)
161

162
        :raises KeyError: if argterm not in self.arguments of this object
163
        """
164
        return self.arguments[argterm].value
165

    
166
    def __setitem__(self, argterm, arg):
167
        """Install an argument as argterm
168
        If argterm points to another argument, the other argument is lost
169

170
        :param argterm: (str)
171

172
        :param arg: (Argument)
173
        """
174
        if not hasattr(self, 'arguments'):
175
            self.arguments = {}
176
        self.arguments[argterm] = arg
177

    
178
    def get_argument_object(self, argterm):
179
        """
180
        :param argterm: (str) the name/label of an argument in self.arguments
181

182
        :returns: the arument object
183

184
        :raises KeyError: if argterm not in self.arguments of this object
185
        """
186
        return self.arguments[argterm]
187

    
188
    def get_argument(self, argterm):
189
        """
190
        :param argterm: (str) the name/label of an argument in self.arguments
191

192
        :returns: the value of the arument object
193

194
        :raises KeyError: if argterm not in self.arguments of this object
195
        """
196
        return self[argterm]
197

    
198

    
199
#  feature classes - inherit them to get special features for your commands
200

    
201

    
202
class _optional_output_cmd(object):
203

    
204
    oo_arguments = dict(
205
        with_output=FlagArgument('show response headers', ('--with-output')),
206
        json_output=FlagArgument('show headers in json', ('-j', '--json'))
207
    )
208

    
209
    def _optional_output(self, r):
210
        if self['json_output']:
211
            print_json(r)
212
        elif self['with_output']:
213
            print_items([r] if isinstance(r, dict) else r)
214

    
215

    
216
class _optional_json(object):
217

    
218
    oj_arguments = dict(
219
        json_output=FlagArgument('show headers in json', ('-j', '--json'))
220
    )
221

    
222
    def _print(self, output, print_method=print_items, **print_method_kwargs):
223
        if self['json_output']:
224
            print_json(output)
225
        else:
226
            print_method(output, **print_method_kwargs)
227

    
228

    
229
class _name_filter(object):
230

    
231
    nf_arguments = dict(
232
        name=ValueArgument('filter by name', '--name'),
233
        name_pref=ValueArgument(
234
            'filter by name prefix (case insensitive)', '--name-prefix'),
235
        name_suff=ValueArgument(
236
            'filter by name suffix (case insensitive)', '--name-suffix'),
237
        name_like=ValueArgument(
238
            'print only if name contains this (case insensitive)',
239
            '--name-like')
240
    )
241

    
242
    def _non_exact_name_filter(self, items):
243
        np, ns, nl = self['name_pref'], self['name_suff'], self['name_like']
244
        return [item for item in items if (
245
            (not np) or item['name'].lower().startswith(np.lower())) and (
246
            (not ns) or item['name'].lower().endswith(ns.lower())) and (
247
            (not nl) or nl.lower() in item['name'].lower())]
248

    
249
    def _exact_name_filter(self, items):
250
        return filter_dicts_by_dict(items, dict(name=self['name'])) if (
251
            self['name']) else items
252

    
253
    def _filter_by_name(self, items):
254
        return self._non_exact_name_filter(self._exact_name_filter(items))
255

    
256

    
257
class _id_filter(object):
258

    
259
    if_arguments = dict(
260
        id=ValueArgument('filter by id', '--id'),
261
        id_pref=ValueArgument(
262
            'filter by id prefix (case insensitive)', '--id-prefix'),
263
        id_suff=ValueArgument(
264
            'filter by id suffix (case insensitive)', '--id-suffix'),
265
        id_like=ValueArgument(
266
            'print only if id contains this (case insensitive)',
267
            '--id-like')
268
    )
269

    
270
    def _non_exact_id_filter(self, items):
271
        np, ns, nl = self['id_pref'], self['id_suff'], self['id_like']
272
        return [item for item in items if (
273
            (not np) or (
274
                '%s' % item['id']).lower().startswith(np.lower())) and (
275
            (not ns) or ('%s' % item['id']).lower().endswith(ns.lower())) and (
276
            (not nl) or nl.lower() in ('%s' % item['id']).lower())]
277

    
278
    def _exact_id_filter(self, items):
279
        return filter_dicts_by_dict(items, dict(id=self['id'])) if (
280
            self['id']) else items
281

    
282
    def _filter_by_id(self, items):
283
        return self._non_exact_id_filter(self._exact_id_filter(items))