def uninstallPlugin(cls, plugin_name = None, plugin_pk = None, dev_mode = False): # Get plugin by name or pk if plugin_pk: plugin_list = Plugin.objects(pk = plugin_pk) else: plugin_list = Plugin.objects(name = plugin_name) # Should be only one result if not plugin_list or len(plugin_list) != 1: return {'status': 'fail', 'log': 'Plugin not installed'} plugin = plugin_list[0] # Uninstall pip package if not dev_mode: pip.main(['uninstall', '--quiet', 'lisa-plugin-' + plugin_name]) # Remove plugin crons for cron in Cron.objects(plugin = plugin): cron.delete() # Remove plugin intents for oIntent in Intent.objects(plugin = plugin): oIntent.delete() # Remove plugin plugin.delete() return {'status': 'success', 'log': 'Plugin uninstalled'}
def getIntent(cls, intent_name): # TODO get these intents from a core function # Create the default core intents defaults_intent_list = { 'name': "core_i_can", 'method_name': "list_plugins", 'plugin_name': "Core", 'enabled': True } intent_list, created = Intent.objects.get_or_create( name='core_i_can', defaults=defaults_intent_list) defaults_intent_list = { 'name': "core_i_can_plugin", 'method_name': "list_plugin_intents", 'plugin_name': "Core", 'enabled': True } intent_list, created = Intent.objects.get_or_create( name='core_i_can_plugin', defaults=defaults_intent_list) # List enabled intents intent_list = Intent.objects(name=intent_name, enabled=True) # If not a unique enabled intent if intent_list is None or len(intent_list) != 1: return None return intent_list[0]
def uninstallPlugin(cls, plugin_name=None, plugin_pk=None, dev_mode=False): # Get plugin by name or pk if plugin_pk: plugin_list = Plugin.objects(pk=plugin_pk) else: plugin_list = Plugin.objects(name=plugin_name) # Should be only one result if not plugin_list or len(plugin_list) != 1: return {'status': 'fail', 'log': 'Plugin not installed'} plugin = plugin_list[0] # Uninstall pip package if not dev_mode: pip.main(['uninstall', '--quiet', 'lisa-plugin-' + plugin_name]) # Remove plugin crons for cron in Cron.objects(plugin=plugin): cron.delete() # Remove plugin intents for oIntent in Intent.objects(plugin=plugin): oIntent.delete() # Remove plugin plugin.delete() return {'status': 'success', 'log': 'Plugin uninstalled'}
def list(self, jsonInput): intentstr = [] listintents = self.wit.get_intents() for oIntent in oIntents.objects(enabled=True): for witintent in listintents: print witintent if witintent["name"] == oIntent.name and 'metadata' in witintent: if witintent['metadata']: metadata = json.loads(witintent['metadata']) intentstr.append(metadata['tts']) return {"plugin": "Intents", "method": "list", "body": unicode(_('I can %(intentslist)s' % {'intentslist': ', '.join(intentstr)})) }
def list(self, jsonInput): intentstr = [] listintents = self.wit.get_intents() for oIntent in oIntents.objects(enabled=True): for witintent in listintents: if witintent[ "name"] == oIntent.name and 'metadata' in witintent: if witintent['metadata']: metadata = json.loads(witintent['metadata']) intentstr.append(metadata['tts']) return { "plugin": "Intents", "method": "list", "body": _('I can %(intentslist)s' % {'intentslist': ', '.join(intentstr)}) }
def uninstallPlugin(self, plugin_name=None, plugin_pk=None, dev_mode=False): if plugin_pk: plugin_list = Plugin.objects(pk=plugin_pk) else: plugin_list = Plugin.objects(name=plugin_name) if not plugin_list: return {'status': 'fail', 'log': unicode(_('Plugin not installed'))} else: for plugin in plugin_list: if not dev_mode: pip.main(['uninstall', '--quiet', 'lisa-plugin-' + plugin_name]) plugin.delete() for cron in Cron.objects(plugin=plugin): cron.delete() for rule in Rule.objects(plugin=plugin): rule.delete() intent_list = Intent.objects(plugin=plugin) for oIntent in intent_list: oIntent.delete() return {'status': 'success', 'log': unicode(_('Plugin uninstalled'))}
def _set_plugin_enabled(cls, enabled, plugin_name=None, plugin_pk=None): # Log string if enabled == True: astr = "enabled" else: astr = "disabled" # Get plugin by name or pk if plugin_pk: plugin_list = Plugin.objects(pk=plugin_pk) else: plugin_list = Plugin.objects(name=plugin_name) # Should be only one result if not plugin_list or len(plugin_list) != 1: return {'status': 'fail', 'log': 'Plugin not installed'} plugin = plugin_list[0] # If already done if enabled == True and plugin.enabled == enabled: return {'status': 'fail', 'log': 'Plugin already ' + astr} if enabled == False and plugin.enabled == enabled: return {'status': 'fail', 'log': 'Plugin already ' + astr} # Enable plugin plugin.enabled = enabled plugin.save() # Enable plugin crons for cron in Cron.objects(plugin=plugin): cron.enabled = enabled cron.save() # Enable plugin intents for intent in Intent.objects(plugin=plugin): intent.enabled = enabled intent.save() return {'status': 'success', 'log': 'Plugin ' + astr}
def getIntent(cls, intent_name): # TODO get these intents from a core function # Create the default core intents defaults_intent_list = {'name': "core_i_can", 'method_name': "list_plugins", 'plugin_name': "Core", 'enabled': True} intent_list, created = Intent.objects.get_or_create(name = 'core_i_can', defaults = defaults_intent_list) defaults_intent_list = {'name': "core_i_can_plugin", 'method_name': "list_plugin_intents", 'plugin_name': "Core", 'enabled': True} intent_list, created = Intent.objects.get_or_create(name = 'core_i_can_plugin', defaults = defaults_intent_list) # List enabled intents intent_list = Intent.objects(name = intent_name, enabled = True) # If not a unique enabled intent if intent_list is None or len(intent_list) != 1: return None return intent_list[0]
def _set_plugin_enabled(cls, enabled, plugin_name = None, plugin_pk = None): # Log string if enabled == True: astr = "enabled" else: astr = "disabled" # Get plugin by name or pk if plugin_pk: plugin_list = Plugin.objects(pk = plugin_pk) else: plugin_list = Plugin.objects(name = plugin_name) # Should be only one result if not plugin_list or len(plugin_list) != 1: return {'status': 'fail', 'log': 'Plugin not installed'} plugin = plugin_list[0] # If already done if enabled == True and plugin.enabled == enabled: return {'status': 'fail', 'log': 'Plugin already ' + astr} if enabled == False and plugin.enabled == enabled: return {'status': 'fail', 'log': 'Plugin already ' + astr} # Enable plugin plugin.enabled = enabled plugin.save() # Enable plugin crons for cron in Cron.objects(plugin = plugin): cron.enabled = enabled cron.save() # Enable plugin intents for intent in Intent.objects(plugin = plugin): intent.enabled = enabled intent.save() return {'status': 'success', 'log': 'Plugin ' + astr}
def enablePlugin(self, plugin_name=None, plugin_pk=None): if plugin_pk: plugin_list = Plugin.objects(pk=plugin_pk) else: plugin_list = Plugin.objects(name=plugin_name) for plugin in plugin_list: if plugin.enabled: return {'status': 'fail', 'log': unicode(_('Plugin already enabled'))} else: plugin.enabled = True plugin.save() for cron in Cron.objects(plugin=plugin): cron.enabled = True cron.save() for rule in Rule.objects(plugin=plugin): rule.enabled = True rule.save() intent_list = Intent.objects(plugin=plugin) for oIntent in intent_list: oIntent.enabled = True oIntent.save() return {'status': 'success', 'log': unicode(_('Plugin enabled'))}
def disablePlugin(self, plugin_name=None, plugin_pk=None): if plugin_pk: plugin_list = Plugin.objects(pk=plugin_pk) else: plugin_list = Plugin.objects(name=plugin_name) for plugin in plugin_list: if not plugin.enabled: return {'status': 'fail', 'log': 'Plugin already disabled'} else: plugin.enabled = False plugin.save() for cron in Cron.objects(plugin=plugin): cron.enabled = False cron.save() for rule in Rule.objects(plugin=plugin): rule.enabled = False rule.save() intent_list = Intent.objects(plugin=plugin) for oIntent in intent_list: oIntent.enabled = False oIntent.save() return {'status': 'success', 'log': 'Plugin disabled'}
def getEnabledIntents(cls): return Intent.objects(enabled=True)
def _updatePlugin(cls, plugin): # Load JSON plugin_path = os.path.normpath(plugins_path + '/' + plugin.name) jsonfile = os.path.normpath(plugin_path + '/' + plugin.name.lower() + '.json') try: metadata = json.load(open(jsonfile)) except: log.err("Invalid JSON file for plugin {plugin} : {file}".format( plugin=plugin.name, file=jsonfile)) return # Parse file for item in metadata: if item == 'enabled': if metadata[item] == 0 or (type(metadata[item]) == str and metadata[item].lower() == 'false'): setattr(plugin, item, False) else: setattr(plugin, item, True) elif item != 'crons': setattr(plugin, item, metadata[item]) # Delete older items d = plugin.__dict__.copy() for k in d: if k.startswith( "_") == False and k not in metadata and k != 'enabled': delattr(plugin, k) # TODO remove when uid are not useful setattr(plugin, 'uid', plugin.pk) #TODO setattr(plugin, 'steps', {'count': 0, 'first': None, 'last': None}) # Add langages from directory search setattr(plugin, 'lang', []) localedir = os.path.normpath(plugin_path + '/lang') for x in os.listdir(localedir): try: if os.path.isfile( "{localedir}/{lang}/LC_MESSAGES/{plugin}.po".format( localedir=localedir, lang=x, plugin=plugin.name.lower())) == True: plugin.lang.append(x) except: pass # Add items setattr(plugin, 'path', plugin_path) setattr( plugin, 'module', '.'.join([ 'lisa.plugins', plugin.name, 'modules', plugin.name.lower(), plugin.name ])) # Save updated plugin plugin.save() # Update crons remove_list = list(Cron.objects(plugin=plugin)) if metadata.has_key('crons'): for cron_name, cron_item in metadata['crons'].iteritems(): # Get cron from DB cron = None cron_list = Cron.objects(plugin=plugin, name=cron_name) if cron_list is not None and len(cron_list) == 1: cron = cron_list[0] # It's a new cron if cron is None: # Create a new cron cron = Cron() new_cron = True else: # Remove cron from list of crons to remove remove_list.remove(cron) new_cron = False # Update cron for parameter in cron_item: if parameter != 'enabled': setattr(cron, parameter, cron_item[parameter]) # Delete older items d = cron.__dict__.copy() for k in d: if k.startswith( "_" ) == False and k not in cron_item and k != 'enabled': delattr(cron, k) # Set enabled only for new crons if new_cron == True: if cron_item.has_key('enabled') == True and ( cron_item['enabled'] == 0 or (type(cron_item['enabled']) == str and cron_item['enabled'].lower() == 'false')): setattr(cron, 'enabled', False) else: setattr(cron, 'enabled', True) # Add items setattr(cron, 'name', cron_name) # Connect cron to plugin cron.plugin = plugin # Save cron cron.save() # Delete older crons for this plugin for i in remove_list: i.delete() # Update intents remove_list = list(Intent.objects(plugin=plugin)) metadata_intents = None if metadata.has_key('configuration' ) and metadata['configuration'].has_key('intents'): metadata_intents = metadata['configuration']['intents'] if metadata.has_key('intents'): metadata_intents = metadata['intents'] if metadata_intents is not None: for wit_intent, intent_item in metadata_intents.iteritems(): # Get intent from DB intent = None intent_list = Intent.objects(plugin=plugin, name=wit_intent) if intent_list is not None and len(intent_list) == 1: intent = intent_list[0] # It's a new intent if intent is None: # Create a new intent intent = Intent() new_intent = True else: # Remove intent from list of intents to remove remove_list.remove(intent) new_intent = False # Update intent for parameter in intent_item: if parameter == 'method': setattr(intent, 'method_name', intent_item[parameter]) elif parameter == 'i_can': setattr(intent, parameter, intent_item[parameter]) # Delete older items d = intent.__dict__.copy() for k in d: if k.startswith( "_" ) == False and k not in intent_item and k != 'enabled': delattr(intent, k) # Set enabled only for new intents if new_intent == True: setattr(intent, 'enabled', True) # Connect intent to plugin intent.plugin = plugin intent.plugin_name = plugin.name # Add items setattr(intent, 'name', wit_intent) # Save intent intent.save() # Delete older intents for this plugin for i in remove_list: i.delete()
def getEnabledIntents(cls): return Intent.objects(enabled = True)
def _updatePlugin(cls, plugin): # Load JSON plugin_path = os.path.normpath(plugins_path + '/' + plugin.name) jsonfile = os.path.normpath(plugin_path + '/' + plugin.name.lower() + '.json') try: metadata = json.load(open(jsonfile)) except: log.err("Invalid JSON file for plugin {plugin} : {file}".format(plugin = plugin.name, file = jsonfile)) return # Parse file for item in metadata: if item == 'enabled': if metadata[item] == 0 or (type(metadata[item]) == str and metadata[item].lower() == 'false'): setattr(plugin, item, False) else: setattr(plugin, item, True) elif item != 'crons': setattr(plugin, item, metadata[item]) # Delete older items d = plugin.__dict__.copy() for k in d: if k.startswith("_") == False and k not in metadata and k != 'enabled': delattr(plugin, k) # TODO remove when uid are not useful setattr(plugin, 'uid', plugin.pk) #TODO setattr(plugin, 'steps', {'count': 0, 'first': None, 'last': None}) # Add langages from directory search setattr(plugin, 'lang', []) localedir = os.path.normpath(plugin_path + '/lang') for x in os.listdir(localedir): try: if os.path.isfile("{localedir}/{lang}/LC_MESSAGES/{plugin}.po".format(localedir = localedir, lang = x, plugin = plugin.name.lower())) == True: plugin.lang.append(x) except: pass # Add items setattr(plugin, 'path', plugin_path) setattr(plugin, 'module', '.'.join(['lisa.plugins', plugin.name, 'modules', plugin.name.lower(), plugin.name])) # Save updated plugin plugin.save() # Update crons remove_list = list(Cron.objects(plugin = plugin)) if metadata.has_key('crons'): for cron_name, cron_item in metadata['crons'].iteritems(): # Get cron from DB cron = None cron_list = Cron.objects(plugin = plugin, name = cron_name) if cron_list is not None and len(cron_list) == 1: cron = cron_list[0] # It's a new cron if cron is None: # Create a new cron cron = Cron() new_cron= True else: # Remove cron from list of crons to remove remove_list.remove(cron) new_cron = False # Update cron for parameter in cron_item: if parameter != 'enabled': setattr(cron, parameter, cron_item[parameter]) # Delete older items d = cron.__dict__.copy() for k in d: if k.startswith("_") == False and k not in cron_item and k != 'enabled': delattr(cron, k) # Set enabled only for new crons if new_cron == True: if cron_item.has_key('enabled') == True and (cron_item['enabled'] == 0 or (type(cron_item['enabled']) == str and cron_item['enabled'].lower() == 'false')): setattr(cron, 'enabled', False) else: setattr(cron, 'enabled', True) # Add items setattr(cron, 'name', cron_name) # Connect cron to plugin cron.plugin = plugin # Save cron cron.save() # Delete older crons for this plugin for i in remove_list: i.delete() # Update intents remove_list = list(Intent.objects(plugin = plugin)) metadata_intents = None if metadata.has_key('configuration') and metadata['configuration'].has_key('intents'): metadata_intents = metadata['configuration']['intents'] if metadata.has_key('intents'): metadata_intents = metadata['intents'] if metadata_intents is not None: for wit_intent, intent_item in metadata_intents.iteritems(): # Get intent from DB intent = None intent_list = Intent.objects(plugin = plugin, name = wit_intent) if intent_list is not None and len(intent_list) == 1: intent = intent_list[0] # It's a new intent if intent is None: # Create a new intent intent = Intent() new_intent= True else: # Remove intent from list of intents to remove remove_list.remove(intent) new_intent = False # Update intent for parameter in intent_item: if parameter == 'method': setattr(intent, 'method_name', intent_item[parameter]) elif parameter == 'i_can': setattr(intent, parameter, intent_item[parameter]) # Delete older items d = intent.__dict__.copy() for k in d: if k.startswith("_") == False and k not in intent_item and k != 'enabled': delattr(intent, k) # Set enabled only for new intents if new_intent == True: setattr(intent, 'enabled', True) # Connect intent to plugin intent.plugin = plugin intent.plugin_name = plugin.name # Add items setattr(intent, 'name', wit_intent) # Save intent intent.save() # Delete older intents for this plugin for i in remove_list: i.delete()
def installPlugin(self, plugin_name=None, test_mode=False, dev_mode=False, version=None): version_str = "" if Plugin.objects(name=plugin_name): return {'status': 'fail', 'log': unicode(_('Plugin already installed'))} if version: version_str = ''.join(["==", version]) if not dev_mode: # This test mode is here only for travis to allow installing plugin in a readable directory if test_mode: pip.main(['install', '--quiet', '--install-option=--install-platlib=' + os.getcwd() + '/../', '--install-option=--install-purelib=' + os.getcwd() + '/../', 'lisa-plugin-' + plugin_name + version_str]) else: pip.main(['install', 'lisa-plugin-' + plugin_name + version_str]) jsonfile = self.pkgpath + '/' + plugin_name + '/' + plugin_name.lower() + '.json' metadata = json.load(open(jsonfile)) plugin = Plugin() description_list = [] for item in metadata: if item != 'crons' and item != 'rules': if item == 'description': for description in metadata[item]: oDescription = Description() for k,v in description.iteritems(): setattr(oDescription, k, v) description_list.append(oDescription) setattr(plugin, item, description_list) elif item == 'enabled': if metadata[item] == 0: setattr(plugin, item, False) else: setattr(plugin, item, True) else: setattr(plugin, item, metadata[item]) plugin.save() for item in metadata: if item == 'rules': for rule_item in metadata['rules']: rule = Rule() for parameter in rule_item: if parameter == 'enabled': if rule_item[parameter] == 0: setattr(rule, parameter, False) else: setattr(rule, parameter, True) else: setattr(rule, parameter, rule_item[parameter]) rule.plugin = plugin rule.save() if item == 'crons': for cron_item in metadata['crons']: cron = Cron() for parameter in cron_item: if parameter == 'enabled': if cron_item[parameter] == 0: setattr(cron, parameter, False) else: setattr(cron, parameter, True) else: setattr(cron, parameter, cron_item[parameter]) cron.plugin = plugin cron.save() for intent, value in metadata['configuration']['intents'].iteritems(): oIntent = Intent() oIntent.name = intent oIntent.function = value['method'] oIntent.module = '.'.join(['lisa.plugins', plugin_name, 'modules', plugin_name.lower(), plugin_name]) oIntent.enabled = True oIntent.plugin = plugin oIntent.save() return {'status': 'success', 'log': unicode(_('Plugin installed'))}
def upgradePlugin(self, plugin_name=None, plugin_pk=None, test_mode=False, dev_mode=False): if plugin_pk: plugin_list = Plugin.objects(pk=plugin_pk) else: plugin_list = Plugin.objects(name=plugin_name) if not plugin_list: return {'status': 'fail', 'log': unicode(_('Plugin not installed'))} if not dev_mode: # This test mode is here only for travis to allow installing plugin in a readable directory if test_mode: pip.main(['install', '--quiet', '--install-option=--install-platlib=' + os.getcwd() + '/../', '--install-option=--install-purelib=' + os.getcwd() + '/../', 'lisa-plugin-' + plugin_name, '--upgrade']) else: pip.main(['install', 'lisa-plugin-' + plugin_name, '--upgrade']) jsonfile = self.pkgpath + '/' + plugin_name + '/' + plugin_name.lower() + '.json' try: metadata = json.load(open(jsonfile)) except: return {'status': 'fail', 'log': unicode(_("The json of the plugin can't be loaded"))} for plugin in plugin_list: if self.versioncompare(version1=metadata['version'], version2=plugin.version) > 0: description_list = [] for item in metadata: if item != 'crons' and item != 'rules': if item == 'description': # Does we really need to update description object ? pass elif item == 'configuration': #TODO Shouldn't override the configuration of the user # But we should add only the missing entries (only adding) pass elif item == 'enabled': # Shouldn't override the choice of the user pass else: setattr(plugin, item, metadata[item]) plugin.save() for item in metadata: if item == 'rules': for rule_item in metadata['rules']: oRule_list = Rule.objects(name=rule_item['name']) for rule in oRule_list: for parameter in rule_item: if parameter == 'enabled': if rule_item[parameter] == 0: setattr(rule, parameter, False) else: setattr(rule, parameter, True) else: setattr(rule, parameter, rule_item[parameter]) rule.plugin = plugin rule.save() if item == 'crons': for cron_item in metadata['crons']: oCron_list = Cron.objects(name=cron_item['name']) for cron in oCron_list: for parameter in cron_item: if parameter == 'enabled': if cron_item[parameter] == 0: setattr(cron, parameter, False) else: setattr(cron, parameter, True) else: setattr(cron, parameter, cron_item[parameter]) cron.plugin = plugin cron.save() for intent, value in metadata['configuration']['intents'].iteritems(): oIntent_list = Intent.objects(name=intent) for oIntent in oIntent_list: oIntent.name = intent oIntent.function = value['method'] oIntent.module = '.'.join(['lisa.plugins', plugin_name, 'modules', plugin_name.lower(), plugin_name]) oIntent.enabled = True oIntent.plugin = plugin oIntent.save() else: return {'status': 'fail', 'log': unicode(_('Plugin already up to date'))} return {'status': 'success', 'log': unicode(_('Plugin upgraded'))}