<s> """<STR_LIT>""" <EOL> import re <EOL> import struct <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> import pyhsm . exception <EOL> import pyhsm . defines <EOL> class YHSM_Cmd ( ) : <EOL> """<STR_LIT>""" <EOL> response_status = None <EOL> executed = False <EOL> def __init__ ( self , stick , command , payload = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . stick = stick <EOL> self . command = command <EOL> self . payload = payload <EOL> return None <EOL> def execute ( self , read_response = True ) : <EOL> """<STR_LIT>""" <EOL> if self . command != pyhsm . defines . YSM_NULL : <EOL> cmd_buf = struct . pack ( '<STR_LIT>' , len ( self . payload ) + <NUM_LIT:1> , self . command ) <EOL> else : <EOL> cmd_buf = chr ( self . command ) <EOL> cmd_buf += self . payload <EOL> debug_info = None <EOL> unlock = self . stick . acquire ( ) <EOL> try : <EOL> if self . stick . debug : <EOL> debug_info = "<STR_LIT>" % ( pyhsm . defines . cmd2str ( self . command ) , len ( self . payload ) , len ( self . payload ) ) <EOL> self . stick . write ( cmd_buf , debug_info ) <EOL> if not read_response : <EOL> return None <EOL> return self . _read_response ( ) <EOL> finally : <EOL> unlock ( ) <EOL> def _read_response ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . stick . read ( <NUM_LIT:2> , '<STR_LIT>' ) <EOL> if len ( res ) != <NUM_LIT:2> : <EOL> self . _handle_invalid_read_response ( res , <NUM_LIT:2> ) <EOL> response_len , response_status = struct . unpack ( '<STR_LIT>' , res ) <EOL> response_len -= <NUM_LIT:1> <EOL> debug_info = None <EOL> if response_status & pyhsm . defines . YSM_RESPONSE : <EOL> debug_info = "<STR_LIT>" % ( pyhsm . defines . cmd2str ( response_status - pyhsm . defines . YSM_RESPONSE ) , response_len , response_len ) <EOL> res = self . stick . read ( response_len , debug_info ) <EOL> if res : <EOL> if response_status == self . command | pyhsm . defines . YSM_RESPONSE : <EOL> self . executed = True <EOL> self . response_status = response_status <EOL> return self . parse_result ( res ) <EOL> else : <EOL> reset ( self . stick ) <EOL> raise pyhsm . exception . YHSM_Error ( '<STR_LIT>' ) <EOL> else : <EOL> raise pyhsm . exception . YHSM_Error ( '<STR_LIT>' ) <EOL> def _handle_invalid_read_response ( self , res , expected_len ) : <EOL> """<STR_LIT>""" <EOL> if not res : <EOL> reset ( self . stick ) <EOL> raise pyhsm . exception . YHSM_Error ( '<STR_LIT>' % ( pyhsm . defines . cmd2str ( self . command ) ) ) <EOL> self . stick . write ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> res2 = self . stick . read ( <NUM_LIT:50> ) <EOL> lines = res2 . split ( '<STR_LIT:\n>' ) <EOL> for this in lines : <EOL> if re . match ( '<STR_LIT>' , this ) : <EOL> raise pyhsm . exception . YHSM_Error ( '<STR_LIT>' ) <EOL> raise pyhsm . exception . YHSM_Error ( '<STR_LIT>' % ( self . stick . device , res . encode ( '<STR_LIT>' ) ) ) <EOL> def parse_result ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return data <EOL> def reset ( stick ) : <EOL> """<STR_LIT>""" <EOL> nulls = ( pyhsm . defines . YSM_MAX_PKT_SIZE - <NUM_LIT:1> ) * '<STR_LIT:\x00>' <EOL> res = YHSM_Cmd ( stick , pyhsm . defines . YSM_NULL , payload = nulls ) . execute ( read_response = False ) <EOL> unlock = stick . acquire ( ) <EOL> try : <EOL> stick . drain ( ) <EOL> stick . flush ( ) <EOL> finally : <EOL> unlock ( ) <EOL> return res == <NUM_LIT:0> </s>
