示例#1
0
def view_switch(view, args):
    """
    General method which implements view logic
    :param view:        str, view to construct a response for
    :param args:        view arguments passed along
    :return:            text response from databayes or error
    """

    log.debug('Processing view: "{0}"'.format(view))
    log.debug('Processing args: "{0}"'.format(str(args)))

    query_param_obj = unpack_query_params(request)
    if (not query_param_obj['ok']):
        return Response(json.dumps([query_param_obj['message']]),
                        mimetype='application/json')

    # Retrieve a valid queue item
    qid = handle_queue_validation()
    if qid == -1:
        return Response(json.dumps(['Queue is full, try again later.']),
                        mimetype='application/json')

    # Construct command
    cmd = ""
    if view == 'define_entity':
        if 'values' in query_param_obj.keys() and \
                        'fields' in query_param_obj.keys():
            arg_str = get_arg_str(query_param_obj['fields'],
                                  query_param_obj['values'], '_')
        else:
            arg_str = ""
            log.info('Warning: entity has no attributes')
        cmd = 'def {0}({1})'.format(args['entity'], arg_str) \
            if arg_str else 'def ' + str(args['entity'])

    elif view == 'add_relation':
        arg_str_1 = get_arg_str(query_param_obj['fields1'], query_param_obj['values1'], '=')
        arg_str_2 = get_arg_str(query_param_obj['fields2'], query_param_obj['values2'], '=')
        cmd = 'add rel {0}({1}) {2}({3})'.format(args['entity_1'], arg_str_1,
                                                 args['entity_2'], arg_str_2)

    elif view == 'generate':
        pass

    elif view == 'list_entity':
        cmd = 'lst ent {0}'.format(args['pattern'])

    elif view == 'list_relation':
        arg_str_1 = get_arg_str(query_param_obj['fields1'], query_param_obj['values1'], '=')
        arg_str_2 = get_arg_str(query_param_obj['fields2'], query_param_obj['values2'], '=')
        cmd = 'lst rel {0}({1}) {2}({3})'.format(args['entity_1'], arg_str_1,
                                                 args['entity_2'], arg_str_2)

    elif view == 'remove_entity':
        cmd = 'rm ent {0}'.format(args['entity'])

    elif view == 'remove_relation':
        arg_str_1 = get_arg_str(query_param_obj['fields1'], query_param_obj['values1'], '=')
        arg_str_2 = get_arg_str(query_param_obj['fields2'], query_param_obj['values2'], '=')
        cmd = 'rm rel {0}({1}) {2}({3})'.format(args['entity_1'], arg_str_1,
                                                args['entity_2'], arg_str_2)

    log.info('sending command: "{0}"'.format(cmd))

    # Send cmd to databayes daemon
    redisio.DataIORedis().connect()
    redisio.DataIORedis().write(config.DBY_CMD_QUEUE_PREFIX + qid, cmd)

    # check response
    rsp = wait_for_response(qid)
    if not rsp:
        rsp = "Could not find response before max retires expired."

    return rsp
示例#2
0
def init_views():
    for key in route_deco:
        log.info('Registering view - {0}'.format(key))
        route = route_deco[key]
        view_method = view_list[key]
        view_list[key] = route(view_method)
示例#3
0
        args, 'silent') and hasattr(args, 'quiet'):
        level = logging.WARNING - ((args.verbose - args.quiet) * 10)
        if args.silent:
            level = logging.CRITICAL + 1
    else:
        level = logging.INFO
    log_set_level(level)

    # Apply routing & auth deco to views
    init_views()

    if args.vagrant:
        host = config.__instance_host_vagrant__
    else:
        host = config.__instance_host__
    log.info('host = ' + host)

    app.run(debug=args.debug,
            use_reloader=args.reloader,
            host=host,
            port=config.__instance_port__,)

else:
    # Invocation by apache mod_wsgi

    # Setup file logger, flask context exceptions will be
    # written to the handler
    #

    from logging import FileHandler, Formatter
    log = FileHandler(config.FLASK_LOG)