def testPluginEvent(self): logger.debug('-' * 26 + '\n') logger.info('Running plugin event test...') import onionrplugins as plugins, onionrevents as events, os if not plugins.exists('test'): os.makedirs(plugins.get_plugins_folder('test')) with open(plugins.get_plugins_folder('test') + '/main.py', 'a') as main: main.write( "print('Running')\n\ndef on_test(pluginapi, data = None):\n print('received test event!')\n print('thread test started...')\n import time\n time.sleep(1)\n \n return True\n\ndef on_start(pluginapi, data = None):\n print('start event called')\n\ndef on_stop(pluginapi, data = None):\n print('stop event called')\n\ndef on_enable(pluginapi, data = None):\n print('enable event called')\n\ndef on_disable(pluginapi, data = None):\n print('disable event called')\n" ) plugins.enable('test') plugins.start('test') if not events.call(plugins.get_plugin('test'), 'enable'): self.assertTrue(False) logger.debug('preparing to start thread', timestamp=False) thread = events.event('test', data={'tests': self}) logger.debug('thread running...', timestamp=False) thread.join() logger.debug('thread finished.', timestamp=False) self.assertTrue(True)
def create_plugin(o_inst): ''' Creates the directory structure for a plugin name ''' if len(sys.argv) >= 3: try: plugin_name = re.sub('[^0-9a-zA-Z_]+', '', str(sys.argv[2]).lower()) if not plugins.exists(plugin_name): logger.info('Creating plugin "%s"...' % plugin_name) os.makedirs(plugins.get_plugins_folder(plugin_name)) with open(plugins.get_plugins_folder(plugin_name) + '/main.py', 'a') as main: contents = '' with open('static-data/default_plugin.py', 'rb') as file: contents = file.read().decode() # TODO: Fix $user. os.getlogin() is B U G G Y main.write(contents.replace('$user', 'some random developer').replace('$date', datetime.datetime.now().strftime('%Y-%m-%d')).replace('$name', plugin_name)) with open(plugins.get_plugins_folder(plugin_name) + '/info.json', 'a') as main: main.write(json.dumps({'author' : 'anonymous', 'description' : 'the default description of the plugin', 'version' : '1.0'})) logger.info('Enabling plugin "%s"...' % plugin_name) plugins.enable(plugin_name, o_inst) else: logger.warn('Cannot create plugin directory structure; plugin "%s" exists.' % plugin_name) except Exception as e: logger.error('Failed to create plugin directory structure.', e) else: logger.info('%s %s <plugin>' % (sys.argv[0], sys.argv[1]))
def testPluginReload(self): logger.debug('-' * 26 + '\n') logger.info('Running simple plugin reload test...') import onionrplugins, os if not onionrplugins.exists('test'): os.makedirs(onionrplugins.get_plugins_folder('test')) with open( onionrplugins.get_plugins_folder('test') + '/main.py', 'a') as main: main.write( "print('Running')\n\ndef on_test(pluginapi, data = None):\n print('received test event!')\n return True\n\ndef on_start(pluginapi, data = None):\n print('start event called')\n\ndef on_stop(pluginapi, data = None):\n print('stop event called')\n\ndef on_enable(pluginapi, data = None):\n print('enable event called')\n\ndef on_disable(pluginapi, data = None):\n print('disable event called')\n" ) onionrplugins.enable('test') try: onionrplugins.reload('test') self.assertTrue(True) except: self.assertTrue(False)