<s> import json <EOL> import zmq <EOL> from mdp . client import MDPClient , mdp_request <EOL> from binder . settings import LogSettings , BinderDSettings <EOL> from binder . binderd import modules <EOL> class BinderClient ( object ) : <EOL> def __init__ ( self , name = "<STR_LIT>" ) : <EOL> if name not in modules and name != "<STR_LIT>" : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . name = name <EOL> self . module = modules . get ( self . name ) <EOL> self . _sock = None <EOL> self . _connect ( ) <EOL> def _connect ( self ) : <EOL> if self . name == "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> context = zmq . Context ( ) <EOL> socket = context . socket ( zmq . REQ ) <EOL> socket . setsockopt ( zmq . LINGER , <NUM_LIT:0> ) <EOL> url = "<STR_LIT>" . format ( BinderDSettings . BROKER_HOST , BinderDSettings . BROKER_PORT ) <EOL> socket . connect ( url ) <EOL> self . _sock = socket <EOL> def send ( self , msg ) : <EOL> if not self . _sock : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if not isinstance ( msg , str ) : <EOL> if isinstance ( msg , dict ) : <EOL> msg = json . dumps ( msg ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( type ( msg ) ) ) <EOL> if self . name == "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> res = mdp_request ( self . _sock , bytes ( self . name ) , [ bytes ( msg ) , ] , <NUM_LIT:10> ) <EOL> return json . loads ( res [ <NUM_LIT:1> ] ) if res else None <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self ) : <EOL> self . close ( ) <EOL> def close ( self ) : <EOL> self . _sock . close ( ) <EOL> def stop_daemon ( self ) : <EOL> if self . name != "<STR_LIT>" : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . send ( { "<STR_LIT:type>" : "<STR_LIT>" } ) <EOL> def start_module ( self , name ) : <EOL> if self . name != "<STR_LIT>" : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . send ( { "<STR_LIT:type>" : "<STR_LIT:start>" , "<STR_LIT:name>" : name } ) <EOL> def stop_module ( self , name ) : <EOL> if self . name != "<STR_LIT>" : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . send ( { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT:name>" : name } ) </s>
