def pexcept(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> None: """Print Exception message to sys.stderr. If debug is true, print exception traceback if one exists. :param msg: message or Exception to print :param end: string appended after the end of the message, default a newline :param apply_style: If True, then ansi.style_error will be applied to the message text. Set to False in cases where the message text already has the desired style. Defaults to True. """ if self.debug and sys.exc_info() != (None, None, None): import traceback traceback.print_exc() if isinstance(msg, Exception): final_msg = f"EXCEPTION of type '{type(msg).__name__}' occurred with message: {msg}" else: final_msg = str(msg) if apply_style: final_msg = ansi.style_error(final_msg) if not self.debug and 'debug' in self.settables: warning = "\n [!] To enable full traceback, run the following command: 'setg debug true'" final_msg += ansi.style_warning(warning) self.perror(final_msg, end=end, apply_style=False)
def emit(self, record: logging.LogRecord): output = self.format(record) if record.levelno >= logging.ERROR: self.__shell.poutput(ansi.style_error(output)) elif record.levelno >= logging.WARN: self.__shell.poutput(ansi.style_warning(output)) else: self.__shell.pfeedback(ansi.style_success(output))
def emit(self, record: logging.LogRecord): output = self.format(record) if record.levelno >= logging.ERROR: print(ansi.style_error(output)) elif record.levelno >= logging.WARN: print(ansi.style_warning(output)) else: print(ansi.style_success(output))
def error(self, message: str) -> None: """Custom override that applies custom formatting to the error message""" lines = message.split('\n') linum = 0 formatted_message = '' for line in lines: if linum == 0: formatted_message = 'Error: ' + line else: formatted_message += '\n ' + line linum += 1 self.print_usage(sys.stderr) formatted_message = ansi.style_warning(formatted_message) self.exit(2, '{}\n\n'.format(formatted_message))