From e864cde5968ea9c35bf521358a74b20385ac8567 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sun, 26 Nov 2017 12:26:06 +0100 Subject: Make backup file logic smarter Automatically re-use an existing target layer backup file in subsequent runs instead of using a previous run's output. I.e., if the target extension is set to ".GTO" and both "foo.GTO" and "foo.GTO.bak" exist, automatically use "foo.GTO.bak" and write to "foo.GTO". This behavior can be overridden by stating an explicit filename. --- gerbimg.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/gerbimg.py b/gerbimg.py index 4bfdac4..d3ed7f9 100755 --- a/gerbimg.py +++ b/gerbimg.py @@ -58,7 +58,7 @@ def paste_image( status_print(' * target layer') ctx.render_layer(target, settings=fg, bgsettings=bg) for fn, sub in subtract_gerber: - status_print(' * extra layer', fn) + status_print(' * extra layer', os.path.basename(fn)) layer = gerber.loads(sub) ctx.render_layer(layer, settings=fg, bgsettings=bg) status_print('Rendering keepout composite') @@ -210,14 +210,20 @@ def replace_file_in_zip(zip_path, filename, contents): def paste_image_file(zip_or_dir, target, outline, source_img, subtract=[], status_print=lambda *args:None, debugdir=None): if path.isdir(zip_or_dir): tname, target = find_gerber_in_dir(zip_or_dir, target) - _fn, outline = find_gerber_in_dir(zip_or_dir, outline) + status_print('Target layer file {}'.format(os.path.basename(tname))) + oname, outline = find_gerber_in_dir(zip_or_dir, outline) + status_print('Outline layer file {}'.format(os.path.basename(oname))) subtract = [ (fn, layer) for fn, layer in (find_gerber_in_dir(zip_or_dir, elem) for elem in subtract) ] out = paste_image(target, outline, source_img, subtract, debugdir=debugdir, status_print=status_print) - shutil.copy(tname, tname+'.bak') - with open(tname, 'w') as f: - f.write(out) + if not tname.endswith('.bak'): + shutil.copy(tname, tname+'.bak') + with open(tname, 'w') as f: + f.write(out) + else: + with open(tname[:-4], 'w') as f: + f.write(out) elif zipfile.is_zipfile(zip_or_dir): _fn, outline = find_gerber_in_zip(zip_or_dir, outline) subtract = [ (fn, layer) for fn, layer in (find_gerber_in_zip(zip_or_dir, elem) for elem in subtract) ] @@ -243,7 +249,7 @@ if __name__ == '__main__': args = parser.parse_args() if not args.target: - args.target = '.GBO' if args.bottom else '.GTO' + args.target = '.GBO.bak,.GBO' if args.bottom else '.GTO.bak,.GTO' if not args.subtract: args.subtract = ['.GBS', '.TXT'] if args.bottom else ['.GTS', '.TXT'] -- cgit