annotate engine/extensions/pythonize.py @ 331:48c99636453e

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