<s> """<STR_LIT>""" <EOL> import struct <EOL> STRING = '<STR_LIT:">' <EOL> NODE_OPEN = '<STR_LIT:{>' <EOL> NODE_CLOSE = '<STR_LIT:}>' <EOL> COMMENT = '<STR_LIT:/>' <EOL> CR = '<STR_LIT:\r>' <EOL> LF = '<STR_LIT:\n>' <EOL> BIN_NONE = '<STR_LIT:\x00>' <EOL> BIN_STRING = '<STR_LIT>' <EOL> BIN_INT32 = '<STR_LIT>' <EOL> BIN_FLOAT32 = '<STR_LIT>' <EOL> BIN_POINTER = '<STR_LIT>' <EOL> BIN_WIDESTRING = '<STR_LIT>' <EOL> BIN_COLOR = '<STR_LIT>' <EOL> BIN_UINT64 = '<STR_LIT>' <EOL> BIN_END = '<STR_LIT>' <EOL> def _symtostr ( line , i ) : <EOL> opening = i + <NUM_LIT:1> <EOL> closing = <NUM_LIT:0> <EOL> ci = line . find ( '<STR_LIT:">' , opening ) <EOL> while ci != - <NUM_LIT:1> : <EOL> if line [ ci - <NUM_LIT:1> ] != '<STR_LIT:\\>' or line [ ci - <NUM_LIT:2> ] == '<STR_LIT:\\>' : <EOL> closing = ci <EOL> break <EOL> ci = line . find ( '<STR_LIT:">' , ci + <NUM_LIT:1> ) <EOL> finalstr = line [ opening : closing ] . decode ( '<STR_LIT>' ) <EOL> return finalstr , i + ( closing - opening ) + <NUM_LIT:1> <EOL> def _readtonull ( buffer , i ) : <EOL> end = buffer . find ( BIN_NONE , i ) <EOL> return buffer [ i : end ] , end <EOL> def _parse ( stream , ptr = <NUM_LIT:0> ) : <EOL> i = ptr <EOL> laststr = None <EOL> lasttok = None <EOL> deserialized = { } <EOL> while i < len ( stream ) : <EOL> c = stream [ i ] <EOL> if c == STRING : <EOL> string , i = _symtostr ( stream , i ) <EOL> if lasttok == STRING : <EOL> deserialized [ laststr ] = string <EOL> laststr = string <EOL> elif c == NODE_OPEN : <EOL> deserialized [ laststr ] , i = _parse ( stream , i + <NUM_LIT:1> ) <EOL> elif c == NODE_CLOSE : <EOL> return deserialized , i <EOL> elif c == COMMENT : <EOL> if ( i + <NUM_LIT:1> ) < len ( stream ) and stream [ i + <NUM_LIT:1> ] == '<STR_LIT:/>' : <EOL> i = stream . find ( '<STR_LIT:\n>' , i ) <EOL> elif c == CR or c == LF : <EOL> ni = i + <NUM_LIT:1> <EOL> if ni < len ( stream ) and stream [ ni ] == LF : <EOL> i = ni <EOL> if lasttok != LF : <EOL> c = LF <EOL> else : <EOL> c = lasttok <EOL> lasttok = c <EOL> i += <NUM_LIT:1> <EOL> return deserialized , i <EOL> def _parse_binary ( stream , ptr = <NUM_LIT:0> ) : <EOL> i = ptr <EOL> deserialized = { } <EOL> while i < len ( stream ) : <EOL> c = stream [ i ] <EOL> if c == BIN_END : <EOL> return deserialized , i <EOL> nodename , i = _readtonull ( stream , i + <NUM_LIT:1> ) <EOL> if c == BIN_NONE : <EOL> deserialized [ nodename ] , i = _parse_binary ( stream , i + <NUM_LIT:1> ) <EOL> elif c == BIN_STRING : <EOL> deserialized [ nodename ] , i = _readtonull ( stream , i + <NUM_LIT:1> ) <EOL> elif c == BIN_WIDESTRING : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> elif c == BIN_INT32 or c == BIN_COLOR or c == BIN_POINTER : <EOL> if len ( stream ) - i < <NUM_LIT:4> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> value , = struct . unpack_from ( '<STR_LIT:i>' , stream , i + <NUM_LIT:1> ) <EOL> deserialized [ nodename ] , i = value , i + <NUM_LIT:4> <EOL> elif c == BIN_UINT64 : <EOL> if len ( stream ) - i < <NUM_LIT:8> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> value , = struct . unpack_from ( '<STR_LIT:q>' , stream , i + <NUM_LIT:1> ) <EOL> deserialized [ nodename ] , i = value , i + <NUM_LIT:8> <EOL> elif c == BIN_FLOAT32 : <EOL> if len ( stream ) - i < <NUM_LIT:4> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> value , = struct . unpack_from ( '<STR_LIT:f>' , stream , i + <NUM_LIT:1> ) <EOL> deserialized [ nodename ] , i = <NUM_LIT:0> , i + <NUM_LIT:4> <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> i += <NUM_LIT:1> <EOL> return deserialized , i <EOL> def _run_parse_encoded ( string ) : <EOL> try : <EOL> encoded = unicode ( string , "<STR_LIT:ascii>" ) <EOL> except UnicodeDecodeError : <EOL> encoded = unicode ( string , "<STR_LIT>" ) <EOL> res , ptr = _parse ( encoded ) <EOL> return res <EOL> def loadbinary ( input ) : <EOL> return _parse_binary ( input ) <EOL> def load ( stream ) : <EOL> return _run_parse_encoded ( stream . read ( ) ) <EOL> def loads ( string ) : <EOL> return _run_parse_encoded ( string ) <EOL> indent = <NUM_LIT:0> <EOL> mult = <NUM_LIT:2> <EOL> def _i ( ) : <EOL> return '<STR_LIT:U+0020>' * ( indent * mult ) <EOL> def _dump ( obj ) : <EOL> nodefmt = unicode ( '<STR_LIT:\n>' + _i ( ) + '<STR_LIT>' + _i ( ) + '<STR_LIT>' + _i ( ) + '<STR_LIT>' ) <EOL> podfmt = unicode ( _i ( ) + '<STR_LIT>' ) <EOL> lstfmt = unicode ( _i ( ) + ( '<STR_LIT:U+0020>' * mult ) + '<STR_LIT>' ) <EOL> global indent <EOL> indent += <NUM_LIT:1> <EOL> nodes = [ ] <EOL> for k , v in obj . iteritems ( ) : <EOL> if isinstance ( v , dict ) : <EOL> nodes . append ( nodefmt . format ( k , _dump ( v ) ) ) <EOL> else : <EOL> try : <EOL> try : <EOL> v . isdigit <EOL> nodes . append ( podfmt . format ( k , v ) ) <EOL> except AttributeError : <EOL> lst = map ( lstfmt . format , v ) <EOL> nodes . append ( nodefmt . format ( k , '<STR_LIT:\n>' . join ( lst ) + '<STR_LIT:\n>' ) ) <EOL> except TypeError : <EOL> nodes . append ( podfmt . format ( k , v ) ) <EOL> indent -= <NUM_LIT:1> <EOL> return unicode ( '<STR_LIT>' . join ( nodes ) ) <EOL> def _run_dump ( obj ) : <EOL> res = _dump ( obj ) <EOL> return res . encode ( "<STR_LIT>" ) <EOL> def dump ( obj , stream ) : <EOL> stream . write ( _run_dump ( obj ) ) <EOL> def dumps ( obj ) : <EOL> return _run_dump ( obj ) </s>
