<s> import re <EOL> from lxml import etree <EOL> from lxml . builder import E <EOL> class _RpcMetaExec ( object ) : <EOL> def __init__ ( self , junos ) : <EOL> """<STR_LIT>""" <EOL> self . _junos = junos <EOL> def get_config ( self , filter_xml = None , options = { } ) : <EOL> """<STR_LIT>""" <EOL> rpc = E ( '<STR_LIT>' , options ) <EOL> if filter_xml is not None : <EOL> cfg_tag = '<STR_LIT>' <EOL> at_here = rpc if cfg_tag == filter_xml . tag else E ( cfg_tag ) <EOL> at_here . append ( filter_xml ) <EOL> if at_here is not rpc : rpc . append ( at_here ) <EOL> return self . _junos . execute ( rpc ) <EOL> def load_config ( self , contents , ** options ) : <EOL> """<STR_LIT>""" <EOL> rpc = E ( '<STR_LIT>' , options ) <EOL> if ( '<STR_LIT:action>' in options ) and ( options [ '<STR_LIT:action>' ] == '<STR_LIT>' ) : <EOL> rpc . append ( E ( '<STR_LIT>' , contents ) ) <EOL> elif ( '<STR_LIT>' in options ) and ( options [ '<STR_LIT>' ] == '<STR_LIT:text>' ) : <EOL> rpc . append ( E ( '<STR_LIT>' , contents ) ) <EOL> else : <EOL> if contents . tag != '<STR_LIT>' : <EOL> etree . SubElement ( rpc , '<STR_LIT>' ) . append ( contents ) <EOL> else : <EOL> rpc . append ( contents ) <EOL> return self . _junos . execute ( rpc ) <EOL> def cli ( self , command , format = '<STR_LIT:text>' ) : <EOL> rpc = E ( '<STR_LIT>' , command ) <EOL> if format . lower ( ) in [ '<STR_LIT:text>' , '<STR_LIT>' ] : <EOL> rpc . attrib [ '<STR_LIT>' ] = format <EOL> return self . _junos . execute ( rpc ) <EOL> def __getattr__ ( self , rpc_cmd_name ) : <EOL> """<STR_LIT>""" <EOL> rpc_cmd = re . sub ( '<STR_LIT:_>' , '<STR_LIT:->' , rpc_cmd_name ) <EOL> def _exec_rpc ( * vargs , ** kvargs ) : <EOL> rpc = etree . Element ( rpc_cmd ) <EOL> if kvargs : <EOL> for arg_name , arg_value in kvargs . items ( ) : <EOL> if arg_name not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> arg_name = re . sub ( '<STR_LIT:_>' , '<STR_LIT:->' , arg_name ) <EOL> if isinstance ( arg_value , ( tuple , list ) ) : <EOL> for a in arg_value : <EOL> arg = etree . SubElement ( rpc , arg_name ) <EOL> if a is not True : <EOL> arg . text = a <EOL> else : <EOL> arg = etree . SubElement ( rpc , arg_name ) <EOL> if arg_value is not True : <EOL> arg . text = arg_value <EOL> if vargs : <EOL> for k , v in vargs [ <NUM_LIT:0> ] . items ( ) : <EOL> if v is not True : <EOL> rpc . attrib [ k ] = v <EOL> timeout = kvargs . get ( '<STR_LIT>' ) <EOL> normalize = kvargs . get ( '<STR_LIT>' ) <EOL> dec_args = { } <EOL> if timeout is not None : <EOL> dec_args [ '<STR_LIT>' ] = timeout <EOL> if normalize is not None : <EOL> dec_args [ '<STR_LIT>' ] = normalize <EOL> return self . _junos . execute ( rpc , ** dec_args ) <EOL> _exec_rpc . __doc__ = rpc_cmd <EOL> _exec_rpc . __name__ = rpc_cmd_name <EOL> return _exec_rpc <EOL> def __call__ ( self , rpc_cmd , ** kvargs ) : <EOL> """<STR_LIT>""" <EOL> return self . _junos . execute ( rpc_cmd , ** kvargs ) </s>
