This file is indexed.

/usr/share/pyshared/allmydata/util/dictutil.py is in tahoe-lafs 1.9.2-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
"""
Tools to mess with dicts.
"""

import copy, operator
from bisect import bisect_left, insort_left

from allmydata.util.assertutil import _assert, precondition

def move(k, d1, d2, strict=False):
    """
    Move item with key k from d1 to d2.
    """
    if strict and not d1.has_key(k):
        raise KeyError, k

    d2[k] = d1[k]
    del d1[k]

def subtract(d1, d2):
    """
    Remove all items from d1 whose key occurs in d2.

    @returns d1
    """
    if len(d1) > len(d2):
        for k in d2.keys():
            if d1.has_key(k):
                del d1[k]
    else:
        for k in d1.keys():
            if d2.has_key(k):
                del d1[k]
    return d1

class DictOfSets(dict):
    def add(self, key, value):
        if key in self:
            self[key].add(value)
        else:
            self[key] = set([value])

    def update(self, otherdictofsets):
        for key, values in otherdictofsets.iteritems():
            if key in self:
                self[key].update(values)
            else:
                self[key] = set(values)

    def discard(self, key, value):
        if not key in self:
            return
        self[key].discard(value)
        if not self[key]:
            del self[key]

class UtilDict:
    def __init__(self, initialdata={}):
        self.d = {}
        self.update(initialdata)

    def del_if_present(self, key):
        if self.has_key(key):
            del self[key]

    def items_sorted_by_value(self):
        """
        @return a sequence of (key, value,) pairs sorted according to value
        """
        l = [(x[1], x[0],) for x in self.d.iteritems()]
        l.sort()
        return [(x[1], x[0],) for x in l]

    def items_sorted_by_key(self):
        """
        @return a sequence of (key, value,) pairs sorted according to key
        """
        l = self.d.items()
        l.sort()
        return l

    def __repr__(self, *args, **kwargs):
        return self.d.__repr__(*args, **kwargs)

    def __str__(self, *args, **kwargs):
        return self.d.__str__(*args, **kwargs)

    def __contains__(self, *args, **kwargs):
        return self.d.__contains__(*args, **kwargs)

    def __len__(self, *args, **kwargs):
        return self.d.__len__(*args, **kwargs)

    def __cmp__(self, other):
        try:
            return self.d.__cmp__(other)
        except TypeError, le:
            # maybe we should look for a .d member in other.  I know this is insanely kludgey, but the Right Way To Do It is for dict.__cmp__ to use structural typing ("duck typing")
            try:
                return self.d.__cmp__(other.d)
            except:
                raise le

    def __eq__(self, *args, **kwargs):
        return self.d.__eq__(*args, **kwargs)

    def __ne__(self, *args, **kwargs):
        return self.d.__ne__(*args, **kwargs)

    def __gt__(self, *args, **kwargs):
        return self.d.__gt__(*args, **kwargs)

    def __ge__(self, *args, **kwargs):
        return self.d.__ge__(*args, **kwargs)

    def __le__(self, *args, **kwargs):
        return self.d.__le__(*args, **kwargs)

    def __lt__(self, *args, **kwargs):
        return self.d.__lt__(*args, **kwargs)

    def __getitem__(self, *args, **kwargs):
        return self.d.__getitem__(*args, **kwargs)

    def __setitem__(self, *args, **kwargs):
        return self.d.__setitem__(*args, **kwargs)

    def __delitem__(self, *args, **kwargs):
        return self.d.__delitem__(*args, **kwargs)

    def __iter__(self, *args, **kwargs):
        return self.d.__iter__(*args, **kwargs)

    def clear(self, *args, **kwargs):
        return self.d.clear(*args, **kwargs)

    def copy(self, *args, **kwargs):
        return self.__class__(self.d.copy(*args, **kwargs))

    def fromkeys(self, *args, **kwargs):
        return self.__class__(self.d.fromkeys(*args, **kwargs))

    def get(self, key, default=None):
        return self.d.get(key, default)

    def has_key(self, *args, **kwargs):
        return self.d.has_key(*args, **kwargs)

    def items(self, *args, **kwargs):
        return self.d.items(*args, **kwargs)

    def iteritems(self, *args, **kwargs):
        return self.d.iteritems(*args, **kwargs)

    def iterkeys(self, *args, **kwargs):
        return self.d.iterkeys(*args, **kwargs)

    def itervalues(self, *args, **kwargs):
        return self.d.itervalues(*args, **kwargs)

    def keys(self, *args, **kwargs):
        return self.d.keys(*args, **kwargs)

    def pop(self, *args, **kwargs):
        return self.d.pop(*args, **kwargs)

    def popitem(self, *args, **kwargs):
        return self.d.popitem(*args, **kwargs)

    def setdefault(self, *args, **kwargs):
        return self.d.setdefault(*args, **kwargs)

    def update(self, *args, **kwargs):
        self.d.update(*args, **kwargs)

    def values(self, *args, **kwargs):
        return self.d.values(*args, **kwargs)

