~ubuntu-branches/ubuntu/lucid/desktopcouch/lucid

« back to all changes in this revision

Viewing changes to desktopcouch/records/record.py

  • Committer: Elliot Murphy
  • Date: 2010-03-01 17:18:07 UTC
  • mfrom: (1.5.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: elliot@elliotmurphy.com-20100301171807-fb8hzyzfbgcuig2j
* New upstream bugfix release.
  - Include a new program bin/desktopcouch-get-port .
  - Use DBus to get port, and thereby start the service daemon, on normal
    Python access to the database. (LP: #519444)
  - Update docs to be explicit about put_record(r) mutating r .
  - Add new method put_records_batch(iterable_of_records) .
  - Fix up apport collection.
  - Fix a problem with couchdb mutating its INI file to store a hashed
    password, which is useless for HTTP Basic auth in the bookmark file.
  - Fix obscure bugs in couchdb startup regarding port availability.
  - Update execute_view() ti take a dict of additional parameters for
    execution.
  - Add has_key() method to desktopcouch.record.RecordDict so that it
    behaves more like a dictionary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
198
198
    def __contains__(self, key):
199
199
        return key in self._data
200
200
 
 
201
    def __cmp__(self, value):
 
202
        if isinstance(value, RecordDict):
 
203
            return cmp(self._data, value._data)
 
204
        return -1
 
205
        
201
206
    def get(self, key, default=None):
202
207
        """Override get method."""
203
208
        if not key in self._data:
226
231
            self[key] = default
227
232
        return self[key]
228
233
 
 
234
    def has_key(self, key):
 
235
        return key in self
 
236
        
 
237
 
229
238
 
230
239
class MergeableList(RecordData):
231
240
    """An object that represents a list of complex values."""
267
276
            value = value._data
268
277
        return value in self._data.values()
269
278
 
 
279
    def __cmp__(self, value):
 
280
        """
 
281
        Implement the compare to be able to compare with other mergeable
 
282
        lists, tuples and lists.
 
283
        """
 
284
        if (hasattr(value, "__iter__") 
 
285
            and hasattr(value, "__getitem__")
 
286
            and hasattr(value, "__len__")):
 
287
            # we should have the same value in the same order
 
288
            length  = len(value)
 
289
            if len(self) == length:
 
290
                for index, current_value in enumerate(self):
 
291
                    cmp_value = cmp(self[index], value[index])
 
292
                    if cmp_value != 0:
 
293
                        return cmp_value
 
294
                return 0
 
295
        return -1
 
296
        
270
297
    def _get_ordered_keys(self):
271
298
        """Get list of uuid keys ordered by 'order' property or uuid key."""
272
299
        result = []
295
322
            new_uuid)
296
323
        super(MergeableList, self).__setitem__(new_uuid, value)
297
324
 
 
325
    def remove(self, value):
 
326
        if len(self) == 1:
 
327
            raise ValueError("MergeableList cannot be empty.")
 
328
        index = 0
 
329
        for current_value in self:
 
330
            # important! use the data in self first 'cause mergeable lists
 
331
            # can be compared with list and tuples but no the other way around
 
332
            if cmp(current_value, value) == 0:
 
333
                del self[index]
 
334
                return
 
335
            index += 1
 
336
        raise ValueError("list.remove(x): x not in list")
 
337
        
 
338
    def pop(self, index):
 
339
        if len(self) == 1:
 
340
            raise ValueError("MergeableList cannot be empty.")
 
341
        value = self[index]
 
342
        del self[index]
 
343
        return value
 
344
        
298
345
    def index(self, key):
299
346
        """Get value by index."""
300
347
        return self.__getitem__(key)