Fix a UTF8 convert issue in some BOM python scripts.

Fixes #12435
https://gitlab.com/kicad/code/kicad/issues/12435
This commit is contained in:
jean-pierre charras 2022-09-16 10:25:51 +02:00
parent 4e67b94b00
commit f379fa4f7f
5 changed files with 30 additions and 44 deletions

View File

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

View File

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

View File

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

View File

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

View File

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