Source code for kvalikirstu2.reader

""" A module for reading text and ODT files.
"""

import logging
import os
from kvalikirstu2 import argument_parser
from kvalikirstu2 import odt_reader
from kvalikirstu2.localization import _
from kvalikirstu2.exceptions import EncodingError


logger = logging.getLogger(__name__)


[docs]def get_reader(path: str): """Gets a reader for the given path. :param str path: The path of the file to be read. """ if not path or not os.path.isfile(path): return None logger.debug('Getting reader for path %s', path) args = argument_parser.get_args() _, extension = os.path.splitext(path) if extension in ['.txt', '.temp']: return TxtReader(path, args.encoding) if extension == '.odt': return odt_reader.OdtReader(path) logger.debug('Invalid file format, cant read.') return None
[docs]class TxtReader: """Reads .txt files line by line. :var fstream: A filestream in read-mode, that reads the file line by line. """ def __init__(self, path: str, encoding): """Constructor. :param str path: The .txt file to be read. :param str encoding: The encoding to be used for reading. """ logger.debug('Initializing txt reader to path %s, encoding %s', path, encoding) self.fstream = open(path, "r", encoding=encoding) self.path = path
[docs] def can_read(self): """Can read from the stream? :rtype: bool :return: True if can read, False otherwise. """ return not self.fstream.closed
def __enter__(self): return self def __exit__(self, _, __, ___): """Closes the file.""" logger.debug('Closing file handle %s', self.fstream.name) self.fstream.close()
[docs] def read_line(self): """Reads a line from the .txt. file :rtype: str :return: A line. """ try: output = self.fstream.readline() except UnicodeDecodeError: raise EncodingError(self.path, _('Encoding error while reading file %s') % self.path) if not output: self.fstream.close() output = output.strip() return output