Source code for text_lint.sequencers.textfile

"""TextfileSequencer class."""

import re
from typing import TYPE_CHECKING, Optional, Pattern

from text_lint.sequencers.bases.sequencer_base import SequencerBase

if TYPE_CHECKING:  # pragma: no cover
  from text_lint.schema import Schema


[docs] class TextFileSequencer(SequencerBase[str]): """Iterator that returns the lines of a text file.""" _comment_regex: Optional[Pattern[str]] = None
[docs] def __init__(self, file_path: str) -> None: """Initialize TextFileSequencer instances. :param file_path: The text file to load lines from. """ with open(file_path, "r", encoding='utf-8') as fh: super().__init__(fh.read().splitlines()) self.path = file_path
[docs] def configure(self, schema: "Schema") -> None: """Apply schema configuration to the text file. :param schema: The schema containing the required configuration. """ if schema.settings.comment_regex: self._comment_regex = re.compile(schema.settings.comment_regex, re.DOTALL)
[docs] def __next__(self) -> str: """Return the next line in the text file.""" if self.index < len(self._entities): next_line = self.current self.index += 1 if self._is_comment(next_line): return self.__next__() return next_line raise StopIteration
def _is_comment(self, next_line: str) -> bool: if self._comment_regex: return re.match(self._comment_regex, next_line) is not None return False