fitzzftw.patch.patcher.FtwPatch

class FtwPatch[source]

Bases: object

Main class for the ftwpatch program.

Implements the PIMPLE idiom by storing the parsed argparse.Namespace object and providing command-line arguments via read-only properties (getters).

Public Data Attributes:

patch_file_path

The path to the patch or diff file (ro).

strip_count

The number of leading path components to strip from file names (ro).

target_directory

The directory containing the files to be patched (ro).

normalize_whitespace

Indicates if non-leading whitespace should be normalized (ro).

ignore_blank_lines

Indicates if pure blank lines should be ignored or normalized (ro).

ignore_all_whitespace

Indicates if all whitespace differences should be completely ignored (ro).

dry_run

Indicates whether the patch should only be simulated without writing to the file system (ro).

verbose

Get the verbosity level for console output (ro).

backup_ext

Get the normalized backup file extension (ro).

backup_path

Get the base directory for backup files (ro).

start_time

Get the global reference timestamp for this session (ro).

parsed_files

Return the list of code files extracted from the patch (ro).

Public Methods:

__init__(args)

Initializes the FtwPatch instance by storing the parsed command-line arguments.

__repr__()

Return a machine-readable representation of the instance.

run()

Execute the patching process and handle high-level errors.

apply(options)

Orchestrate the staging of changes by applying hunks to temporary files.

Private Methods:

_get_patch_stream()

Open the patch file and return a stream.

_parse()

Initialize the parser and load patch data.

_create_backups(file_paths[, extension, ...])

Create mandatory backups of all files before any modification.

_commit_changes(results, options)

Move patched files to their final destination and clean up.


__init__(args)[source]

Initializes the FtwPatch instance by storing the parsed command-line arguments.

Parameters:

args (ArgParsOptions) – The argparse.Namespace object containing command-line arguments. Expected attributes: patch_file, strip_count, target_directory, normalize_whitespace, ignore_blank_lines, ignore_all_whitespace.

Raises:
apply(options)[source]

Orchestrate the staging of changes by applying hunks to temporary files.

Orchestrates the patching process: 1. Calculate changes (Logical) 2. Stage changes (IO - Temporary) 3. Commit changes (IO - Final)

Parameters:

options (FtwPatchApplyOptions) – Command line options for patch application.

Raises:
  • PatchParseError – If the patch content is invalid (Indirect).

  • OSError – If reading or writing files fails (Indirect).

property backup_ext: str

Get the normalized backup file extension (ro).

This property returns the extension including the dot and the optional timestamp if a keyword was used during initialization.

Returns:

The backup extension string.

property backup_path: Path

Get the base directory for backup files (ro).

This path is resolved and contains the processed timestamp if any keywords were present in the initial configuration.

Returns:

The Path object for the backup directory.

property dry_run: bool

Indicates whether the patch should only be simulated without writing to the file system (ro).

Returns:

The dry run status.

property ignore_all_whitespace: bool

Indicates if all whitespace differences should be completely ignored (ro).

Returns:

The ignore status.

property ignore_blank_lines: bool

Indicates if pure blank lines should be ignored or normalized (ro).

Returns:

The ignore status.

property normalize_whitespace: bool

Indicates if non-leading whitespace should be normalized (ro).

Returns:

The normalization status.

property parsed_files: list[DiffCodeFile]

Return the list of code files extracted from the patch (ro).

Raises:
Returns:

List of DiffCodeFile objects.

property patch_file_path: Path

The path to the patch or diff file (ro).

Returns:

The path object for the patch file.

run()[source]

Execute the patching process and handle high-level errors.

Returns:

Exit code (0 for success, 1 or 2 for errors).

Return type:

int | None

property start_time: datetime

Get the global reference timestamp for this session (ro).

This timestamp is fixed at program start to ensure consistency between directory names and file extensions.

Returns:

The reference datetime.datetime object.

property strip_count: int

The number of leading path components to strip from file names (ro).

Returns:

The strip count value.

property target_directory: Path

The directory containing the files to be patched (ro).

Returns:

The target directory path.

property verbose: int

Get the verbosity level for console output (ro).

Returns:

The verbosity level ranging from 0 to 3.