예제 #1
0
    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()
예제 #2
0
    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()