From 50c1a91d44fb0664b3c10df8043f935110714de0 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Thu, 2 Dec 2021 01:07:41 -0500 Subject: [PATCH] Add a simple open wrapper for the bom python plugins to handle directory creation --- .../python_scripts/bom_csv_grouped_by_value.py | 3 ++- .../bom_csv_grouped_by_value_with_fp.py | 3 ++- .../python_scripts/bom_csv_sorted_by_ref.py | 3 ++- .../python_scripts/bom_html_grouped_by_value.py | 3 ++- .../bom_html_with_advanced_grouping.py | 3 ++- .../python_scripts/bom_txt_sorted_by_ref.py | 3 ++- eeschema/plugins/python_scripts/kicad_utils.py | 15 +++++++++++++++ 7 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 eeschema/plugins/python_scripts/kicad_utils.py diff --git a/eeschema/plugins/python_scripts/bom_csv_grouped_by_value.py b/eeschema/plugins/python_scripts/bom_csv_grouped_by_value.py index 6722da7423..ea6655aad2 100644 --- a/eeschema/plugins/python_scripts/bom_csv_grouped_by_value.py +++ b/eeschema/plugins/python_scripts/bom_csv_grouped_by_value.py @@ -21,6 +21,7 @@ from __future__ import print_function # Import the KiCad python helper module and the csv formatter import kicad_netlist_reader +import kicad_utils import csv import sys @@ -73,7 +74,7 @@ net = kicad_netlist_reader.netlist(sys.argv[1]) # Open a file to write to, if the file cannot be opened output to stdout # instead try: - f = open(sys.argv[2], 'w') + f = kicad_utils.open_file_write(sys.argv[2], 'w') except IOError: e = "Can't open output file for writing: " + sys.argv[2] print( __file__, ":", e, sys.stderr ) diff --git a/eeschema/plugins/python_scripts/bom_csv_grouped_by_value_with_fp.py b/eeschema/plugins/python_scripts/bom_csv_grouped_by_value_with_fp.py index edfd624b67..98884153df 100644 --- a/eeschema/plugins/python_scripts/bom_csv_grouped_by_value_with_fp.py +++ b/eeschema/plugins/python_scripts/bom_csv_grouped_by_value_with_fp.py @@ -17,6 +17,7 @@ # Import the KiCad python helper module and the csv formatter import kicad_netlist_reader +import kicad_utils import csv import sys @@ -38,7 +39,7 @@ net = kicad_netlist_reader.netlist(sys.argv[1]) # Open a file to write to, if the file cannot be opened output to stdout # instead try: - f = open(sys.argv[2], 'w') + f = kicad_utils.open_file_write(sys.argv[2], 'w') except IOError: e = "Can't open output file for writing: " + sys.argv[2] print(__file__, ":", e, sys.stderr) diff --git a/eeschema/plugins/python_scripts/bom_csv_sorted_by_ref.py b/eeschema/plugins/python_scripts/bom_csv_sorted_by_ref.py index 7e6ff19486..401ba705fa 100644 --- a/eeschema/plugins/python_scripts/bom_csv_sorted_by_ref.py +++ b/eeschema/plugins/python_scripts/bom_csv_sorted_by_ref.py @@ -19,6 +19,7 @@ from __future__ import print_function # Import the KiCad python helper module import kicad_netlist_reader +import kicad_utils import csv import sys @@ -40,7 +41,7 @@ net = kicad_netlist_reader.netlist(sys.argv[1]) # Open a file to write to, if the file cannot be opened output to stdout # instead try: - f = open(sys.argv[2], 'w') + f = kicad_utils.open_file_write(sys.argv[2], 'w') except IOError: e = "Can't open output file for writing: " + sys.argv[2] print( __file__, ":", e, sys.stderr ) diff --git a/eeschema/plugins/python_scripts/bom_html_grouped_by_value.py b/eeschema/plugins/python_scripts/bom_html_grouped_by_value.py index 0e104f55ec..c9bf546d6e 100644 --- a/eeschema/plugins/python_scripts/bom_html_grouped_by_value.py +++ b/eeschema/plugins/python_scripts/bom_html_grouped_by_value.py @@ -20,6 +20,7 @@ from __future__ import print_function # Import the KiCad python helper module and the csv formatter import kicad_netlist_reader +import kicad_utils import sys # Start with a basic html template @@ -49,7 +50,7 @@ net = kicad_netlist_reader.netlist(sys.argv[1]) # Open a file to write to, if the file cannot be opened output to stdout # instead try: - f = open(sys.argv[2], 'wb') + f = kicad_utils.open_file_write(sys.argv[2], 'wb') except IOError: e = "Can't open output file for writing: " + sys.argv[2] print(__file__, ":", e, file=sys.stderr) diff --git a/eeschema/plugins/python_scripts/bom_html_with_advanced_grouping.py b/eeschema/plugins/python_scripts/bom_html_with_advanced_grouping.py index 5134294f15..596a6d6c80 100644 --- a/eeschema/plugins/python_scripts/bom_html_with_advanced_grouping.py +++ b/eeschema/plugins/python_scripts/bom_html_with_advanced_grouping.py @@ -20,6 +20,7 @@ from __future__ import print_function # Import the KiCad python helper module and the csv formatter import kicad_netlist_reader +import kicad_utils import sys @@ -83,7 +84,7 @@ net = kicad_netlist_reader.netlist(sys.argv[1]) # Open a file to write to, if the file cannot be opened output to stdout # instead try: - f = open(sys.argv[2], 'wb') + f = kicad_utils.open_file_write(sys.argv[2], 'wb') except IOError: e = "Can't open output file for writing: " + sys.argv[2] print(__file__, ":", e, file=sys.stderr) diff --git a/eeschema/plugins/python_scripts/bom_txt_sorted_by_ref.py b/eeschema/plugins/python_scripts/bom_txt_sorted_by_ref.py index b14cdb2c76..059278e14d 100644 --- a/eeschema/plugins/python_scripts/bom_txt_sorted_by_ref.py +++ b/eeschema/plugins/python_scripts/bom_txt_sorted_by_ref.py @@ -17,6 +17,7 @@ from __future__ import print_function # Import the KiCad python helper module and the csv formatter import kicad_netlist_reader +import kicad_utils import csv import sys @@ -38,7 +39,7 @@ net = kicad_netlist_reader.netlist(sys.argv[1]) # Open a file to write to, if the file cannot be opened output to stdout # instead try: - f = open(sys.argv[2], 'w') + f = kicad_utils.open_file_write(sys.argv[2], 'w') except IOError: e = "Can't open output file for writing: " + sys.argv[2] print(__file__, ":", e, sys.stderr) diff --git a/eeschema/plugins/python_scripts/kicad_utils.py b/eeschema/plugins/python_scripts/kicad_utils.py new file mode 100644 index 0000000000..9ba9d9e9f2 --- /dev/null +++ b/eeschema/plugins/python_scripts/kicad_utils.py @@ -0,0 +1,15 @@ +# +# KiCad python module for some helper functions +# + +import os + +def open_file_write(path, mode): + ''' Open "path" for writing, creating any parent directories as needed. + ''' + dir_path = os.path.dirname(path) + + if not os.path.isdir(dir_path): + os.makedirs(dir_path) + + return open(path, mode) \ No newline at end of file