示例#1
0
文件: ajax.py 项目: pannal/XDM
    def pluginCall(self, **kwargs):
        log("Plugin ajay call with: %s" % kwargs)
        p_type = kwargs['p_type']
        p_instance = kwargs['p_instance']
        action = kwargs['action']
        p = common.PM.getInstanceByName(p_type, p_instance)
        p_function = getattr(p, action)
        fn_args = []
        if hasattr(p_function, 'args'):
            for name in p_function.args:
                log('function %s needs %s' % (action, name))
                field_name = 'field_%s' % name
                if field_name in kwargs:
                    fn_args.append(convertV(kwargs[field_name]))
                    continue
                else:
                    log("Field '%s' not found in kwargs. tring array" % field_name)
                field_name = 'field_%s[]' % name
                if field_name in kwargs:
                    fn_args.append(convertV(kwargs[field_name]))
                else:
                    log.warning("Field %s not found in kwargs. this will probably not work out" % field_name)

        try:
            log("calling %s with %s" % (p_function, fn_args))
            status, data, msg = p_function(*fn_args)
        except Exception as ex:
            tb = traceback.format_exc()
            log.error("Error during %s of %s(%s) \nError: %s\n\n%s\n" % (action, p_type, p_instance, ex, tb))
            return json.dumps({'result': False, 'data': {}, 'msg': 'Internal Error in plugin'})
        return json.dumps({'result': status, 'data': data, 'msg': msg})
示例#2
0
    def pluginCall(self, **kwargs):
        log("Plugin ajax call with: %s" % kwargs)
        p_type = kwargs.get('p_type')
        p_identifier = kwargs.get("p_identifier")
        p_instance = kwargs.get('p_instance')
        action = kwargs['action']
        p = None
        if p_type:
            p = common.PM.getInstanceByName(p_type, p_instance)
        elif p_identifier:
            p = common.PM.getPluginByIdentifier(p_identifier, p_instance)
        if p is None:
            return json.dumps({
                'result': False, 'data': {},
                'msg': 'Plugin not found {} {} {}'.format(p_type, p_identifier, p_instance)})

        if "." in action:
            last_attr = None
            for attr in action.split("."):
                if last_attr is None:
                    last_attr = getattr(p, attr)
                else:
                    last_attr = getattr(last_attr, attr)
            p_function = last_attr
        else:
            p_function = getattr(p, action)
        fn_args = []
        if hasattr(p_function, 'args'):
            for name in p_function.args:
                log('function %s needs %s' % (action, name))
                field_name = 'field_%s' % name
                if field_name in kwargs:
                    fn_args.append(convertV(kwargs[field_name]))
                    continue
                else:
                    log("Field '%s' not found in kwargs. tring array" % field_name)
                field_name = 'field_%s[]' % name
                if field_name in kwargs:
                    fn_args.append(convertV(kwargs[field_name]))
                else:
                    log.warning("Field %s not found in kwargs. this will probably not work out" % field_name)

        try:
            log("calling %s with %s" % (p_function, fn_args))
            status, data, msg = p_function(*fn_args)
        except Exception as ex:
            tb = traceback.format_exc()
            log.error("Error during %s of %s(%s) \nError: %s\n\n%s\n" % (action, p_type, p_instance, ex, tb))
            return json.dumps({'result': False, 'data': {}, 'msg': 'Internal Error in plugin'})
        return json.dumps({'result': status, 'data': data, 'msg': msg})
示例#3
0
    def pluginCall(self, **kwargs):
        log("Plugin ajay call with: %s" % kwargs)
        p_type = kwargs['p_type']
        p_instance = kwargs['p_instance']
        action = kwargs['action']
        p = common.PM.getInstanceByName(p_type, p_instance)
        p_function = getattr(p, action)
        fn_args = []
        if hasattr(p_function, 'args'):
            for name in p_function.args:
                log('function %s needs %s' % (action, name))
                field_name = 'field_%s' % name
                if field_name in kwargs:
                    fn_args.append(convertV(kwargs[field_name]))
                    continue
                else:
                    log("Field '%s' not found in kwargs. tring array" %
                        field_name)
                field_name = 'field_%s[]' % name
                if field_name in kwargs:
                    fn_args.append(convertV(kwargs[field_name]))
                else:
                    log.warning(
                        "Field %s not found in kwargs. this will probably not work out"
                        % field_name)

        try:
            log("calling %s with %s" % (p_function, fn_args))
            status, data, msg = p_function(*fn_args)
        except Exception as ex:
            tb = traceback.format_exc()
            log.error("Error during %s of %s(%s) \nError: %s\n\n%s\n" %
                      (action, p_type, p_instance, ex, tb))
            return json.dumps({
                'result': False,
                'data': {},
                'msg': 'Internal Error in plugin'
            })
        return json.dumps({'result': status, 'data': data, 'msg': msg})
