/usr/lib/python2.7/dist-packages/stuf/desc.py is in python-stuf 0.9.16-0ubuntu1.
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 | # -*- coding: utf-8 -*-
'''stuf descriptors.'''
from threading import local
from functools import update_wrapper, partial
from .six import items
from .iterable import exhaustmap
from .deep import selfname, setter, getcls, setpart
class lazybase(object):
'''Base class for lazy descriptors.'''
class _lazyinit(lazybase):
'''Base initializer for lazy descriptors.'''
def __init__(self, method, _wrap=update_wrapper):
super(_lazyinit, self).__init__()
self.method = method
self.name = selfname(method)
_wrap(self, method)
def _set(self, this):
return setter(this, self.name, self.method(this))
class lazy(_lazyinit):
'''Lazily assign attributes on an instance upon first use.'''
def __get__(self, this, that):
return self if this is None else self._set(this)
class lazy_class(_lazyinit):
'''Lazily assign attributes on an class upon first use.'''
def __get__(self, this, that):
return self._set(that)
class lazypartial(lazy):
'''Lazily assign attributes on an instance upon first use.'''
def _set(self, this):
return setter(this, self.name, partial(*self.method(this)))
class lazyset(lazy):
'''Lazily assign attributes with a custom setter.'''
def __init__(self, method, fget=None, _wrap=update_wrapper):
super(lazyset, self).__init__(method)
self.fget = fget
_wrap(self, method)
def __set__(self, this, value):
self.fget(this, value)
def __delete__(self, this):
del this.__dict__[self.name]
def setter(self, func):
self.fget = func
return self
class bothbase(_lazyinit):
'''Base for two-way lazy descriptors.'''
def __init__(self, method, expr=None, _wrap=update_wrapper):
super(bothbase, self).__init__(method)
self.expr = expr or method
_wrap(self, method)
def expression(self, expr):
'''
Modifying decorator that defines a general method.
'''
self.expr = expr
return self
class both(bothbase):
'''
Descriptor that caches results of instance-level results while allowing
class-level results.
'''
def __get__(self, this, that):
return self.expr(that) if this is None else self._set(this)
class either(bothbase):
'''
Descriptor that caches results of both instance- and class-level results.
'''
def __get__(self, this, that):
if this is None:
return setter(that, self.name, self.expr(that))
return self._set(this)
class twoway(bothbase):
'''Descriptor that enables instance and class-level results.'''
def __get__(self, this, that):
return self.expr(that) if this is None else self.method(this)
class readonly(lazybase):
'''Read-only lazy descriptor.'''
def __set__(self, this, value):
raise AttributeError('attribute is read-only')
def __delete__(self, this):
raise AttributeError('attribute is read-only')
class ResetMixin(local):
'''Mixin for reseting descriptors subclassing :class:`lazybase`\.'''
def reset(self):
'''Reset previously accessed :class:`lazybase` attributes.'''
attrs = set(vars(self))
exhaustmap(
delattr,
items(vars(getcls(self))),
lambda x, y: x in attrs and isinstance(y, lazybase),
)
class ContextMixin(ResetMixin):
'''Resetable context manager mixin.'''
def __enter__(self):
return self
class Setter(object):
'''Partial setter.'''
@lazypartial
def _setter(self):
return setpart, self
|