def run_replacer(val):
    '''
    If list given calls run_replacer on each element of string.
    
    If string replaces the match_pattern given in config with wrapping_links given in config.
    
    Otherwise calls recursive_wrap if it is a list of dict.
    '''
    if isinstance(val, list):
        new_val = map(run_replacer, val)
        return new_val
    elif isinstance(val, str) or isinstance(val, unicode):
        val = val.replace(Server.get_config()["Search"]["match_pattern"], Server.get_config()["Search"]["replace_with"])
        return Server.get_config()["Search"]["wrapping_link"] + val
    else:
        return recursive_wrap(val)
def recursive_wrap(data):
    '''
    Recursively goes through the whole structure(dict or list), searching for the keys specified in link_fields in the config.
    If found calls run_replacer.
    
    If data is neither list nor dict returns the data.
    '''
    if isinstance(data, dict):
        for key, value in data.iteritems():
            data[key] = run_replacer(value) if key in Server.get_config()["Search"]["link_fields"] else recursive_wrap(value)  
        return data
    elif isinstance(data, list):
        new_data = map(recursive_wrap, data)
        return new_data
    else:
        return data
Example #3
0
 def wrapper(*args, **kargs):
     url = bottle.request.GET.get('url', '#') #@UndefinedVariable
     logging.debug("URL:%s",url)
     url = url.replace(Server.get_config()["Search"]["replace_with"], Server.get_config()["Search"]["match_pattern"])
     return func(*args, url=url, **kargs)