Revision 5576a4eb kamaki/cli/commands/image.py

b/kamaki/cli/commands/image.py
209 209
            'output results in pages (-n to set items per page, default 10)',
210 210
            '--more'),
211 211
        enum=FlagArgument('Enumerate results', '--enumerate'),
212
        prop=KeyValueArgument('filter by property key=value', ('--property'))
212
        prop=KeyValueArgument('filter by property key=value', ('--property')),
213
        prop_like=KeyValueArgument(
214
            'fliter by property key=value where value is part of actual value',
215
            ('--property-like'))
213 216
    )
214 217

  
215 218
    def _filtered_by_owner(self, detail, *list_params):
......
227 230

  
228 231
    def _filtered_by_name(self, images):
229 232
        np, ns, nl = self['name_pref'], self['name_suff'], self['name_like']
230
        uuids = {}
231

  
232
        def fish_uuids(img):
233
            if self['detail'] and not self['json_output']:
234
                uuids[img['owner']] = ''
235
            return img
236

  
237
        r = [fish_uuids(img) for img in images if (
233
        return [img for img in images if (
238 234
            (not np) or img['name'].lower().startswith(np.lower())) and (
239 235
            (not ns) or img['name'].lower().endswith(ns.lower())) and (
240 236
            (not nl) or nl.lower() in img['name'].lower())]
241 237

  
242
        if self['detail'] and not self['json_output']:
243
            uuids = self._uuids2usernames(uuids.keys())
244
            for img in r:
245
                img['owner'] += ' (%s)' % uuids[img['owner']]
246

  
247
        return r
238
    def _add_owner_name(self, images):
239
        uuids = self._uuids2usernames(
240
            list(set([img['owner'] for img in images])))
241
        for img in images:
242
            img['owner'] += ' (%s)' % uuids[img['owner']]
243
        return images
248 244

  
249 245
    def _filtered_by_properties(self, images):
250 246
        new_images = []
247

  
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
255

  
251 256
        for img in images:
252
            if set(self['prop'].items()).difference(img['properties'].items()):
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)):
253 262
                continue
254
            if self['detail']:
263
            elif self['detail']:
255 264
                new_images.append(dict(img))
256 265
            else:
257 266
                new_images.append(dict())
......
274 283
            filters[arg] = self[arg]
275 284

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

  
283 292
        images = self._filtered_by_name(images)
284
        if self['prop']:
293
        if self['detail'] and not self['json_output']:
294
            images = self._add_owner_name(images)
295
        if self['prop'] or self['prop_like']:
285 296
            images = self._filtered_by_properties(images)
286 297
        kwargs = dict(with_enumeration=self['enum'])
287 298
        if self['more']:

Also available in: Unified diff