Package brisa :: Package core :: Package reactors :: Module glib2
[hide private]
[frames] | no frames]

Source Code for Module brisa.core.reactors.glib2

  1  # Licensed under the MIT license 
  2  # http://opensource.org/licenses/mit-license.php or see LICENSE file. 
  3  # Copyright 2007-2008 Brisa Team <brisa-develop@garage.maemo.org> 
  4   
  5  """ Glib2/gobject based reactor. Works trasparently with gobject-dependent 
  6  things, such as Dbus. 
  7  """ 
  8   
  9  from brisa.core.ireactor import ReactorInterface, EVENT_TYPE_READ, \ 
 10                                  EVENT_TYPE_WRITE, EVENT_TYPE_EXCEPTION 
 11   
 12  import signal 
 13   
 14  try: 
 15      import gobject 
 16      __all__ = ('GLib2Reactor', ) 
 17  except ImportError: 
 18      __all__ = () 
 19   
 20   
21 -class GLib2Reactor(ReactorInterface):
22 23 _stop_funcs = [] 24 _start_funcs = [] 25 _main_loop = gobject.MainLoop() 26
27 - def __init__(self, *args, **kwargs):
28 ReactorInterface.__init__(self, *args, **kwargs) 29 signal.signal(signal.SIGTERM, self.main_quit) 30 signal.signal(signal.SIGINT, self.main_quit)
31
32 - def add_timer(self, interval, callback, threshold=0):
33 """ Add timer. 34 35 @note: should return an ID assigned to the timer, so that it can be 36 removed by rem_timer(). 37 """ 38 return gobject.timeout_add(int(interval*(10**3)), callback)
39
40 - def rem_timer(self, timer_id):
41 """ Removes a timer. 42 """ 43 return gobject.source_remove(timer_id)
44
45 - def add_fd(self, fd, event_callback, event_type):
46 """ Adds a fd for watch. 47 """ 48 condition = None 49 if event_type & EVENT_TYPE_READ: 50 condition = gobject.IO_IN | gobject.IO_PRI 51 if event_type & EVENT_TYPE_WRITE: 52 if not condition: 53 condition = gobject.IO_OUT 54 else: 55 condition = condition | gobject.IO_OUT 56 if event_type & EVENT_TYPE_EXCEPTION: 57 if not condition: 58 condition = gobject.IO_ERR 59 else: 60 condition = condition | gobject.IO_ERR 61 62 return gobject.io_add_watch(fd, condition, event_callback)
63
64 - def rem_fd(self, fd_handler):
65 """ Removes a fd from being watched. 66 """ 67 return gobject.source_remove(fd_handler)
68
69 - def add_after_stop_func(self, func):
70 """ Registers a function to be called before entering the STOPPED 71 state. 72 73 @param func: function 74 @type func: callable 75 """ 76 if func not in self._stop_funcs: 77 self._stop_funcs.append(func)
78
79 - def rem_after_stop_func(self, func):
80 """ Removes a registered function. 81 82 @param func: function 83 @type func: callable 84 """ 85 if func in self._stop_funcs: 86 self._stop_funcs.remove(func)
87
88 - def add_before_start_func(self, func):
89 """ Registers a function to be called before starting the main loop. 90 91 @param func: function 92 @type func: callable 93 """ 94 if func not in self._start_funcs: 95 self._start_funcs.append(func)
96
97 - def rem_before_start_func(self, func):
98 """ Removes a registered function. 99 100 @param func: function 101 @type func: callable 102 """ 103 if func in self._start_funcs: 104 self._start_funcs.remove(func)
105
106 - def main_loop_iterate(self):
107 """ Runs a single iteration of the main loop. Reactor enters the 108 RUNNING state while this method executes. 109 """ 110 self._main_loop.get_context().iteration()
111
112 - def main(self):
113 """ Enters the RUNNING state by running the main loop until 114 main_quit() is called. 115 """ 116 self._main_call_before_start_funcs() 117 try: 118 self._main_loop.run() 119 except KeyboardInterrupt: 120 pass 121 self._main_call_after_stop_funcs()
122
123 - def main_quit(self):
124 """ Terminates the main loop. 125 """ 126 self._main_loop.quit()
127
128 - def is_running(self):
129 """ Returns True if the main loop is running 130 """ 131 return self._main_loop.get_context().is_running()
132
134 """ Calls registered functions to be called after the main loop is 135 stopped. 136 """ 137 for cb in self._stop_funcs: 138 cb()
139
141 """ Call registered functions to be called before starting the main 142 loop. 143 """ 144 for cb in self._start_funcs: 145 cb()
146