# standard imports import typing # bsie imports from bsie.utils import errors, filematcher # inner-module imports from .. import base # constants MATCH_RULE = 'mime=text/plain' # exports __all__: typing.Sequence[str] = ( 'Plain', ) ## code ## class Plain(base.Reader): """Read paragraphs (seperated by newline) from a plain text file.""" _match: filematcher.Matcher def __init__(self): self._match = filematcher.parse(MATCH_RULE) def __call__(self, path: str) -> typing.Sequence[str]: # perform quick checks first if not self._match(path): raise errors.UnsupportedFileFormatError(path) # open file in text mode with open(path, 'rt', encoding='UTF-8') as ifile: return [line.strip() for line in ifile.read().split('\n') if len(line.strip()) > 0] ## EOF ##