<s> from __future__ import absolute_import <EOL> import socket <EOL> from . import util , exception <EOL> from . const import * <EOL> try : <EOL> from settings import RAW_LOG <EOL> except ImportError : <EOL> RAW_LOG = False <EOL> class BaseSocket ( object ) : <EOL> def __init__ ( self , addr , charset = '<STR_LIT:utf-8>' ) : <EOL> """<STR_LIT>""" <EOL> self . addr = addr <EOL> self . charset = charset <EOL> self . msgqueue = [ ] <EOL> self . recvbuffer = '<STR_LIT>' <EOL> self . connected = None <EOL> self . create_socket ( ) <EOL> def create_socket ( self ) : <EOL> """<STR_LIT>""" <EOL> self . socket = socket . socket ( ) <EOL> def send ( self , line ) : <EOL> raise NotImplementedError <EOL> def connect ( self ) : <EOL> self . msgqueue . append ( CONNECTED ) <EOL> self . connected = True <EOL> raise NotImplementedError <EOL> def disconnect ( self ) : <EOL> self . msgqueue . append ( DISCONNECTED ) <EOL> raise NotImplementedError <EOL> def _recv ( self ) : <EOL> raise NotImplementedError <EOL> def sendln ( self , line , * arg , ** kwargs ) : <EOL> if len ( arg ) or len ( kwargs ) : <EOL> line = line . format ( * arg , ** kwargs ) <EOL> self . send ( line + '<STR_LIT:\r\n>' ) <EOL> def cmds ( self , cmd , * args ) : <EOL> self . sendln ( u'<STR_LIT:U+0020>' . join ( map ( unicode , [ cmd ] + list ( args ) ) ) ) <EOL> def cmdl ( self , cmd , * args ) : <EOL> nargs = list ( args [ : - <NUM_LIT:1> ] ) + [ '<STR_LIT::>' + args [ - <NUM_LIT:1> ] ] <EOL> self . cmds ( cmd , * nargs ) <EOL> def cmd ( self , cmd , * args ) : <EOL> if '<STR_LIT:U+0020>' in args [ - <NUM_LIT:1> ] : <EOL> self . cmdl ( cmd , * args ) <EOL> else : <EOL> self . cmds ( cmd , * args ) <EOL> def dispatch ( self ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . msgqueue ) == <NUM_LIT:0> : <EOL> return None <EOL> msg = self . msgqueue [ <NUM_LIT:0> ] <EOL> del ( self . msgqueue [ <NUM_LIT:0> ] ) <EOL> if RAW_LOG : <EOL> print '<STR_LIT:>>' , msg <EOL> return msg <EOL> def dispatch_all ( self ) : <EOL> msg = True <EOL> while msg : <EOL> msg = self . dispatch ( ) <EOL> if msg : <EOL> yield msg <EOL> raise StopIteration <EOL> def _split_buffer ( self ) : <EOL> """<STR_LIT>""" <EOL> newline , self . recvbuffer = self . recvbuffer . split ( '<STR_LIT:\r\n>' , <NUM_LIT:1> ) <EOL> try : <EOL> return newline . decode ( self . charset ) <EOL> except : <EOL> print '<STR_LIT>' , self . charset , '<STR_LIT>' , newline <EOL> return newline <EOL> def _enqueue ( self , line ) : <EOL> self . msgqueue . append ( line ) <EOL> def _enqueue_buffer ( self ) : <EOL> try : <EOL> while True : <EOL> newline = self . _split_buffer ( ) <EOL> self . _enqueue ( newline ) <EOL> except ValueError : <EOL> pass <EOL> def recv ( self , wait_enqueue = False ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> try : <EOL> recv = self . _recv ( ) <EOL> if recv == '<STR_LIT>' : raise socket . error ( "<STR_LIT>" ) <EOL> self . recvbuffer += recv <EOL> self . _enqueue_buffer ( ) <EOL> except Exception as e : <EOL> if isinstance ( e , socket . error ) : <EOL> self . msgqueue . append ( DISCONNECTED ) <EOL> self . connected = False <EOL> else : <EOL> import traceback <EOL> print '<STR_LIT>' <EOL> traceback . print_exc ( ) <EOL> raise e <EOL> self . _enqueue ( e ) <EOL> if not wait_enqueue or len ( self . msgqueue ) > <NUM_LIT:0> : <EOL> break <EOL> class Socket ( BaseSocket ) : <EOL> def send ( self , line ) : <EOL> if RAW_LOG : <EOL> print '<STR_LIT:<>' , line , <EOL> self . socket . send ( line . encode ( self . charset ) ) <EOL> def connect ( self ) : <EOL> self . socket . connect ( self . addr ) <EOL> self . msgqueue . append ( CONNECTED ) <EOL> self . connected = True <EOL> def disconnect ( self ) : <EOL> self . socket . close ( ) <EOL> def _recv ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . socket . recv ( <NUM_LIT> ) </s>
