<commit_before>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4


import time
import pygsm

from rapidsms.message import Message
from rapidsms.connection import Connection
from rapidsms.backends import Backend


# number of seconds to wait between
# polling the modem for incoming SMS
POLL_INTERVAL = 10


class Backend(Backend):
    _title = "pyGSM"


    def configure(self, *args, **kwargs):
        self.modem = None
        self.modem_args = args
        self.modem_kwargs = kwargs


    def send(self, message):
        self.modem.send_sms(
            str(message.connection.identity),
            message.text)


    def run(self):
        while self._running:
            msg = self.modem.next_message()
        
            if msg is not None:
                # we got an sms! create RapidSMS Connection and
                # Message objects, and hand it off to the router
                c = Connection(self, msg.sender)
                m = Message(c, msg.text)
                self.router.send(m)
            
            # poll for new messages
            # every POLL_INTERVAL seconds
            time.sleep(POLL_INTERVAL)


    def start(self):
        self.modem = pygsm.GsmModem(
            *self.modem_args,
            **self.modem_kwargs)

        # If we got the connection, call superclass to
        # start the run loop--it just sets self._running to True
        # and calls run.
        if self.modem is not None:
            Backend.start(self)


    def stop(self):
        # call superclass to stop--sets self._running
        # to False so that the 'run' loop will exit cleanly.
        backend.Backend.stop(self)

        # disconnect from modem
        if self.modem is not None:
            self.modem.disconnect()<commit_msg>added AT logging and better shutdown to gsm backend<commit_after>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4


import time
import pygsm

from rapidsms.message import Message
from rapidsms.connection import Connection
from rapidsms.backends import Backend as BackendBase


# number of seconds to wait between
# polling the modem for incoming SMS
POLL_INTERVAL = 10


class Backend(BackendBase):
    _title = "pyGSM"


    def configure(self, *args, **kwargs):
        self.modem = None
        self.modem_args = args
        self.modem_kwargs = kwargs


    def send(self, message):
        self.modem.send_sms(
            str(message.connection.identity),
            message.text)


    def gsm_log(self, modem, str, level):
        self.debug("%s: %s" % (level, str))
    
    
    def run(self):
        while self.running:
            self.info("Polling modem for messages")
            msg = self.modem.next_message()
        
            if msg is not None:
                # we got an sms! create RapidSMS Connection and
                # Message objects, and hand it off to the router
                c = Connection(self, msg.sender)
                m = Message(c, msg.text)
                self.router.send(m)
            
            # wait for POLL_INTERVAL seconds before continuing
            # (in a slightly bizarre way, to ensure that we abort
            # as soon as possible when the backend is asked to stop)
            for n in range(0, POLL_INTERVAL):
                if not self.running: return None
                time.sleep(1)


    def start(self):
        self.modem = pygsm.GsmModem(
            logger=self.gsm_log,
            *self.modem_args,
            **self.modem_kwargs)
        
        # If we got the connection, call superclass to
        # start the run loop--it just sets self._running to True
        # and calls run.
        if self.modem is not None:
            BackendBase.start(self)


    def stop(self):
        
        # call superclass to stop--sets self._running
        # to False so that the 'run' loop will exit cleanly.
        BackendBase.stop(self)

        # disconnect from modem
        if self.modem is not None:
            self.modem.disconnect()
{'fraction_non_alphanumeric': 0.07700025081514923, 'fraction_numerical': 0.0030097817908201654, 'mean_word_length': 1.8384341637010677, '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': '4094273', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1123, 'n_words': 373}