<s> """<STR_LIT>""" <EOL> import re <EOL> import unicodedata <EOL> from pycopia import socket <EOL> from pycopia import asyncio <EOL> from pycopia . iscsi import headers <EOL> from pycopia . iscsi import constants <EOL> from pycopia . iscsi import exceptions <EOL> ALLOWED_CHARS = re . compile ( u'<STR_LIT>' ) <EOL> def normalize_name ( uname ) : <EOL> """<STR_LIT>""" <EOL> assert type ( uname ) is unicode <EOL> nrm = unicodedata . normalize ( "<STR_LIT>" , uname . lower ( ) ) <EOL> if ALLOWED_CHARS . match ( nrm ) : <EOL> return nrm . encode ( "<STR_LIT:utf8>" ) <EOL> else : <EOL> raise exceptions . ValidationError ( "<STR_LIT>" % ( uname , ) ) <EOL> def get_iqn_name ( year , month , hostname , identifier = None ) : <EOL> """<STR_LIT>""" <EOL> revdomain = "<STR_LIT:.>" . join ( reversed ( hostname . split ( "<STR_LIT:.>" ) ) ) <EOL> name = u"<STR_LIT>" % ( year , month , revdomain ) <EOL> if identifier : <EOL> name += ( u"<STR_LIT::>" + unicode ( identifier ) ) <EOL> return normalize_name ( name ) <EOL> class Task ( object ) : <EOL> def __init__ ( self , id = <NUM_LIT:0> ) : <EOL> self . id = id <EOL> class Connection ( socket . AsyncSocket ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , debug = False ) : <EOL> super ( Connection , self ) . __init__ ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> def __init__ ( self , sock , session_context ) : <EOL> self . _sock = sock <EOL> self . _context = session_context <EOL> self . cid = <NUM_LIT:0> <EOL> def fileno ( self ) : <EOL> return self . _sock . fileno ( ) <EOL> def close ( self ) : <EOL> sock = self . _sock <EOL> self . _sock = None <EOL> sock . close ( ) <EOL> def login ( self ) : <EOL> pdu = headers . LoginPDU ( ) <EOL> pdu . current_stage = SECURITY_NEGOTIATION_STAGE <EOL> pdu . next_stage = OP_PARMS_NEGOTIATION_STAGE <EOL> pdu . ISID = self . _context [ "<STR_LIT>" ] <EOL> pdu [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> pdu [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> pdu [ "<STR_LIT>" ] = self . _context [ "<STR_LIT>" ] <EOL> pdu [ "<STR_LIT>" ] = self . _context [ "<STR_LIT:target>" ] <EOL> self . _sock . send ( pdu . encode ( ) ) <EOL> class Session ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , target ) : <EOL> self . host = host <EOL> self . _connections = [ ] <EOL> self . _context = dict ( host = host , target = target , tsih = <NUM_LIT:0> , <EOL> isid = headers . ISID ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:0> ) ) <EOL> def add_connection ( self , port = constants . LISTEN_PORT ) : <EOL> sock = Connection ( self . _context ) <EOL> h = asyncio . register ( sock ) <EOL> self . _connections . append ( sock ) <EOL> def remove_connection ( self ) : <EOL> try : <EOL> conn = self . _connections . pop ( ) <EOL> except IndexError : <EOL> pass <EOL> else : <EOL> conn . close ( ) <EOL> def login ( self ) : <EOL> if not self . _connections : <EOL> self . add_connection ( ) <EOL> conn = self . _connections [ <NUM_LIT:0> ] <EOL> self . tsih = conn . login ( ) <EOL> def logout ( self ) : <EOL> conn = self . _connections [ <NUM_LIT:0> ] <EOL> self . tsih = conn . logout ( ) <EOL> def command ( self ) : <EOL> pass <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> ref = '<STR_LIT>' <EOL> assert normalize_name ( u'<STR_LIT>' ) == ref <EOL> assert normalize_name ( u'<STR_LIT>' ) == ref <EOL> try : <EOL> normalize_name ( u'<STR_LIT>' ) <EOL> except exceptions . ValidationError : <EOL> print "<STR_LIT>" <EOL> assert get_iqn_name ( <NUM_LIT> , <NUM_LIT:1> , '<STR_LIT>' , u'<STR_LIT>' ) == ref </s>