class NumDict:
    def __init__(self, initialdict={}):
        self.d = copy.deepcopy(initialdict)

    def add_num(self, key, val, default=0):
        """
        If the key doesn't appear in self then it is created with value default
        (before addition).
        """
        self.d[key] = self.d.get(key, default) + val

    def subtract_num(self, key, val, default=0):
        self.d[key] = self.d.get(key, default) - val

    def sum(self):
        """
        @return: the sum of all values
        """
        return reduce(operator.__add__, self.d.values())

    def inc(self, key, default=0):
        """
        Increment the value associated with key in dict.  If there is no such
        key, then one will be created with initial value 0 (before inc() --
        therefore value 1 after inc).
        """
        self.add_num(key, 1, default)

    def dec(self, key, default=0):
        """
        Decrement the value associated with key in dict.  If there is no such
        key, then one will be created with initial value 0 (before dec() --
        therefore value -1 after dec).
        """
        self.subtract_num(key, 1, default)

    def items_sorted_by_value(self):
        """
        @return a sequence of (key, value,) pairs sorted according to value
        """
        l = [(x[1], x[0],) for x in self.d.iteritems()]
        l.sort()
        return [(x[1], x[0],) for x in l]

    def item_with_largest_value(self):
        it = self.d.iteritems()
        (winner, winnerval,) = it.next()
        try:
            while True:
                n, nv = it.next()
                if nv > winnerval:
                    winner = n
                    winnerval = nv
        except StopIteration:
            pass
        return (winner, winnerval,)

    def items_sorted_by_key(self):
        """
        @return a sequence of (key, value,) pairs sorted according to key
        """
        l = self.d.items()
        l.sort()
        return l

    def __repr__(self, *args, **kwargs):
        return self.d.__repr__(*args, **kwargs)

    def __str__(self, *args, **kwargs):
        return self.d.__str__(*args, **kwargs)

    def __contains__(self, *args, **kwargs):
        return self.d.__contains__(*args, **kwargs)

    def __len__(self, *args, **kwargs):
        return self.d.__len__(*args, **kwargs)

    def __cmp__(self, other):
        try:
            return self.d.__cmp__(other)
        except TypeError, le:
            # maybe we should look for a .d member in other.  I know this is insanely kludgey, but the Right Way To Do It is for dict.__cmp__ to use structural typing ("duck typing")
            try:
                return self.d.__cmp__(other.d)
            except:
                raise le

    def __eq__(self, *args, **kwargs):
        return self.d.__eq__(*args, **kwargs)

    def __ne__(self, *args, **kwargs):
        return self.d.__ne__(*args, **kwargs)

    def __gt__(self, *args, **kwargs):
        return self.d.__gt__(*args, **kwargs)

    def __ge__(self, *args, **kwargs):
        return self.d.__ge__(*args, **kwargs)

    def __le__(self, *args, **kwargs):
        return self.d.__le__(*args, **kwargs)

    def __lt__(self, *args, **kwargs):
        return self.d.__lt__(*args, **kwargs)

    def __getitem__(self, *args, **kwargs):
        return self.d.__getitem__(*args, **kwargs)

    def __setitem__(self, *args, **kwargs):
        return self.d.__setitem__(*args, **kwargs)

    def __delitem__(self, *args, **kwargs):
        return self.d.__delitem__(*args, **kwargs)

    def __iter__(self, *args, **kwargs):
        return self.d.__iter__(*args, **kwargs)

    def clear(self, *args, **kwargs):
        return self.d.clear(*args, **kwargs)

    def copy(self, *args, **kwargs):
        return self.__class__(self.d.copy(*args, **kwargs))

    def fromkeys(self, *args, **kwargs):
        return self.__class__(self.d.fromkeys(*args, **kwargs))

    def get(self, key, default=0):
        return self.d.get(key, default)

    def has_key(self, *args, **kwargs):
        return self.d.has_key(*args, **kwargs)

    def items(self, *args, **kwargs):
        return self.d.items(*args, **kwargs)

    def iteritems(self, *args, **kwargs):
        return self.d.iteritems(*args, **kwargs)

    def iterkeys(self, *args, **kwargs):
        return self.d.iterkeys(*args, **kwargs)

    def itervalues(self, *args, **kwargs):
        return self.d.itervalues(*args, **kwargs)

    def keys(self, *args, **kwargs):
        return self.d.keys(*args, **kwargs)

    def pop(self, *args, **kwargs):
        return self.d.pop(*args, **kwargs)

    def popitem(self, *args, **kwargs):
        return self.d.popitem(*args, **kwargs)

    def setdefault(self, *args, **kwargs):
        return self.d.setdefault(*args, **kwargs)

    def update(self, *args, **kwargs):
        return self.d.update(*args, **kwargs)

    def values(self, *args, **kwargs):
        return self.d.values(*args, **kwargs)

