From e22acbb1597deeb27f52f3233a366c6fd3d07ebf Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Tue, 2 Apr 2024 11:17:29 -0300 Subject: [PATCH] Better error message when kicad_common.json is corrupted See INTI-CMNB/KiBot#599 --- CHANGELOG.md | 6 ++++++ debian/changelog | 6 ++++++ kiauto/file_util.py | 2 +- kiauto/misc.py | 14 ++++++++++---- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a68cd57..4685d65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [2.3.1] - Unreleased +### Changed +- Better error message when kicad_common.json is corrupted (INTI-CMNB/KiBot#599) + + ## [2.3.0] - 2024-03-21 ### Added - Support for KiCad 8 diff --git a/debian/changelog b/debian/changelog index 70f0255..1106904 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +kiauto (2.3.1-1) UNRELEASED; urgency=medium + + * Next release + + -- Salvador Eduardo Tropea Tue, 02 Apr 2024 11:16:39 -0300 + kiauto (2.3.0-1) stable; urgency=medium * KiCad 8.0.0 support diff --git a/kiauto/file_util.py b/kiauto/file_util.py index c284d0c..8b922a8 100644 --- a/kiauto/file_util.py +++ b/kiauto/file_util.py @@ -247,7 +247,7 @@ def create_kicad_config(cfg): kiconf['system'] = {"editor_name": "/bin/cat"} # Copy the environment vars if available if cfg.conf_kicad_bkp: - vars = Config.get_config_vars_json(cfg.conf_kicad_bkp) + vars = Config.get_config_vars_json(cfg.conf_kicad_bkp, logger) if vars: kiconf['environment']['vars'] = vars text_file.write(json.dumps(kiconf)) diff --git a/kiauto/misc.py b/kiauto/misc.py index fff4310..707a810 100644 --- a/kiauto/misc.py +++ b/kiauto/misc.py @@ -37,6 +37,7 @@ READ_ONLY_PROBLEM = 17 WONT_OVERWRITE = 18 KICAD_CLI_ERROR = 19 +CORRUPTED_CONFIG = 20 # Wait 60 s to pcbnew/eeschema window to be present WAIT_START = 60 # Name for testing versions @@ -291,7 +292,7 @@ def __init__(self, logger, input_file=None, args=None, is_pcbnew=False): def load_kicad_environment(self, logger): self.env = {} if self.conf_kicad_json: - env = self.get_config_vars_json(self.conf_kicad) + env = self.get_config_vars_json(self.conf_kicad, logger) if env: self.env = env else: @@ -302,9 +303,14 @@ def load_kicad_environment(self, logger): logger.debug('KiCad environment: '+str(self.env)) @staticmethod - def get_config_vars_json(file): + def get_config_vars_json(file, logger): with open(file, "rt") as f: - data = json.load(f) + raw_data = f.read() + try: + data = json.loads(raw_data) + except json.decoder.JSONDecodeError: + logger.error(f"Corrupted KiCad config file `{file}`:\n{raw_data}") + exit(CORRUPTED_CONFIG) if 'environment' in data and 'vars' in data['environment']: return data['environment']['vars'] return None @@ -348,4 +354,4 @@ def get_en_locale(logger): __email__ = 'stropea@inti.gob.ar' __status__ = 'stable' __url__ = 'https://github.com/INTI-CMNB/KiAuto/' -__version__ = '2.3.0' +__version__ = '2.3.1'