Beispiel #1
0
    def handler(self, data):

        if data.message == (0, 'shutdown'):
            assert self.identification.require_level(data, 2)
            data.origin.message('Preparing for shutdown...')
            # System event code 1 -> shutdown
            self.eventhandler.fire_once(self.system_event, (1,))
            data.origin.message('Shutting down..')
            get_connection().shutdown()
        elif data.message == (0, 'reload'):
            assert self.identification.require_level(data, 2)
            # System event code 0 -> reload
            self.eventhandler.fire_once(self.system_event, (0,))
            try:
                get_connection().pluginloader.reload()
            except Exception, e:
                data.origin.message("Exception %s while reloading plugins." % (e))
                [logging.getLogger("ashiema").error("%s" % (tb)) for tb in traceback.format_exc(4).split('\n')]
                return
            data.origin.message('Plugins reloaded')
Beispiel #2
0
class System(Plugin):
    def __init__(self, connection, eventhandler):

        Plugin.__init__(self, connection, eventhandler, needs_dir=False)

        self.eventhandler.get_events()['PMEvent'].register(self.handler)
        self.eventhandler.get_events()['PluginsLoadedEvent'].register(
            self.load_identification)

        self.system_event = self.eventhandler.get_events()['SystemEvent']

    def __deinit__(self):

        self.eventhandler.get_events()['PMEvent'].deregister(self.handler)
        self.eventhandler.get_events()['PluginsLoadedEvent'].deregister(
            self.load_identification)

    def load_identification(self):

        self.identification = get_connection().pluginloader.get_plugin(
            'IdentificationPlugin')

    def handler(self, data):

        if data.message == (0, 'shutdown'):
            assert self.identification.require_level(data, 2)
            data.origin.message('Preparing for shutdown...')
            # System event code 1 -> shutdown
            self.eventhandler.fire_once(self.system_event, (1, ))
            data.origin.message('Shutting down..')
            get_connection().shutdown()
        elif data.message == (0, 'reload'):
            assert self.identification.require_level(data, 2)
            # System event code 0 -> reload
            self.eventhandler.fire_once(self.system_event, (0, ))
            try:
                get_connection().pluginloader.reload()
            except Exception, e:
                data.origin.message("Exception %s while reloading plugins." %
                                    (e))
                [
                    logging.getLogger("ashiema").error("%s" % (tb))
                    for tb in traceback.format_exc(4).split('\n')
                ]
                return
            data.origin.message('Plugins reloaded')
        elif data.message == (0, 'rehash'):
            assert self.identification.require_level(data, 2)
            # System event code 2 -> rehash
            self.eventhandler.fire_once(self.system_event, (2, ))
            get_connection().configuration.reload()
            data.origin.message('Rehash completed!')
Beispiel #3
0
    def handler(self, data):

        if data.message == (0, 'shutdown'):
            assert self.identification.require_level(data, 2)
            data.origin.message('Preparing for shutdown...')
            # System event code 1 -> shutdown
            self.eventhandler.fire_once(self.system_event, (1, ))
            data.origin.message('Shutting down..')
            get_connection().shutdown()
        elif data.message == (0, 'reload'):
            assert self.identification.require_level(data, 2)
            # System event code 0 -> reload
            self.eventhandler.fire_once(self.system_event, (0, ))
            try:
                get_connection().pluginloader.reload()
            except Exception, e:
                data.origin.message("Exception %s while reloading plugins." %
                                    (e))
                [
                    logging.getLogger("ashiema").error("%s" % (tb))
                    for tb in traceback.format_exc(4).split('\n')
                ]
                return
            data.origin.message('Plugins reloaded')
Beispiel #4
0
    def load_identification(self):

        self.identification = get_connection().pluginloader.get_plugin('IdentificationPlugin')
Beispiel #5
0
    def load_identification(self):

        self.identification = get_connection().pluginloader.get_plugin(
            'IdentificationPlugin')
Beispiel #6
0
 def match(self, data = None):
     if isinstance(data, tuple) and get_connection().pluginloader._loaded:
         return True
     return False
Beispiel #7
0
 def load(self):
     # this loads all plugins
     plugin_dir = os.getcwd() + '/plugins/'
     files = os.listdir(plugin_dir)
     plugins = []
     # find plugin files
     for file in files:
         if file[-3:] == ".py":
             plugins.append(file)
     # update container with plugin information
     for plugin in plugins:
         try: source = load_source(plugin.split('.')[0], plugin_dir + plugin)
         except:
             self.log.info('an error has occurred while loading plugins.')
             [self.log.error(trace) for trace in traceback.format_exc(4).split('\n')]
             continue
         if not hasattr(source, '__data__'): continue
         if source.__data__['name'] in self._skip: continue
         self.container.update(
             {
                 source.__data__['name']:
                 {
                     'version': source.__data__['version'],
                     'require': source.__data__['require'],
                     'main': source.__data__['main'],
                     'events': (source.__data__['events'] or [])
                 }
             }
         )
         # load the help, if it's there.
         if hasattr(source, '__help__'):
             self.helpfactory.register(source.__data__['name'], source.__help__)
     # check requires
     self.container = self.__depcheck__(self.container)    
     """ setting the loading variable here makes dependency requires work correctly, and at this point, all plugins
         are effectively loaded, but they are just not yet initialised. """
     self._loaded = True
     # initialise plugins
     unload = []
     if self.container:
         # initialise events from plugins
         for data in self.container.values():
             for event in data['events']:
                 event(self.eventhandler)
         # initialise plugins
         for plugin, data in self.container.iteritems():
             try:
                 self.loaded.update(
                     {
                         plugin : data['main'](self.connection, self.eventhandler)
                     }
                 )
             except:
                 self.log.info('an error has occurred in %s (%s):' % (plugin, data['version']))
                 [self.log.error(trace) for trace in traceback.format_exc(4).split('\n')]
                 unload.append(plugin)
                 continue
         if len(unload) > 0:
             self.log.info('some plugins have failed to load.')
             for plugin in unload:
                 self.container.pop(plugin)
                 self.log.error('%s failed to load and has been unloaded.' % (plugin))
         self.log.info('all plugins have been loaded.')
         # run the PluginsLoadedEvent
         get_connection()._evh.fire_once(get_connection()._evh.get_events()['PluginsLoadedEvent'], ())
     elif not self.container: self.log.info('no plugins to load.')