diff --git a/kibot/bom/xlsx_writer.py b/kibot/bom/xlsx_writer.py index 11164aa9..3a7f25a1 100644 --- a/kibot/bom/xlsx_writer.py +++ b/kibot/bom/xlsx_writer.py @@ -20,7 +20,6 @@ from .columnlist import ColumnList from .kibot_logo import KIBOT_LOGO from .. import log from ..misc import W_NOKICOST, W_UNKDIST, KICOST_ERROR, W_BADFIELD -from ..error import trace_dump from ..gs import GS from .. import __version__ # Init the logger first @@ -708,9 +707,7 @@ def create_kicost_sheet(workbook, groups, image_data, fmt_title, fmt_info, fmt_s try: return _create_kicost_sheet(workbook, groups, image_data, fmt_title, fmt_info, fmt_subtitle, fmt_head, fmt_cols, cfg) except KiCostError as e: - trace_dump() - logger.error('KiCost error: `{}` ({})'.format(e.msg, e.id)) - exit(KICOST_ERROR) + GS.exit_with_error('KiCost error: `{}` ({})'.format(e.msg, e.id), KICOST_ERROR) def write_xlsx(filename, groups, col_fields, head_names, cfg): diff --git a/kibot/error.py b/kibot/error.py index 9be0acbe..ee6a33c6 100644 --- a/kibot/error.py +++ b/kibot/error.py @@ -6,14 +6,8 @@ """ KiBot errors """ -from sys import (exit, exc_info) -from traceback import print_tb from .gs import GS -from .misc import (EXIT_BAD_CONFIG) -# Logger -from . import log - -logger = log.get_logger() +from .misc import EXIT_BAD_CONFIG class KiPlotError(Exception): @@ -28,14 +22,5 @@ class KiPlotConfigurationError(KiPlotError): pass -def trace_dump(): - if GS.debug_enabled: - logger.error('Trace stack:') - (type, value, traceback) = exc_info() - print_tb(traceback) - - def config_error(msg): - trace_dump() - logger.error(msg) - exit(EXIT_BAD_CONFIG) + GS.exit_with_error(msg, EXIT_BAD_CONFIG) diff --git a/kibot/gs.py b/kibot/gs.py index 7d8eb79c..d92d9f3c 100644 --- a/kibot/gs.py +++ b/kibot/gs.py @@ -14,8 +14,9 @@ except ImportError: IU_PER_MM = 1 IU_PER_MILS = 1 from datetime import datetime -from sys import exit from shutil import copy2 +from sys import exit, exc_info +from traceback import extract_stack, format_list, print_tb from .misc import EXIT_BAD_ARGS, W_DATEFORMAT, W_UNKVAR, WRONG_INSTALL from .log import get_logger @@ -438,14 +439,12 @@ class GS(object): @staticmethod def check_pcb(): if not GS.pcb_file: - logger.error('No PCB file found (*.kicad_pcb), use -b to specify one.') - exit(EXIT_BAD_ARGS) + GS.exit_with_error('No PCB file found (*.kicad_pcb), use -b to specify one.', EXIT_BAD_ARGS) @staticmethod def check_sch(): if not GS.sch_file: - logger.error('No SCH file found (*.sch), use -e to specify one.') - exit(EXIT_BAD_ARGS) + GS.exit_with_error('No SCH file found (*.sch), use -e to specify one.', EXIT_BAD_ARGS) @staticmethod def copy_project(new_pcb_name): @@ -531,8 +530,7 @@ class GS(object): dir_name = os.path.join(os.path.sep, 'usr', 'share', 'kibot', name) if os.path.isdir(dir_name): return dir_name - logger.error('Missing resource directory `{}`'.format(name)) - exit(WRONG_INSTALL) + GS.exit_with_error('Missing resource directory `{}`'.format(name), WRONG_INSTALL) @staticmethod def create_eda_rect(tlx, tly, brx, bry): @@ -693,3 +691,23 @@ class GS(object): elif GS.ki6: po.SetSvgPrecision(svg_precision, False) # No ki5 equivalent + + @staticmethod + def trace_dump(): + if GS.debug_enabled: + logger.error('Trace stack:') + (type, value, traceback) = exc_info() + if traceback is None: + print(''.join(format_list(extract_stack()[:-2]))) + else: + print_tb(traceback) + + @staticmethod + def exit_with_error(msg, level): + GS.trace_dump() + if isinstance(msg, tuple): + for m in msg: + logger.error(m) + else: + logger.error(msg) + exit(level) diff --git a/kibot/kiplot.py b/kibot/kiplot.py index 495c6d11..eebfd9f2 100644 --- a/kibot/kiplot.py +++ b/kibot/kiplot.py @@ -27,7 +27,7 @@ from .misc import (PLOT_ERROR, CORRUPTED_PCB, EXIT_BAD_ARGS, CORRUPTED_SCH, vers MOD_VIRTUAL, W_PCBNOSCH, W_NONEEDSKIP, W_WRONGCHAR, name2make, W_TIMEOUT, W_KIAUTO, W_VARSCH, NO_SCH_FILE, NO_PCB_FILE, W_VARPCB, NO_YAML_MODULE, WRONG_ARGUMENTS, FAILED_EXECUTE, MOD_EXCLUDE_FROM_POS_FILES, MOD_EXCLUDE_FROM_BOM, MOD_BOARD_ONLY, hide_stderr) -from .error import PlotError, KiPlotConfigurationError, config_error, trace_dump +from .error import PlotError, KiPlotConfigurationError, config_error from .config_reader import CfgYamlReader from .pre_base import BasePreFlight from .dep_downloader import register_deps @@ -74,11 +74,9 @@ def _import(name, path): try: spec.loader.exec_module(mod) except ImportError as e: - trace_dump() - logger.error('Unable to import plug-ins: '+str(e)) - logger.error('Make sure you used `--no-compile` if you used pip for installation') - logger.error('Python path: '+str(sys_path)) - exit(WRONG_INSTALL) + GS.exit_with_error(('Unable to import plug-ins: '+str(e), + 'Make sure you used `--no-compile` if you used pip for installation', + 'Python path: '+str(sys_path)), WRONG_INSTALL) try_register_deps(mod, name) @@ -240,10 +238,8 @@ def load_board(pcb_file=None, forced=False): def ki_conf_error(e): - trace_dump() - logger.error('At line {} of `{}`: {}'.format(e.line, e.file, e.msg)) - logger.error('Line content: `{}`'.format(e.code.rstrip())) - exit(EXIT_BAD_CONFIG) + GS.exit_with_error(('At line {} of `{}`: {}'.format(e.line, e.file, e.msg), + 'Line content: `{}`'.format(e.code.rstrip())), EXIT_BAD_CONFIG) def load_any_sch(file, project): @@ -260,15 +256,10 @@ def load_any_sch(file, project): if GS.debug_level > 1: logger.debug('Schematic dependencies: '+str(sch.get_files())) except SchFileError as e: - trace_dump() - logger.error('At line {} of `{}`: {}'.format(e.line, e.file, e.msg)) - logger.error('Line content: `{}`'.format(e.code)) - exit(CORRUPTED_SCH) + GS.exit_with_error(('At line {} of `{}`: {}'.format(e.line, e.file, e.msg), + 'Line content: `{}`'.format(e.code)), CORRUPTED_SCH) except SchError as e: - trace_dump() - logger.error('While loading `{}`'.format(file)) - logger.error(str(e)) - exit(CORRUPTED_SCH) + GS.exit_with_error(('While loading `{}`'.format(file), str(e)), CORRUPTED_SCH) except KiConfError as e: ki_conf_error(e) return sch