Пример #1
0
 def makeService(self, options):
     """
     Construct the talkbackbot TCP client
     """
     if settings.USE_SSL:
         bot = internet.SSLClient(settings.HOST, settings.PORT,
                                  TalkBackBotFactory(settings),
                                  ssl.ClientContextFactory())
     else:
         bot = internet.TCPClient(settings.HOST, settings.PORT,
                                  TalkBackBotFactory(settings))
     return bot
    def startService(self):
        # construct a client & connect to server
        # nested import to avoid installing the default reactor
        from twisted.internet import reactor

        # assigns private var _bot to the passed-in parameter, bot
        def connected(bot):
            self._bot = bot

        #
        def failure(err):
            log.err(err, _why='Could not connect to specified server.')
            reactor.stop()

        responses = ResponsePicker()

        # define a client that constructs an endpoint based on a string with clientFromString function
        # clientFromString takes in the reactor that was imported and the endpoint (defined in settings.ini)
        client = clientFromString(reactor, self._endpoint)

        # instantiate TalkBackBotFactory defined in boy.py
        factory = TalkBackBotFactory(self._channel, self._nickname,
                                     self._realname, responses, self._triggers)

        # return client, connected to the endpoint with the factory variable
        # addCallBacks() takes a pair of functions of what happens on success/failure
        return client.connect(factory).addCallbacks(connected, failure)
Пример #3
0
    def test_init_with_url(self):
        settings = types.ModuleType('test_url_settings')
        settings.CHANNEL = "#inane"
        settings.QUOTES_URL = "https://example.com/api/v1/quotations/?limit=1"

        bot = TalkBackBotFactory(settings)

        self.assertEqual("https://example.com/api/v1/quotations/?limit=1",
                         bot.quotation.quotes_url)
Пример #4
0
 def setUp(self):
     factory = TalkBackBotFactory(self._channel, self._us, 'Jane Doe',
                                  FakePicker(QUOTE), ['twss'])
     self.bot = factory.buildProtocol(('127.0.0.1', 0))
     self.fake_transport = proto_helpers.StringTransport()
     self.bot.makeConnection(self.fake_transport)
     self.bot.signedOn()
     self.bot.joined(self.channel)
     self.fake_transport.clear()
Пример #5
0
    def test_init_with_file(self):
        settings = types.ModuleType('test_url_settings')
        settings.CHANNEL = "#inane"
        settings.QUOTES_FILE = os.path.join(os.getcwd(),
                                            "tests/test_quotes.txt")

        bot = TalkBackBotFactory(settings)

        expected = [
            ' A fool without fear is sometimes wiser than an angel with fear. ~ Nancy Astor\n',
            "    You don't manage people, you manage things. You lead people. ~ Grace Hopper"
        ]
        self.assertEqual(expected, bot.quotation.quotes)
Пример #6
0
    def test_init_no_quotes_source(self):
        settings = types.ModuleType('test_url_settings')
        settings.CHANNEL = "#inane"

        try:
            TalkBackBotFactory(settings)
            self.fail(
                "Should not be able to initialize bot with no QUOTES_FILE or QUOTES_URL"
            )
        except AttributeError as e:
            self.assertEqual(
                'Must specify either QUOTES_URL or QUOTES_FILE in settings',
                str(e))
Пример #7
0
 def setUp(self):
     # gets called to prepare our tests
     factory = TalkBackBotFactory(
         self._channel,
         self._us,
         "Jane Doe",
         FakePicker(QUOTE),
         ['twss'],  # trigger
     )
     # working with fake network connection, face IRC server
     self.bot = factory.buildProtocol(('127.0.0.1', 0))  # localhost, port 0
     self.fake_transport = proto_helpers.StringTransport()
     self.bot.makeConnection(self.fake_transport)
     self.bot.signedOn()
     self.bot.joined(self._channel)
     self.fake_transport.clear()
Пример #8
0
    def makeService(self, options):
        """Construct the talkbackbot service."""
        config = ConfigParser()
        config.read([options['config']])
        triggers = [
            trigger.strip()
            for trigger in config.get('talkback', 'triggers').split('\n')
            if trigger.strip()
        ]

        return TalkBackBotFactory(
            endpoint=config.get('irc', 'endpoint'),
            channel=config.get('irc', 'channel'),
            nickname=config.get('irc', 'nickname'),
            realname=config.get('irc', 'realname'),
            quotesFilename=config.get('talkback', 'quotesFilename'),
            triggers=triggers,
        )
Пример #9
0
    def startService(self):
        from twisted.internet import reactor

        def connected(bot):
            self._bot = bot

        def failure(err):
            log.err(err, _why='Could not connect to specified server.')
            reactor.stop()

        quotes = QuotePicker(self._quotesFilename)
        client = clientFromString(reactor, self._endpoint)
        factory = TalkBackBotFactory(
            self._channel,
            self._nickname,
            self._realname,
            quotes,
            self._triggers,
        )

        return client.connect(factory).addCallbacks(connected, failure)
Пример #10
0
    def startService(self):
        """Construct a client & connect to server."""
        from twisted.internet import reactor
        '''If you import twisted.internet.reactor without first installing 
        a specific reactor implementation, then Twisted will install
         the default reactor for you. The particular one you get will 
         depend on the operating system and Twisted version you are using. 
         For that reason, it is general practice not to import the reactor 
         at the top level of modules to avoid accidentally installing 
         the default reactor. Instead, import the reactor in the same scope 
         in which you use it.'''
        ''' The event loop is a programming construct that waits for and 
        dispatches events or messages in a program. It works by calling some 
        internal or external “event provider”, which generally blocks 
        until an event has arrived, and then calls the relevant event handler 
        (“dispatches the event”). The reactor provides basic interfaces 
        to a number of services, including network communications, threading, 
        and event dispatching.'''

        def connected(bot):
            self._bot = bot

        def failure(err):
            log.err(err, _why="Could not connect to specified server.")
            reactor.stop()

        quotes = QuotePicker(self._quotesFilename)
        client = clientFromString(reactor, self._endpoint)
        factory = TalkBackBotFactory(
            self._channel,
            self._nickname,
            self._realname,
            quotes,
            self._triggers,
        )
        return client.connect(factory).addCallbacks(connected, failure)
Пример #11
0
 def setUp(self):
     super(TestTalkBackBot, self).setUp()
     self.factory = TalkBackBotFactory(test_settings)
     self.bot = self.factory.buildProtocol(None)
     self.bot.msg = mock.MagicMock()
     self.bot.join = mock.MagicMock()