Source code for passengersim._demo
import importlib.resources
import pathlib
[docs]
def demo_network(name: str) -> pathlib.Path:
"""
Load a demo network configuration file.
A few demo networks are bundled with the PassengerSim package, and are always
available to users.
Parameters
----------
name : str
The name of the network directory or a network file. If it is a directory,
the function will look for a `__main__.yaml` file in that directory. Otherwise
it will look for a file with the given name, with a .yaml or .yml extension.
If the extension is not provided, it will be added automatically.
Returns
-------
pathlib.Path
Path object pointing to the network configuration file.
Notes
-----
The network name is converted to lowercase to handle case sensitivity
differences between macOS (case-insensitive) and Linux (case-sensitive)
file systems. All packaged network files are stored in lowercase.
"""
demo_file_base = importlib.resources.files(__package__).joinpath("networks")
for ext in (".yaml", ".yml"):
if demo_file_base.joinpath(name.lower(), "__main__" + ext).exists():
return demo_file_base.joinpath(name.lower(), "__main__" + ext)
if not name.endswith(".yaml"):
name = f"{name}.yaml"
# note: on macOS by default the file system is not case sensitive, but
# on Linux it is. All the network files are lowercase, so we can get around
# this by forcing the name to lowercase.
return demo_file_base.joinpath(name.lower())
[docs]
def demo_output(name: str) -> pathlib.Path:
"""
Load a demo output file.
A few demo output files are bundled with the PassengerSim package, and are always
available to user, without needing to load or run a simulation.
Parameters
----------
name : str
The name of the output file, with or without the .pxsim extension.
If the extension is not provided, it will be added automatically.
Returns
-------
pathlib.Path
Path object pointing to the output file.
Notes
-----
The output name is converted to lowercase to handle case sensitivity
differences between macOS (case-insensitive) and Linux (case-sensitive)
file systems. All output files are stored in lowercase.
"""
if not name.endswith(".pxsim"):
name = f"{name}.pxsim"
return importlib.resources.files(__package__).joinpath("networks").joinpath(name.lower())