def test_vfs_contents(self): vfs = VirtualFileSystem(root_package="os") with self.assertRaises(VfsError): vfs.contents() vfs = VirtualFileSystem(root_path=".") self.assertIn("MANIFEST.in", vfs.contents()) self.assertIn("make.bat", vfs.contents("docs")) with self.assertRaises(FileNotFoundError): vfs.contents("@dummy@")
def test_vfs_write_stream(self): vfs = VirtualFileSystem(root_path=".", readonly=False) with vfs.open_write("unittest.txt") as f: f.write("test write") self.assertEqual("test write", vfs["unittest.txt"].text) with vfs.open_write("unittest.txt", append=False) as f: f.write("overwritten") self.assertEqual("overwritten", vfs["unittest.txt"].text) with vfs.open_write("unittest.txt", append=True) as f: f.write("appended") self.assertEqual("overwrittenappended", vfs["unittest.txt"].text) del vfs["unittest.txt"]
def test_vfs_read_files(self): vfs = VirtualFileSystem(root_path=".", readonly=True) # text file resource = vfs["tests/files/test.txt"] mtime = os.path.getmtime("tests/files/test.txt") self.assertEqual(mtime, resource.mtime) self.assertTrue(resource.is_text) self.assertEqual("text/plain", resource.mimetype) self.assertEqual( "€ This is a test text file. This is line 1.\n€ This is a test text file. This is line 2.\n", resource.text[:88]) lines = resource.text.splitlines() self.assertEqual(10, len(lines)) self.assertEqual(441, len(resource)) self.assertEqual(441, len(resource.text)) self.assertEqual("T", resource[2]) # binary file resource = vfs["tests/files/image.png"] self.assertEqual("image/png", resource.mimetype) self.assertFalse(resource.is_text) self.assertEqual(487, len(resource)) self.assertEqual(487, len(resource.data)) self.assertEqual(78, resource[2]) self.assertEqual(b"\x89PNG", resource.data[0:4]) # text file but without proper suffix, so read as binary resource = vfs["tests/files/readme"] self.assertEqual("application/octet-stream", resource.mimetype) self.assertFalse(resource.is_text) self.assertEqual(471, len(resource.data))
def test_vfs_load_and_names(self): vfs = VirtualFileSystem(root_package="os") with self.assertRaises(VfsError): _ = vfs["a\\b"] with self.assertRaises(VfsError): _ = vfs["/abs/path"] with self.assertRaises(IOError): _ = vfs["normal/text"] with self.assertRaises(IOError): _ = vfs["normal/image"] vfs = VirtualFileSystem(root_path=".") with self.assertRaises(IOError): _ = vfs["test_doesnt_exist_999.txt"] with self.assertRaises(VfsError): _ = VirtualFileSystem(root_path="/@@@does_not_exist.foo@@@") with self.assertRaises(VfsError): _ = VirtualFileSystem(root_package="non.existing.package.name") with self.assertRaises(VfsError): _ = VirtualFileSystem(root_package="non_existing_package_name")
def get_shops(vfs: VirtualFileSystem = None) -> Dict[int, SimpleNamespace]: if not _shops: vfs = vfs or VirtualFileSystem(root_package="zones.circledata", everythingtext=True) for filename in vfs["world/shp/index"].text.splitlines(): if filename == "$": break data = vfs["world/shp/" + filename].text.splitlines() for shop in parse_file(data): _shops[shop.circle_vnum] = shop assert len(_shops) == 46, "all shops must be loaded" return _shops
def get_zones(vfs: VirtualFileSystem = None) -> Dict[int, ZZone]: if not _zones: vfs = vfs or VirtualFileSystem(root_package="zones.circledata", everythingtext=True) for filename in vfs["world/zon/index"].text.splitlines(): if filename == "$": break zone = parse_file(vfs["world/zon/" + filename].text) _zones[zone.vnum] = zone assert len(_zones) == 30, "all zones must be loaded" return _zones
def get_mobs(vfs: VirtualFileSystem = None) -> Dict[int, SimpleNamespace]: if not _mobs: vfs = vfs or VirtualFileSystem(root_package="zones.circledata", everythingtext=True) for filename in vfs["world/mob/index"].text.splitlines(): if filename == "$": break data = vfs["world/mob/" + filename].text.splitlines() for mob in parse_file(data): _mobs[mob.circle_vnum] = mob assert len(_mobs) == 569, "all mobs must be loaded" return _mobs
def get_rooms(vfs: VirtualFileSystem = None) -> Dict[int, SimpleNamespace]: if not _rooms: vfs = vfs or VirtualFileSystem(root_package="zones.circledata", everythingtext=True) for filename in vfs["world/wld/index"].text.splitlines(): if filename == "$": break data = vfs["world/wld/" + filename].text.splitlines() for room in parse_file(data): _rooms[room.circle_vnum] = room assert len(_rooms) == 1878, "all rooms must be loaded" return _rooms
def get_objs(vfs: VirtualFileSystem = None) -> Dict[int, SimpleNamespace]: if not _objs: vfs = vfs or VirtualFileSystem(root_package="zones.circledata", everythingtext=True) for filename in vfs["world/obj/index"].text.splitlines(): if filename == "$": break data = vfs["world/obj/" + filename].text.splitlines() result = parse_file(data) for obj in result: _objs[obj.circle_vnum] = obj assert len(_objs) == 679, "all objs must be loaded" return _objs
def test_vfs_everythingtext(self): vfs = VirtualFileSystem(root_path=".", everythingtext=True) # text file resource = vfs["tests/files/test.txt"] self.assertEqual(441, len(resource.text)) # text file without proper suffix resource2 = vfs["tests/files/readme"] self.assertEqual("text/plain", resource2.mimetype) self.assertEqual(441, len(resource2.text)) self.assertEqual(resource.text, resource2.text) # binary file, but is read as text with self.assertRaises(UnicodeError): resource = vfs["tests/files/image.png"]
def test_vfs_read_compressed(self): vfs = VirtualFileSystem(root_path=".", readonly=True) uncompressed = vfs["tests/files/test.txt"].text resource = vfs["tests/files/test.txt.bz2"] self.assertEqual(uncompressed, resource.text) resource = vfs["tests/files/test.txt.gz"] self.assertEqual(uncompressed, resource.text) resource = vfs["tests/files/test.txt.xz"] self.assertEqual(uncompressed, resource.text) with self.assertRaises(VfsError) as x: resource = vfs["tests/files/test.txt.Z"] self.assertTrue(str(x.exception).startswith("unsupported compressor")) uncompressed = vfs["tests/files/image.png"].data resource = vfs["tests/files/image.png.gz"] self.assertEqual(uncompressed, resource.data)
def test_vfs_storage(self): with self.assertRaises(ValueError): _ = VirtualFileSystem(root_package="os", readonly=False) vfs = VirtualFileSystem(root_path=".", readonly=False) with self.assertRaises(IOError): _ = vfs["test_doesnt_exist_999.txt"] vfs["unittest.txt"] = "Test1\nTest2\n" rsc = vfs["unittest.txt"] self.assertEqual("Test1\nTest2\n", rsc.text) self.assertEqual("text/plain", rsc.mimetype) self.assertEqual(12, len(rsc)) self.assertEqual("unittest.txt", rsc.name) vfs["unittest.txt"] = "Test1\nTest2\n" rsc = vfs["unittest.txt"] self.assertEqual("Test1\nTest2\n", rsc.text) vfs["unittest.jpg"] = b"imagedata\nblob" rsc = vfs["unittest.jpg"] self.assertEqual(b"imagedata\nblob", rsc.data) self.assertTrue(rsc.mimetype in ("image/jpeg", "image/pjpeg")) self.assertEqual(14, len(rsc)) self.assertEqual("unittest.jpg", rsc.name) vfs["unittest.jpg"] = rsc del vfs["unittest.txt"] del vfs["unittest.jpg"]
def test_vfs_validate_path(self): vfs = VirtualFileSystem(root_path=".") vfs.validate_path(".") vfs.validate_path("./foo") vfs.validate_path("./foo/bar") vfs.validate_path(".") with self.assertRaises(VfsError): vfs.validate_path(r".\wrong\slash") with self.assertRaises(VfsError): vfs.validate_path(r"/absolute/not/allowed") with self.assertRaises(VfsError): vfs.validate_path(r"./foo/../../../../../rootescape/notallowed")
def test_vfs_readonly(self): vfs = VirtualFileSystem(root_path=".") with self.assertRaises(VfsError): vfs.open_write("test.txt") with self.assertRaises(VfsError): vfs["test.txt"] = "data"
msg_shopsolditem=shopsolditemArg, msg_shopboughtitem=shopboughtitemArg, msg_temper=temperArg, rooms={int(vnum) for vnum in shopRoomsArg}, wontdealwith=wontdealattr ) result.append(shop) raise IOError("Expected $~ at end of file") _shops = {} # type: Dict[int, SimpleNamespace] def get_shops(vfs: VirtualFileSystem = None) -> Dict[int, SimpleNamespace]: if not _shops: vfs = vfs or VirtualFileSystem(root_package="zones.circledata", everythingtext=True) for filename in vfs["world/shp/index"].text.splitlines(): if filename == "$": break data = vfs["world/shp/" + filename].text.splitlines() for shop in parse_file(data): _shops[shop.circle_vnum] = shop assert len(_shops) == 46, "all shops must be loaded" return _shops if __name__ == "__main__": vfs = VirtualFileSystem(root_path=".", everythingtext=True) result = get_shops(vfs=vfs) print("parsed", len(result), "shops.")
def test_vfs_read_autoselectcompressed(self): vfs = VirtualFileSystem(root_path=".", readonly=True) resource = vfs["tests/files/compressed.png"] self.assertEqual(487, len(resource)) self.assertEqual("tests/files/compressed.png.gz", resource.name)