/usr/share/pyshared/guppy/heapy/Doc.py is in python-guppy 0.1.9-2ubuntu4.
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 | #._cv_part guppy.heapy.Doc
class Doc: # base class
def __mod__(self, other):
other = self.mod.getdoc(other)
return self.mapchildren(lambda x: x % other)
def __rmod__(self, other):
return self.mod.getdoc(other) % self
def __str__(self):
return self.getstr()
def __eq__(self, other):
if not isinstance(other, self.__class__):
return 0
return str(self) == str(other)
def __hash__(self):
return hash(str(self))
def shortest(self):
return self.mapchildren(lambda x: x.shortest())
class Anon(Doc):
def __init__(self, mod, obj):
self.mod = mod
self.obj = obj
def getstr(self):
return repr(self.obj)
def mapchildren(self, f):
return self
class Source(Doc):
def __init__(self, mod, text):
self.mod = mod
self.text = text
def getstr(self):
return self.text
def mapchildren(self, f):
return self
class Attribute(Doc):
def __init__(self, mod, obj, name):
self.mod = mod
self.obj = obj
self.name = name
def __mod__(self, other):
if self.obj is other:
return self.mod.rootattribute(other, self.name)
return self.mapchildren(lambda x: x % other)
def getstr(self):
return '%s.%s'%(self.obj.getstr(), self.name)
def mapchildren(self, f):
return self.__class__(self.mod, f(self.obj), self.name)
class RootAttribute(Doc):
def __init__(self, mod, obj, name):
self.mod = mod
self.obj = obj
self.name = name
def getstr(self):
return '%s'%(self.name,)
def mapchildren(self, f):
return self
class BinaryOp(Doc):
table = {
'and':'&',
'or':'|',
'sub':'-',
'mul':'*',
'pow':'**',
'lshift':'<<',
'rshift':'>>',
}
def __init__(self, mod, op, a, b):
self.mod = mod
self.op = op
self.a = a
self.b = b
def getstr(self):
return '%s %s %s'%(self.a.getstr(),
self.table[self.op],
self.b.getstr())
def mapchildren(self, f):
return self.__class__(self.mod, self.op, f(self.a), f(self.b))
class UnaryOp(Doc):
table = {
'invert': '~',
'neg' : '-',
'pos' : '+',
}
def __init__(self, mod, op, a):
self.mod = mod
self.op = op
self.a = a
def getstr(self):
return '%s %s'%(self.table[self.op], self.a.getstr())
def mapchildren(self, f):
return self.__class__(self.mod, self.op, f(self.a))
class CallFunc(Doc):
def __init__(self, mod, obj, *args, **kwds):
self.mod = mod
self.obj = obj
self.args = args
self.kwds = kwds
def getstr(self):
return '%s(%s%s)'%(
self.obj.getstr(),
', '.join([x.getstr() for x in self.args]),
', '.join(['%s=%s'%(k,v.getstr()) for k, v in self.kwds.items()]))
def mapchildren(self, f):
obj = f(self.obj)
args = [f(a) for a in self.args]
kwds = dict([(k, f(v)) for (k, v) in self.kwds.items()])
return self.__class__(self.mod, obj, *args, **kwds)
class Multi(Doc):
def __init__(self, mod, set):
self.mod = mod
self.str = '{%s}'%', '.join([x.getstr() for x in set])
self.set = set
def getstr(self):
return self.str
def mapchildren(self, f):
return self.__class__(self.mod, dict([(f(x), 1) for x in self.set]))
def shortest(self):
ls = None
for a in self.set:
a = a.shortest()
l = len(a.getstr())
if ls is None or l < ls:
ls = l
st = a
return st
class Root(Doc):
def __init__(self, mod, name='<root>'):
self.mod = mod
self.name = name
def __call__(self, name):
return self.__class__(self.mod, name)
def mapchildren(self, f):
return self
def getstr(self):
return self.name
class Tuple(Doc):
def __init__(self, mod, *args):
self.mod = mod
self.args = args
#pdb.set_trace()
def mapchildren(self, f):
return self.__class__(self.mod, *[f(x) for x in self.args])
def getstr(self):
x = '(%s)'%', '.join([x.getstr() for x in self.args])
if len(self.args) == 1:
x = x[:-1]+',)'
return x
class DocError(Exception):
pass
class _GLUECLAMP_:
def add_origin(self, obj, origin):
o = getattr(obj, '_origin_', None)
if o is None:
obj._origin_ = origin
else:
obj._origin_ = self.multi(o, origin)
return obj
def add_wrapdict(self, obj, doc):
wd = self.wrapdict
o = wd.get(id(obj))
if o is None:
o = (obj, doc)
else:
o = (obj, self.multi(o[1], doc))
wd[id(obj)] = o
return obj
def anon(self, obj):
return Anon(self, obj)
def attribute(self, obj, name):
return Attribute(self, self.getdoc(obj), name)
def binop(self, op, a, b):
return BinaryOp(self, op, self.getdoc(a), self.getdoc(b))
def callfunc(self, obj, *args, **kwds):
getdoc = self.getdoc
obj = getdoc(obj)
args = [getdoc(a) for a in args]
kwds = dict([(k, getdoc(v)) for (k, v) in kwds.items()])
return CallFunc(self, obj, *args, **kwds)
def getdoc(self, obj):
if isinstance(obj, Doc):
return obj
w = getattr(obj, '_origin_', None)
if isinstance(w, Doc):
return w
w = self.wrapdict.get(id(obj))
if w is not None:
return w[1]
if isinstance(obj, tuple):
return self.tuple(*obj)
return self.anon(obj)
def multi(self, a, b):
a = self.getdoc(a)
b = self.getdoc(b)
if isinstance(a, Multi):
#pdb.set_trace()
set = a.set.copy()
if 1 and len(set) > 4:
return a
else:
set = {a:1}
if isinstance(b, Multi):
set.update(b.set)
else:
set[b] = 1
return Multi(self, set)
def _get_root(self):
return Root(self)
def rootattribute(self, root, name):
return RootAttribute(self, self.getdoc(root), name)
def source(self, text):
return Source(self, text)
def tuple(self, *args):
return Tuple(self, *[self.getdoc(x) for x in args])
def unop(self, op, a):
return UnaryOp(self, op, self.getdoc(a))
def wrap(self, obj, doc):
if obj is self._parent.UniSet.UniSet:
pdb.set_trace()
w = getattr(obj, '_derive_origin_', None)
if w is not None:
if getattr(w, 'im_self', None) is obj or isinstance(w, self._root.types.FunctionType):
obj = w(doc)
elif w == 'ADD':
#pdb.set_trace()
obj = self.add_origin(obj, doc)
else:
raise DocError, "Doc.wrap: attribute '_derive_origin_' has invalid value"
elif isinstance(obj, self._root.types.MethodType):
obj = self.wrap_method(obj, doc)
elif isinstance(obj, self._root.types.FunctionType):
obj = self.wrap_function(obj, doc)
else:
obj = self.add_wrapdict(obj, doc)
return obj
def _get_wrapdict(self):
return {}
def wrap_function(mod, obj, doc):
def f(*args, **kwds):
r = obj(*args, **kwds)
r = mod.wrap(r, mod.callfunc(doc, *args, **kwds))
return r
f._origin_ = doc
return f
def wrap_method(mod, obj, doc):
im_func = obj.im_func
def f(self, *args, **kwds):
r = im_func(self, *args, **kwds)
r = mod.wrap(r, mod.callfunc(doc, *args, **kwds))
return r
return mod._root.new.instancemethod(f, obj.im_self, obj.im_self.__class__)
|