<commit_before>import copy
import urllib
import urllib2

from rapidsms.backends.base import BackendBase


class KannelBackend(BackendBase):
    """Backend for use with the Kannel SMS Gateway."""

    def configure(self, sendsms_url='http://127.0.0.1:13013/cgi-bin/sendsms',
                  sendsms_params=None, charset=None, coding=None,
                  encode_errors=None, **kwargs):
        self.sendsms_url = sendsms_url
        self.sendsms_params = sendsms_params or {}
        self.charset = charset or 'ascii'
        self.coding = coding or 0
        self.encode_errors = encode_errors or 'ignore'

    def prepare_message(self, text, identities):
        """Prepare URL query string with message context."""
        query = copy.copy(self.sendsms_params)
        query['to'] = ' '.join(identities)
        query['text'] = text.encode(self.charset, self.encode_errors)
        query['coding'] = self.coding
        query['charset'] = self.charset
        return query

    def send(self, id_, text, identities, context):
        """Open request to Kannel instance."""
        self.info('Sending message: %s' % text)
        url_args = self.prepare_message(text, identities)
        url = '?'.join([self.sendsms_url, urllib.urlencode(url_args)])
        try:
            self.debug('Opening URL: %s' % url)
            response = urllib2.urlopen(url)
        except:
            self.exception('Failed to send message')
            return
        self.info('SENT')
        self.debug('response body: %s' % response.read())
        return True<commit_msg>let exception raise so database router can log the error<commit_after>import copy
import urllib
import urllib2

from rapidsms.backends.base import BackendBase


class KannelBackend(BackendBase):
    """Backend for use with the Kannel SMS Gateway."""

    def configure(self, sendsms_url='http://127.0.0.1:13013/cgi-bin/sendsms',
                  sendsms_params=None, charset=None, coding=None,
                  encode_errors=None, **kwargs):
        self.sendsms_url = sendsms_url
        self.sendsms_params = sendsms_params or {}
        self.charset = charset or 'ascii'
        self.coding = coding or 0
        self.encode_errors = encode_errors or 'ignore'

    def prepare_message(self, text, identities):
        """Prepare URL query string with message context."""
        query = copy.copy(self.sendsms_params)
        query['to'] = ' '.join(identities)
        query['text'] = text.encode(self.charset, self.encode_errors)
        query['coding'] = self.coding
        query['charset'] = self.charset
        return query

    def send(self, id_, text, identities, context):
        """Open request to Kannel instance."""
        self.info('Sending message: %s' % text)
        url_args = self.prepare_message(text, identities)
        url = '?'.join([self.sendsms_url, urllib.urlencode(url_args)])
        self.debug('Opening URL: %s' % url)
        response = urllib2.urlopen(url)
        self.info('SENT')
        self.debug('response body: %s' % response.read())
        return True
{'fraction_non_alphanumeric': 0.11847258485639686, 'fraction_numerical': 0.009138381201044387, 'mean_word_length': 2.6927710843373496, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4024358', 'n_tokens_mistral': 950, 'n_tokens_neox': 901, 'n_words': 221}