comparison engine/python/fife/extensions/pythonize.py @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children 4f36c890b1dd
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 # -*- coding: utf-8 -*-
2
3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team
5 # http://www.fifengine.de
6 # This file is part of FIFE.
7 #
8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # ####################################################################
23
24 """\
25 Pythonize FIFE
26
27 Import this extension to get a more
28 pythonic interface to FIFE.
29
30 Currently it implements the following
31 conveniences:
32
33 * FIFE Exceptions print their message.
34 * Automatic property generation for:
35 * fife.Engine
36 * fife.Instance
37 * fife.Image
38 * fife.Animation
39 * fife.Point
40 * fife.Rect
41
42 """
43
44 from fife import fife
45 import re
46
47 __all__ = ()
48
49 fife.Exception.__str__ = fife.Exception.getMessage
50 def _Color2Str(c):
51 return 'Color(%s)' % ','.join(map(str,(c.r,c.g,c.b,c.a)))
52 fife.Color.__str__ = _Color2Str
53
54 classes = [ fife.Engine, fife.Instance, fife.Point, fife.Rect, fife.Image, fife.Animation,
55 fife.RenderBackend, fife.Event, fife.Command, fife.Container ]
56
57 def createProperties():
58 """ Autocreate properties for getXYZ/setXYZ functions.
59 """
60 try:
61 import inspect
62 getargspec = inspect.getargspec
63 except ImportError:
64 print "Pythonize: inspect not available - properties are generated with dummy argspec."
65 getargspec = lambda func : ([],'args',None,None)
66
67 def isSimpleGetter(func):
68 if not callable(func):
69 return False
70 try:
71 argspec = getargspec(func)
72 return not (argspec[0] or [s for s in argspec[2:] if s])
73 except TypeError, e:
74 #print func, e
75 return False
76
77 def createNames(name):
78 for prefix in ('get', 'is', 'are'):
79 if name.startswith(prefix):
80 new_name = name[len(prefix):]
81 break
82 settername = 'set' + new_name
83 propertyname = new_name[0].lower() + new_name[1:]
84 return settername, propertyname
85
86 getter = re.compile(r"^(get|are|is)[A-Z]")
87 for class_ in classes:
88 methods = [(name,attr) for name,attr in class_.__dict__.items()
89 if isSimpleGetter(attr) ]
90 setmethods = [(name,attr) for name,attr in class_.__dict__.items() if callable(attr)]
91 getters = []
92 for name,method in methods:
93 if getter.match(name):
94 getters.append((name,method))
95 settername, propertyname = createNames(name)
96 setter = dict(setmethods).get(settername,None)
97 #print name, settername, "--->",propertyname,'(',method,',',setter,')'
98 setattr(class_,propertyname,property(method,setter))
99 if not getters: continue
100
101 # We need to override the swig setattr function
102 # to get properties to work.
103 class_._property_names = set([name for name,method in getters])
104 def _setattr_wrapper_(self,*args):
105 if name in class_._property_names:
106 object.__setattr__(self,*args)
107 else:
108 class_.__setattr__(self,*args)
109 class_.__setattr__ = _setattr_wrapper_
110
111 createProperties()