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

Source Code for Module brisa.core.reactors._ecore

  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  """ Ecore based reactor. 
  6  """ 
  7   
  8  from brisa.core import log 
  9  from brisa.core.ireactor import * 
 10   
 11   
 12  try: 
 13      import ecore 
 14      __all__ = ('EcoreReactor', ) 
 15  except ImportError: 
 16      __all__ = () 
 17   
 18  log = log.getLogger('reactor.ecore') 
 19   
 20   
21 -class EcoreReactor(ReactorInterface):
22 23 _stop_funcs = [] 24 _start_funcs = [] 25
26 - def add_timer(self, interval, callback, threshold=0):
27 """ Add timer. 28 29 @note: should return an ID assigned to the timer, so that it can be 30 removed by rem_timer(). 31 """ 32 return ecore.Timer(interval, callback)
33
34 - def rem_timer(self, timer):
35 """ Removes a timer. 36 """ 37 return timer.delete()
38
39 - def add_fd(self, fd, event_callback, event_type):
40 """ Adds a fd for watch. 41 """ 42 condition = None 43 if event_type & EVENT_TYPE_READ: 44 condition = ecore.ECORE_FD_READ 45 if event_type & EVENT_TYPE_WRITE: 46 if not condition: 47 condition = ecore.ECORE_FD_WRITE 48 else: 49 condition = condition | ecore.ECORE_FD_WRITE 50 if event_type & EVENT_TYPE_EXCEPTION: 51 if not condition: 52 condition = ecore.ECORE_FD_ERROR 53 else: 54 condition = condition | ecore.ECORE_FD_ERROR 55 56 return ecore.fd_handler_add(fd, condition, event_callback)
57
58 - def rem_fd(self, fd_handler):
59 """ Removes a fd from being watched. 60 """ 61 return fd_handler.delete()
62
63 - def add_after_stop_func(self, func):
64 """ Registers a function to be called before entering the STOPPED 65 state. 66 67 @param func: function 68 @type func: callable 69 """ 70 if func not in self._stop_funcs: 71 self._stop_funcs.append(func)
72
73 - def rem_after_stop_func(self, func):
74 """ Removes a registered function. 75 76 @param func: function 77 @type func: callable 78 """ 79 if func in self._stop_funcs: 80 self._stop_funcs.remove(func)
81
82 - def add_before_start_func(self, func):
83 """ Registers a function to be called before starting the main loop. 84 85 @param func: function 86 @type func: callable 87 """ 88 if func not in self._start_funcs: 89 self._start_funcs.append(func)
90
91 - def rem_before_start_func(self, func):
92 """ Removes a registered function. 93 94 @param func: function 95 @type func: callable 96 """ 97 if func in self._start_funcs: 98 self._start_funcs.remove(func)
99
100 - def main_loop_iterate(self):
101 """ Runs a single iteration of the main loop. Reactor enters the 102 RUNNING state while this method executes. 103 """ 104 ecore.main_loop_iterate()
105
106 - def main(self):
107 """ Enters the RUNNING state by running the main loop until 108 main_quit() is called. 109 """ 110 self._main_call_before_start_funcs() 111 self.state = REACTOR_STATE_RUNNING 112 ecore.main_loop_begin() 113 self.state = REACTOR_STATE_STOPPED 114 self._main_call_after_stop_funcs()
115
116 - def main_quit(self):
117 """ Terminates the main loop. 118 """ 119 ecore.main_loop_quit()
120
121 - def is_running(self):
122 """ Returns True if the main loop is running 123 """ 124 return True if self.state else False
125
127 """ Calls registered functions to be called after the main loop is 128 stopped. 129 """ 130 for cb in self._stop_funcs: 131 cb()
132
134 """ Call registered functions to be called before starting the main 135 loop. 136 """ 137 for cb in self._start_funcs: 138 cb()
139