Revision 6d190dd1 kamaki/cli/commands/__init__.py
b/kamaki/cli/commands/__init__.py | ||
---|---|---|
32 | 32 |
# or implied, of GRNET S.A.command |
33 | 33 |
|
34 | 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 |
|
35 |
from kamaki.cli.utils import print_json, print_items, filter_dicts_by_dict
|
|
36 |
from kamaki.cli.argument import FlagArgument, ValueArgument
|
|
37 | 37 |
|
38 | 38 |
log = get_logger(__name__) |
39 | 39 |
|
... | ... | |
66 | 66 |
arguments.update(self.oo_arguments) |
67 | 67 |
if isinstance(self, _optional_json): |
68 | 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) |
|
69 | 73 |
try: |
70 | 74 |
arguments.update(self.wait_arguments) |
71 | 75 |
except AttributeError: |
... | ... | |
209 | 213 |
print_json(output) |
210 | 214 |
else: |
211 | 215 |
print_method(output, **print_method_kwargs) |
216 |
|
|
217 |
|
|
218 |
class _name_filter(object): |
|
219 |
|
|
220 |
nf_arguments = dict( |
|
221 |
name=ValueArgument('filter by name', '--name'), |
|
222 |
name_pref=ValueArgument( |
|
223 |
'filter by name prefix (case insensitive)', '--name-prefix'), |
|
224 |
name_suff=ValueArgument( |
|
225 |
'filter by name suffix (case insensitive)', '--name-suffix'), |
|
226 |
name_like=ValueArgument( |
|
227 |
'print only if name contains this (case insensitive)', |
|
228 |
'--name-like') |
|
229 |
) |
|
230 |
|
|
231 |
def _non_exact_name_filter(self, items): |
|
232 |
np, ns, nl = self['name_pref'], self['name_suff'], self['name_like'] |
|
233 |
return [item for item in items if ( |
|
234 |
(not np) or item['name'].lower().startswith(np.lower())) and ( |
|
235 |
(not ns) or item['name'].lower().endswith(ns.lower())) and ( |
|
236 |
(not nl) or nl.lower() in item['name'].lower())] |
|
237 |
|
|
238 |
def _exact_name_filter(self, items): |
|
239 |
return filter_dicts_by_dict(items, dict(name=self['name'])) if ( |
|
240 |
self['name']) else items |
|
241 |
|
|
242 |
def _filter_by_name(self, items): |
|
243 |
return self._non_exact_name_filter(self._exact_name_filter(items)) |
|
244 |
|
|
245 |
|
|
246 |
class _id_filter(object): |
|
247 |
|
|
248 |
if_arguments = dict( |
|
249 |
id=ValueArgument('filter by id', '--id'), |
|
250 |
id_pref=ValueArgument( |
|
251 |
'filter by id prefix (case insensitive)', '--id-prefix'), |
|
252 |
id_suff=ValueArgument( |
|
253 |
'filter by id suffix (case insensitive)', '--id-suffix'), |
|
254 |
id_like=ValueArgument( |
|
255 |
'print only if id contains this (case insensitive)', |
|
256 |
'--id-like') |
|
257 |
) |
|
258 |
|
|
259 |
def _non_exact_id_filter(self, items): |
|
260 |
np, ns, nl = self['id_pref'], self['id_suff'], self['id_like'] |
|
261 |
return [item for item in items if ( |
|
262 |
(not np) or ( |
|
263 |
'%s' % item['id']).lower().startswith(np.lower())) and ( |
|
264 |
(not ns) or ('%s' % item['id']).lower().endswith(ns.lower())) and ( |
|
265 |
(not nl) or nl.lower() in ('%s' % item['id']).lower())] |
|
266 |
|
|
267 |
def _exact_id_filter(self, items): |
|
268 |
return filter_dicts_by_dict(items, dict(id=self['id'])) if ( |
|
269 |
self['id']) else items |
|
270 |
|
|
271 |
def _filter_by_id(self, items): |
|
272 |
return self._non_exact_id_filter(self._exact_id_filter(items)) |
Also available in: Unified diff