Mercurial > fife-parpg
comparison engine/extensions/pythonize.py @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | 9a1529f9625e |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
1 # coding: utf-8 | |
2 | |
3 """\ | |
4 Pythonize FIFE | |
5 | |
6 Import this extension to get a more | |
7 pythonic interface to FIFE. | |
8 | |
9 Currently it implements the following | |
10 conveniences: | |
11 | |
12 * FIFE Exceptions print their message. | |
13 * Automatic property generation for: | |
14 * fife.Engine | |
15 * fife.Instance | |
16 * fife.Image | |
17 * fife.Animation | |
18 * fife.Point | |
19 * fife.Rect | |
20 | |
21 """ | |
22 | |
23 import fife, re | |
24 | |
25 __all__ = () | |
26 | |
27 fife.Exception.__str__ = fife.Exception.getMessage | |
28 def _Color2Str(c): | |
29 return 'Color(%s)' % ','.join(map(str,(c.r,c.g,c.b,c.a))) | |
30 fife.Color.__str__ = _Color2Str | |
31 | |
32 classes = [ fife.Engine, fife.Instance, fife.Point, fife.Rect, fife.Image, fife.Animation, | |
33 fife.RenderBackend, fife.Event, fife.Command, fife.Container ] | |
34 | |
35 def createProperties(): | |
36 """ Autocreate properties for getXYZ/setXYZ functions. | |
37 """ | |
38 try: | |
39 import inspect | |
40 getargspec = inspect.getargspec | |
41 except ImportError: | |
42 print "Pythonize: inspect not available - properties are generated with dummy argspec." | |
43 getargspec = lambda func : ([],'args',None,None) | |
44 | |
45 def isSimpleGetter(func): | |
46 if not callable(func): | |
47 return False | |
48 try: | |
49 argspec = getargspec(func) | |
50 return not (argspec[0] or [s for s in argspec[2:] if s]) | |
51 except TypeError, e: | |
52 #print func, e | |
53 return False | |
54 | |
55 def createNames(name): | |
56 for prefix in ('get', 'is', 'are'): | |
57 if name.startswith(prefix): | |
58 new_name = name[len(prefix):] | |
59 break | |
60 settername = 'set' + new_name | |
61 propertyname = new_name[0].lower() + new_name[1:] | |
62 return settername, propertyname | |
63 | |
64 getter = re.compile(r"^(get|are|is)[A-Z]") | |
65 for class_ in classes: | |
66 methods = [(name,attr) for name,attr in class_.__dict__.items() | |
67 if isSimpleGetter(attr) ] | |
68 setmethods = [(name,attr) for name,attr in class_.__dict__.items() if callable(attr)] | |
69 getters = [] | |
70 for name,method in methods: | |
71 if getter.match(name): | |
72 getters.append((name,method)) | |
73 settername, propertyname = createNames(name) | |
74 setter = dict(setmethods).get(settername,None) | |
75 #print name, settername, "--->",propertyname,'(',method,',',setter,')' | |
76 setattr(class_,propertyname,property(method,setter)) | |
77 if not getters: continue | |
78 | |
79 # We need to override the swig setattr function | |
80 # to get properties to work. | |
81 class_._property_names = set([name for name,method in getters]) | |
82 def _setattr_wrapper_(self,*args): | |
83 if name in class_._property_names: | |
84 object.__setattr__(self,*args) | |
85 else: | |
86 class_.__setattr__(self,*args) | |
87 class_.__setattr__ = _setattr_wrapper_ | |
88 | |
89 createProperties() |