More powerful patch script
This commit is contained in:
parent
86d84cc2af
commit
e84df0790d
1 changed files with 96 additions and 16 deletions
112
patch.py
112
patch.py
|
@ -1,20 +1,100 @@
|
||||||
#!/bin/python3.6
|
#!/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()
|
class ConfigFile():
|
||||||
orig=open(sys.argv[2],"r")
|
def __init__(self, filepath):
|
||||||
origlines = orig.readlines()
|
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):
|
def patch(self, patchfile):
|
||||||
osubst = oline.split('=')
|
with open(patchfile, 'r') as file:
|
||||||
for idx1,pline in enumerate(patch):
|
idx = 0
|
||||||
psubst = pline.split('=')
|
for line in file:
|
||||||
if osubst[0] == psubst[0]:
|
l = line.strip()
|
||||||
origlines[idx] = pline
|
if l == '':
|
||||||
patch.pop(idx1)
|
continue
|
||||||
origlines += patch
|
|
||||||
orig.close
|
|
||||||
|
|
||||||
new=open(sys.argv[2],"w")
|
try:
|
||||||
new.writelines(origlines)
|
key, value = l.split('=')
|
||||||
new.close
|
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()
|
||||||
|
|
Loading…
Reference in a new issue