csvpath.managers.results.readers.file_printouts_reader
1import os 2from abc import ABC, abstractmethod 3from csvpath.util.nos import Nos 4from csvpath.util.file_readers import DataFileReader 5from .readers import PrintoutsReader 6 7 8class Printouts(dict): 9 def __setitem__(self, key, value): 10 if not isinstance(value, list): 11 raise ValueError(f"Printouts must be a list[str] not {type(value)}") 12 super().__setitem__(key, value) 13 14 15class FilePrintoutsReader(PrintoutsReader): 16 def __init__(self) -> None: 17 super().__init__() 18 self._printouts = None 19 20 @property 21 def printouts(self) -> dict[str, list[str]]: 22 if self._printouts is None: 23 if self.result is not None and self.result.instance_dir: 24 d = os.path.join(self.result.instance_dir, "printouts.txt") 25 if Nos(d).exists(): 26 self._printouts = Printouts() 27 with DataFileReader(d) as file: 28 t = file.source.read() 29 printouts = t.split("---- PRINTOUT:") 30 for p in printouts: 31 name = p[0 : p.find("\n")] 32 name = name.strip() 33 body = p[p.find("\n") + 1 :] 34 ps = self._body_to_lines(body) 35 self._printouts[name] = ps 36 else: 37 self.result.csvpath.logger.debug( 38 "There is no printouts file at %s", d 39 ) 40 if self._printouts is None: 41 self._printouts = Printouts() 42 return self._printouts 43 44 def _body_to_lines(self, body) -> list[str]: 45 ps = [line for line in body.split("\n")] 46 for i, line in enumerate(ps): 47 # 48 # print allows newlines. we don't keep newlines from creating new list 49 # entries today, but probably should. we would transform the backslash-n 50 # to a literal 2-char backslash-n and then convert back here. we can do 51 # the back conversion now w/o waiting for the forward. busy, one thing at 52 # a time. 53 # 54 line = line.replace("\\n", "\n") 55 if line.strip() == "" and i == len(ps) - 1: 56 ps = ps[0 : len(ps) - 1] 57 return ps
class
Printouts(builtins.dict):
16class FilePrintoutsReader(PrintoutsReader): 17 def __init__(self) -> None: 18 super().__init__() 19 self._printouts = None 20 21 @property 22 def printouts(self) -> dict[str, list[str]]: 23 if self._printouts is None: 24 if self.result is not None and self.result.instance_dir: 25 d = os.path.join(self.result.instance_dir, "printouts.txt") 26 if Nos(d).exists(): 27 self._printouts = Printouts() 28 with DataFileReader(d) as file: 29 t = file.source.read() 30 printouts = t.split("---- PRINTOUT:") 31 for p in printouts: 32 name = p[0 : p.find("\n")] 33 name = name.strip() 34 body = p[p.find("\n") + 1 :] 35 ps = self._body_to_lines(body) 36 self._printouts[name] = ps 37 else: 38 self.result.csvpath.logger.debug( 39 "There is no printouts file at %s", d 40 ) 41 if self._printouts is None: 42 self._printouts = Printouts() 43 return self._printouts 44 45 def _body_to_lines(self, body) -> list[str]: 46 ps = [line for line in body.split("\n")] 47 for i, line in enumerate(ps): 48 # 49 # print allows newlines. we don't keep newlines from creating new list 50 # entries today, but probably should. we would transform the backslash-n 51 # to a literal 2-char backslash-n and then convert back here. we can do 52 # the back conversion now w/o waiting for the forward. busy, one thing at 53 # a time. 54 # 55 line = line.replace("\\n", "\n") 56 if line.strip() == "" and i == len(ps) - 1: 57 ps = ps[0 : len(ps) - 1] 58 return ps
Helper class that provides a standard way to create an ABC using inheritance.
printouts: dict[str, list[str]]
21 @property 22 def printouts(self) -> dict[str, list[str]]: 23 if self._printouts is None: 24 if self.result is not None and self.result.instance_dir: 25 d = os.path.join(self.result.instance_dir, "printouts.txt") 26 if Nos(d).exists(): 27 self._printouts = Printouts() 28 with DataFileReader(d) as file: 29 t = file.source.read() 30 printouts = t.split("---- PRINTOUT:") 31 for p in printouts: 32 name = p[0 : p.find("\n")] 33 name = name.strip() 34 body = p[p.find("\n") + 1 :] 35 ps = self._body_to_lines(body) 36 self._printouts[name] = ps 37 else: 38 self.result.csvpath.logger.debug( 39 "There is no printouts file at %s", d 40 ) 41 if self._printouts is None: 42 self._printouts = Printouts() 43 return self._printouts