<s> """<STR_LIT>""" <EOL> from json import JSONDecoder , JSONEncoder <EOL> import logging <EOL> from multiprocessing import Pipe <EOL> from twisted . internet . protocol import Protocol <EOL> from base import CoqProc <EOL> from parser . gram import parser <EOL> logging . basicConfig ( level = logging . DEBUG ) <EOL> class CoqProtocol ( Protocol ) : <EOL> """<STR_LIT>""" <EOL> class ActiveConn ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , userid , modules ) : <EOL> self . userid = userid <EOL> self . here , self . there = Pipe ( duplex = True ) <EOL> self . proc = CoqProc ( ) <EOL> self . proc . start ( modules ) <EOL> self . read ( ) <EOL> logging . debug ( "<STR_LIT>" , self . proc ) <EOL> def read ( self ) : <EOL> """<STR_LIT>""" <EOL> res = None <EOL> self . proc . run ( self . there ) <EOL> if self . here . poll ( ) : <EOL> res = self . here . recv ( ) <EOL> logging . debug ( "<STR_LIT>" ) <EOL> return { '<STR_LIT>' : self . userid , '<STR_LIT>' : res } <EOL> def send ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if self . proc . alive : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> self . here . send ( data + "<STR_LIT:U+0020>" ) <EOL> return True <EOL> else : <EOL> return False <EOL> def quit ( self ) : <EOL> if self . proc . alive : <EOL> return self . proc . terminate ( True ) <EOL> active_conns = { } <EOL> def connectionMade ( self ) : <EOL> self . serialize = self . factory . serialize <EOL> self . modules = self . factory . modules <EOL> self . serialize = self . factory . serialize <EOL> def dataReceived ( self , data ) : <EOL> """<STR_LIT>""" <EOL> req_data = JSONDecoder ( ) . decode ( data ) <EOL> logging . debug ( "<STR_LIT>" , req_data ) <EOL> command = req_data . get ( '<STR_LIT>' ) <EOL> userid = req_data . get ( '<STR_LIT>' ) <EOL> resp_data = self . handle_command ( userid , command ) <EOL> data = resp_data [ '<STR_LIT>' ] <EOL> if self . serialize : <EOL> resp_data [ '<STR_LIT>' ] = self . do_parse ( resp_data [ '<STR_LIT>' ] ) <EOL> data = JSONEncoder ( ) . encode ( resp_data ) <EOL> self . transport . write ( data ) <EOL> self . transport . loseConnection ( ) <EOL> def do_parse ( self , output ) : <EOL> """<STR_LIT>""" <EOL> logging . debug ( "<STR_LIT>" % output ) <EOL> output = '<STR_LIT:U+0020>' . join ( output . splitlines ( ) ) <EOL> result = parser . parse ( output ) <EOL> return result <EOL> def handle_command ( self , userid , command ) : <EOL> """<STR_LIT>""" <EOL> if not command : <EOL> resp_dict = { '<STR_LIT>' : userid , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> elif '<STR_LIT>' == command : <EOL> self . cleanup_session ( userid ) <EOL> resp_dict = { '<STR_LIT>' : userid , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> else : <EOL> active_sess = self . active_conns . get ( userid ) <EOL> if not active_sess : <EOL> active_sess = self . ActiveConn ( userid , self . modules ) <EOL> self . active_conns . update ( { active_sess . userid : active_sess } ) <EOL> if not active_sess . send ( command ) : <EOL> logging . error ( "<STR_LIT>" ) <EOL> self . cleanup_session ( userid ) <EOL> resp_dict = active_sess . read ( ) <EOL> return resp_dict <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in '<STR_LIT>' . join ( reason . parents ) : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> else : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> raise reason <EOL> def cleanup_session ( self , userid ) : <EOL> """<STR_LIT>""" <EOL> active_sess = self . active_conns . get ( userid ) <EOL> if active_sess : <EOL> active_sess . quit ( ) <EOL> del self . active_conns [ userid ] </s>
