Package cherrypy :: Package lib
[hide private]
[frames] | no frames]

Source Code for Package cherrypy.lib

  1  """CherryPy Library""" 
  2   
  3  import sys as _sys 
  4   
  5   
6 -def modules(modulePath):
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 # The last [''] is important. 14 mod = __import__(modulePath, globals(), locals(), ['']) 15 return mod
16
17 -def attributes(full_attribute_name):
18 """Load a module and retrieve an attribute of that module.""" 19 20 # Parse out the path, module, and attribute 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 # Let an AttributeError propagate outward. 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 # Return a reference to the attribute. 34 return attr
35 36 37 # public domain "unrepr" implementation, found on the web and then improved. 38
39 -class _Builder:
40
41 - def build(self, o):
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
48 - def build_CallFunc(self, o):
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
56 - def build_List(self, o):
57 return map(self.build, o.getChildren())
58
59 - def build_Const(self, o):
60 return o.value
61
62 - def build_Dict(self, o):
63 d = {} 64 i = iter(map(self.build, o.getChildren())) 65 for el in i: 66 d[el] = i.next() 67 return d
68
69 - def build_Tuple(self, o):
70 return tuple(self.build_List(o))
71
72 - def build_Name(self, o):
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 # See if the Name is a package or module. If it is, import it. 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
88 - def build_Add(self, o):
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
98 - def build_Getattr(self, o):
99 parent = self.build(o.expr) 100 return getattr(parent, o.attrname)
101
102 - def build_NoneType(self, o):
103 return None
104
105 - def build_UnarySub(self, o):
106 return -self.build_Const(o.getChildren()[0])
107
108 - def build_UnaryAdd(self, o):
109 return self.build_Const(o.getChildren()[0])
110 111
112 -def unrepr(s):
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 # Fallback to eval when compiler package is not available, 121 # e.g. IronPython 1.0. 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