srctools

A number of utilities are present in the toplevel of the srctools package.

File Handling

srctools.clean_line(line: str) str

Removes extra spaces and comments from the input.

srctools.is_plain_text(
name: str,
valid_chars: Container[str] = frozenset({' ', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '|'}),
) bool

Check to see if any characters are not in the whitelist.

srctools.escape_quote_split(line: str) List[str]

Split quote values on a line, handling " correctly.

class srctools.AtomicWriter(
filename: str | PathLike[str],
is_bytes: Literal[True],
)
class srctools.AtomicWriter(
filename: str | PathLike[str],
is_bytes: Literal[False] = False,
encoding: str = 'utf8',
)

Atomically overwrite a file.

Use as a context manager - the returned temporary file should be written to. When cleanly exiting, the file will be transferred. If an exception occurs in the body, the temporary data will be discarded.

This is not reentrant, but can be repeated - starting the context manager clears the file.

Type Conversion

srctools.bool_as_int(val: object) Literal['0', '1']

Convert a True/False value into ‘1’ or ‘0’.

Valve uses these strings for True/False in editoritems and other config files.

srctools.conv_int(val: int | float | str) int
srctools.conv_int(
val: int | float | str,
default: ValT,
) ValT | int

Converts a string to an integer, using a default if it fails.

The value may also be an integer or float, which is also converted.

srctools.conv_float(val: int | float | str) float
srctools.conv_float(
val: int | float | str,
default: ValT,
) ValT | float

Converts a string to a float, using a default if it fails.

The value may also be an integer or float, which is also converted.

srctools.conv_bool(val: str | bool | None) bool
srctools.conv_bool(
val: str | bool | None,
default: ValT,
) ValT | bool

Converts a string to a boolean, using a default if it fails.

Accepts any of 0, 1, false, true, yes, no. If val is None, this always returns the default. 0, 1, True and False will be passed through unchanged.

srctools.BOOL_LOOKUP: Mapping[str, bool]

BOOL_LOOKUP is a constant mapping string values to the appropriate boolean result, the same way conv_bool() functions.

srctools.EmptyMapping: collections.abc.MutableMapping[Any, Any]

This is a constant mapping which behaves as if it is always empty. It is intended for use in default parameter values, and other fallbacks. It also supports writing operations, which simply do nothing.

Aliases

Several classes from other modules are automatically imported, allowing for more convenient access.

class srctools.Vec
class srctools.Angle
class srctools.Matrix
class srctools.Vec_tuple
srctools.parse_vec_str()
srctools.lerp()

These are found in srctools.math.


class srctools.NoKeyError
class srctools.KeyValError
class srctools.Keyvalues

These are found in srctools.keyvalues.


class srctools.FileSystem
class srctools.FileSystemChain
srctools.get_filesystem()

These are found in srctools.filesys.


class srctools.VMF
class srctools.Entity
class srctools.Solid
class srctools.Side
class srctools.Output
class srctools.UVAxis

These are found in srctools.vmf.


class srctools.SurfaceProp
class srctools.SurfChar

These are found in srctools.surfaceprop.


class srctools.VPK

This is srctools.vpk.VPK.

class srctools.FGD

This is srctools.fgd.FGD.

class srctools.GameID

This is srctools.const.GameID.

class srctools.VTF

This is srctools.vtf.VTF.