class EmptyStringTest(unittest.TestCase): def setUp(self): self.golden_value = b"\x0A\0\x04Test\x08\0\x0Cempty string\0\0\0" self.nbtfile = NBTFile(buffer=BytesIO(self.golden_value)) def testReadEmptyString(self): self.assertEqual(self.nbtfile.name, "Test") self.assertEqual(self.nbtfile["empty string"].value, "") def testWriteEmptyString(self): buffer = BytesIO() self.nbtfile.write_file(buffer=buffer) self.assertEqual(buffer.getvalue(), self.golden_value)
def unpack_raw(raw, export=False): """Unpack raw b64 encoded gzipped nbt data Steps: 1. Decode base 64 2. nbt.nbt.NBTFile(fileobj=io.BytesIO()) 3. Walk tree and return it as json/dict """ decoded = base64.b64decode(raw) nbtfile = NBTFile(fileobj=io.BytesIO(decoded)) if export: nbtfile.write_file( f'nbt{datetime.datetime.now().strftime("%Y%m%d-%H%M%S-%f.nbt")}') return
def testProperlyClosed(self): """ test that files opened from a file name are closed after being written to. i.e. will read correctly in the future """ #open the file mynbt = NBTFile(self.filename) mynbt['test'] = TAG_Int(123) mynbt.write_file() if hasattr(mynbt.file, "closed"): self.assertTrue(mynbt.file.closed) else: # GZipFile does not yet have a closed attribute in Python 2.6 self.assertTrue(mynbt.file.fileobj == None) # make sure it can be read again directly after closing, # and contains the updated contents. mynbt = NBTFile(self.filename) self.assertEqual(mynbt['test'].value, 123)
def testProperlyClosed(self): """ test that files opened from a file name are closed after being written to. i.e. will read correctly in the future """ # copy the file (don't work on the original test file) tempdir = tempfile.mkdtemp() filename = os.path.join(tempdir, 'bigtest.nbt') shutil.copy(NBTTESTFILE, filename) #open the file f = NBTFile(filename) f.write_file() # make sure it can be read again directly after f = NBTFile(filename) # remove the temporary file try: shutil.rmtree(tempdir) except OSError as e: raise
def testWriteback(self): mynbt = NBTFile(self.filename) mynbt.write_file()
def testWriteBig(self): mynbt = NBTFile(self.filename) output = BytesIO() mynbt.write_file(buffer=output) self.assertEqual(GzipFile(NBTTESTFILE).read(), output.getvalue())
def tree_to_nbt(nbt: Token, filename: str): nbt_file = NBTFile() nbt_file.tags = [x.value for x in nbt._value] # Fix this nbt_file.write_file(filename)