示例#4
0
    def rest(self, *args, **kwargs):
        identifier = args[0]
        instance = args[1]
        method = args[2]

        p = common.PM.getPluginByIdentifier(identifier, instance)
        if p is None:
            cherrypy.response.status = 501
            return "No plugin with identifier %s and instance %s found." % (identifier, instance)

        p_function = getattr(p, "_%s" % method)
        fn_args = []
        if not (hasattr(p_function, 'rest') and p_function.rest):
            return "no such method"

        if hasattr(p_function, 'args'):
            for name in p_function.args:
                log('function %s needs %s' % (method, name))
                if name in kwargs:
                    fn_args.append(convertV(kwargs[name]))

        try:
            log("calling %s with %s" % (p_function, fn_args))
            output = p_function(*fn_args)
        except Exception as ex:
            tb = traceback.format_exc()
            log.error("Error during %s of %s(%s) \nError: %s\n\n%s\n" % (method, identifier, instance, ex, tb))
            cherrypy.response.status = 500
            return json.dumps({'result': False, 'data': {}, 'msg': 'Internal Error in plugin'})
        if isinstance(output, dict):
            cherrypy.response.headers['Content-Type'] = 'application/json'
            return json.dumps(output)
        elif isinstance(output, tuple):
            if len(output) == 2:
                headers = output[1]
                for header_name, header_value in headers.items():
                    cherrypy.response.headers[header_name] = header_value
                return output[0]
            elif len(output) == 3:
                cherrypy.response.headers['Content-Type'] = 'application/json'
                return json.dumps({'result': output[0], 'data': output[1], 'msg': output[2]})
        return output



        print args
        print kwargs

        return "blupp"
示例#5
0
文件: ajax.py 项目: pannal/XDM
    def _save(self, **kwargs):
        actions = {}
        # print kwargs
        if 'saveOn' in kwargs:
            del kwargs['saveOn']

        element = None
        if 'element_id' in kwargs:
            element = Element.get(Element.id == kwargs['element_id'])
            del kwargs['element_id']

        # this is slow !!
        # because i create each plugin for each config value that is for that plugin
        # because i need the config_meta from the class to create the action list
        # but i can use the plugins own c obj for saving the value
        _plugin_cache = {} # first try to make it faster is by using a cache for each plugin instance
        for k, v in kwargs.items():
            try:
                k = k.decode('utf-8')
            except UnicodeDecodeError:
                k = k.decode('latin-1') # for some obscure reason cherrypy (i think) encodes param key into latin-1 some times
                k = k.encode('utf-8') # but i like utf-8

            log(u"config K:%s V:%s" % (k, v))
            parts = k.split('-')
            # print parts
            # parts[0] plugin class name
            # parts[1] plugin instance name
            # parts[2] config name
            # v value for config -> parts[2]
            class_name = parts[0]
            instance_name = parts[1]
            config_name = parts[2]
            config_type = parts[3] if len(parts) > 3 else None
            _cacheName = "%s %s" % (class_name, instance_name)
            if _cacheName in _plugin_cache:
                plugin = _plugin_cache[_cacheName]
            else:
                plugin = common.PM.getInstanceByName(class_name, instance_name)
                _plugin_cache[_cacheName] = plugin
            if plugin:
                log(u"We have a plugin: %s (%s)" % (class_name, instance_name))
                new_value = helper.convertV(v, config_type)
                if element is None: # normal settings page
                    old_value = getattr(plugin.c, config_name)
                    new_value = helper.convertV(v, config_type)
                    if old_value == new_value:
                        continue
                if element is not None: # we got an element id so its an element config
                    cur_c = plugin.e.getConfig(config_name, element)
                    if cur_c.value == new_value:
                        continue
                    log.debug("setting element config. %s K:%s, V:%s" % (element, config_name, new_value))
                    cur_c.element = element
                    cur_c.value = new_value
                    cur_c.save()
                else: # normal settings page
                    setattr(plugin.c, config_name, convertV(v, config_type)) # saving value

                if plugin.config_meta[config_name] and element is None: # this returns none
                    if 'on_change_actions' in plugin.config_meta[config_name] and old_value != new_value:
                        actions[plugin] = plugin.config_meta[config_name]['on_change_actions'] # this is a list of actions
                    if 'actions' in plugin.config_meta[config_name]:
                        actions[plugin] = plugin.config_meta[config_name]['actions'] # this is a list of actions
                    if 'on_enable' in plugin.config_meta[config_name] and new_value:
                        actions[plugin] = plugin.config_meta[config_name]['on_enable'] # this is a list of actions
                elif plugin.elementConfig_meta[config_name] and element is not None:
                    pass
                continue
            else: # no plugin with that class_name or instance found
                log(u"We don't have a plugin: %s (%s)" % (class_name, instance_name))
                continue

        if element is None:
            common.PM.reinstanceiate()
            # This is provate find a better place to the cache
            common._provider_tags_cache = []
        final_actions = {}
        for cur_class_name, cur_actions in actions.items():
            for cur_action in cur_actions:
                if not cur_action in final_actions:
                    final_actions[cur_action] = []
                final_actions[cur_action].append(cur_class_name)
        for action, plugins_that_called_it  in final_actions.items():
            actionManager.executeAction(action, plugins_that_called_it)

        return json.dumps({'result': True, 'data': {}, 'msg': 'Configuration saved.'})
