Add a simple open wrapper for the bom python plugins to handle directory creation

This commit is contained in:
Marek Roszko 2021-12-02 01:07:41 -05:00
parent 0f67c7ed25
commit 50c1a91d44
7 changed files with 27 additions and 6 deletions

View File

@ -21,6 +21,7 @@ from __future__ import print_function
# Import the KiCad python helper module and the csv formatter # Import the KiCad python helper module and the csv formatter
import kicad_netlist_reader import kicad_netlist_reader
import kicad_utils
import csv import csv
import sys 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 # Open a file to write to, if the file cannot be opened output to stdout
# instead # instead
try: try:
f = open(sys.argv[2], 'w') f = kicad_utils.open_file_write(sys.argv[2], 'w')
except IOError: except IOError:
e = "Can't open output file for writing: " + sys.argv[2] e = "Can't open output file for writing: " + sys.argv[2]
print( __file__, ":", e, sys.stderr ) print( __file__, ":", e, sys.stderr )

View File

@ -17,6 +17,7 @@
# Import the KiCad python helper module and the csv formatter # Import the KiCad python helper module and the csv formatter
import kicad_netlist_reader import kicad_netlist_reader
import kicad_utils
import csv import csv
import sys 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 # Open a file to write to, if the file cannot be opened output to stdout
# instead # instead
try: try:
f = open(sys.argv[2], 'w') f = kicad_utils.open_file_write(sys.argv[2], 'w')
except IOError: except IOError:
e = "Can't open output file for writing: " + sys.argv[2] e = "Can't open output file for writing: " + sys.argv[2]
print(__file__, ":", e, sys.stderr) print(__file__, ":", e, sys.stderr)

View File

@ -19,6 +19,7 @@ from __future__ import print_function
# Import the KiCad python helper module # Import the KiCad python helper module
import kicad_netlist_reader import kicad_netlist_reader
import kicad_utils
import csv import csv
import sys 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 # Open a file to write to, if the file cannot be opened output to stdout
# instead # instead
try: try:
f = open(sys.argv[2], 'w') f = kicad_utils.open_file_write(sys.argv[2], 'w')
except IOError: except IOError:
e = "Can't open output file for writing: " + sys.argv[2] e = "Can't open output file for writing: " + sys.argv[2]
print( __file__, ":", e, sys.stderr ) print( __file__, ":", e, sys.stderr )

View File

@ -20,6 +20,7 @@ from __future__ import print_function
# Import the KiCad python helper module and the csv formatter # Import the KiCad python helper module and the csv formatter
import kicad_netlist_reader import kicad_netlist_reader
import kicad_utils
import sys import sys
# Start with a basic html template # 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 # Open a file to write to, if the file cannot be opened output to stdout
# instead # instead
try: try:
f = open(sys.argv[2], 'wb') f = kicad_utils.open_file_write(sys.argv[2], 'wb')
except IOError: except IOError:
e = "Can't open output file for writing: " + sys.argv[2] e = "Can't open output file for writing: " + sys.argv[2]
print(__file__, ":", e, file=sys.stderr) print(__file__, ":", e, file=sys.stderr)

View File

@ -20,6 +20,7 @@ from __future__ import print_function
# Import the KiCad python helper module and the csv formatter # Import the KiCad python helper module and the csv formatter
import kicad_netlist_reader import kicad_netlist_reader
import kicad_utils
import sys 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 # Open a file to write to, if the file cannot be opened output to stdout
# instead # instead
try: try:
f = open(sys.argv[2], 'wb') f = kicad_utils.open_file_write(sys.argv[2], 'wb')
except IOError: except IOError:
e = "Can't open output file for writing: " + sys.argv[2] e = "Can't open output file for writing: " + sys.argv[2]
print(__file__, ":", e, file=sys.stderr) print(__file__, ":", e, file=sys.stderr)

View File

@ -17,6 +17,7 @@ from __future__ import print_function
# Import the KiCad python helper module and the csv formatter # Import the KiCad python helper module and the csv formatter
import kicad_netlist_reader import kicad_netlist_reader
import kicad_utils
import csv import csv
import sys 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 # Open a file to write to, if the file cannot be opened output to stdout
# instead # instead
try: try:
f = open(sys.argv[2], 'w') f = kicad_utils.open_file_write(sys.argv[2], 'w')
except IOError: except IOError:
e = "Can't open output file for writing: " + sys.argv[2] e = "Can't open output file for writing: " + sys.argv[2]
print(__file__, ":", e, sys.stderr) print(__file__, ":", e, sys.stderr)

View File

@ -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)