def make_plugin(application, app_config): slack_config = config.parse_config(app_config, { "token": config.String, }) api_client = SlackWebClient(slack_config.token) endpoint = SlackEndpoint(api_client) plugin = SlackPlugin(api_client) factory = SlackClientFactory( plugin=plugin, useragent="Harold ([email protected])", ) factory.setProtocolOptions( autoPingInterval=5, autoPingTimeout=10, ) service = ClientService(endpoint, factory) service.setServiceParent(application) return plugin
class ClientSessionService(MultiService): ''' A service that maintains a connection to a hpfeeds broker and provides helpers for reading and writing to the broker. ''' def __init__(self, endpoint, ident, secret, retryPolicy=None): super(ClientSessionService, self).__init__() self.ident = ident self.read_queue = defer.DeferredQueue() self.subscriptions = set() self.protocol = None from twisted.internet import reactor if isinstance(endpoint, str): self.client_endpoint = clientFromString(reactor, endpoint) elif hasattr(endpoint, 'connect'): self.client_endpoint = endpoint else: raise ValueError( 'endpoint must be a str or implement IStreamClientEndpoint') self.client_factory = ClientFactory.forProtocol( _Protocol, ident, secret) self.client_factory.service = self self.client_service = ClientService( self.client_endpoint, self.client_factory, retryPolicy=retryPolicy, ) self.client_service.setServiceParent(self) self.whenConnected = defer.Deferred() def publish(self, chan, payload): if self.protocol: self.protocol.publish(self.ident, chan, payload) def subscribe(self, topic): if topic not in self.subscriptions: self.subscriptions.add(topic) if self.protocol: self.protocol.subscribe(self.ident, topic) def unsubscribe(self, topic): if topic in self.subscriptions: self.subscriptions.discard(topic) if self.protocol: self.protocol.unsubscribe(self.ident, topic) def read(self): ''' Returns a Deferred which fires with the next available message received from the broker. If a message has already been received and is queue the deferred will fire immediately. ''' return self.read_queue.get() def __enter__(self): raise TypeError("Use async with instead") def __exit__(self, exc_type, exc_val, exc_tb): pass if sys.version_info[0] > 2: import asyncio @asyncio.coroutine @defer.inlineCallbacks def __aenter__(self): self.startService() yield self.whenConnected defer.returnValue(self) @asyncio.coroutine @defer.inlineCallbacks def __aexit__(self, exc_type, exc_val, exc_tb): yield self.stopService() @asyncio.coroutine @defer.inlineCallbacks def __aiter__(self): defer.returnValue(self) @asyncio.coroutine @defer.inlineCallbacks def __anext__(self): if not self.running: raise StopAsyncIteration() message = yield self.read() defer.returnValue(message)
'--config', help='Configuration file', default='config.json') parser.add_argument('-d', '--database', help='Database file', default='forkingdongles.db') args = parser.parse_args() app = Application('ForkingDongles') config = JSONConfig(args.config, default={ 'core': { 'host': 'irc.example.com', 'port': 6667, 'ssl': False, 'nickname': 'ForkingDongles', 'channels': [] } }) db = ConnectionPool('sqlite3', args.database) log.startLogging(sys.stderr) uri = '{}:{}:{}'.format('ssl' if config['core']['ssl'] else 'tcp', config['core']['host'], config['core']['port']) endpoint = endpoints.clientFromString(reactor, uri) factory = ForkingDonglesFactory(config, db) service = ClientService(endpoint, factory) service.setServiceParent(app) service.startService() reactor.run()