annotate engine/extensions/pychan/tools.py @ 366:dcf6f6091868

Filebrowser: * Created a new function setDirectory. Separated the code from the _selectDir callback * Filebrowser will now refresh current directory after being reopened (Fixes #391)
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 29 Oct 2009 20:19:08 +0000
parents dfd48d49c044
children
rev   line source
330
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
1 # -*- coding: utf-8 -*-
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
2
331
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
3 # ####################################################################
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
4 # 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: 330
diff changeset
5 # http://www.fifengine.de
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
6 # This file is part of FIFE.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
7 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
8 # 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: 330
diff changeset
9 # 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: 330
diff changeset
10 # 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: 330
diff changeset
11 # 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: 330
diff changeset
12 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
13 # 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: 330
diff changeset
14 # 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: 330
diff changeset
15 # 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: 330
diff changeset
16 # Lesser General Public License for more details.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
17 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
18 # 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: 330
diff changeset
19 # 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: 330
diff changeset
20 # Free Software Foundation, Inc.,
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
21 # 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: 330
diff changeset
22 # ####################################################################
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
23
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
24 """
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
25 Functional utilities designed for pychan use cases.
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
26 """
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
27
163
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
28 import exceptions
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
29
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
30 ### Functools ###
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
31
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
32 def applyOnlySuitable(func,*args,**kwargs):
0
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 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
35 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
36 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
37 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
38 """
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
39 if hasattr(func,'im_func'):
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
40 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
41 varnames = code.co_varnames[1:code.co_argcount]#ditch bound instance
350
7ea98190a75e Allow objects with an __call__ attribute used as event callbacks.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
42 elif hasattr(func,'func_code'):
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
43 code = func.func_code
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
44 varnames = code.co_varnames[0:code.co_argcount]
350
7ea98190a75e Allow objects with an __call__ attribute used as event callbacks.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
45 elif hasattr(func,'__call__'):
7ea98190a75e Allow objects with an __call__ attribute used as event callbacks.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
46 func = func.__call__
7ea98190a75e Allow objects with an __call__ attribute used as event callbacks.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
47 if hasattr(func,'im_func'):
7ea98190a75e Allow objects with an __call__ attribute used as event callbacks.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
48 code = func.im_func.func_code
7ea98190a75e Allow objects with an __call__ attribute used as event callbacks.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
49 varnames = code.co_varnames[1:code.co_argcount]#ditch bound instance
7ea98190a75e Allow objects with an __call__ attribute used as event callbacks.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
50 elif hasattr(func,'func_code'):
7ea98190a75e Allow objects with an __call__ attribute used as event callbacks.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
51 code = func.func_code
7ea98190a75e Allow objects with an __call__ attribute used as event callbacks.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
52 varnames = code.co_varnames[0:code.co_argcount]
0
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 #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
55 if code.co_flags & 8:
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
56 return func(*args,**kwargs)
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
57 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
58 if name not in varnames:
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
59 del kwargs[name]
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
60 return func(*args,**kwargs)
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
61
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
62 def callbackWithArguments(callback,*args,**kwargs):
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
63 """
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
64 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
65 create a suitable callback.
129
9a1529f9625e * Indentation patch by GreyGhost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 0
diff changeset
66
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
67 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
68 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
69 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
70 with different argumnets.
129
9a1529f9625e * Indentation patch by GreyGhost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 0
diff changeset
71
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
72 Usage::
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
73 # The target callback
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
74 def printStuff(text):
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
75 print text
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
76 # Mapping the events
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
77 gui.mapEvents({
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
78 'buttonHello' : callbackWithArguments(printStuff,"Hello"),
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
79 'buttonBye' : callbackWithArguments(printStuff,"Adieu")
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
80 })
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
81 """
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
82 def real_callback():
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
83 callback(*args,**kwargs)
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
84 return real_callback
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
85
163
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
86 def attrSetCallback(**kwargs):
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
87 """
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
88 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
89 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
90 events - to create hover effects.
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
91
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
92 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
93 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
94 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
95
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
96 Usage - Example adapted from demo application::
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
97 eventMap = {
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
98 'creditsLink' : showCreditsCallback,
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
99 'creditsLink/mouseEntered' : attrSetCallback(
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
100 text = "Show credits!",
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
101 background_color = (255,255,0,255),
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
102 do__adaptLayout=True),
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
103 'creditsLink/mouseExited' : attrSetCallback(text = "Credits"),
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
104 gui.mapEvents(eventMap)
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
105
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
106 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
107 #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
108 widget.text = "Show credits!"
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
109 widget.background_color = (255,255,0,255)
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
110 widget.adaptLayout()
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
111
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
112 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
113 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
114 of execution of such calls is undefined.
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
115
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
116 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
117 """
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
118 do_calls = []
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
119
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
120 for name in kwargs.keys():
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
121 if name.startswith("_"):
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
122 raise exceptions.PrivateFunctionalityError(name)
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
123 if name.startswith("do__"):
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
124 do_calls.append(name[4:])
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
125 del kwargs[name]
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
126
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
127 def attrSet_callback(widget=None):
163
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
128 for name,value in kwargs.items():
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
129 setattr(widget,name,value)
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
130 for method_name in do_calls:
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
131 method = getattr(widget,method_name)
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
132 method()
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
133 return attrSet_callback
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
134
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
135 def chainCallbacks(*args):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
136 """
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
137 Chains callbacks to be called one after the other.
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
138
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
139 Example Usage::
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
140 def print_event(event=0):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
141 print event
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
142 def print_widget(widget=0):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
143 print widget
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
144 callback = tools.chainCallbacks(doSomethingUseful, print_event, print_widget)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
145 guiElement.capture(callback)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
146 """
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
147 callbacks = args
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
148 def chain_callback(event=0,widget=0):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
149 for callback in callbacks:
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
150 applyOnlySuitable(callback, event=event, widget=widget)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
151 return chain_callback
163
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
152
330
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
153 def repeatALot(n = 1000):
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
154 """
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
155 Internal decorator used to profile some pychan functions.
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
156 Only use with functions without side-effect.
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
157
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
158 Usage::
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
159 @repeatALot(n=10000)
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
160 def findChild(self,**kwargs):
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
161 ...
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
162 """
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
163 def wrap_f(f):
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
164 def new_f(*args,**kwargs):
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
165 for i in xrange(n):
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
166 f(*args,**kwargs)
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
167 return f(*args,**kwargs)
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
168 return new_f
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
169 return wrap_f
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
170
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
171 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
172 if message is None:
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
173 message = repr(func)
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
174 def wrapped_func(*args,**kwargs):
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
175 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
176 return func(*args,**kwargs)
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
177 return wrapped_func