always write LF line endings

small pitfall: I windows users use a filehandle that results from a call
to open() without the newline option, they get still CRLF line endings
This commit is contained in:
Martin Diehl 2021-02-10 10:03:35 +01:00
parent ef45e856a1
commit 6895ef6b18
5 changed files with 12 additions and 12 deletions

View File

@ -238,7 +238,7 @@ class Colormap(mpl.colors.ListedColormap):
fhandle = None fhandle = None
else: else:
try: try:
fhandle = open(fname,'w') fhandle = open(fname,'w',newline='\n')
except TypeError: except TypeError:
fhandle = fname fhandle = fname
@ -254,7 +254,7 @@ class Colormap(mpl.colors.ListedColormap):
'RGBPoints':colors 'RGBPoints':colors
}] }]
with open(self.name.replace(' ','_')+'.json', 'w') if fhandle is None else fhandle as f: with open(self.name.replace(' ','_')+'.json','w',newline='\n') if fhandle is None else fhandle as f:
json.dump(out, f,indent=4) json.dump(out, f,indent=4)
@ -273,14 +273,14 @@ class Colormap(mpl.colors.ListedColormap):
fhandle = None fhandle = None
else: else:
try: try:
fhandle = open(fname,'w') fhandle = open(fname,'w',newline='\n')
except TypeError: except TypeError:
fhandle = fname fhandle = fname
labels = {'RGBA':4} if self.colors.shape[1] == 4 else {'RGB': 3} labels = {'RGBA':4} if self.colors.shape[1] == 4 else {'RGB': 3}
t = Table(self.colors,labels,f'Creator: {util.execution_stamp("Colormap")}') t = Table(self.colors,labels,f'Creator: {util.execution_stamp("Colormap")}')
with open(self.name.replace(' ','_')+'.txt', 'w') if fhandle is None else fhandle as f: with open(self.name.replace(' ','_')+'.txt','w',newline='\n') if fhandle is None else fhandle as f:
t.save(f) t.save(f)
@ -299,7 +299,7 @@ class Colormap(mpl.colors.ListedColormap):
fhandle = None fhandle = None
else: else:
try: try:
fhandle = open(fname,'w') fhandle = open(fname,'w',newline='\n')
except TypeError: except TypeError:
fhandle = fname fhandle = fname
# ToDo: test in GOM # ToDo: test in GOM
@ -308,7 +308,7 @@ class Colormap(mpl.colors.ListedColormap):
+ f'30 NO_UNIT 1 1 64 64 64 255 1 0 0 0 0 0 0 3 0 {len(self.colors)}' \ + f'30 NO_UNIT 1 1 64 64 64 255 1 0 0 0 0 0 0 3 0 {len(self.colors)}' \
+ ' '.join([f' 0 {c[0]} {c[1]} {c[2]} 255 1' for c in reversed((self.colors*255).astype(int))]) \ + ' '.join([f' 0 {c[0]} {c[1]} {c[2]} 255 1' for c in reversed((self.colors*255).astype(int))]) \
+ '\n' + '\n'
with open(self.name.replace(' ','_')+'.legend', 'w') if fhandle is None else fhandle as f: with open(self.name.replace(' ','_')+'.legend','w',newline='\n') if fhandle is None else fhandle as f:
f.write(GOM_str) f.write(GOM_str)
@ -327,14 +327,14 @@ class Colormap(mpl.colors.ListedColormap):
fhandle = None fhandle = None
else: else:
try: try:
fhandle = open(fname,'w') fhandle = open(fname,'w',newline='\n')
except TypeError: except TypeError:
fhandle = fname fhandle = fname
# ToDo: test in gmsh # ToDo: test in gmsh
gmsh_str = 'View.ColorTable = {\n' \ gmsh_str = 'View.ColorTable = {\n' \
+'\n'.join([f'{c[0]},{c[1]},{c[2]},' for c in self.colors[:,:3]*255]) \ +'\n'.join([f'{c[0]},{c[1]},{c[2]},' for c in self.colors[:,:3]*255]) \
+'\n}\n' +'\n}\n'
with open(self.name.replace(' ','_')+'.msh', 'w') if fhandle is None else fhandle as f: with open(self.name.replace(' ','_')+'.msh','w',newline='\n') if fhandle is None else fhandle as f:
f.write(gmsh_str) f.write(gmsh_str)

View File

@ -75,7 +75,7 @@ class Config(dict):
""" """
try: try:
fhandle = open(fname,'w') fhandle = open(fname,'w',newline='\n')
except TypeError: except TypeError:
fhandle = fname fhandle = fname

View File

@ -1287,7 +1287,7 @@ class Result:
np.prod(shape))} np.prod(shape))}
data_items[-1].text=f'{os.path.split(self.fname)[1]}:{name}' data_items[-1].text=f'{os.path.split(self.fname)[1]}:{name}'
with open(self.fname.with_suffix('.xdmf').name,'w') as f: with open(self.fname.with_suffix('.xdmf').name,'w',newline='\n') as f:
f.write(xml.dom.minidom.parseString(ET.tostring(xdmf).decode()).toprettyxml()) f.write(xml.dom.minidom.parseString(ET.tostring(xdmf).decode()).toprettyxml())

View File

@ -380,7 +380,7 @@ class Table:
[f'# {comment}' for comment in self.comments] [f'# {comment}' for comment in self.comments]
try: try:
fhandle = open(fname,'w') fhandle = open(fname,'w',newline='\n')
except TypeError: except TypeError:
fhandle = fname fhandle = fname

View File

@ -66,7 +66,7 @@ class Marc:
if logfile is not None: if logfile is not None:
try: try:
f = open(logfile,'w+') f = open(logfile,'w+',newline='\n')
except TypeError: except TypeError:
f = logfile f = logfile
else: else: