/usr/lib/python2.7/dist-packages/fdb/utils.py is in python-fdb 1.4.1+dfsg1-4.
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 | #!/usr/bin/python
#
# PROGRAM: fdb
# MODULE: utils.py
# DESCRIPTION: Various utility classes and functions
# CREATED: 10.5.2013
#
# Software distributed under the License is distributed AS IS,
# WITHOUT WARRANTY OF ANY KIND, either express or implied.
# See the License for the specific language governing rights
# and limitations under the License.
#
# The Original Code was created by Pavel Cisar
#
# Copyright (c) 2013 Pavel Cisar <pcisar@users.sourceforge.net>
# and all contributors signed below.
#
# All Rights Reserved.
# Contributor(s): ______________________________________.
def update_meta (self, other):
"Helper function for :class:`LateBindingProperty` class."
self.__name__ = other.__name__
self.__doc__ = other.__doc__
self.__dict__.update(other.__dict__)
return self
class LateBindingProperty (property):
"""Peroperty class that binds to getter/setter/deleter methods when **instance**
of class that define the property is created. This allows you to override
these methods in descendant classes (if they are not private) without
necessity to redeclare the property itself in descendant class.
Recipe from Tim Delaney, 2005/03/31
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/408713
::
class C(object):
def getx(self):
print 'C.getx'
return self._x
def setx(self, x):
print 'C.setx'
self._x = x
def delx(self):
print 'C.delx'
del self._x
x = LateBindingProperty(getx, setx, delx)
class D(C):
def setx(self, x):
print 'D.setx'
super(D, self).setx(x)
def delx(self):
print 'D.delx'
super(D, self).delx()
c = C()
c.x = 1
c.x
c.x
del c.x
print
d = D()
d.x = 1
d.x
d.x
del d.x
This has the advantages that:
a. You get back an actual property object (with attendant memory savings, performance increases, etc);
b. It's the same syntax as using property(fget, fset, fdel, doc) except for the name;
c. It will fail earlier (when you define the class as opposed to when you use it).
d. It's shorter ;)
e. If you inspect the property you will get back functions with the correct __name__, __doc__, etc.
"""
def __new__(self, fget=None, fset=None, fdel=None, doc=None):
if fget is not None:
def __get__(obj, objtype=None, name=fget.__name__):
fget = getattr(obj, name)
return fget()
fget = update_meta(__get__, fget)
if fset is not None:
def __set__(obj, value, name=fset.__name__):
fset = getattr(obj, name)
return fset(value)
fset = update_meta(__set__, fset)
if fdel is not None:
def __delete__(obj, name=fdel.__name__):
fdel = getattr(obj, name)
return fdel()
fdel = update_meta(__delete__, fdel)
return property(fget, fset, fdel, doc)
class Iterator(object):
"""Generic iterator implementation.
"""
def __init__(self, method, sentinel = None):
"""
:param method: Callable without parameters that returns next item.
:param sentinel: Value that when returned by `method` indicates the end
of sequence.
"""
self.getnext = method
self.sentinel = sentinel
self.exhausted = False
def __iter__(self):
return self
def next(self):
if self.exhausted:
raise StopIteration
else:
result = self.getnext()
self.exhausted = (result == self.sentinel)
if self.exhausted:
raise StopIteration
else:
return result
__next__ = next
class EmbeddedProperty(property):
"""Property class that forwards calls to getter/setter/deleter methods to
respective property methods of another object. This class allows you to "inject"
properties from embedded object into class definition of parent object."""
def __init__(self,obj,prop):
"""
:param string obj: Attribute name with embedded object.
:param property prop: Property instance from embedded object.
"""
self.obj = obj
self.prop = prop
self.__doc__ = prop.__doc__
def __get__(self,obj,objtype):
if obj is None:
return self
return self.prop.__get__(getattr(obj,self.obj))
def __set__(self,obj,val):
self.prop.__set__(getattr(obj,self.obj),val)
def __delete__(self,obj):
self.prop.__delete__(getattr(obj,self.obj))
class EmbeddedAttribute(property):
"""Property class that gets/sets attribute of another object. This class
allows you to "inject" attributes from embedded object into class definition
of parent object."""
def __init__(self,obj,attr):
"""
:param string obj: Attribute name with embedded object.
:param string attr: Attribute name from embedded object.
"""
self.obj = obj
self.attr = attr
self.__doc__ = attr.__doc__
def __get__(self,obj,objtype):
if obj is None:
return self
return getattr(getattr(obj,self.obj),self.attr)
def __set__(self,obj,val):
setattr(getattr(obj,self.obj),self.attr,val)
def iter_class_properties(cls):
"""Iterator that yields `name, property` pairs for all properties in class.
:param class cls: Class object."""
for varname in vars(cls):
value = getattr(cls, varname)
if isinstance(value, property):
yield varname, value
def iter_class_variables(cls):
"""Iterator that yields names of all non-callable attributes in class.
:param class cls: Class object."""
for varname in vars(cls):
value = getattr(cls, varname)
if not (isinstance(value, property) or callable(value)):
yield varname
def embed_attributes(from_class,attr):
"""Class decorator that injects properties and non-callable attributes
from another class instance embedded in class instances.
:param class from_class: Class that should extend decorated class.
:param string attr: Attribute name that holds instance of embedded class
within decorated class instance."""
def d(class_):
for pname,prop in iter_class_properties(from_class):
if not hasattr(class_,pname):
setattr(class_,pname,EmbeddedProperty(attr,prop))
for attrname in iter_class_variables(from_class):
if not hasattr(class_,attrname):
setattr(class_,attrname,EmbeddedAttribute(attr,attrname))
return class_
return d
|