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 ea6655aad2..b0fe999ef0 100644 --- a/eeschema/plugins/python_scripts/bom_csv_grouped_by_value.py +++ b/eeschema/plugins/python_scripts/bom_csv_grouped_by_value.py @@ -25,19 +25,10 @@ import kicad_utils import csv import sys -# A helper function to convert a UTF8/Unicode/locale string read in netlist -# for python2 or python3 (Windows/unix) +# A helper function to filter/convert a string read in netlist +#currently: do nothing def fromNetlistText( aText ): - currpage = sys.stdout.encoding #the current code page. can be none - if currpage is None: - return aText - if currpage != 'utf-8': - try: - return aText.encode('utf-8').decode(currpage) - except UnicodeDecodeError: - return aText - else: - return aText + return aText def myEqu(self, other): """myEqu is a more advanced equivalence function for components which is @@ -74,7 +65,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 = kicad_utils.open_file_write(sys.argv[2], 'w') + f = kicad_utils.open_file_writeUTF8(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 5fc01022cc..d707f14f59 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 @@ -21,16 +21,10 @@ import kicad_utils import csv import sys -# A helper function to convert a UTF8/Unicode/locale string read in netlist -# for python2 or python3 +# A helper function to filter/convert a string read in netlist +#currently: do nothing def fromNetlistText( aText ): - if sys.platform.startswith('win32'): - try: - return aText.encode('utf-8').decode('cp1252') - except UnicodeDecodeError: - return aText - else: - return aText + return aText # Generate an instance of a generic netlist, and load the netlist tree from # the command line option. If the file doesn't exist, execution will stop @@ -39,7 +33,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 = kicad_utils.open_file_write(sys.argv[2], 'w') + f = kicad_utils.open_file_writeUTF8(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 401ba705fa..58042f448b 100644 --- a/eeschema/plugins/python_scripts/bom_csv_sorted_by_ref.py +++ b/eeschema/plugins/python_scripts/bom_csv_sorted_by_ref.py @@ -23,16 +23,10 @@ import kicad_utils import csv import sys -# A helper function to convert a UTF8/Unicode/locale string read in netlist -# for python2 or python3 +# A helper function to filter/convert a string read in netlist +#currently: do nothing def fromNetlistText( aText ): - if sys.platform.startswith('win32'): - try: - return aText.encode('utf-8').decode('cp1252') - except UnicodeDecodeError: - return aText - else: - return aText + return aText # Generate an instance of a generic netlist, and load the netlist tree from # the command line option. If the file doesn't exist, execution will stop @@ -41,7 +35,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 = kicad_utils.open_file_write(sys.argv[2], 'w') + f = kicad_utils.open_file_writeUTF8(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_txt_sorted_by_ref.py b/eeschema/plugins/python_scripts/bom_txt_sorted_by_ref.py index 059278e14d..e50974c301 100644 --- a/eeschema/plugins/python_scripts/bom_txt_sorted_by_ref.py +++ b/eeschema/plugins/python_scripts/bom_txt_sorted_by_ref.py @@ -21,16 +21,11 @@ import kicad_utils import csv import sys -# A helper function to convert a UTF8/Unicode/locale string read in netlist -# for python2 or python3 (Windows/unix) + +# A helper function to filter/convert a string read in netlist +#currently: do nothing def fromNetlistText( aText ): - if sys.platform.startswith('win32'): - try: - return aText.encode('utf-8').decode('cp1252') - except UnicodeDecodeError: - return aText - else: - return aText + return aText # Generate an instance of a generic netlist, and load the netlist tree from # the command line option. If the file doesn't exist, execution will stop @@ -39,7 +34,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 = kicad_utils.open_file_write(sys.argv[2], 'w') + f = kicad_utils.open_file_writeUTF8(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 index 9ba9d9e9f2..e55e165c4d 100644 --- a/eeschema/plugins/python_scripts/kicad_utils.py +++ b/eeschema/plugins/python_scripts/kicad_utils.py @@ -12,4 +12,16 @@ def open_file_write(path, mode): if not os.path.isdir(dir_path): os.makedirs(dir_path) - return open(path, mode) \ No newline at end of file + return open(path, mode) + +def open_file_writeUTF8(path, mode): + ''' + Open "path" for writing, creating any parent directories as needed. + Use it only for text files. Force text encoding in UTF-8. + ''' + dir_path = os.path.dirname(path) + + if not os.path.isdir(dir_path): + os.makedirs(dir_path) + + return open(path, mode, encoding='utf-8') \ No newline at end of file