<s> import logging <EOL> import re <EOL> import glob <EOL> import os . path as osp <EOL> from contextlib import contextmanager <EOL> from gevent_zeromq import zmq <EOL> from . common import patterns , format_method <EOL> from . . lib import ReqError <EOL> class ProxySocket ( object ) : <EOL> reserved = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , obj , parsed = [ ] ) : <EOL> self . _obj = obj <EOL> self . _parsed = [ ] <EOL> self . _str = None <EOL> def __getattr__ ( self , key ) : <EOL> self . _key = key <EOL> self . _attr = '<STR_LIT>' <EOL> return self . _rpc ( ) <EOL> def __setattr__ ( self , key , value ) : <EOL> if key in self . reserved : <EOL> return super ( ProxySocket , self ) . __setattr__ ( key , value ) <EOL> self . _key = key <EOL> self . _value = value <EOL> self . _attr = '<STR_LIT>' <EOL> return self . _rpc ( ) <EOL> def __delattr__ ( self , key ) : <EOL> if key in self . reserved : <EOL> return super ( ProxySocket , self ) . __delattr__ ( key ) <EOL> self . _key = key <EOL> self . _attr = '<STR_LIT>' <EOL> return self . _rpc ( ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> self . _attr = '<STR_LIT>' <EOL> return self . _rpc ( * args , ** kwargs ) <EOL> def _rpc ( self , * args , ** kwargs ) : <EOL> if self . _attr is '<STR_LIT>' : <EOL> blob = ( '<STR_LIT>' , args , kwargs ) <EOL> elif self . _attr is '<STR_LIT>' : <EOL> blob = ( '<STR_LIT>' , [ self . _key ] , { } ) <EOL> elif self . _attr is '<STR_LIT>' : <EOL> blob = ( '<STR_LIT>' , [ self . _key , self . _value ] , { } ) <EOL> elif self . _attr is '<STR_LIT>' : <EOL> blob = ( '<STR_LIT>' , [ self . _key ] , { } ) <EOL> elif self . _attr is '<STR_LIT>' : <EOL> blob = ( '<STR_LIT>' , [ ] , { } ) <EOL> elif self . _attr is '<STR_LIT>' : <EOL> blob = ( '<STR_LIT>' , [ ] , { } ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % self . attr ) <EOL> self . _parsed . append ( blob ) <EOL> if self . _obj . _sock is not None : <EOL> reply = self . _obj . _send ( self . _parsed ) <EOL> else : <EOL> with self . _obj : <EOL> reply = self . _obj . _send ( self . _parsed ) <EOL> if '<STR_LIT:error>' in reply : <EOL> return ReqError ( reply [ '<STR_LIT:error>' ] ) <EOL> elif '<STR_LIT>' in reply : <EOL> self . _str = '<STR_LIT>' % reply [ '<STR_LIT>' ] <EOL> return self <EOL> elif '<STR_LIT:result>' in reply : <EOL> return reply [ '<STR_LIT:result>' ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return result <EOL> def __str__ ( self ) : <EOL> if self . _str is None : <EOL> return super ( ProxySocket , self ) . __str__ ( ) <EOL> return str ( self . _str ) <EOL> def __repr__ ( self ) : <EOL> if self . _str is None : <EOL> return super ( ProxySocket , self ) . __repr__ ( ) <EOL> return str ( self . _str ) <EOL> def __dir__ ( self ) : <EOL> self . _attr = '<STR_LIT>' <EOL> return self . _rpc ( ) <EOL> def __len__ ( self ) : <EOL> self . _attr = '<STR_LIT>' <EOL> return self . _rpc ( ) <EOL> class Socket ( object ) : <EOL> """<STR_LIT>""" <EOL> reserved = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , name , _type = '<STR_LIT>' , subscription = '<STR_LIT>' , context = None ) : <EOL> self . _name = name <EOL> self . _type = _type . upper ( ) <EOL> self . _pattern = patterns [ self . _type ] <EOL> self . _subscription = subscription <EOL> self . _context = context or zmq . Context . instance ( ) <EOL> self . _sock_file = "<STR_LIT>" % ( self . _pattern , self . _name ) <EOL> self . _sock = None <EOL> def _open ( self ) : <EOL> if self . _sock is not None : <EOL> return <EOL> self . _sock = self . _context . socket ( getattr ( zmq , self . _type ) ) <EOL> self . _sock . connect ( self . _sock_file ) <EOL> if self . _pattern == '<STR_LIT>' : <EOL> self . _sock . setsockopt ( zmq . SUBSCRIBE , self . _subscription ) <EOL> return self <EOL> def _close ( self ) : <EOL> if self . _sock is not None : <EOL> self . _sock . close ( ) <EOL> self . _sock = None <EOL> return self <EOL> def __enter__ ( self ) : <EOL> return self . _open ( ) <EOL> def __exit__ ( self , type , value , trace ) : <EOL> self . _close ( ) <EOL> def _send ( self , blob ) : <EOL> self . _sock . send_json ( blob ) <EOL> logging . debug ( "<STR_LIT>" % ( self . _name , '<STR_LIT>' . join ( [ format_method ( * req ) for req in blob ] ) ) ) <EOL> return self . _sock . recv_json ( ) <EOL> def __getattr__ ( self , key ) : <EOL> return getattr ( ProxySocket ( self ) , key ) <EOL> def __setattr__ ( self , key , value ) : <EOL> if key in self . reserved : <EOL> return super ( Socket , self ) . __setattr__ ( key , value ) <EOL> else : <EOL> return setattr ( ProxySocket ( self ) , key , value ) <EOL> def __delattr__ ( self , key ) : <EOL> if key in self . reserved : <EOL> return super ( Socket , self ) . __delattr__ ( key ) <EOL> else : <EOL> return delattr ( ProxySocket ( self ) , key ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> return ProxySocket ( self ) . __call__ ( * args , ** kwargs ) <EOL> def __dir__ ( self ) : <EOL> return dir ( ProxySocket ( self ) ) </s>
