Package buildxml :: Package xmlgetter :: Module controller
[hide private]
[frames] | no frames]

Source Code for Module buildxml.xmlgetter.controller

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  """ 
  5  This is the controller module. The purpose of the controller is to load all 
  6  plugins and run their C{run()} function. Also it collects 
  7  statistics about each plugins run. 
  8   
  9   
 10  @author: Johannes Schwenk 
 11  @copyright: 2010, Johannes Schwenk 
 12  @version: 2.0 
 13  @date: 2010-09-15 
 14   
 15   
 16  """ 
 17   
 18  import sys 
 19   
 20  # Imortant! 
 21  reload(sys) 
 22  sys.setdefaultencoding('utf-8') 
 23   
 24  from shutil import copyfile, move 
 25  from datetime import datetime 
 26   
 27  from stats import Stats 
 28  from log import BaseLogger 
 29  import config 
 30   
31 -class Controller(BaseLogger):
32 """ 33 The controller has one central function L{run()} which loads all plugins and 34 portals, and runs their C{run} function. Also it collects statistics about 35 each plugins run and makes it available via the L{stats} property. 36 37 """ 38 39 NO_NET = None 40 """ 41 @ivar: Run in no net mode? 42 @type: bool 43 44 """ 45 46 stats = None 47 """ 48 @ivar: Stats gathered from the plugins that have been run by the 49 controller. 50 @type: L{Stats} 51 52 """ 53 54 _plugins = None 55 """ 56 @ivar: List of all plugin instances, whether generic portal plugin 57 or normal plugin instances. 58 @type: list 59 60 """ 61 62
63 - def __init__(self, NO_NET=False, plugin=None, source=None):
64 """ 65 Initialize the Controller. 66 67 @param NO_NET: If C{False} the L{run} function will call the plugins in 68 net mode, which lets them retrieve all data from the net. 69 Otherwise possibly existing data from a prevoius run will be 70 used instead. 71 @type NO_NET: bool 72 73 """ 74 BaseLogger.__init__(self, u'Controller') 75 self.NO_NET = NO_NET 76 self.stats = None 77 self._plugins = [] 78 self._modules = {} 79 PLUGINS = config.PLUGINS 80 PORTALS = config.PORTALS 81 if plugin and source: 82 if plugin == config.PORTAL_PLUGIN_NAME: 83 PLUGINS = [] 84 PORTALS = filter(lambda p: p[u'name'] == source, PORTALS) 85 else: 86 PORTALS = [] 87 PLUGINS = filter(lambda p: p[u'name'] == source, PLUGINS) 88 portal_module_name = u'%s.%s' % (config.PLUGIN_DIR_NAME, 89 config.PORTAL_PLUGIN_NAME) 90 try: 91 portal = __import__(portal_module_name, globals(), locals(), 92 [u'SyncPlugin_%s' % config.PORTAL_PLUGIN_NAME,]) 93 except ImportError, e: 94 self.logger.warn(u'Could not load the generic portals plugin: %s' 95 % e) 96 else: 97 self.logger.debug(u'Sucessfully imported generic portals plugin.') 98 for p in PORTALS: 99 plug = getattr(portal, u'SyncPlugin_%s' 100 % config.PORTAL_PLUGIN_NAME)( 101 p[u'name'], p[u'url'], NO_NET=self.NO_NET) 102 self._plugins.append(plug) 103 self.logger.info(u'Portal plugin "%s" loaded.' % p[u'name']) 104 105 106 for p in PLUGINS: 107 module_name = u'%s.%s' % (config.PLUGIN_DIR_NAME, p[u'name']) 108 try: 109 mod = __import__(module_name, globals(), locals(), 110 [u'SyncPlugin_%s' % p[u'name'],]) 111 except ImportError, e: 112 self.logger.warn(u'Could not load plugin "%s" : %s' 113 % (p[u'name'], e)) 114 else: 115 self.logger.debug(u'Sucessfully imported plugin' 116 u' "%s"' % p[u'name']) 117 plug = getattr(mod, u'SyncPlugin_%s' % p[u'name'])( 118 p[u'name'], p[u'url'], NO_NET=self.NO_NET) 119 self._plugins.append(plug) 120 self.logger.info(u'Plugin "%s" loaded.' % p[u'name'])
121 122
123 - def run(self):
124 """ 125 Run all the plugins and collect statistics in L{self.stats} . 126 127 @todo: 128 TODO return values 129 130 131 @return: C{True} if all plugins returned C{True} from 132 their C{run()} function, C{False} otherwise. 133 134 """ 135 136 """ 137 Create an instance of xmlgetter.stats.Stats to summarize the 138 statistics for the portals and plugins. 139 """ 140 self.stats = Stats() 141 self.stats.update_time_start = datetime.now() 142 143 retval = True 144 145 # Call all the plugins C{run} function. 146 for plug in self._plugins: 147 retval = plug.run() and retval 148 self.stats.addStats(plug.stats) 149 150 self.stats.update_time_end = datetime.now() 151 return retval
152