Skip to content

pyproject

dev_tool.config.pyproject

log = logging.getLogger(__name__) module-attribute

PyprojectConfig

A class for managing pyproject.toml configuration.

This class provides methods for loading, parsing, and retrieving settings from the pyproject.toml file.

The constructor for the PyprojectConfig class.

This method initializes an empty configuration dictionary.

Source code in dev_tool/config/pyproject.py
def __init__(self) -> None:
    """
    The constructor for the PyprojectConfig class.

    This method initializes an empty configuration dictionary.
    """

    self._pyproject = {}

create_default

A method that creates a default pyproject.toml file.

This method copies the content from a template file to create a new pyproject.toml file if it doesn't already exist.

Parameters:

  • path (Path) –

    The path where the new file should be created.

  • template (Path) –

    The path to the template file.

Source code in dev_tool/config/pyproject.py
def create_default(self, path: Path, template: Path) -> None:
    """
    A method that creates a default pyproject.toml file.

    This method copies the content from a template file to create a new
    pyproject.toml file if it doesn't already exist.

    :param path: The path where the new file should be created.
    :param template: The path to the template file.
    """

    if path.exists():
        return

    if not template.exists():
        return

    with template.open('r', encoding='utf-8') as handle:
        content = handle.read()

    with path.open('w', encoding='utf-8') as handle:
        handle.write(content)

    self.reload(path)

get_value

A method that retrieves a value from the pyproject.toml configuration.

This method traverses the configuration using the provided keys and returns the default value if the path doesn't exist.

Parameters:

  • keys (str, default: () ) –

    The sequence of keys to navigate through.

  • default (Any, default: None ) –

    The default value to return if the path doesn't exist.

Returns:

  • Any

    The value at the specified path, or the default.

Source code in dev_tool/config/pyproject.py
def get_value(self, *keys: str, default: Any = None) -> Any:
    """
    A method that retrieves a value from the pyproject.toml configuration.

    This method traverses the configuration using the provided keys
    and returns the default value if the path doesn't exist.

    :param keys: The sequence of keys to navigate through.
    :param default: The default value to return if the path doesn't exist.
    :return: The value at the specified path, or the default.
    """

    value = self._pyproject

    for key in keys:
        if not isinstance(value, dict) or key not in value:
            return default

        value = value[key]

    return value

get_project_name

A method that retrieves the project name.

This method gets the project name from the configuration.

Returns:

  • str

    The project name, or 'project' if not specified.

Source code in dev_tool/config/pyproject.py
def get_project_name(self) -> str:
    """
    A method that retrieves the project name.

    This method gets the project name from the configuration.

    :return: The project name, or 'project' if not specified.
    """

    return self.get_value('project', 'name', default='project')

get_project_config

A method that retrieves a project configuration section.

This method gets a specific section from the project configuration.

Parameters:

  • key (str) –

    The section key to retrieve.

Returns:

  • dict[str, Any]

    The configuration section as a dictionary.

Source code in dev_tool/config/pyproject.py
def get_project_config(self, key: str) -> dict[str, Any]:
    """
    A method that retrieves a project configuration section.

    This method gets a specific section from the project configuration.

    :param key: The section key to retrieve.
    :return: The configuration section as a dictionary.
    """

    project_config = self.get_value('project', default={})
    return project_config.get(key, {}) if isinstance(project_config, dict) else {}

get_tool_config

A method that retrieves a tool configuration section.

This method gets a specific section from the tool configuration.

Parameters:

  • key (str) –

    The section key to retrieve.

Returns:

  • dict[str, Any]

    The configuration section as a dictionary.

Source code in dev_tool/config/pyproject.py
def get_tool_config(self, key: str) -> dict[str, Any]:
    """
    A method that retrieves a tool configuration section.

    This method gets a specific section from the tool configuration.

    :param key: The section key to retrieve.
    :return: The configuration section as a dictionary.
    """

    tool_config = self.get_value('tool', default={})
    return tool_config.get(key, {}) if isinstance(tool_config, dict) else {}

get_dev_tool_config

A method that retrieves the dev_tool configuration.

This method gets the entire dev_tool configuration section.

Returns:

  • dict[str, Any]

    The dev_tool configuration as a dictionary.

Source code in dev_tool/config/pyproject.py
def get_dev_tool_config(self) -> dict[str, Any]:
    """
    A method that retrieves the dev_tool configuration.

    This method gets the entire dev_tool configuration section.

    :return: The dev_tool configuration as a dictionary.
    """

    return self.get_tool_config('dev_tool')

get_docker_config

A method that retrieves the Docker configuration.

This method gets the Docker configuration section from dev_tool config.

Returns:

  • dict[str, Any]

    The Docker configuration as a dictionary.

Source code in dev_tool/config/pyproject.py
def get_docker_config(self) -> dict[str, Any]:
    """
    A method that retrieves the Docker configuration.

    This method gets the Docker configuration section from dev_tool config.

    :return: The Docker configuration as a dictionary.
    """

    return self.get_dev_tool_config().get('docker', {})

get_coverage_config

A method that retrieves the coverage configuration.

This method gets the coverage configuration section from dev_tool config.

Returns:

  • dict[str, Any]

    The coverage configuration as a dictionary.

