"""General utility classes to measuring time it takes to do work, to loggingand fork/exec operations."""__author__='Paul Landes'fromtypingimportDict,Tuple,Any,Typefromdataclassesimportdataclass,fieldimportsysimporttracebackfromcollectionsimportOrderedDictfromioimportStringIO,TextIOBasefrom.importWritable
[docs]classAPIError(Exception):"""Base exception from which almost all library raised errors extend. """pass
[docs]@dataclassclassFailure(Writable):"""Contains information for failures as caught exceptions used by programatic methods. """exception:Exception=field(default=None)"""The exception that was generated."""thrower:Any=field(default=None)"""The instance of the class that is throwing the exception."""traceback:traceback=field(default=None,repr=False)"""The stack trace."""message:str=field(default=None)"""A high level explaination of what went wrong"""def__post_init__(self):self._exc_info:Tuple[Type[Exception],Exception,traceback]= \
sys.exc_info()ifself.exceptionisNone:self.exception:Exception=self._exc_info[1]ifself.tracebackisNone:self.traceback:traceback=self._exc_info[2]self._traceback_str:str=Nonedef_print_stack(self,writer:TextIOBase=sys.stdout):"""Print the stack trace of the exception that caused the failure."""traceback.print_exception(*self._exc_info,file=writer)@propertydeftraceback_str(self)->str:"""The stack trace as a string. :see :meth:`print_stack` """ifself._traceback_strisNone:sio=StringIO()self._print_stack(writer=sio)self._traceback_str=sio.getvalue()returnself._traceback_str
[docs]defprint_stack(self,writer:TextIOBase=sys.stdout):"""Print the stack trace of the exception that caused the failure."""writer.write(self.traceback_str)
[docs]defwrite(self,depth:int=0,writer:TextIOBase=sys.stdout):"""Write the contents of this instance to ``writer`` using indention ``depth``. :param depth: the starting indentation depth :param writer: the writer to dump the content of this writable """message:str=self.messageifmessageisNoneorlen(self.message)==0:message=str(self.exception)iflen(message)==0:message=f'Exception: {type(self.exception)}'self._write_line(f'{message}:',depth,writer)self._write_block(self.traceback_str,depth+1,writer)
[docs]defasdict(self)->Dict[str,Any]:"""Return the content of the object as a dictionary."""returnOrderedDict([['message',self.message],['exception',self.exception],['trace',self.traceback_str]])