Mercurial > fife-parpg
annotate engine/extensions/pychan/tools.py @ 199:4ea1e0b56a07
fixes based on the updated SDK from barra. These scripts were still trying to use swig 1.3.35 and in the new SDK swig was updated to 1.3.38
author | vtchill@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Fri, 13 Mar 2009 01:58:04 +0000 |
parents | 31718fa356f8 |
children | 54bfd1015b35 |
rev | line source |
---|---|
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
1 # coding: utf-8 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
2 |
163
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
3 import exceptions |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
4 |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
5 ### Functools ### |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
6 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
7 def applyOnlySuitable(func,**kwargs): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
8 """ |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
9 This nifty little function takes another function and applies it to a dictionary of |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
10 keyword arguments. If the supplied function does not expect one or more of the |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
11 keyword arguments, these are silently discarded. The result of the application is returned. |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
12 This is useful to pass information to callbacks without enforcing a particular signature. |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
13 """ |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
14 if hasattr(func,'im_func'): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
15 code = func.im_func.func_code |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
16 varnames = code.co_varnames[1:code.co_argcount]#ditch bound instance |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
17 else: |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
18 code = func.func_code |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
19 varnames = code.co_varnames[0:code.co_argcount] |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
20 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
21 #http://docs.python.org/lib/inspect-types.html |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
22 if code.co_flags & 8: |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
23 return func(**kwargs) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
24 for name,value in kwargs.items(): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
25 if name not in varnames: |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
26 del kwargs[name] |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
27 return func(**kwargs) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
28 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
29 def callbackWithArguments(callback,*args,**kwargs): |
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 Curries a function with extra arguments to |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
32 create a suitable callback. |
129
9a1529f9625e
* Indentation patch by GreyGhost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
0
diff
changeset
|
33 |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
34 If you don't know what this means, don't worry. |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
35 It is designed for the case where you need |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
36 different buttons to execute basically the same code |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
37 with different argumnets. |
129
9a1529f9625e
* Indentation patch by GreyGhost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
0
diff
changeset
|
38 |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
39 Usage:: |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
40 # The target callback |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
41 def printStuff(text): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
42 print text |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
43 # Mapping the events |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
44 gui.mapEvents({ |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
45 'buttonHello' : callbackWithArguments(printStuff,"Hello"), |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
46 'buttonBye' : callbackWithArguments(printStuff,"Adieu") |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
47 }) |
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 def real_callback(): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
50 callback(*args,**kwargs) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
51 return real_callback |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
52 |
163
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
53 def attrSetCallback(**kwargs): |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
54 """ |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
55 Generates an event callback that sets attributes on the widget |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
56 it is called on. This is especially useful for mouseEntered/Exited |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
57 events - to create hover effects. |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
58 |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
59 It takes a set of keyword arguments. The keys are treated as attribute names, |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
60 which are then set to the corresponding value when the callback is called. |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
61 Some key names are treated special - see below. |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
62 |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
63 Usage - Example adapted from demo application:: |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
64 eventMap = { |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
65 'creditsLink' : showCreditsCallback, |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
66 'creditsLink/mouseEntered' : attrSetCallback( |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
67 text = "Show credits!", |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
68 background_color = (255,255,0,255), |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
69 do__adaptLayout=True), |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
70 'creditsLink/mouseExited' : attrSetCallback(text = "Credits"), |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
71 gui.mapEvents(eventMap) |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
72 |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
73 Now when the mouse enters the creditsLink (a Label in our case), the following code will be executed:: |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
74 #widget is the creditsLink - given to the event callback. |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
75 widget.text = "Show credits!" |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
76 widget.background_color = (255,255,0,255) |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
77 widget.adaptLayout() |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
78 |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
79 The C{do__adaptLayout} argument causes the method C{adaptLayout} to be called. |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
80 In fact any key starting with C{do__} results in such a method call. The order |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
81 of execution of such calls is undefined. |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
82 |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
83 Keys starting with an underscore raise a L{execptions.PrivateFunctionalityError}. |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
84 """ |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
85 do_calls = [] |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
86 |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
87 for name in kwargs.keys(): |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
88 if name.startswith("_"): |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
89 raise exceptions.PrivateFunctionalityError(name) |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
90 if name.startswith("do__"): |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
91 do_calls.append(name[4:]) |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
92 del kwargs[name] |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
93 |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
94 def callback(widget=None): |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
95 for name,value in kwargs.items(): |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
96 setattr(widget,name,value) |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
97 for method_name in do_calls: |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
98 method = getattr(widget,method_name) |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
99 method() |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
100 return callback |
31718fa356f8
Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
129
diff
changeset
|
101 |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
102 def this_is_deprecated(func,message=None): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
103 if message is None: |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
104 message = repr(func) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
105 def wrapped_func(*args,**kwargs): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
106 print "PyChan: You are using the DEPRECATED functionality: %s" % message |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
107 return func(*args,**kwargs) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
108 return wrapped_func |