Revision 6d190dd1 kamaki/cli/commands/image.py

b/kamaki/cli/commands/image.py
47 47
from kamaki.cli.commands.cyclades import _init_cyclades
48 48
from kamaki.cli.errors import raiseCLIError, CLIBaseUrlError
49 49
from kamaki.cli.commands import _command_init, errors, addLogSettings
50
from kamaki.cli.commands import _optional_output_cmd, _optional_json
50
from kamaki.cli.commands import (
51
    _optional_output_cmd, _optional_json, _name_filter, _id_filter)
51 52

  
52 53

  
53 54
image_cmds = CommandTree(
......
172 173

  
173 174

  
174 175
@command(image_cmds)
175
class image_list(_init_image, _optional_json):
176
class image_list(_init_image, _optional_json, _name_filter, _id_filter):
176 177
    """List images accessible by user"""
177 178

  
178 179
    PERMANENTS = (
......
185 186
            'filter by container format',
186 187
            '--container-format'),
187 188
        disk_format=ValueArgument('filter by disk format', '--disk-format'),
188
        name=ValueArgument('filter by name', '--name'),
189
        name_pref=ValueArgument(
190
            'filter by name prefix (case insensitive)', '--name-prefix'),
191
        name_suff=ValueArgument(
192
            'filter by name suffix (case insensitive)', '--name-suffix'),
193
        name_like=ValueArgument(
194
            'print only if name contains this (case insensitive)',
195
            '--name-like'),
196 189
        size_min=IntArgument('filter by minimum size', '--size-min'),
197 190
        size_max=IntArgument('filter by maximum size', '--size-max'),
198 191
        status=ValueArgument('filter by status', '--status'),
......
213 206
            ('--property-like')),
214 207
    )
215 208

  
216
    def _filtered_by_owner(self, images):
209
    def _filter_by_owner(self, images):
217 210
        ouuid = self['owner'] or self._username2uuid(self['owner_name'])
218 211
        return filter_dicts_by_dict(images, dict(owner=ouuid))
219 212

  
220
    def _filtered_by_name(self, images):
221
        np, ns, nl = self['name_pref'], self['name_suff'], self['name_like']
222
        return [img for img in images if (
223
            (not np) or img['name'].lower().startswith(np.lower())) and (
224
            (not ns) or img['name'].lower().endswith(ns.lower())) and (
225
            (not nl) or nl.lower() in img['name'].lower())]
226

  
227 213
    def _add_owner_name(self, images):
228 214
        uuids = self._uuids2usernames(
229 215
            list(set([img['owner'] for img in images])))
......
231 217
            img['owner'] += ' (%s)' % uuids[img['owner']]
232 218
        return images
233 219

  
234
    def _filtered_by_properties(self, images):
220
    def _filter_by_properties(self, images):
235 221
        new_images = []
236 222
        for img in images:
237 223
            props = [dict(img['properties'])]
......
266 252
        images = self.client.list_public(detail, filters, order)
267 253

  
268 254
        if self['owner'] or self['owner_name']:
269
            images = self._filtered_by_owner(images)
255
            images = self._filter_by_owner(images)
270 256
        if self['prop'] or self['prop_like']:
271
            images = self._filtered_by_properties(images)
272
        images = self._filtered_by_name(images)
257
            images = self._filter_by_properties(images)
258
        images = self._filter_by_id(images)
259
        images = self._non_exact_name_filter(images)
273 260

  
274 261
        if self['detail'] and not self['json_output']:
275 262
            images = self._add_owner_name(images)
......
622 609

  
623 610

  
624 611
@command(image_cmds)
625
class image_compute_list(_init_cyclades, _optional_json):
612
class image_compute_list(
613
        _init_cyclades, _optional_json, _name_filter, _id_filter):
626 614
    """List images"""
627 615

  
628 616
    PERMANENTS = ('id', 'name')
......
634 622
            'output results in pages (-n to set items per page, default 10)',
635 623
            '--more'),
636 624
        enum=FlagArgument('Enumerate results', '--enumerate'),
637
        name=ValueArgument('filter by name', '--name'),
638
        name_pref=ValueArgument(
639
            'filter by name prefix (case insensitive)',
640
            '--name-prefix'),
641
        name_suff=ValueArgument(
642
            'filter by name suffix (case insensitive)',
643
            '--name-suffix'),
644
        name_like=ValueArgument(
645
            'print only if name contains this (case insensitive)',
646
            '--name-like'),
647 625
        user_id=ValueArgument('filter by user_id', '--user-id'),
648 626
        user_name=ValueArgument('filter by username', '--user-name'),
649 627
        meta=KeyValueArgument(
......
653 631
            ('--metadata-like'))
654 632
    )
655 633

  
656
    def _filtered_by_name(self, images):
657
        np, ns, nl = self['name_pref'], self['name_suff'], self['name_like']
658
        return [img for img in images if (
659
            (not np) or img['name'].lower().startswith(np.lower())) and (
660
            (not ns) or img['name'].lower().endswith(ns.lower())) and (
661
            (not nl) or nl.lower() in img['name'].lower())]
662

  
663 634
    def _filter_by_metadata(self, images):
664 635
        new_images = []
665 636
        for img in images:
......
691 662
        withuser = bool(self['user_id'] or self['user_name'])
692 663
        detail = self['detail'] or withmeta or withuser
693 664
        images = self.client.list_images(detail)
694
        images = self._filtered_by_name(images)
665
        images = self._filter_by_name(images)
666
        images = self._filter_by_id(images)
695 667
        if withuser:
696 668
            images = self._filter_by_user(images)
697 669
        if withmeta:

Also available in: Unified diff