Source code in dev_tool/config/pyproject.py
def get_coverage_config(self) -> dict[str, Any]:
    """
    A method that retrieves the coverage configuration.

    This method gets the coverage configuration section from dev_tool config.

    :return: The coverage configuration as a dictionary.
    """

    return self.get_dev_tool_config().get('coverage', {})

get_django_coverage_config

A method that retrieves the Django coverage configuration.

This method gets the Django-specific coverage configuration and merges it with the base.

Returns:

  • dict[str, Any]

    The merged Django coverage configuration as a dictionary.

Source code in dev_tool/config/pyproject.py
def get_django_coverage_config(self) -> dict[str, Any]:
    """
    A method that retrieves the Django coverage configuration.

    This method gets the Django-specific coverage configuration and merges it with the base.

    :return: The merged Django coverage configuration as a dictionary.
    """

    base = self.get_coverage_config()
    django_config = self._get_nested_config(self.get_dev_tool_config(), 'coverage', 'django', default={})
    return {**base, **django_config}

get_python_coverage_config

A method that retrieves the Python coverage configuration.

This method gets the Python-specific coverage configuration and merges it with the base.

Returns:

  • dict[str, Any]

    The merged Python coverage configuration as a dictionary.

Source code in dev_tool/config/pyproject.py
def get_python_coverage_config(self) -> dict[str, Any]:
    """
    A method that retrieves the Python coverage configuration.

    This method gets the Python-specific coverage configuration and merges it with the base.

    :return: The merged Python coverage configuration as a dictionary.
    """

    base = self.get_coverage_config()
    python_config = self._get_nested_config(self.get_dev_tool_config(), 'coverage', 'python', default={})
    return {**base, **python_config}

get_dev_tool_port

A method that retrieves the dev_tool port setting.

This method gets the port number configured for the dev_tool.

Returns:

  • str | None

    The port string, or None if not configured.

Source code in dev_tool/config/pyproject.py
def get_dev_tool_port(self) -> str | None:
    """
    A method that retrieves the dev_tool port setting.

    This method gets the port number configured for the dev_tool.

    :return: The port string, or None if not configured.
    """

    config = self.get_dev_tool_config()
    return config.get('port')

get_scripting_config

A method that retrieves the scripting configuration.

This method gets the scripting configuration section from dev_tool config.

Returns:

  • dict[str, Any]

    The scripting configuration as a dictionary.

Source code in dev_tool/config/pyproject.py
def get_scripting_config(self) -> dict[str, Any]:
    """
    A method that retrieves the scripting configuration.

    This method gets the scripting configuration section from dev_tool config.

    :return: The scripting configuration as a dictionary.
    """

    return self.get_dev_tool_config().get('scripting', {})

get_unittest_config

A method that retrieves the unittest configuration.

This method gets the unittest configuration section from dev_tool config.

Returns:

  • dict[str, Any]

    The unittest configuration as a dictionary.

Source code in dev_tool/config/pyproject.py
def get_unittest_config(self) -> dict[str, Any]:
    """
    A method that retrieves the unittest configuration.

    This method gets the unittest configuration section from dev_tool config.

    :return: The unittest configuration as a dictionary.
    """

    return self.get_dev_tool_config().get('unittest', {})

get_django_unittest_config

A method that retrieves the Django unittest configuration.

This method gets the Django-specific unittest configuration and merges it with the base.

Returns:

  • dict[str, Any]

    The merged Django unittest configuration as a dictionary.

Source code in dev_tool/config/pyproject.py
def get_django_unittest_config(self) -> dict[str, Any]:
    """
    A method that retrieves the Django unittest configuration.

    This method gets the Django-specific unittest configuration and merges it with the base.

    :return: The merged Django unittest configuration as a dictionary.
    """

    base = self.get_unittest_config()
    django_config = self._get_nested_config(self.get_dev_tool_config(), 'unittest', 'django', default={})
    return {**base, **django_config}

get_python_unittest_config

A method that retrieves the Python unittest configuration.

This method gets the Python-specific unittest configuration and merges it with the base.

Returns:

  • dict[str, Any]

    The merged Python unittest configuration as a dictionary.

Source code in dev_tool/config/pyproject.py
def get_python_unittest_config(self) -> dict[str, Any]:
    """
    A method that retrieves the Python unittest configuration.

    This method gets the Python-specific unittest configuration and merges it with the base.

    :return: The merged Python unittest configuration as a dictionary.
    """

    base = self.get_unittest_config()
    python_config = self._get_nested_config(self.get_dev_tool_config(), 'unittest', 'python', default={})
    return {**base, **python_config}

reload

A method that reloads the pyproject.toml configuration.

This method reads the configuration from the specified file path and updates the internal configuration dictionary.

Parameters:

  • path (Path) –

    The path to the pyproject.toml file.

Source code in dev_tool/config/pyproject.py
def reload(self, path: Path) -> None:
    """
    A method that reloads the pyproject.toml configuration.

    This method reads the configuration from the specified file path
    and updates the internal configuration dictionary.

    :param path: The path to the pyproject.toml file.
    """

    if path.exists():
        with open(path, 'rb') as handle:
            self._pyproject = tomllib.load(handle)
    else:
        from dev_tool.context import CONTEXT  # noqa: PLC0415

        self._pyproject = {}

        message = f'{path} does not exist'
        CONTEXT.notification.normal_text(message)

        log.debug(message)