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")
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")
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))
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
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
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
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.')
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")
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")
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")
def invalid_typecode(self): raise StructError("Invalid type byte: " + str(self.t))
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)
def read_list(self): value = list(self._reads()) if self.eof: raise StructError("EOF before end-of-list marker") return value
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)
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
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)