def parse_environment(
lib_dir: str, name: str, options: Dict[str, any]
) -> Tool:
"""
Create an environment tool based on the given options.
Parameters
----------
lib_dir : str
The lib directory of the suite where the tools are installed.
name : str
The name of the environment.
options : dict
A dictionary containing the environment options.
Returns
-------
Tool
A Tool object representing the environment.
Raises
------
Exception
If the environment type is not supported or if certain
options are used together.
NotImplementedError
If the environment type is not implemented.
"""
type = options["type"]
depends = options.get("depends", [])
if type == "folder":
env = FolderTool(name, lib_dir, depends=depends)
elif type == "system_venv":
extra_packages = options.get("extra_packages", [])
venv_options = options.get("venv_options", [])
env = SystemEnvTool(
name,
lib_dir,
venv_options=venv_options,
extra_packages=extra_packages,
depends=depends,
)
elif type == "conda" and "environment" in options:
if "env_file" in options or "extra_packages" in options:
raise Exception(
"environment, file and extra_packages options cannot be used at the same time for conda" # noqa: E501
)
environment = options["environment"]
env = CondaEnvTool(
name,
environment,
depends=depends,
conda_activate_cmd=options.get("conda_activate_cmd", "conda"),
)
elif type == "conda" and "env_file" in options:
if "environment" in options:
raise Exception(
"environment and file options cannot be used at the same time for conda" # noqa: E501
)
env = FileCondaEnvTool(
name,
lib_dir,
options["env_file"],
depends,
conda_cmd=options.get("conda_cmd", "conda"),
conda_activate_cmd=options.get("conda_activate_cmd", "conda"),
)
elif type == "conda" and "extra_packages" in options:
if "environment" in options:
raise Exception(
"environment and extra_packages options cannot be used at the same time for conda" # noqa: E501
)
conda_packages = options["extra_packages"]
env = SimpleCondaEnvTool(
name,
lib_dir,
conda_packages,
depends,
conda_cmd=options.get("conda_cmd", "conda"),
conda_activate_cmd=options.get("conda_activate_cmd", "conda"),
)
elif type == "custom":
env = Tool(
name,
depends,
options.get("load", ""),
options.get("unload", ""),
options.get("setup", None),
)
elif type == "venv":
venv_options = options.get("venv_options", "")
extra_packages = options.get("extra_packages", [])
env = VirtualEnvTool(
name,
lib_dir,
venv_options=venv_options,
extra_packages=extra_packages,
depends=depends,
options=options,
)
else:
raise Exception("Environment type {} not supported".format(type))
return env