<s> """<STR_LIT>""" <EOL> from struct import Struct , error as StructError <EOL> from gzip import GzipFile <EOL> import zlib <EOL> from collections import MutableMapping , MutableSequence , Sequence <EOL> import os , io <EOL> PISS = True <EOL> try : <EOL> unicode <EOL> basestring <EOL> except NameError : <EOL> unicode = str <EOL> basestring = str <EOL> TAG_END = <NUM_LIT:0> <EOL> TAG_BYTE = <NUM_LIT:1> <EOL> TAG_SHORT = <NUM_LIT:2> <EOL> TAG_INT = <NUM_LIT:3> <EOL> TAG_LONG = <NUM_LIT:4> <EOL> TAG_FLOAT = <NUM_LIT:5> <EOL> TAG_DOUBLE = <NUM_LIT:6> <EOL> TAG_BYTE_ARRAY = <NUM_LIT:7> <EOL> TAG_STRING = <NUM_LIT:8> <EOL> TAG_LIST = <NUM_LIT:9> <EOL> TAG_COMPOUND = <NUM_LIT:10> <EOL> TAG_INT_ARRAY = <NUM_LIT:11> <EOL> class MalformedFileError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class TAG ( object ) : <EOL> """<STR_LIT>""" <EOL> id = None <EOL> def __init__ ( self , value = None , name = None ) : <EOL> self . name = name <EOL> self . value = value <EOL> def _parse_buffer ( self , buffer ) : <EOL> raise NotImplementedError ( self . __class__ . __name__ ) <EOL> def _render_buffer ( self , buffer ) : <EOL> raise NotImplementedError ( self . __class__ . __name__ ) <EOL> def tag_info ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ . __name__ + ( '<STR_LIT>' % self . name if self . name else "<STR_LIT>" ) + "<STR_LIT>" + self . valuestr ( ) <EOL> def valuestr ( self ) : <EOL> """<STR_LIT>""" <EOL> return unicode ( self . value ) <EOL> def pretty_tree ( self , indent = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return ( "<STR_LIT:\t>" * indent ) + self . tag_info ( ) <EOL> def __unicode__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return unicode ( self . value ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return str ( self . value ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . name , id ( self ) ) <EOL> class _TAG_Numeric ( TAG ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value = None , name = None , buffer = None ) : <EOL> super ( _TAG_Numeric , self ) . __init__ ( value , name ) <EOL> if buffer : <EOL> self . _parse_buffer ( buffer ) <EOL> def _parse_buffer ( self , buffer ) : <EOL> self . value = self . fmt . unpack ( buffer . read ( self . fmt . size ) ) [ <NUM_LIT:0> ] <EOL> def _render_buffer ( self , buffer ) : <EOL> buffer . write ( self . fmt . pack ( self . value ) ) <EOL> class _TAG_End ( TAG ) : <EOL> id = TAG_END <EOL> fmt = Struct ( "<STR_LIT>" ) <EOL> def _parse_buffer ( self , buffer ) : <EOL> value = self . fmt . unpack ( buffer . read ( <NUM_LIT:1> ) ) [ <NUM_LIT:0> ] <EOL> if value != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % ( value ) ) <EOL> def _render_buffer ( self , buffer ) : <EOL> buffer . write ( b'<STR_LIT:\x00>' ) <EOL> class TAG_Byte ( _TAG_Numeric ) : <EOL> """<STR_LIT>""" <EOL> id = TAG_BYTE <EOL> fmt = Struct ( "<STR_LIT>" ) <EOL> class TAG_Short ( _TAG_Numeric ) : <EOL> """<STR_LIT>""" <EOL> id = TAG_SHORT <EOL> fmt = Struct ( "<STR_LIT>" ) <EOL> class TAG_Int ( _TAG_Numeric ) : <EOL> """<STR_LIT>""" <EOL> id = TAG_INT <EOL> fmt = Struct ( "<STR_LIT>" ) <EOL> """<STR_LIT>""" <EOL> class TAG_Long ( _TAG_Numeric ) : <EOL> """<STR_LIT>""" <EOL> id = TAG_LONG <EOL> fmt = Struct ( "<STR_LIT>" ) <EOL> class TAG_Float ( _TAG_Numeric ) : <EOL> """<STR_LIT>""" <EOL> id = TAG_FLOAT <EOL> fmt = Struct ( "<STR_LIT>" ) <EOL> class TAG_Double ( _TAG_Numeric ) : <EOL> """<STR_LIT>""" <EOL> id = TAG_DOUBLE <EOL> fmt = Struct ( "<STR_LIT>" ) <EOL> class TAG_Byte_Array ( TAG , MutableSequence ) : <EOL> """<STR_LIT>""" <EOL> id = TAG_BYTE_ARRAY <EOL> def __init__ ( self , name = None , buffer = None ) : <EOL> super ( TAG_Byte_Array , self ) . __init__ ( name = name ) <EOL> if buffer : <EOL> self . _parse_buffer ( buffer ) <EOL> def _parse_buffer ( self , buffer ) : <EOL> length = TAG_Int ( buffer = buffer ) <EOL> self . value = bytearray ( buffer . read ( length . value ) ) <EOL> def _render_buffer ( self , buffer ) : <EOL> length = TAG_Int ( len ( self . value ) ) <EOL> length . _render_buffer ( buffer ) <EOL> buffer . write ( bytes ( self . value ) ) <EOL> def __len__ ( self ) : <EOL> return len ( self . value ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . value ) <EOL> def __contains__ ( self , item ) : <EOL> return item in self . value <EOL> def __getitem__ ( self , key ) : <EOL> return self . value [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> self . value [ key ] = value <EOL> def __delitem__ ( self , key ) : <EOL> del ( self . value [ key ] ) <EOL> def insert ( self , key , value ) : <EOL> self . value . insert ( key , value ) <EOL> def valuestr ( self ) : <EOL> return "<STR_LIT>" % len ( self . value ) <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT:[>' + "<STR_LIT:U+002C>" . join ( [ unicode ( x ) for x in self . value ] ) + '<STR_LIT:]>' <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:[>' + "<STR_LIT:U+002C>" . join ( [ str ( x ) for x in self . value ] ) + '<STR_LIT:]>' <EOL> class TAG_Int_Array ( TAG , MutableSequence ) : <EOL> """<STR_LIT>""" <EOL> id = TAG_INT_ARRAY <EOL> def __init__ ( self , name = None , buffer = None ) : <EOL> super ( TAG_Int_Array , self ) . __init__ ( name = name ) <EOL> if buffer : <EOL> self . _parse_buffer ( buffer ) <EOL> def update_fmt ( self , length ) : <EOL> """<STR_LIT>""" <EOL> self . fmt = Struct ( "<STR_LIT:>>" + str ( length ) + "<STR_LIT:i>" ) <EOL> def _parse_buffer ( self , buffer ) : <EOL> length = TAG_Int ( buffer = buffer ) . value <EOL> self . update_fmt ( length ) <EOL> self . value = list ( self . fmt . unpack ( buffer . read ( self . fmt . size ) ) ) <EOL> def _render_buffer ( self , buffer ) : <EOL> length = len ( self . value ) <EOL> self . update_fmt ( length ) <EOL> TAG_Int ( length ) . _render_buffer ( buffer ) <EOL> buffer . write ( self . fmt . pack ( * self . value ) ) <EOL> def __len__ ( self ) : <EOL> return len ( self . value ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . value ) <EOL> def __contains__ ( self , item ) : <EOL> return item in self . value <EOL> def __getitem__ ( self , key ) : <EOL> return self . value [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> self . value [ key ] = value <EOL> def __delitem__ ( self , key ) : <EOL> del ( self . value [ key ] ) <EOL> def insert ( self , key , value ) : <EOL> self . value . insert ( key , value ) <EOL> def valuestr ( self ) : <EOL> return "<STR_LIT>" % len ( self . value ) <EOL> class TAG_String ( TAG , Sequence ) : <EOL> """<STR_LIT>""" <EOL> id = TAG_STRING <EOL> def __init__ ( self , value = None , name = None , buffer = None ) : <EOL> super ( TAG_String , self ) . __init__ ( value , name ) <EOL> if buffer : <EOL> self . _parse_buffer ( buffer ) <EOL> def _parse_buffer ( self , buffer ) : <EOL> length = TAG_Short ( buffer = buffer ) <EOL> read = buffer . read ( length . value ) <EOL> if len ( read ) != length . value : <EOL> raise StructError ( ) <EOL> self . value = read . decode ( "<STR_LIT:utf-8>" ) <EOL> def _render_buffer ( self , buffer ) : <EOL> save_val = self . value . encode ( "<STR_LIT:utf-8>" ) <EOL> length = TAG_Short ( len ( save_val ) ) <EOL> length . _render_buffer ( buffer ) <EOL> buffer . write ( save_val ) <EOL> def __len__ ( self ) : <EOL> return len ( self . value ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . value ) <EOL> def __contains__ ( self , item ) : <EOL> return item in self . value <EOL> def __getitem__ ( self , key ) : <EOL> return self . value [ key ] <EOL> def __repr__ ( self ) : <EOL> return self . value <EOL> class TAG_List ( TAG , MutableSequence ) : <EOL> """<STR_LIT>""" <EOL> id = TAG_LIST <EOL> def __init__ ( self , type = None , value = None , name = None , buffer = None ) : <EOL> super ( TAG_List , self ) . __init__ ( value , name ) <EOL> if type : <EOL> self . tagID = type . id <EOL> else : <EOL> self . tagID = None <EOL> self . tags = [ ] <EOL> if buffer : <EOL> self . _parse_buffer ( buffer ) <EOL> if self . tagID == None : <EOL> raise ValueError ( "<STR_LIT>" % ( name ) ) <EOL> def _parse_buffer ( self , buffer ) : <EOL> self . tagID = TAG_Byte ( buffer = buffer ) . value <EOL> self . tags = [ ] <EOL> length = TAG_Int ( buffer = buffer ) <EOL> for x in range ( length . value ) : <EOL> self . tags . append ( TAGLIST [ self . tagID ] ( buffer = buffer ) ) <EOL> def _render_buffer ( self , buffer ) : <EOL> TAG_Byte ( self . tagID ) . _render_buffer ( buffer ) <EOL> length = TAG_Int ( len ( self . tags ) ) <EOL> length . _render_buffer ( buffer ) <EOL> for i , tag in enumerate ( self . tags ) : <EOL> if tag . id != self . tagID : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> ( i , tag , tag . id , self . tagID ) ) <EOL> tag . _render_buffer ( buffer ) <EOL> def __len__ ( self ) : <EOL> return len ( self . tags ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . tags ) <EOL> def __contains__ ( self , item ) : <EOL> return item in self . tags <EOL> def __getitem__ ( self , key ) : <EOL> return self . tags [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> self . tags [ key ] = value <EOL> def __delitem__ ( self , key ) : <EOL> del ( self . tags [ key ] ) <EOL> def insert ( self , key , value ) : <EOL> self . tags . insert ( key , value ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( len ( self . tags ) , TAGLIST [ self . tagID ] . __name__ ) <EOL> def valuestr ( self ) : <EOL> return "<STR_LIT>" % ( len ( self . tags ) , TAGLIST [ self . tagID ] . __name__ ) <EOL> def __unicode__ ( self ) : <EOL> return "<STR_LIT:[>" + "<STR_LIT:U+002CU+0020>" . join ( [ tag . tag_info ( ) for tag in self . tags ] ) + "<STR_LIT:]>" <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT:[>" + "<STR_LIT:U+002CU+0020>" . join ( [ tag . tag_info ( ) for tag in self . tags ] ) + "<STR_LIT:]>" <EOL> def pretty_tree ( self , indent = <NUM_LIT:0> ) : <EOL> output = [ super ( TAG_List , self ) . pretty_tree ( indent ) ] <EOL> if len ( self . tags ) : <EOL> output . append ( ( "<STR_LIT:\t>" * indent ) + "<STR_LIT:{>" ) <EOL> output . extend ( [ tag . pretty_tree ( indent + <NUM_LIT:1> ) for tag in self . tags ] ) <EOL> output . append ( ( "<STR_LIT:\t>" * indent ) + "<STR_LIT:}>" ) <EOL> return '<STR_LIT:\n>' . join ( output ) <EOL> class TAG_Compound ( TAG , MutableMapping ) : <EOL> """<STR_LIT>""" <EOL> id = TAG_COMPOUND <EOL> def __init__ ( self , buffer = None ) : <EOL> super ( TAG_Compound , self ) . __init__ ( ) <EOL> self . tags = [ ] <EOL> self . name = "<STR_LIT>" <EOL> if buffer : <EOL> self . _parse_buffer ( buffer ) <EOL> def _parse_buffer ( self , buffer ) : <EOL> while True : <EOL> type = TAG_Byte ( buffer = buffer ) <EOL> if type . value == TAG_END : <EOL> break <EOL> else : <EOL> name = TAG_String ( buffer = buffer ) . value <EOL> try : <EOL> tag = TAGLIST [ type . value ] ( buffer = buffer ) <EOL> tag . name = name <EOL> self . tags . append ( tag ) <EOL> except KeyError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def _render_buffer ( self , buffer ) : <EOL> for tag in self . tags : <EOL> TAG_Byte ( tag . id ) . _render_buffer ( buffer ) <EOL> TAG_String ( tag . name ) . _render_buffer ( buffer ) <EOL> tag . _render_buffer ( buffer ) <EOL> buffer . write ( b'<STR_LIT:\x00>' ) <EOL> def __len__ ( self ) : <EOL> return len ( self . tags ) <EOL> def __iter__ ( self ) : <EOL> for key in self . tags : <EOL> yield key . name <EOL> def __contains__ ( self , key ) : <EOL> if isinstance ( key , int ) : <EOL> return key <= len ( self . tags ) <EOL> elif isinstance ( key , basestring ) : <EOL> for tag in self . tags : <EOL> if tag . name == key : <EOL> return True <EOL> return False <EOL> elif isinstance ( key , TAG ) : <EOL> return key in self . tags <EOL> return False <EOL> def __getitem__ ( self , key ) : <EOL> if isinstance ( key , int ) : <EOL> return self . tags [ key ] <EOL> elif isinstance ( key , basestring ) : <EOL> for tag in self . tags : <EOL> if tag . name == key : <EOL> return tag <EOL> else : <EOL> raise KeyError ( "<STR_LIT>" % key ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % type ( key ) . __name__ ) <EOL> def __setitem__ ( self , key , value ) : <EOL> assert isinstance ( value , TAG ) , "<STR_LIT>" <EOL> if isinstance ( key , int ) : <EOL> self . tags [ key ] = value <EOL> elif isinstance ( key , basestring ) : <EOL> value . name = key <EOL> for i , tag in enumerate ( self . tags ) : <EOL> if tag . name == key : <EOL> self . tags [ i ] = value <EOL> return <EOL> self . tags . append ( value ) <EOL> def __delitem__ ( self , key ) : <EOL> if isinstance ( key , int ) : <EOL> del ( self . tags [ key ] ) <EOL> elif isinstance ( key , basestring ) : <EOL> self . tags . remove ( self . __getitem__ ( key ) ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def keys ( self ) : <EOL> return [ tag . name for tag in self . tags ] <EOL> def iteritems ( self ) : <EOL> for tag in self . tags : <EOL> yield ( tag . name , tag ) <EOL> def __unicode__ ( self ) : <EOL> return "<STR_LIT:{>" + "<STR_LIT:U+002CU+0020>" . join ( [ tag . tag_info ( ) for tag in self . tags ] ) + "<STR_LIT:}>" <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT:{>" + "<STR_LIT:U+002CU+0020>" . join ( [ tag . tag_info ( ) for tag in self . tags ] ) + "<STR_LIT:}>" <EOL> def valuestr ( self ) : <EOL> return '<STR_LIT>' % len ( self . tags ) <EOL> def pretty_tree ( self , indent = <NUM_LIT:0> ) : <EOL> output = [ super ( TAG_Compound , self ) . pretty_tree ( indent ) ] <EOL> if len ( self . tags ) : <EOL> output . append ( ( "<STR_LIT:\t>" * indent ) + "<STR_LIT:{>" ) <EOL> output . extend ( [ tag . pretty_tree ( indent + <NUM_LIT:1> ) for tag in self . tags ] ) <EOL> output . append ( ( "<STR_LIT:\t>" * indent ) + "<STR_LIT:}>" ) <EOL> return '<STR_LIT:\n>' . join ( output ) <EOL> TAGLIST = { TAG_END : _TAG_End , TAG_BYTE : TAG_Byte , TAG_SHORT : TAG_Short , TAG_INT : TAG_Int , TAG_LONG : TAG_Long , TAG_FLOAT : TAG_Float , TAG_DOUBLE : TAG_Double , TAG_BYTE_ARRAY : TAG_Byte_Array , TAG_STRING : TAG_String , TAG_LIST : TAG_List , TAG_COMPOUND : TAG_Compound , TAG_INT_ARRAY : TAG_Int_Array } <EOL> class NBTFile ( TAG_Compound ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename = None , buffer = None , fileobj = None ) : <EOL> super ( NBTFile , self ) . __init__ ( ) <EOL> self . filename = filename <EOL> self . type = TAG_Byte ( self . id ) <EOL> closefile = True <EOL> if filename : <EOL> self . file = GzipFile ( filename , '<STR_LIT:rb>' ) <EOL> elif buffer : <EOL> if hasattr ( buffer , '<STR_LIT:name>' ) : <EOL> self . filename = buffer . name <EOL> self . file = buffer <EOL> closefile = False <EOL> elif fileobj : <EOL> if hasattr ( fileobj , '<STR_LIT:name>' ) : <EOL> self . filename = fileobj . name <EOL> self . file = GzipFile ( fileobj = fileobj ) <EOL> else : <EOL> self . file = None <EOL> closefile = False <EOL> if self . file : <EOL> self . parse_file ( ) <EOL> if closefile : <EOL> try : <EOL> self . file . close ( ) <EOL> except ( AttributeError , IOError ) : <EOL> pass <EOL> self . file = None <EOL> def parse_file ( self , filename = None , buffer = None , fileobj = None ) : <EOL> """<STR_LIT>""" <EOL> if filename : <EOL> self . file = GzipFile ( filename , '<STR_LIT:rb>' ) <EOL> elif buffer : <EOL> if hasattr ( buffer , '<STR_LIT:name>' ) : <EOL> self . filename = buffer . name <EOL> self . file = buffer <EOL> elif fileobj : <EOL> if hasattr ( fileobj , '<STR_LIT:name>' ) : <EOL> self . filename = fileobj . name <EOL> self . file = GzipFile ( fileobj = fileobj ) <EOL> if self . file : <EOL> try : <EOL> type = TAG_Byte ( buffer = self . file ) <EOL> if type . value == self . id : <EOL> name = TAG_String ( buffer = self . file ) . value <EOL> self . _parse_buffer ( self . file ) <EOL> self . name = name <EOL> self . file . close ( ) <EOL> else : <EOL> raise MalformedFileError ( "<STR_LIT>" ) <EOL> except StructError as e : <EOL> raise MalformedFileError ( "<STR_LIT>" ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def write_file ( self , filename = None , buffer = None , fileobj = None ) : <EOL> """<STR_LIT>""" <EOL> closefile = True <EOL> if buffer : <EOL> self . filename = None <EOL> self . file = buffer <EOL> closefile = False <EOL> elif filename : <EOL> self . filename = filename <EOL> self . file = GzipFile ( filename , "<STR_LIT:wb>" ) <EOL> elif fileobj : <EOL> self . filename = None <EOL> self . file = GzipFile ( fileobj = fileobj , mode = "<STR_LIT:wb>" ) <EOL> elif self . filename : <EOL> self . file = GzipFile ( self . filename , "<STR_LIT:wb>" ) <EOL> elif not self . file : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> TAG_Byte ( self . id ) . _render_buffer ( self . file ) <EOL> TAG_String ( self . name ) . _render_buffer ( self . file ) <EOL> self . _render_buffer ( self . file ) <EOL> try : <EOL> self . file . flush ( ) <EOL> except ( AttributeError , IOError ) : <EOL> pass <EOL> if closefile : <EOL> try : <EOL> self . file . close ( ) <EOL> except ( AttributeError , IOError ) : <EOL> pass <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . filename : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . filename , TAG_Compound . __name__ , self . name , id ( self ) ) <EOL> else : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , TAG_Compound . __name__ , self . name , id ( self ) ) </s>
