From e84df0790dcd7f2e40a2370cb95ec0b3a574cb13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rene=20P=C3=B6schl?= Date: Sun, 28 Jul 2019 10:44:06 +0200 Subject: [PATCH 1/4] More powerful patch script --- patch.py | 112 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 96 insertions(+), 16 deletions(-) diff --git a/patch.py b/patch.py index 6290c5f..7a26c6b 100755 --- a/patch.py +++ b/patch.py @@ -1,20 +1,100 @@ #!/bin/python3.6 -import sys +import sys, os +import argparse +from collections import OrderedDict +from pathlib import Path -patch=open(sys.argv[1], "r").readlines() -orig=open(sys.argv[2],"r") -origlines = orig.readlines() +class ConfigFile(): + def __init__(self, filepath): + self.content = OrderedDict() + self.filepath = filepath + with open(filepath, 'r') as file: + idx = 0 + for line in file: + l = line.strip() + if l != '': + try: + key, value = l.split('=', 1) + except: + raise ValueError("line {}: '{}' is of invalid format in config file.".format(idx, l)) + self.content.update({key: value}) + idx += 1 -for idx,oline in enumerate(origlines): - osubst = oline.split('=') - for idx1,pline in enumerate(patch): - psubst = pline.split('=') - if osubst[0] == psubst[0]: - origlines[idx] = pline - patch.pop(idx1) -origlines += patch -orig.close + def patch(self, patchfile): + with open(patchfile, 'r') as file: + idx = 0 + for line in file: + l = line.strip() + if l == '': + continue -new=open(sys.argv[2],"w") -new.writelines(origlines) -new.close + try: + key, value = l.split('=') + except: + raise ValueError("line {}: '{}' is of invalid format in patch file.".format(idx, l)) + + self.content[key] = value + idx += 1 + + def write(self): + with open(self.filepath, 'w') as file: + for key, value in self.content.items(): + print("{}={}".format(key, value), file=file) + +parser = argparse.ArgumentParser(description='Patch the KiCad settings file with the given colour scheme.') +parser.add_argument('config_dir', type=Path, nargs=1, + help='Path to kicad config directory') +parser.add_argument('scheme_path', type=Path, nargs=1, + help='Path to scheme definition.') + +parser.add_argument('-p', '--pcb_disable', action='store_true', help='Disable patching of pcb_new colour definition') +parser.add_argument('-f', '--footprint_disable', action='store_true', help='Disable patching of footprint editor colour definition') +parser.add_argument('-e', '--eeschema_disable', action='store_true', help='Disable patching of eeschema and symbol editor colour definition') + +args = parser.parse_args() + + +if args.pcb_disable and args.footprint_disable and args.eeschema_disable: + print("All definitions disabled. Nothing to do. (Use --help for instructions.)") + exit() + +if not args.config_dir[0].is_dir(): + print("'{}' expected to be the kicad config directory but it is not a directory or does not exist. (Use --help for instructions.)".format(args.config_dir[0])) + exit() + +if not args.scheme_path[0].is_dir(): + print("'{}' expected to be the colour scheme definition directory but it is not a directory or does not exist. (Use --help for instructions.)".format(args.scheme_path[0])) + +if not args.eeschema_disable: + ee_patch = args.scheme_path[0] / 'eeschema' + if not ee_patch.is_file(): + print("Scheme does not contain a definition for EESchema, skipped.") + else: + print("Updating EESchema configuration.") + eeschema_handler = ConfigFile(args.config_dir[0] / 'eeschema') + eeschema_handler.patch(ee_patch) + eeschema_handler.write() + +if args.pcb_disable and args.footprint_disable: + print("Done") + exit() + +pcb_handler = ConfigFile(args.config_dir[0] / 'pcbnew') + +if not args.pcb_disable: + pcb_patch = args.scheme_path[0] / 'pcbnew' + if not pcb_patch.is_file(): + print("Scheme does not contain a definition for pcb_new, skipped.") + else: + print("Updating pcb_new configuration.") + pcb_handler.patch(pcb_patch) + +if not args.footprint_disable: + fpe_patch = args.scheme_path[0] / 'footprint_editor' + if not pcb_patch.is_file(): + print("Scheme does not contain a definition for the footprint editor, skipped.") + else: + print("Updating footprint editor configuration.") + pcb_handler.patch(fpe_patch) + +pcb_handler.write() From 59fe03768cee5350e0bc316947b0dfbada8ad161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rene=20P=C3=B6schl?= Date: Sun, 28 Jul 2019 13:35:45 +0200 Subject: [PATCH 2/4] Create a bakup of the settingsfiles before doing anything --- patch.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/patch.py b/patch.py index 7a26c6b..885731d 100755 --- a/patch.py +++ b/patch.py @@ -3,6 +3,7 @@ import sys, os import argparse from collections import OrderedDict from pathlib import Path +import shutil class ConfigFile(): def __init__(self, filepath): @@ -42,11 +43,10 @@ class ConfigFile(): print("{}={}".format(key, value), file=file) parser = argparse.ArgumentParser(description='Patch the KiCad settings file with the given colour scheme.') -parser.add_argument('config_dir', type=Path, nargs=1, - help='Path to kicad config directory') parser.add_argument('scheme_path', type=Path, nargs=1, help='Path to scheme definition.') - +parser.add_argument('config_dir', type=Path, nargs=1, + help='Path to kicad config directory') parser.add_argument('-p', '--pcb_disable', action='store_true', help='Disable patching of pcb_new colour definition') parser.add_argument('-f', '--footprint_disable', action='store_true', help='Disable patching of footprint editor colour definition') parser.add_argument('-e', '--eeschema_disable', action='store_true', help='Disable patching of eeschema and symbol editor colour definition') @@ -71,7 +71,18 @@ if not args.eeschema_disable: print("Scheme does not contain a definition for EESchema, skipped.") else: print("Updating EESchema configuration.") - eeschema_handler = ConfigFile(args.config_dir[0] / 'eeschema') + ee_config = args.config_dir[0] / 'eeschema' + try: + shutil.copy(ee_config, str(ee_config)+".bak") + except: + answer = input("Unable to create backup file. Continue anyways? [y/n] ") + while(answer not in ['y', 'n']): + answer = input("Unable to create backup file. Continue anyways? [y/n] ") + if answer == 'n': + exit() + + + eeschema_handler = ConfigFile(ee_config) eeschema_handler.patch(ee_patch) eeschema_handler.write() @@ -79,7 +90,17 @@ if args.pcb_disable and args.footprint_disable: print("Done") exit() -pcb_handler = ConfigFile(args.config_dir[0] / 'pcbnew') +pcb_config = args.config_dir[0] / 'pcbnew' +try: + shutil.copy(pcb_config, str(pcb_config)+".bak") +except: + answer = input("Unable to create backup file. Continue anyways? [y/n] ") + while(answer not in ['y', 'n']): + answer = input("Unable to create backup file. Continue anyways? [y/n] ") + if answer == 'n': + exit() + +pcb_handler = ConfigFile(pcb_config) if not args.pcb_disable: pcb_patch = args.scheme_path[0] / 'pcbnew' From 565066316f857fa765193d5ba1162e2acbeff9b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rene=20P=C3=B6schl?= Date: Sun, 28 Jul 2019 13:36:00 +0200 Subject: [PATCH 3/4] Update readme --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7ea6ece..3c628b9 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,12 @@ The pcbnew config file content has been split into the sections responsible for ## Automatic patcher -You can use patch.py to automatically patch the configuration files. The python script searches for settings that have to be overwritten and appends all new settings to the end of the file. +An automatic patch script can be used to transfer a colour scheme into your KiCad settings files. Make sure KiCad is closed before using it. -`python3.6 patch.py ` +The script expects the directory containing the colour scheme and the kicad config directory as arguments. Switches are included to disable transfer of a particular part of the scheme definition. (use --help for detailed instructions.) A bakup of your settings files is created before changes are made. -For example: - -`python3.6 patch.py ~/kicad-color-schemes/blue-green-dark/pcbnew ~/.config/kicad/pcbnew` +Example: +`python3 patch.py ~/kicad-color-schemes/blue-green-dark/ ~/.config/kicad/` ## eeschema From 1d938b51c36a83329ccc63bf4317371a2ebe4045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rene=20P=C3=B6schl?= Date: Sun, 28 Jul 2019 22:50:55 +0200 Subject: [PATCH 4/4] Correct shebang line --- .../kit-dev-coldfire-xilinx_5213.kicad_pcb | 2 +- patch.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/_help/kit-dev-coldfire-xilinx_5213/kit-dev-coldfire-xilinx_5213.kicad_pcb b/_help/kit-dev-coldfire-xilinx_5213/kit-dev-coldfire-xilinx_5213.kicad_pcb index 244f915..e8f3ea8 100644 --- a/_help/kit-dev-coldfire-xilinx_5213/kit-dev-coldfire-xilinx_5213.kicad_pcb +++ b/_help/kit-dev-coldfire-xilinx_5213/kit-dev-coldfire-xilinx_5213.kicad_pcb @@ -1,4 +1,4 @@ -(kicad_pcb (version 20171130) (host pcbnew 5.1.1-8be2ce7~80~ubuntu16.04.1) +(kicad_pcb (version 20171130) (host pcbnew 5.1.2-f72e74a~84~ubuntu16.04.1) (general (thickness 1.6) diff --git a/patch.py b/patch.py index 885731d..725842a 100755 --- a/patch.py +++ b/patch.py @@ -1,4 +1,4 @@ -#!/bin/python3.6 +#!/usr/bin/env python3 import sys, os import argparse from collections import OrderedDict