This file is indexed.

/usr/share/pyshared/peak/util/addons.py is in python-peak.util 20110909-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
from peak.util.decorators import decorate, decorate_class, enclosing_frame, classy
from weakref import ref
import sys

__all__ = ['AddOn', 'ClassAddOn', 'Registry', 'addons_for']

_addons = {}

def addons_for(ob):
    """Get the dictionary that should contain add-ons for `ob`"""
    try:
        d = ob.__dict__
        sd = d.setdefault
        return d
    except (AttributeError, TypeError):
        r = ref(ob)
        try:
            return _addons[r]
        except KeyError:
            return _addons.setdefault(ref(ob, _addons.__delitem__), {})


def additional_tests():
    import doctest
    return doctest.DocFileSuite(
        'README.txt', package='__main__',
        optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
    )













class AddOn(classy):
    """Attach extra state to (almost) any object"""

    __slots__ = ()

    decorate(classmethod)
    def __class_call__(cls, ob, *data):
        a = addons_for(ob)
        addon_key = cls.addon_key(*data)
        try:
            return a[addon_key]
        except KeyError:
            # Use setdefault() to prevent race conditions
            ob = a.setdefault(addon_key, super(AddOn, cls).__class_call__(ob, *data))
            return ob

    decorate(classmethod)
    def addon_key(cls, *args):
        if args: return (cls,)+args
        return cls

    decorate(classmethod)
    def exists_for(cls, ob, *key):
        """Does an aspect of this type for the given key exist?"""
        return cls.addon_key(*key) in addons_for(ob)

    decorate(classmethod)
    def delete_from(cls, ob, *key):
        """Ensure an aspect of this type for the given key does not exist"""
        a = addons_for(ob)
        try:
            del a[cls.addon_key(*key)]
        except KeyError:
            pass

    def __init__(self, subject):
        pass




class ClassAddOn(AddOn):
    """Attachment/annotation for classes and types"""
    __slots__ = ()

    decorate(classmethod)
    def __class_call__(cls, ob, *data):
        addon_key = cls.addon_key(*data)
        d = ob.__dict__
        if addon_key in d:
            return d[addon_key]
        d2 = addons_for(ob)
        try:
            return d2[addon_key]
        except KeyError:
            # Use setdefault() to prevent race conditions
            ob = d2.setdefault(
                addon_key,
                super(ClassAddOn, cls).__class_call__(ob, *data)
            )
            return ob

    decorate(classmethod)
    def for_enclosing_class(cls, *args, **kw):
        if 'frame' in kw:
            frame = kw.pop('frame')
        else:
            if 'level' in kw:
                level = kw.pop('level')
            else:
                level = 2
            frame = sys._getframe(level)
        if kw:
            raise TypeError("Unexpected keyword arguments", kw)
        return cls.for_frame(frame, *args)







    decorate(classmethod)
    def for_frame(cls, frame, *args):
        a = enclosing_frame(frame).f_locals
        addon_key = cls.addon_key(*args)
        try:
            return a[addon_key]
        except KeyError:
            # Use setdefault() to prevent race conditions
            ob = a.setdefault(addon_key, type.__call__(cls, None, *args))
            # we use a lambda here so that if we are a registry, Python 2.5
            # won't consider our method equal to some other registry's method
            decorate_class(lambda c: ob.__decorate(c), frame=frame)
            return ob

    decorate(classmethod)
    def exists_for(cls, ob, *key):
        """Does an aspect of this type for the given key exist?"""
        addon_key = cls.addon_key(*key)
        return addon_key in ob.__dict__ or addon_key in addons_for(ob)

    decorate(classmethod)
    def delete_from(cls, ob, *key):
        """Class AddOns are not deletable!"""
        raise TypeError("ClassAddOns cannot be deleted")

    def __decorate(self, cls):
        self.created_for(cls)
        return cls

    def created_for(self, cls):
        """Override to access the decorated class, as soon as it's known"""

    def __init__(self, subject):
        """Ensure ``created_for()`` is called, if class already exists"""
        if subject is not None:
            self.created_for(subject)





class Registry(ClassAddOn, dict):
    """ClassAddOn that's a dictionary with mro-based inheritance"""

    __slots__ = ()

    def __new__(cls, subject):
        if cls is Registry:
            raise TypeError("You must subclass Registry to use it")
        return super(Registry, cls).__new__(cls)

    def __init__(self, subject):
        dict.__init__(self)
        super(Registry, self).__init__(subject)

    def created_for(self, cls):
        """Inherit the contents of base classes"""
        try:
            mro = cls.__mro__[::-1]
        except AttributeError:
            mro = type(cls.__name__, (cls,object), {}).__mro__[1:][::-1]

        data = {}
        self.defined_in_class = dict(self)

        mytype = type(self)
        for base in mro[:-1]:
            data.update(mytype(base))
        data.update(self)

        self.update(data)

    def set(self, key, value):
        if key in self and self[key]!=value:
            raise ValueError("%s[%r] already contains %r; can't set to %r"
                % (self.__class__.__name__, key, self[key], value)
            )
        self[key] = value