53
53
54
54
An application using pyNotifier has to initialise the notifier,
55
55
i.e. it has to choose which notifier implementation should be
56
- used. This is done by calling the ' init' function:
56
+ used. This is done by calling the ` init ` function:
57
57
58
- def init( type = GENERIC ):
58
+ def init(type= GENERIC):
59
59
60
- If no argument is given to the ' init' function the internal
60
+ If no argument is given to the ` init ` function the internal
61
61
implementation of pyNotifier is used. Other possible choices for the
62
- ' type' argument are GTK, QT and WX (current support for wxWindows is
62
+ ` type ` argument are GTK, QT and WX (current support for wxWindows is
63
63
not up-to-date).
64
64
65
65
Some notifier implementations provide a set of options that control the
66
66
behaviour of the notifier. The options are passed to the implementation
67
- as a keyword list through the ' init' function as shown in the following
67
+ as a keyword list through the ` init ` function as shown in the following
68
68
example:
69
69
70
- def init( type = GTK, x11 = False )
70
+ def init(type= GTK, x11= False)
71
71
72
- This example uses the ' x11' option for the GTK notifier. If set
72
+ This example uses the ` x11 ` option for the GTK notifier. If set
73
73
to False the notifier does not require gtk anymore and uses gobject
74
74
instead.
75
75
@@ -78,25 +78,25 @@ Sockets
78
78
79
79
To get notified when a specific event occurs the application has to
80
80
register its interest for this event. For sockets and files this is
81
- done with the 'socket \_ add' function' :
81
+ done with the ` socket_add ` function:
82
82
83
- def socket_add( id, method, condition = IO_IN )
83
+ def socket_add(id, method, condition= IO_IN)
84
84
85
- The 'id' argument may be a socket or file object or a file descriptor
86
- that can be retrieved by calling the ' fileno()' member function of
87
- these objects. The second argument ' method' has to be a callable
85
+ The ` id ` argument may be a socket or file object or a file descriptor
86
+ that can be retrieved by calling the ` fileno() ` member function of
87
+ these objects. The second argument ` method ` has to be a callable
88
88
python object that is invoked by the notifier if the registered event
89
- has occured. The function is invoked with the 'id' as an
89
+ has occured. The function is invoked with the ` id ` as an
90
90
argument. Instead of a normal function the [ Callback] ( #callbacks )
91
91
object provided by pyNotifier may be useful at this point.
92
92
93
93
To remove a registered socket or file from the notifier the
94
- 'socket \_ remove' function has to be invoked. The 'id' is the socket or
95
- file object or the file descriptor given to 'socket \_ add' and the
96
- optional argument ' condition' may be set to IO \_ IN or IO \_ OUT depending
94
+ ` socket_remove ` function has to be invoked. The ` id ` is the socket or
95
+ file object or the file descriptor given to ` socket_add ` and the
96
+ optional argument ` condition ` may be set to ` IO_IN ` or ` IO_OUT ` depending
97
97
on the previously registered event:
98
98
99
- def socket_remove( id, condition = IO_IN )
99
+ def socket_remove(id, condition= IO_IN)
100
100
101
101
Another way to achieve the removal of a socket or file object from the
102
102
notifier is to return False in the callback function. If a callback
@@ -108,22 +108,22 @@ Timer
108
108
109
109
pyNotifier supports just one type of timer. If a timer is registered
110
110
for a given interval of time the application is recurrently triggered
111
- when the timer expires. To register a timer the 'timer \_ add' function
112
- has to be invoked. The first argument ' interval' must be specified in
113
- milliseconds. ' method' is the callback function that is invoked by the
111
+ when the timer expires. To register a timer the ` timer_add ` function
112
+ has to be invoked. The first argument ` interval ` must be specified in
113
+ milliseconds. ` method ` is the callback function that is invoked by the
114
114
notifier without any argument when the timer expires.
115
115
116
- def timer_add( interval, method ) -> unique timer identifier
116
+ def timer_add(interval, method) -> unique timer identifier
117
117
118
118
To implement a one-shot timer that is just triggered once and never
119
119
again the application can use the return value of its callback
120
120
function for this timer and return False or None. In this case the
121
121
notifier automatically removes the timer. Another way to remove a
122
- timer is to call the method 'timer \_ remove' .
122
+ timer is to call the method ` timer_remove ` .
123
123
124
- def timer_remove( id )
124
+ def timer_remove(id )
125
125
126
- The 'id' argument is the unique timer identifier returned by 'timer \_ add' .
126
+ The ` id ` argument is the unique timer identifier returned by ` timer_add ` .
127
127
128
128
Signals
129
129
-------
@@ -149,32 +149,32 @@ context.
149
149
import notifier.signals as signals
150
150
151
151
# global context
152
- signals.new( 'signal1' )
152
+ signals.new('signal1')
153
153
154
- class Test( signals.Provider ):
155
- def __init__( self ):
156
- signals.Provider.__init__( self )
157
- self.signal_new( 'signal1' )
154
+ class Test(signals.Provider):
155
+ def __init__(self):
156
+ signals.Provider.__init__(self)
157
+ self.signal_new('signal1')
158
158
159
159
To get informed when a signal is emitted a connection must be created as
160
160
shown in the following code snippet.
161
161
162
162
# global context
163
- def _cb_signal( signal ):
163
+ def _cb_signal(signal):
164
164
pass
165
165
166
- signals.connect( 'signal1', _cb_signal )
166
+ signals.connect('signal1', _cb_signal)
167
167
168
168
# within the object
169
- class Test2( object ):
170
- def __init__( self, test ):
171
- test.signal_connect( 'signal1', self._cb_signal )
169
+ class Test2(object):
170
+ def __init__(self, test):
171
+ test.signal_connect('signal1', self._cb_signal)
172
172
173
- def _cb_signal( self, signal ):
173
+ def _cb_signal(self, signal):
174
174
pass
175
175
176
176
test = Test()
177
- test2 = Test2( test )
177
+ test2 = Test2(test)
178
178
179
179
There is no restriction in how many connection to a signal may exist,
180
180
but it should be noted, that to many connected instances may result an a
@@ -186,10 +186,10 @@ emit a signal its name and possibly optional arguments are passed to a
186
186
function as shown in the following snippet.
187
187
188
188
# global context
189
- signals.emit( 'signal1', a, b )
189
+ signals.emit('signal1', a, b)
190
190
191
191
# within an object
192
- test.signal_emit( 'signal1', a, b, c )
192
+ test.signal_emit('signal1', a, b, c)
193
193
194
194
The signature of the callback function for a signal depends on the
195
195
specific signal provider, e.g. each signal may provide as many arguments
@@ -210,49 +210,49 @@ pyNotifier provides the feature to add so called external
210
210
dispatchers. These dispatchers are functions that will be invoked in
211
211
each notifier step after all timers and sockets were checked. To add a
212
212
dispatcher function to the notifier main loop the function
213
- 'dispatcher \_ add' is provided. The only argument to this function is the
213
+ ` dispatcher_add ` is provided. The only argument to this function is the
214
214
callback method that will be called.
215
215
216
- def dispatcher_add( method )
216
+ def dispatcher_add(method)
217
217
218
218
To remove such a dispatcher function from the notifier main loop
219
- 'dispatcher \_ remove' is used with the call back method as the only argument.
219
+ ` dispatcher_remove ` is used with the call back method as the only argument.
220
220
221
- def dispatcher_remove( method )
221
+ def dispatcher_remove(method)
222
222
223
223
Callbacks
224
224
---------
225
225
226
- pyNotifier provides a class ' Callback' that can be used as a callback
227
- function that is passed to the 'socket \_ add' and 'timer \_ add'
226
+ pyNotifier provides a class ` Callback ` that can be used as a callback
227
+ function that is passed to the ` socket_add ` and ` timer_add `
228
228
function. This class provides the possibility to pass more than the
229
229
specified arguments to the callback functions. For example the socket
230
230
callback function is called with one single argument. To pass some
231
- state information to the callback function the ' Callback' object may
231
+ state information to the callback function the ` Callback ` object may
232
232
be used. A default callback function for a socket event would look
233
- like ' socket1' .
233
+ like ` socket1 ` .
234
234
235
235
import notifier
236
236
...
237
- notifier.socket_add( fd, socket1 )
237
+ notifier.socket_add(fd, socket1)
238
238
...
239
239
240
- def socket1( fd ):
241
- print 'data received on socket', fd
240
+ def socket1(fd ):
241
+ print( 'data received on socket', fd)
242
242
return True
243
243
244
- 'fd' is the 'id' given to 'socket \_ add' . To pass some state information
244
+ ` fd ` is the ` id ` given to ` socket_add ` . To pass some state information
245
245
to the callback function it can be done as shown in the following
246
246
example.
247
247
248
248
import notifier
249
249
...
250
- notifier.socket_add( fd, notifier.Callback( socket1, arg1, arg2 ) )
250
+ notifier.socket_add(fd, notifier.Callback(socket1, arg1, arg2) )
251
251
...
252
252
253
- def socket1( fd, arg1, arg2 ):
254
- print 'data received on socket', fd
255
- print 'additional state information', arg1, arg2
253
+ def socket1(fd, arg1, arg2):
254
+ print( 'data received on socket', fd)
255
+ print( 'additional state information', arg1, arg2)
256
256
257
257
The arguments given to the Callback object are appended to the
258
258
original list of arguments for the callback function. The argument
@@ -270,26 +270,26 @@ pyNotifier in the examples sub-directory.
270
270
import notifier
271
271
272
272
def another_minute():
273
- print "another minute is elapsed"
273
+ print( "another minute is elapsed")
274
274
# callback should be invoked again
275
275
return True
276
276
277
- def first_10_secs( secs ):
278
- print "the first %d secs are elapsed" % secs
277
+ def first_10_secs(secs):
278
+ print( "the first %d secs are elapsed" % secs)
279
279
# this should be a one-shot timer
280
280
return False
281
281
282
- def standard_in( in ):
283
- print "someone entered some data on stdin"
284
- print in.read( 80 )
282
+ def standard_in(in ):
283
+ print( "someone entered some data on stdin")
284
+ print( in.read(80) )
285
285
# still want to watch it
286
286
return True
287
287
288
288
if __name__ == "__main__":
289
- notifier.init( notifier.GENERIC )
289
+ notifier.init(notifier.GENERIC)
290
290
291
- notifier.timer_add( 60000, another_minute )
292
- notifier.timer_add( 10000, notifier.Callback( first_10_secs, 10 ) )
293
- notifier.socket_add( sys.stdin, standard_in )
291
+ notifier.timer_add(60000, another_minute)
292
+ notifier.timer_add(10000, notifier.Callback(first_10_secs, 10) )
293
+ notifier.socket_add(sys.stdin, standard_in)
294
294
295
295
notifier.loop()
0 commit comments