Handlers Module

Handlers package for ModuFlow.

This package provides handlers for working with files, sections, and YAML configurations.

File Handler

File operations for ModuFlow.

This module provides utilities for working with files in a ModuFlow project.

class moduflow.handlers.file_handler.FileHandler(project_root: str | None = None)[source]

Bases: object

Handler for file operations.

copy_file(source: Path, destination: Path) None[source]

Copy a file.

Parameters:
  • source – Path to the source file.

  • destination – Path to the destination file.

Raises:

FileNotFoundError – If the source file doesn’t exist.

copy_files(files: List[str], source_dir: Path, dest_dir: Path, skip_missing: bool = True) List[str][source]

Copy multiple files.

Parameters:
  • files – List of file paths relative to source_dir.

  • source_dir – Source directory.

  • dest_dir – Destination directory.

  • skip_missing – If True, skip missing files. If False, raise FileNotFoundError.

Returns:

List of files that were copied.

Raises:

FileNotFoundError – If skip_missing is False and a file doesn’t exist.

ensure_directory(directory: Path) None[source]

Ensure a directory exists.

Parameters:

directory – Path to the directory.

find_files_by_extension(directory: Path, extensions: List[str]) List[Path][source]

Find files with specific extensions.

Parameters:
  • directory – Directory to search in.

  • extensions – List of file extensions (without the dot).

Returns:

List of file paths.

list_files(directory: Path, pattern: str = '*') List[Path][source]

List files in a directory.

Parameters:
  • directory – Directory to list files from.

  • pattern – Glob pattern to match files.

Returns:

List of file paths.

save_yaml(data: dict, file_path: Path) None[source]

Save data to a YAML file.

Parameters:
  • data – Dictionary to save.

  • file_path – Path to the YAML file.

update_gitignore(entries: List[str]) bool[source]

Add entries to .gitignore.

Parameters:

entries – List of entries to add.

Returns:

True if the file was updated, False otherwise.

Section Handler

Section management for ModuFlow.

This module provides functionality for working with sections, including creating, updating, and compiling sections.

class moduflow.handlers.section_handler.SectionHandler(project_root: str | None = None)[source]

Bases: object

Handler for section operations.

add_files_to_section(name: str, files: List[str]) None[source]

Add files to an existing section.

Parameters:
  • name – Name of the section.

  • files – List of files to add.

analyze_project() None[source]

Analyze the project structure and suggest sections.

compile_all_sections() None[source]

Compile all sections to the output directory.

compile_project() None[source]

Compile the entire project to a single directory structure.

compile_section(name: str) None[source]

Compile a section to the output directory.

Parameters:

name – Name of the section to compile.

create_section(name: str, description: str = '', files: List[str] | None = None) None[source]

Create a new section.

Parameters:
  • name – Name of the section.

  • description – Description of the section.

  • files – Optional list of files to include in the section.

init_project() None[source]

Initialize a new project structure.

list_sections() None[source]

List all sections and their files.

update_section(name: str, description: str | None = None, files: List[str] | None = None) None[source]

Update an existing section.

Parameters:
  • name – Name of the section.

  • description – New description of the section (if provided).

  • files – New list of files (if provided).

YAML Handler

YAML handling for ModuFlow.

This module provides utilities for working with YAML files.

class moduflow.handlers.yaml_handler.YamlHandler[source]

Bases: object

Handler for YAML operations.

static load_yaml(file_path: Path) Dict[str, Any][source]

Load a YAML file.

Parameters:

file_path – Path to the YAML file.

Returns:

Dictionary containing the YAML data.

Raises:
  • FileNotFoundError – If the file doesn’t exist.

  • yaml.YAMLError – If the YAML is invalid.

static merge_yaml(base: Dict[str, Any], override: Dict[str, Any]) Dict[str, Any][source]

Merge two YAML dictionaries.

Parameters:
  • base – Base dictionary.

  • override – Dictionary to merge on top of base.

Returns:

Merged dictionary.

static save_yaml(data: Dict[str, Any], file_path: Path) None[source]

Save data to a YAML file.

Parameters:
  • data – Dictionary to save.

  • file_path – Path to the YAML file.

Raises:

yaml.YAMLError – If the data cannot be serialized to YAML.

static string_to_yaml(yaml_str: str) Dict[str, Any][source]

Convert a YAML string to a dictionary.

Parameters:

yaml_str – YAML string to convert.

Returns:

Dictionary containing the YAML data.

Raises:

yaml.YAMLError – If the YAML is invalid.

static yaml_to_string(data: Dict[str, Any]) str[source]

Convert a dictionary to a YAML string.

Parameters:

data – Dictionary to convert.

Returns:

YAML string.