srctools.filesys
Implements a consistent interface for reading files.
This allows accessing different backing file sources interchangably. Files are case-insensitive, and both slashes are converted to ‘/’.
With a filesystem object, you can index it to locate files inside (raising
FileNotFoundError
if missing). Once a File
object is obtained,
call open_bin()
or open_str()
to read the contents. Writing is not
supported.
FileSystem
is the baseABC
, which can be subclassed to define additional subsystems.RawFileSystem
provides access to a directory, optionally prohibiting access to parent folders.ZipFileSystem
andVPKFileSystem
provide access to their respective archives.VirtualFileSystem
redirects to a mapping object, for fake file contents already in memory.FileSystemChain
contains multiple other systems, concatenating them together like how Source treats its search paths.
See the srctools.game.Game
class for parsing gameinfo.txt
files into filesystems.
To mount a BSP
file, use ZipFileSystem(bsp.pakfile)
.
-
class
srctools.filesys.
File
(system: FileSysT, path: str, data: Any) Represents a file in a system. Should only be created by filesystems..
-
sys
: FileSysT
-
-
class
srctools.filesys.
FileSystem
(path: Union[str, _os.PathLike[str]]) Base class for different systems defining the interface.
-
read_kv1
(path: str, encoding: str = 'utf8') Keyvalues Read a Keyvalues1 file from the filesystem.
This handles opening and closing files.
-
read_prop
(path: str, encoding: str = 'utf8') Keyvalues Read a Keyvalues1 file from the filesystem.
Deprecated: Use read_kv1()
.
-
walk_folder
(folder: str = '') Iterator[File[FileSysT]] Iterate over all files in the specified subfolder, yielding each.
-
-
srctools.filesys.
get_filesystem
(path: str) FileSystem Return a filesystem given a path.
If the path is a directory this returns a RawFileSystem. Otherwise, it returns a VPK or zip, depending on extension.
-
srctools.filesys.
CACHE_KEY_INVALID
: Final = -1 This is returned from
FileSystem.cache_key()
to indicate no key could be computed.
-
class
srctools.filesys.
RawFileSystem
() Accesses files in a real folder.
This can prohibit access to folders above the root.
-
class
srctools.filesys.
VPKFileSystem
(path: Union[str, _os.PathLike[str]]) Accesses files in a VPK file.
-
class
srctools.filesys.
ZipFileSystem
() Accesses files in a zip file.
-
open_bin
(name: Union[str, File[ZipFSysT]]) BinaryIO Open a file in bytes mode or raise FileNotFoundError.
The filesystem needs to be open while accessing this.
-
open_str
() TextIOWrapper Open a file in unicode mode or raise FileNotFoundError.
The filesystem needs to be open while accessing this.
-
-
class
srctools.filesys.
VirtualFileSystem
() Access a dict as if it were a filesystem.
The dict should map file paths to either bytes or strings. The encoding arg specifies how text data is presented if open_bin() is called.
-
class
srctools.filesys.
FileSystemChain
() Chains several filesystem into one prioritised whole.
Each system can additionally be filtered to only allow access to files inside a subfolder. These will appear as if they are at the root level.
-
classmethod
get_system
(file: File[ChainFSysT]) FileSystem Retrieve the system for a File, if it was produced from a FileSystemChain.
-
add_sys
() None Add a filesystem to the list.
Parameters: - priority – If True, the system will be added to the start, instead of the end.
- sys – The filesystem to add.
- prefix – If specified, only this subfolder’s contents will be accessible, instead of the whole system.
-
open_str
() TextIO Open a file in unicode mode or raise FileNotFoundError.
This should be closed when done.
-
open_bin
(name: Union[str, File[ChainFSysT]]) BinaryIO Open a file in bytes mode or raise FileNotFoundError.
This should be closed when done.
-
walk_folder
(folder: str = '') Iterator[File[ChainFSysT]] Walk folders, not repeating files.
This requires temporarily storing the visited paths, to prevent revisiting them. If repeated access is not problematic, prefer
walk_folder_repeat()
.
-
walk_folder_repeat
(folder: str = '') Iterator[File[ChainFSysT]] Walk folders, but allow repeating files.
If a file is contained in multiple systems, it will be yielded for each. The first is the highest-priority. Using this instead of
walk_folder()
is cheaper, since a set of visited files must be maintained.
-
classmethod