fitzzftw.patch.parser.PatchParser

class PatchParser[source]

Bases: object

Handles the parsing of the diff or patch file content.

This class is responsible for reading the file, handling potential encoding issues, and iterating over the hunks and files defined in the patch.

Public Methods:

__init__()

Initializes the PatchParser instance.

__repr__()

Return repr(self).

create_line(raw_line)

Factory method that maps a raw patch line to its specialized class.

get_lines(stream)

A generator that transforms a stream of raw strings into PatchLine objects.

iter_files(stream)

Iterates over all file-level patches within the provided stream.


__init__()[source]

Initializes the PatchParser instance.

static create_line(raw_line)[source]

Factory method that maps a raw patch line to its specialized class.

This method analyzes the line prefix to determine the semantic role of the line (Header, Hunk, or Data). It is designed to be the central entry point for line instantiation to ensure consistent parsing and high testability.

Parameters:

raw_line (str) – The complete, unmodified line from the input stream.

Returns:

A specialized instance (HeadLine, HunkHeadLine, or FileLine). Returns a generic PatchLine for unknown metadata or comments.

Return type:

PatchLine

classmethod get_lines(stream)[source]

A generator that transforms a stream of raw strings into PatchLine objects.

Parameters:

stream (Iterable[str]) – Any iterable of strings (e.g., file handle, list, or generator).

Yields:

Specialized PatchLine objects.

Return type:

Generator[PatchLine, None, None]

iter_files(stream)[source]

Iterates over all file-level patches within the provided stream.

This method acts as a high-speed state machine, assembling objects directly from raw strings using an efficient if-elif-else sieve.

Parameters:

stream (Iterable[str]) – An iterable source of raw patch strings.

Raises:

FtwPatchError – If the diff sequence is invalid or corrupted.

Returns:

A generator yielding complete DiffCodeFile objects.

Return type:

Generator[DiffCodeFile, None, None]