<s> from __future__ import absolute_import <EOL> from zope . interface import implementer <EOL> from twisted . plugin import IPlugin <EOL> from twisted . internet . interfaces import IStreamServerEndpointStringParser , IStreamServerEndpoint , IStreamClientEndpoint <EOL> try : <EOL> from twisted . internet . interfaces import IStreamClientEndpointStringParserWithReactor <EOL> _HAS_REACTOR_ARG = True <EOL> except ImportError : <EOL> _HAS_REACTOR_ARG = False <EOL> from twisted . internet . interfaces import IStreamClientEndpointStringParser as IStreamClientEndpointStringParserWithReactor <EOL> from twisted . internet . endpoints import serverFromString , clientFromString <EOL> from autobahn . twisted . websocket import WrappingWebSocketServerFactory , WrappingWebSocketClientFactory <EOL> def _parseOptions ( options ) : <EOL> opts = { } <EOL> if '<STR_LIT:url>' not in options : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> opts [ '<STR_LIT:url>' ] = options [ '<STR_LIT:url>' ] <EOL> if '<STR_LIT>' in options : <EOL> value = options [ '<STR_LIT>' ] . lower ( ) . strip ( ) <EOL> if value == '<STR_LIT:true>' : <EOL> opts [ '<STR_LIT>' ] = True <EOL> elif value == '<STR_LIT:false>' : <EOL> opts [ '<STR_LIT>' ] = False <EOL> else : <EOL> raise Exception ( "<STR_LIT>" . format ( value ) ) <EOL> if '<STR_LIT>' in options : <EOL> try : <EOL> value = int ( options [ '<STR_LIT>' ] ) <EOL> except : <EOL> raise Exception ( "<STR_LIT>" . format ( options [ '<STR_LIT>' ] ) ) <EOL> if value < <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" . format ( value ) ) <EOL> opts [ '<STR_LIT>' ] = value <EOL> if '<STR_LIT>' in options : <EOL> value = options [ '<STR_LIT>' ] . lower ( ) . strip ( ) <EOL> opts [ '<STR_LIT>' ] = value <EOL> if '<STR_LIT>' in options : <EOL> value = options [ '<STR_LIT>' ] . lower ( ) . strip ( ) <EOL> if value == '<STR_LIT:true>' : <EOL> opts [ '<STR_LIT>' ] = True <EOL> elif value == '<STR_LIT:false>' : <EOL> opts [ '<STR_LIT>' ] = False <EOL> else : <EOL> raise Exception ( "<STR_LIT>" . format ( value ) ) <EOL> return opts <EOL> @ implementer ( IPlugin ) <EOL> @ implementer ( IStreamServerEndpointStringParser ) <EOL> class AutobahnServerParser ( object ) : <EOL> prefix = "<STR_LIT>" <EOL> def parseStreamServer ( self , reactor , description , ** options ) : <EOL> opts = _parseOptions ( options ) <EOL> endpoint = serverFromString ( reactor , description ) <EOL> return AutobahnServerEndpoint ( reactor , endpoint , opts ) <EOL> @ implementer ( IPlugin ) <EOL> @ implementer ( IStreamServerEndpoint ) <EOL> class AutobahnServerEndpoint ( object ) : <EOL> def __init__ ( self , reactor , endpoint , options ) : <EOL> self . _reactor = reactor <EOL> self . _endpoint = endpoint <EOL> self . _options = options <EOL> def listen ( self , protocolFactory ) : <EOL> return self . _endpoint . listen ( WrappingWebSocketServerFactory ( protocolFactory , reactor = self . _reactor , ** self . _options ) ) <EOL> @ implementer ( IPlugin ) <EOL> @ implementer ( IStreamClientEndpointStringParserWithReactor ) <EOL> class AutobahnClientParser ( object ) : <EOL> prefix = "<STR_LIT>" <EOL> def parseStreamClient ( self , * args , ** options ) : <EOL> if _HAS_REACTOR_ARG : <EOL> reactor = args [ <NUM_LIT:0> ] <EOL> if len ( args ) != <NUM_LIT:2> : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> description = args [ <NUM_LIT:1> ] <EOL> else : <EOL> from twisted . internet import reactor <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> description = args [ <NUM_LIT:0> ] <EOL> opts = _parseOptions ( options ) <EOL> endpoint = clientFromString ( reactor , description ) <EOL> return AutobahnClientEndpoint ( reactor , endpoint , opts ) <EOL> @ implementer ( IPlugin ) <EOL> @ implementer ( IStreamClientEndpoint ) <EOL> class AutobahnClientEndpoint ( object ) : <EOL> def __init__ ( self , reactor , endpoint , options ) : <EOL> self . _reactor = reactor <EOL> self . _endpoint = endpoint <EOL> self . _options = options <EOL> def connect ( self , protocolFactory ) : <EOL> return self . _endpoint . connect ( WrappingWebSocketClientFactory ( protocolFactory , reactor = self . _reactor , ** self . _options ) ) <EOL> autobahnServerParser = AutobahnServerParser ( ) <EOL> autobahnClientParser = AutobahnClientParser ( ) </s>
