<commit_before>from requests import Request, Session
from settings import KANNEL_SERVERS, DEFAULT_KANNEL_SERVER

def compose_request(msg = {}, server = DEFAULT_KANNEL_SERVER):
    '''composes a proper Request using the given msg and kannel server details'''
    data = {
            'username' : server['username'],
            'password' : server['password'],
            'from'     : msg['from'],
            'to'       : msg['to'],
            'text'     : msg['text'],
    }

    url = "http://%s:%d/%s" % (server['host'],server['port'],server['path']);
    
    #return a prepared Request
    app.logger
    r = Request('GET', url, data = data).prepare()
    return r

def send_to_kannel(app, msg = {}, preferred_kannel_server = None):
    '''sends a given messages to the _RIGHT_ kannel server'''
    server = None
    if preferred_kannel_server is not None:
        try:
            server = KANNEL_SERVERS[preferred_kannel_server.lower()] #locate using ip
        except KeyError as ke:
            server = KANNEL_SERVERS['DEFAULT_KANNEL_SERVER']
    else: #no preferred server was given, select the proper one based on the recipient number
    
        for s,s_info in KANNEL_SERVERS.items():
            prefixes = s_info['series']
            app.logger.debug("Server %s supports all numbers with the prefixes %s", (s, prefixes))
            for p in prefixes:
                recipient = msg['from'].strip('+') 
                app.logger.debug("Trying to match %s with prefix %s ", (recipient, p))
                if recipient.startswith(p): #this is our number series
                    server = s_info
                    app.logger.debug("Selected server %s with prefix (%s) matching with recipient number %s", (server, prefixes,p))
                    break;

            if server is not None: #we have found our server!
                break;


    if server is None:
        app.logger.error("Could not select any server for forwarding message! Check logs.")
    return False

    #compose the complete Request with URL and data for sending sms
    session = Session()

    request = compose_request(msg, server)
    app.logger.notice("Calling %s with data %s", (request.url, request.body))
    response = session.send(request)

    print response.status_code
    print response.text
    app.logger.info("Received response code %s with text %s", (response.status_code, response.text))

    return True
    #call it.
<commit_msg>Fixed string formatting in the debug statements<commit_after>from requests import Request, Session
from settings import KANNEL_SERVERS, DEFAULT_KANNEL_SERVER

def compose_request(msg = {}, server = DEFAULT_KANNEL_SERVER):
    '''composes a proper Request using the given msg and kannel server details'''
    data = {
            'username' : server['username'],
            'password' : server['password'],
            'from'     : msg['from'],
            'to'       : msg['to'],
            'text'     : msg['text'],
    }

    url = "http://%s:%d/%s" % (server['host'],server['port'],server['path']);
    
    #return a prepared Request
    app.logger
    r = Request('GET', url, data = data).prepare()
    return r

def send_to_kannel(app, msg = {}, preferred_kannel_server = None):
    '''sends a given messages to the _RIGHT_ kannel server'''
    server = None
    if preferred_kannel_server is not None:
        try:
            server = KANNEL_SERVERS[preferred_kannel_server.lower()] #locate using ip
        except KeyError as ke:
            server = KANNEL_SERVERS['DEFAULT_KANNEL_SERVER']
    else: #no preferred server was given, select the proper one based on the recipient number
    
        for s,s_info in KANNEL_SERVERS.items():
            prefixes = s_info['series']
            app.logger.debug("Server %s supports all numbers with the prefixes %s", s, prefixes)
            for p in prefixes:
                recipient = msg['from'].strip('+') 
                app.logger.debug("Trying to match %s with prefix %s ", recipient, p)
                if recipient.startswith(p): #this is our number series
                    server = s_info 
                    app.logger.debug("Selected server %s with prefix (%s) matching with recipient number %s", server, prefixes,recipient)
                    break;

            if server is not None: #we have found our server!
                break;


    if server is None:
        app.logger.error("Could not select any server for forwarding message! Check logs.")
    return False

    #compose the complete Request with URL and data for sending sms
    session = Session()

    request = compose_request(msg, server)
    app.logger.notice("Calling %s with data %s", request.url, request.body)
    response = session.send(request)

    print response.status_code
    print response.text
    app.logger.info("Received response code %s with text %s", response.status_code, response.text)

    return True
    #call it.

{'fraction_non_alphanumeric': 0.10500709507399149, 'fraction_numerical': 0.0, 'mean_word_length': 2.499290780141844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '279610', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1323, 'n_words': 454}