예제 #1
0
 def read_vector(self):
     r = self._read
     count = self.file.readInt()
     try:
         return tuple(r() for i in xrange(count))
     except StopIteration:
         raise StructError("EOF before all vector elements read")
예제 #2
0
파일: nbtreader.py 프로젝트: dgm3333/mc2mt
 def _parseContent(self, bstream):
     #print ("Parsing TAG_String")
     length = TAG_Short(bstream)
     readbytes = bstream.read(length.value)
     if len(readbytes) != length.value:
         raise StructError()
     self.value = readbytes.decode('utf-8')  #unicode(read, "utf-8")
예제 #3
0
 def read_bytes(self):
     count = self.file.readInt()
     value = self.file.read(count)
     #print 'in read_bytes, len is ', len(value),'value is', value
     if _len(value) != count:
        raise StructError("EOF before reading all of bytes type") 
     return Bytes(value)
def decode_string_utf8(encoded: bytes, key: str) -> str:
    """ Raises StructError in case it's not a valid utf-8 string
    """
    try:
        decoded = encoded.decode('utf-8')
        return decoded
    except UnicodeDecodeError:
        raise StructError('{} must be a valid utf-8 string.'.format(key))
예제 #5
0
 def reads(self):
     next = reads(self.file, self.lookup).next
     while True:
         key = next()
         try:
             value = next()
         except StopIteration:
             raise StructError('EOF before second item in pair')
         yield key, value
예제 #6
0
 def read(self):
     try:
         key = self._read()
     except StopIteration:
         return None
     try:
         value = self._read()
     except StopIteration:
         raise StructError('EOF before second item in pair')
     return key, value
예제 #7
0
 def reads(self):
     it = self._reads()
     next = it.next
     while 1:
         key = next()
         try:
             value = next()
         except StopIteration:
             raise StructError('EOF before second item in pair')
         yield key, value
예제 #8
0
def tx_or_block_from_bytes(data: bytes) -> BaseTransaction:
    """ Creates the correct tx subclass from a sequence of bytes
    """
    # version field takes up the first 2 bytes
    version = int.from_bytes(data[0:2], 'big')
    try:
        tx_version = TxVersion(version)
        cls = tx_version.get_cls()
        return cls.create_from_struct(data)
    except ValueError:
        raise StructError('Invalid bytes to create transaction subclass.')
예제 #9
0
	def _parse_buffer(self, buffer, offset=None):
		length = TAG_Short(buffer=buffer)
		read = buffer.read(length.value)
		if len(read) != length.value:
			raise StructError()
		self.value = unicode(read, "utf-8")
예제 #10
0
 def _parse_buffer(self, buffer):
     length = TAG_Short(buffer=buffer)
     read = buffer.read(length.value)
     if len(read) != length.value:
         raise StructError()
     self.value = read.decode("utf-8")
예제 #11
0
파일: nbt.py 프로젝트: jirenmaa/Python-NBT
 def _read_buffer(self, buffer):
     length = NBTTagShort(buffer=buffer).value
     s = buffer.read(length)
     if len(s) != length:
         raise StructError()
     self.value = s.decode("utf-8")
예제 #12
0
 def invalid_typecode(self):
     raise StructError("Invalid type byte: " + str(self.t))
예제 #13
0
 def read_pickle(self):
     count = self.file.readInt()
     bytes = self.file.read(count)
     if _len(bytes) != count:
         raise StructError("EOF before reading all of bytes type")
     return loads(bytes)
예제 #14
0
 def read_list(self):
     value = list(self._reads())
     if self.eof:
         raise StructError("EOF before end-of-list marker")
     return value
예제 #15
0
 def read_unicode(self):
     count = self.file.readInt()
     value = self.file.read(count)
     if _len(value) != count:
         raise StructError("EOF before reading all of string")
     return value.decode(UNICODE_ENCODING, self.unicode_errors)
예제 #16
0
 def read_string(self):
     count = self.file.readInt()
     value = self.file.read(count)
     if _len(value) != count:
         raise StructError("EOF before reading all of string")
     return value
예제 #17
0
 def read_bytes(self):
     count = unpack_int(self.file.read(4))[0]
     value = self.file.read(count)
     if _len(value) != count:
         raise StructError("EOF before reading all of bytes type")
     return Bytes(value)