1 """CherryPy Library"""
2
3 import sys as _sys
4
5
7 """Load a module and retrieve a reference to that module."""
8 try:
9 mod = _sys.modules[modulePath]
10 if mod is None:
11 raise KeyError()
12 except KeyError:
13
14 mod = __import__(modulePath, globals(), locals(), [''])
15 return mod
16
18 """Load a module and retrieve an attribute of that module."""
19
20
21 last_dot = full_attribute_name.rfind(u".")
22 attr_name = full_attribute_name[last_dot + 1:]
23 mod_path = full_attribute_name[:last_dot]
24
25 mod = modules(mod_path)
26
27 try:
28 attr = getattr(mod, attr_name)
29 except AttributeError:
30 raise AttributeError("'%s' object has no attribute '%s'"
31 % (mod_path, attr_name))
32
33
34 return attr
35
36
37
38
40
42 m = getattr(self, 'build_' + o.__class__.__name__, None)
43 if m is None:
44 raise TypeError("unrepr does not recognize %s" %
45 repr(o.__class__.__name__))
46 return m(o)
47
49 children = map(self.build, o.getChildren())
50 callee = children.pop(0)
51 kwargs = children.pop() or {}
52 starargs = children.pop() or ()
53 args = tuple(children) + tuple(starargs)
54 return callee(*args, **kwargs)
55
57 return map(self.build, o.getChildren())
58
61
63 d = {}
64 i = iter(map(self.build, o.getChildren()))
65 for el in i:
66 d[el] = i.next()
67 return d
68
71
73 if o.name == 'None':
74 return None
75 if o.name == 'True':
76 return True
77 if o.name == 'False':
78 return False
79
80
81 try:
82 return modules(o.name)
83 except ImportError:
84 pass
85
86 raise TypeError("unrepr could not resolve the name %s" % repr(o.name))
87
89 real, imag = map(self.build_Const, o.getChildren())
90 try:
91 real = float(real)
92 except TypeError:
93 raise TypeError("unrepr could not parse real %s" % repr(real))
94 if not isinstance(imag, complex) or imag.real != 0.0:
95 raise TypeError("unrepr could not parse imag %s" % repr(imag))
96 return real+imag
97
99 parent = self.build(o.expr)
100 return getattr(parent, o.attrname)
101
104
107
110
111
113 """Return a Python object compiled from a string."""
114 if not s:
115 return s
116
117 try:
118 import compiler
119 except ImportError:
120
121
122 return eval(s)
123
124 p = compiler.parse("a=" + s)
125 obj = p.getChildren()[1].getChildren()[0].getChildren()[1]
126
127 return _Builder().build(obj)
128