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
|
"""
pyyaml legacy
Copyright (c) 2001 Steve Howell and Friends; All Rights Reserved
(see open source license information in docs/ directory)
"""
import new
import re
class DefaultResolver:
def resolveType(self, data, typestring):
match = re.match('!!(.*?)\.(.*)', typestring)
if not match:
raise "Invalid private type specifier"
(modname, classname) = match.groups()
return makeClass(modname, classname, data)
def makeClass(module, classname, dict):
exec('import %s' % (module))
klass = eval('%s.%s' % (module, classname))
obj = new.instance(klass)
if hasMethod(obj, 'from_yaml'):
return obj.from_yaml(dict)
obj.__dict__ = dict
return obj
def hasMethod(object, method_name):
try:
klass = object.__class__
except:
return 0
if not hasattr(klass, method_name):
return 0
method = getattr(klass, method_name)
if not callable(method):
return 0
return 1
def isDictionary(data):
return isinstance(data, dict)
try:
isDictionary({})
except:
def isDictionary(data): return type(data) == type({}) # XXX python 2.1
if __name__ == '__main__':
print isDictionary({'foo': 'bar'})
try:
print isDictionary(dict())
from ordered_dict import OrderedDict
print isDictionary(OrderedDict())
except:
pass
|