示例#6
0
    def _save(self, **kwargs):
        actions = {}
        # print kwargs
        if 'saveOn' in kwargs:
            del kwargs['saveOn']

        element = None
        if 'element_id' in kwargs:
            element = Element.get(Element.id == kwargs['element_id'])
            del kwargs['element_id']

        # this is slow !!
        # because i create each plugin for each config value that is for that plugin
        # because i need the config_meta from the class to create the action list
        # but i can use the plugins own c obj for saving the value
        _plugin_cache = {
        }  # first try to make it faster is by using a cache for each plugin instance
        for k, v in kwargs.items():
            try:
                k = k.decode('utf-8')
            except UnicodeDecodeError:
                k = k.decode(
                    'latin-1'
                )  # for some obscure reason cherrypy (i think) encodes param key into latin-1 some times
                k = k.encode('utf-8')  # but i like utf-8
            # print k, repr(k)
            # print v, repr(v)

            log(u"config K:%s V:%s" % (k, v))
            parts = k.split('-')
            # print parts
            # parts[0] plugin class name
            # parts[1] plugin instance name
            # parts[2] config name
            # v value for config -> parts[2]
            class_name = parts[0]
            instance_name = parts[1]
            config_name = parts[2]
            _cacheName = "%s %s" % (class_name, instance_name)
            if _cacheName in _plugin_cache:
                plugin = _plugin_cache[_cacheName]
            else:
                plugin = common.PM.getInstanceByName(class_name, instance_name)
                _plugin_cache[_cacheName] = plugin
            if plugin:
                log(u"We have a plugin: %s (%s)" % (class_name, instance_name))
                new_value = helper.convertV(v)
                if element is None:  # normal settings page
                    old_value = getattr(plugin.c, config_name)
                    new_value = helper.convertV(v)
                    if old_value == new_value:
                        continue
                if element is not None:  # we got an element id so its an element config
                    cur_c = plugin.e.getConfig(config_name, element)
                    if cur_c.value == new_value:
                        continue
                    log.debug("setting element config. %s K:%s, V:%s" %
                              (element, config_name, new_value))
                    cur_c.element = element
                    cur_c.value = new_value
                    cur_c.save()
                else:  # normal settings page
                    setattr(plugin.c, config_name, convertV(v))  # saving value

                if plugin.config_meta[
                        config_name] and element is None:  # this returns none
                    if 'on_change_actions' in plugin.config_meta[
                            config_name] and old_value != new_value:
                        actions[plugin] = plugin.config_meta[config_name][
                            'on_change_actions']  # this is a list of actions
                    if 'actions' in plugin.config_meta[config_name]:
                        actions[plugin] = plugin.config_meta[config_name][
                            'actions']  # this is a list of actions
                    if 'on_enable' in plugin.config_meta[
                            config_name] and new_value:
                        actions[plugin] = plugin.config_meta[config_name][
                            'on_enable']  # this is a list of actions
                elif plugin.elementConfig_meta[
                        config_name] and element is not None:
                    pass
                continue
            else:  # no plugin with that class_name or instance found
                log(u"We don't have a plugin: %s (%s)" %
                    (class_name, instance_name))
                continue

        if element is None:
            common.PM.reinstanceiate()
            # This is provate find a better place to the cache
            common._provider_tags_cache = []
        final_actions = {}
        for cur_class_name, cur_actions in actions.items():
            for cur_action in cur_actions:
                if not cur_action in final_actions:
                    final_actions[cur_action] = []
                final_actions[cur_action].append(cur_class_name)
        for action, plugins_that_called_it in final_actions.items():
            actionManager.executeAction(action, plugins_that_called_it)

        return json.dumps({
            'result': True,
            'data': {},
            'msg': 'Configuration saved.'
        })