Revision 854222c7

b/kamaki/cli/commands/image.py
37 37

  
38 38
from kamaki.cli import command
39 39
from kamaki.cli.command_tree import CommandTree
40
from kamaki.cli.utils import print_dict, print_json
40
from kamaki.cli.utils import print_dict, print_json, filter_dicts_by_dict
41 41
from kamaki.clients.image import ImageClient
42 42
from kamaki.clients.pithos import PithosClient
43 43
from kamaki.clients.astakos import AstakosClient
......
215 215
            ('--property-like')),
216 216
    )
217 217

  
218
    def _filtered_by_owner(self, detail, *list_params):
219
        images = []
218
    def _filtered_by_owner(self, images):
220 219
        ouuid = self['owner'] or self._username2uuid(self['owner_name'])
221
        if not ouuid:
222
            return images
223
        for img in self.client.list_public(True, *list_params):
224
            if img['owner'] == ouuid:
225
                if not detail:
226
                    for key in set(img.keys()).difference(self.PERMANENTS):
227
                        img.pop(key)
228
                images.append(img)
229
        return images
220
        return filter_dicts_by_dict(images, dict(owner=ouuid))
230 221

  
231 222
    def _filtered_by_name(self, images):
232 223
        np, ns, nl = self['name_pref'], self['name_suff'], self['name_like']
......
245 236
    def _filtered_by_properties(self, images):
246 237
        new_images = []
247 238

  
248
        def like_properties(props):
249
            plike = self['prop_like']
250
            for k, v in plike.items():
251
                likestr = props.get(k, '').lower()
252
                if v.lower() not in likestr:
253
                    return False
254
            return True
239
        # def like_properties(props):
240
        #     plike = self['prop_like']
241
        #     for k, v in plike.items():
242
        #         likestr = props.get(k, '').lower()
243
        #         if v.lower() not in likestr:
244
        #             return False
245
        #     return True
255 246

  
256 247
        for img in images:
257
            props = img['properties']
258
            if (
259
                    self['prop'] and set(
260
                        self['prop'].items()).difference(props.items())) or (
261
                    self['prop_like'] and not like_properties(props)):
262
                continue
263
            elif self['detail']:
264
                new_images.append(dict(img))
265
            else:
266
                new_images.append(dict())
267
                for k in set(img).intersection(self.PERMANENTS):
268
                    new_images[-1][k] = img[k]
248
            props = [dict(img['properties'])]
249
            if self['prop']:
250
                props = filter_dicts_by_dict(props, self['prop'])
251
            if props and self['prop_like']:
252
                props = filter_dicts_by_dict(
253
                    props, self['prop_like'], exact_match=False)
254
            if props:
255
                new_images.append(img)
256

  
257
            #if (
258
            #        self['prop'] and set(
259
            #            self['prop'].items()).difference(props.items())) or (
260
            #        self['prop_like'] and not like_properties(props)):
261
            #    continue
262
            #new_images.append(img)
269 263
        return new_images
270 264

  
271 265
    @errors.generic.all
......
283 277
            filters[arg] = self[arg]
284 278

  
285 279
        order = self['order']
286
        detail = self['detail'] or self['prop'] or self['prop_like']
287
        if self['owner'] or self['owner_name']:
288
            images = self._filtered_by_owner(detail, filters, order)
289
        else:
290
            images = self.client.list_public(detail, filters, order)
280
        detail = self['detail'] or (
281
            self['prop'] or self['prop_like']) or (
282
            self['owner'] or self['owner_name'])
291 283

  
284
        images = self.client.list_public(detail, filters, order)
285

  
286
        if self['owner'] or self['owner_name']:
287
            images = self._filtered_by_owner(images)
288
        if self['prop'] or self['prop_like']:
289
            images = self._filtered_by_properties(images)
292 290
        images = self._filtered_by_name(images)
291

  
293 292
        if self['detail'] and not self['json_output']:
294 293
            images = self._add_owner_name(images)
295
        if self['prop'] or self['prop_like']:
296
            images = self._filtered_by_properties(images)
294
        elif detail and not self['detail']:
295
            for img in images:
296
                for key in set(img).difference(self.PERMANENTS):
297
                    img.pop(key)
297 298
        kwargs = dict(with_enumeration=self['enum'])
298 299
        if self['more']:
299 300
            kwargs['page_size'] = self['limit'] or 10
......
661 662
    def _filter_by_metadata(self, images):
662 663
        new_images = []
663 664

  
664
        def like_metadata(meta):
665
            mlike = self['meta_like']
666
            for k, v in mlike.items():
667
                likestr = meta.get(k, '').lower()
668
                if v.lower() not in likestr:
669
                    return False
670
            return True
671

  
672 665
        for img in images:
673
            meta = img['metadata']
674
            if (
675
                    self['meta'] and set(
676
                        self['meta'].items()).difference(meta.items())) or (
677
                    self['meta_like'] and not like_metadata(meta)):
678
                continue
679
            elif self['detail']:
680
                new_images.append(dict(img))
681
            else:
682
                new_images.append(dict())
683
                for k in set(img).intersection(self.PERMANENTS):
684
                    new_images[-1][k] = img[k]
666
            meta = [dict(img['metadata'])]
667
            if self['meta']:
668
                meta = filter_dicts_by_dict(meta, self['meta'])
669
            if meta and self['meta_like']:
670
                meta = filter_dicts_by_dict(
671
                    meta, self['meta_like'], exact_match=False)
672
            if meta:
673
                new_images.append(img)
685 674
        return new_images
686 675

  
687 676
    def _add_name(self, images, key='user_id'):
b/kamaki/cli/utils/test.py
494 494
                (dlist, {'k3': 'v'}, False, False, dlist[0:1] + dlist[2:4]),
495 495
                (dlist, {'k3': 'v'}, False, True, dlist[0:1] + dlist[2:3]),
496 496
                (dlist, {'k3': 'v'}, True, True, []),
497
                (dlist, dlist[4], True, False, dlist),
497 498
                ):
498 499
            self.assertEqual(exp, filter_dicts_by_dict(l, f, em, cs))
499 500

  

Also available in: Unified diff