def del_if_present(d, k):
    if d.has_key(k):
        del d[k]

class ValueOrderedDict:
    """
    Note: this implementation assumes that the values do not mutate and change
    their sort order.  That is, it stores the values in a sorted list and
    as items are added and removed from the dict, it makes updates to the list
    which will keep the list sorted.  But if a value that is currently sitting
    in the list changes its sort order, then the internal consistency of this
    object will be lost.

    If that happens, and if assertion checking is turned on, then you will get
    an assertion failure the very next time you try to do anything with this
    ValueOrderedDict.  However, those internal consistency checks are very slow
    and almost certainly unacceptable to leave turned on in production code.
    """
    class ItemIterator:
        def __init__(self, c):
            self.c = c
            self.i = 0
        def __iter__(self):
            return self
        def next(self):
            precondition(self.i <= len(self.c.l), "The iterated ValueOrderedDict doesn't have this many elements.  Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, self.c)
            precondition((self.i == len(self.c.l)) or self.c.d.has_key(self.c.l[self.i][1]), "The iterated ValueOrderedDict doesn't have this key.  Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, (self.i < len(self.c.l)) and self.c.l[self.i], self.c)
            if self.i == len(self.c.l):
                raise StopIteration
            le = self.c.l[self.i]
            self.i += 1
            return (le[1], le[0],)

    def iteritems(self):
        return ValueOrderedDict.ItemIterator(self)

    def items(self):
        return zip(map(operator.__getitem__, self.l, [1]*len(self.l)), map(operator.__getitem__, self.l, [0]*len(self.l)))

    def values(self):
        return map(operator.__getitem__, self.l, [0]*len(self.l))

    def keys(self):
        return map(operator.__getitem__, self.l, [1]*len(self.l))

    class KeyIterator:
        def __init__(self, c):
            self.c = c
            self.i = 0
        def __iter__(self):
            return self
        def next(self):
            precondition(self.i <= len(self.c.l), "The iterated ValueOrderedDict doesn't have this many elements.  Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, self.c)
            precondition((self.i == len(self.c.l)) or self.c.d.has_key(self.c.l[self.i][1]), "The iterated ValueOrderedDict doesn't have this key.  Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, (self.i < len(self.c.l)) and self.c.l[self.i], self.c)
            if self.i == len(self.c.l):
                raise StopIteration
            le = self.c.l[self.i]
            self.i += 1
            return le[1]

    def iterkeys(self):
        return ValueOrderedDict.KeyIterator(self)

    class ValueIterator:
        def __init__(self, c):
            self.c = c
            self.i = 0
        def __iter__(self):
            return self
        def next(self):
            precondition(self.i <= len(self.c.l), "The iterated ValueOrderedDict doesn't have this many elements.  Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, self.c)
            precondition((self.i == len(self.c.l)) or self.c.d.has_key(self.c.l[self.i][1]), "The iterated ValueOrderedDict doesn't have this key.  Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, (self.i < len(self.c.l)) and self.c.l[self.i], self.c)
            if self.i == len(self.c.l):
                raise StopIteration
            le = self.c.l[self.i]
            self.i += 1
            return le[0]

    def itervalues(self):
        return ValueOrderedDict.ValueIterator(self)

    def __init__(self, initialdata={}):
        self.d = {} # k: key, v: val
        self.l = [] # sorted list of tuples of (val, key,)
        self.update(initialdata)
        assert self._assert_invariants()

    def __len__(self):
        return len(self.l)

    def __repr_n__(self, n=None):
        s = ["{",]
        try:
            iter = self.iteritems()
            x = iter.next()
            s.append(str(x[0])); s.append(": "); s.append(str(x[1]))
            i = 1
            while (n is None) or (i < n):
                x = iter.next()
                s.append(", "); s.append(str(x[0])); s.append(": "); s.append(str(x[1]))
        except StopIteration:
            pass
        s.append("}")
        return ''.join(s)

    def __repr__(self):
        return "<%s %s>" % (self.__class__.__name__, self.__repr_n__(),)

    def __str__(self):
        return "<%s %s>" % (self.__class__.__name__, self.__repr_n__(16),)

    def __eq__(self, other):
        for (k, v,) in other.iteritems():
            if not self.d.has_key(k) or self.d[k] != v:
                return False
        return True

    def __ne__(self, other):
        return not self.__eq__(other)

    def _assert_invariants(self):
        iter = self.l.__iter__()
        try:
            oldx = iter.next()
            while True:
                x = iter.next()
                # self.l is required to be sorted
                _assert(x >= oldx, x, oldx)
                # every element of self.l is required to appear in self.d
                _assert(self.d.has_key(x[1]), x)
                oldx =x
        except StopIteration:
            pass
        for (k, v,) in self.d.iteritems():
            i = bisect_left(self.l, (v, k,))
            while (self.l[i][0] is not v) or (self.l[i][1] is not k):
                i += 1
            _assert(i < len(self.l), i, len(self.l), k, v, self.l)
            _assert(self.l[i][0] is v, i, v, l=self.l, d=self.d)
            _assert(self.l[i][1] is k, i, k, l=self.l, d=self.d)
        return True

    def insert(self, key, val=None):
        assert self._assert_invariants()
        result = self.__setitem__(key, val)
        assert self._assert_invariants()
        return result

    def setdefault(self, key, default=None):
        assert self._assert_invariants()
        if not self.has_key(key):
            self[key] = default
        assert self._assert_invariants()
        return self[key]

    def __setitem__(self, key, val=None):
        assert self._assert_invariants()
        if self.d.has_key(key):
            oldval = self.d[key]
            if oldval != val:
                # re-sort
                i = bisect_left(self.l, (oldval, key,))
                while (self.l[i][0] is not oldval) or (self.l[i][1] is not key):
                    i += 1
                self.l.pop(i)
                insort_left(self.l, (val, key,))
            elif oldval is not val:
                # replace
                i = bisect_left(self.l, (oldval, key,))
                while (self.l[i][0] is not oldval) or (self.l[i][1] is not key):
                    i += 1
                self.l[i] = (val, key,)
        else:
            insort_left(self.l, (val, key,))

        self.d[key] = val
        assert self._assert_invariants()
        return val

    def remove(self, key, default=None, strictkey=True):
        assert self._assert_invariants()
        result = self.__delitem__(key, default, strictkey)
        assert self._assert_invariants()
        return result

    def __getitem__(self, key, default=None, strictkey=True):
        if not self.d.has_key(key):
            if strictkey:
                raise KeyError, key
            else:
                return default
        return self.d[key]

    def __delitem__(self, key, default=None, strictkey=True):
        """
        @param strictkey: True if you want a KeyError in the case that
            key is not there, False if you want a reference to default
            in the case that key is not there
        @param default: the object to return if key is not there; This
            is ignored if strictkey.

        @return: the object removed or default if there is not item by
            that key and strictkey is False
        """
        assert self._assert_invariants()
        if self.d.has_key(key):
            val = self.d.pop(key)
            i = bisect_left(self.l, (val, key,))
            while (self.l[i][0] is not val) or (self.l[i][1] is not key):
                i += 1
            self.l.pop(i)
            assert self._assert_invariants()
            return val
        elif strictkey:
            assert self._assert_invariants()
            raise KeyError, key
        else:
            assert self._assert_invariants()
            return default

    def clear(self):
        assert self._assert_invariants()
        self.d.clear()
        del self.l[:]
        assert self._assert_invariants()

    def update(self, otherdict):
        """
        @return: self
        """
        assert self._assert_invariants()
        for (k, v,) in otherdict.iteritems():
            self.insert(k, v)
        assert self._assert_invariants()
        return self

    def has_key(self, key):
        assert self._assert_invariants()
        return self.d.has_key(key)

    def popitem(self):
        if not self.l:
            raise KeyError, 'popitem(): dictionary is empty'
        le = self.l.pop(0)
        del self.d[le[1]]
        return (le[1], le[0],)

    def pop(self, k, default=None, strictkey=False):
        if not self.d.has_key(k):
            if strictkey:
                raise KeyError, k
            else:
                return default
        v = self.d.pop(k)
        i = bisect_left(self.l, (v, k,))
        while (self.l[i][0] is not v) or (self.l[i][1] is not k):
            i += 1
        self.l.pop(i)
        return v

    def pop_from_list(self, i=0):
        le = self.l.pop(i)
        del self.d[le[1]]
        return le[1]

class AuxValueDict(dict):
    """I behave like a regular dict, but each key is associated with two
    values: the main value, and an auxilliary one. Setting the main value
    (with the usual d[key]=value) clears the auxvalue. You can set both main
    and auxvalue at the same time, and can retrieve the values separately.

    The main use case is a dictionary that represents unpacked child values
    for a directory node, where a common pattern is to modify one or more
    children and then pass the dict back to a packing function. The original
    packed representation can be cached in the auxvalue, and the packing
    function can use it directly on all unmodified children. On large
    directories with a complex packing function, this can save considerable
    time."""

    def __init__(self, *args, **kwargs):
        super(AuxValueDict, self).__init__(*args, **kwargs)
        self.auxilliary = {}

    def __setitem__(self, key, value):
        super(AuxValueDict, self).__setitem__(key, value)
        self.auxilliary[key] = None # clear the auxvalue

    def __delitem__(self, key):
        super(AuxValueDict, self).__delitem__(key)
        self.auxilliary.pop(key)

    def get_aux(self, key, default=None):
        """Retrieve the auxilliary value. There is no way to distinguish
        between an auxvalue of 'None' and a key that does not have an
        auxvalue, and get_aux() will not raise KeyError when called with a
        missing key."""
        return self.auxilliary.get(key, default)

    def set_with_aux(self, key, value, auxilliary):
        """Set both the main value and the auxilliary value. There is no way
        to distinguish between an auxvalue of 'None' and a key that does not
        have an auxvalue."""
        super(AuxValueDict, self).__setitem__(key, value)
        self.auxilliary[key] = auxilliary