Config

class moccasin.config.Config(root_path: Path | None)

Bases: object

A wrapper around the moccasin.toml file, serving as the main entry point for performing almost any action with Moccasin.

This class reads the moccasin.toml file and sets up project configuration.

Variables:
  • _project_root (Path) – The root directory of the project.

  • _toml_data (dict) – The parsed TOML data from the moccasin.toml file.

  • dependencies (list[str]) – A list of dependencies specified in the moccasin.toml file.

  • project (dict[str, str]) – A dictionary containing project details from the moccasin.toml file.

  • extra_data (dict[str, Any]) – A dictionary containing extra data from the moccasin.toml file.

  • networks (_Networks) – A dictionary containing network data from the moccasin.toml file.

dependencies: list[str]
extra_data: dict[str, Any]
project: dict[str, str]
networks: _Networks
reload()

Reload the configuration by reinitializing the Config object.

get_config_path() Path

Get the path to the moccasin.toml configuration file.

Returns:

The path to the moccasin.toml file.

Return type:

Path

read_configs_preserve_comments(moccasin_config_path: Path | None = None, pyproject_config_path: Path | None = None) TOMLDocument

Read configuration files while preserving comments.

Parameters:
  • moccasin_config_path (Path or None) – Path to the moccasin.toml file. Defaults to None.

  • pyproject_config_path (Path or None) – Path to the pyproject.toml file. Defaults to None.

Returns:

Merged configuration as a TOMLDocument.

Return type:

tomlkit.TOMLDocument

read_configs(moccasin_path: Path | None = None, pyproject_path: Path | None = None) dict

Read and merge configuration data from moccasin.toml and pyproject.toml.

Parameters:
  • moccasin_path (Path or None) – Path to the moccasin.toml file. Defaults to None.

  • pyproject_path (Path or None) – Path to the pyproject.toml file. Defaults to None.

Returns:

Merged configuration data.

Return type:

dict

read_moccasin_config(config_path: Path | None = None) dict

Read the moccasin.toml configuration file.

Parameters:

config_path (Path or None) – Path to the moccasin.toml file. Defaults to None.

Returns:

Configuration data from moccasin.toml.

Return type:

dict

read_pyproject_config(pyproject_path: Path | None = None) dict

Read the pyproject.toml configuration file.

Parameters:

pyproject_path (Path or None) – Path to the pyproject.toml file. Defaults to None.

Returns:

Configuration data from pyproject.toml.

Return type:

dict

expand_env_vars(value)

Expand environment variables in the given value.

Parameters:

value (str, list, or dict) – The value to process. Can be a string, list, or dictionary.

Returns:

The value with expanded environment variables.

Return type:

Same as input type

get_networks() dict[str, Network]

Get all network configurations.

Returns:

A dictionary of network configurations.

Return type:

dict[str, Network]

get_active_network() Network

Get the currently active network configuration.

Returns:

The active network.

Return type:

Network

get_default_db_path() Path

Get the default database path for the active network.

Returns:

The path to the default database.

Return type:

Path

get_or_deploy_named_contract(*args, **kwargs) VyperContract | ZksyncContract | ABIContract

Retrieve or deploy a named contract.

Returns:

The deployed contract.

Return type:

VyperContract, ZksyncContract, or ABIContract

get_dependencies() list[str]

Get the list of project dependencies.

Returns:

A list of dependency names.

Return type:

list[str]

write_dependencies(dependencies: list)

Writes the dependencies to the config file.

If a moccasin.toml file exists, it will write there, otherwise, it’ll write to pyproject.toml.

This will overwrite the existing dependencies with the new ones. So if you wish to keep old ones, read from the dependencies first.

Parameters:

dependencies (list) – A list of dependencies to write.

get_base_dependencies_install_path() Path

Get the base path for installing dependencies.

Returns:

The path for dependency installation.

Return type:

Path

get_root() Path

Get the project root path.

Returns:

The project root directory.

Return type:

Path

find_contract(contract_or_contract_path: str) Path

Find a contract by its name or path.

Parameters:

contract_or_contract_path (str) – The name or path of the contract.

Returns:

The path to the contract.

Return type:

Path

set_active_network(name_url_or_id: str | Network, activate_boa=True, **kwargs) Network

Set the active network.

Parameters:
  • name_url_or_id (str or Network) – The name, URL, or ID of the network.

  • activate_boa (bool) – Whether to activate Boa. Defaults to True.

Returns:

The active network.

Return type:

Network

activate_boa()

Activate the boa env for the active network.

property config_path: Path
property pyproject_path: Path
property project_root: Path
property build_folder: str
property out_folder: str
property contracts_folder: str
property src_folder: str
property cov_config: str | None
property dot_env: str
property test_folder: str
property script_folder: str
property lib_folder: str
property default_network: str
property default_network_name: str
static load_config_from_root(project_root: Path | None = None) Config

Load configuration from the project root.

Parameters:

project_root (Path or None) – The project root directory. Defaults to None.

Returns:

The Config instance.

Return type:

Config

static find_project_root(start_path: Path | str | None = None) Path

Find the root directory of the project.

Parameters:

start_path (Path, str, or None) – The starting path to search from. Defaults to None.

Returns:

The project root directory.

Return type:

Path

static read_moccasin_toml(config_path: Path) dict

Read the moccasin.toml configuration file.

Parameters:

config_path (Path) – Path to the moccasin.toml file.

Returns:

Configuration data from the file.

Return type:

dict

static read_moccasin_toml_preserve_comments(config_path: Path) TOMLDocument

Reads the moccasin.toml file at the given path, preserving TOMLDocument comments.

Parameters:

config_path (Path) – The path to the configuration file.

Returns:

The TOML document with preserved comments, or an empty document if the file does not exist.

Return type:

tomlkit.TOMLDocument

static read_pyproject_toml_preserve_comments(config_path: Path) TOMLDocument

Reads the pyproject.toml file at the given path, preserving TOMLDocument comments.

Parameters:

config_path (Path) – The path to the configuration file.

Returns:

The TOML document with preserved comments, or an empty document if the file does not exist.

Return type:

tomlkit.TOMLDocument

static read_pyproject_toml(pyproject_path: Path) dict

Reads the pyproject.toml file and extracts the moccasin configuration.

Parameters:

pyproject_path (Path) – The path to the pyproject.toml file.

Returns:

The moccasin configuration dictionary, or an empty dictionary if the config does not exist.

Return type:

dict

static merge_configs(moccasin_config_dict: dict | TOMLDocument, pyproject_config_dict: dict | TOMLDocument) dict | TOMLDocument

Merges the moccasin and pyproject configuration dictionaries.

If dependencies are defined in both files, moccasin.toml takes precedence.

Parameters:
  • moccasin_config_dict (Union[dict, tomlkit.TOMLDocument]) – Configuration from moccasin.toml.

  • pyproject_config_dict (Union[dict, tomlkit.TOMLDocument]) – Configuration from pyproject.toml.

Returns:

The merged configuration dictionary.

Return type:

Union[dict, tomlkit.TOMLDocument]

static nested_tomlkit_update(toml_data: TOMLDocument, keys: list, value: str | int | float | bool | list | dict) TOMLDocument

Updates a nested key-value pair in a TOML document.

Parameters:
  • toml_data (tomlkit.TOMLDocument) – The TOML document to update.

  • keys (list) – A list of keys representing the nested path.

  • value (str | int | float | bool | list | dict) – The value to set.

Returns:

The updated TOML document.

Return type:

tomlkit.TOMLDocument