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)
.
- exception srctools.filesys.RootEscapeError(root: str, path: str)
Raised when a path tries to refer to a file outside the root of a filesystem.
- srctools.filesys.get_filesystem(
- path: str,
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 = -1
This is returned from
FileSystem.cache_key()
to indicate no key could be computed.
- class srctools.filesys.FileSystem(path: str | _os.PathLike[str])
Base class for different systems defining the interface.
- __init__(
- path: str | _os.PathLike[str],
- read_kv1( ) Keyvalues
Read a Keyvalues1 file from the filesystem.
This handles opening and closing files.
- read_prop( ) Keyvalues
Read a Keyvalues1 file from the filesystem.
- Deprecated:
Use
read_kv1()
.
- classmethod _get_data( ) _FileDataT
Accessor for file._data, to show the relationship to the type checker.
It should only be available to that filesystem, and produces the type.
- walk_folder(
- folder: str = '',
Iterate over all files in the specified subfolder, yielding each.
- open_str( ) TextIO
Open a file in unicode mode or raise FileNotFoundError.
This should be closed when done.
- open_bin( ) BinaryIO
Open a file in bytes mode or raise FileNotFoundError.
This should be closed when done.
- _get_cache_key( ) int
Return a checksum or last-modified date suitable for caching.
This allows preventing reparsing the file. If not possible, return CACHE_KEY_INVALID (-1).
- __weakref__
list of weak references to the object (if defined)
- class srctools.filesys.File(
- system: FileSysT_co,
- path: str,
- data: Any,
Represents a file in a system. Should only be created by filesystems.
- __init__(
- system: FileSysT_co,
- path: str,
- data: Any,
Create a File.
- Parameters:
system – should be the filesystem which matches.
path – is the relative path for the file.
data – is filesystem-specific data, allowing directly opening the file.
- open_str(encoding: str = 'utf8') TextIO
Return a file-like object in unicode mode.
This should be closed when done.
- cache_key() int
Return a checksum or last-modified date suitable for caching.
This allows preventing reparsing the file. If not possible, return -1.
- __weakref__
list of weak references to the object (if defined)
Concrete implementations
- class srctools.filesys.FileSystemChain(
- *systems: FileSystem[Any] | Tuple[FileSystem[Any], str],
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( ) FileSystem[Any]
Retrieve the system for a File, if it was produced from a FileSystemChain.
- add_sys(
- sys: FileSystem[Any],
- prefix: str = '',
- *,
- priority: bool = False,
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( ) BinaryIO
Open a file in bytes mode or raise FileNotFoundError.
This should be closed when done.
- walk_folder(
- folder: str = '',
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 = '',
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.
- class srctools.filesys.RawFileSystem(
- path: str | _os.PathLike[str],
- constrain_path: bool = True,
Accesses files in a real folder.
This can prohibit access to folders above the root.
- 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.ZipFileSystem(
- path: str | _os.PathLike[str],
- zipfile: ZipFile | None = None,
Accesses files in a zip file.