Merge branch 'parse-CRLF-YAML' into 'development'

Stop if YAML contains CRLF line endings

See merge request damask/DAMASK!333
This commit is contained in:
Franz Roters 2021-02-11 09:08:03 +00:00
commit e96352b0ea
7 changed files with 68 additions and 52 deletions

@ -1 +1 @@
Subproject commit d90babadfb0a33afa1b793044cc4efd4d7430731
Subproject commit 5d27d879abaeb1542e3f9f3065172be740af2899

View File

@ -223,25 +223,46 @@ class Colormap(mpl.colors.ListedColormap):
return Colormap(np.array(rev.colors),rev.name[:-4] if rev.name.endswith('_r_r') else rev.name)
def _get_file_handle(self,fname,extension):
"""
Provide file handle.
Parameters
----------
fname : file, str, pathlib.Path, or None
Filename or filehandle, will be name of the colormap+extension if None.
extension: str
Extension of the filename.
Returns
-------
f
File handle
"""
if fname is None:
fhandle = open(self.name.replace(' ','_')+'.'+extension,'w',newline='\n')
else:
try:
fhandle = open(fname,'w',newline='\n')
except TypeError:
fhandle = fname
return fhandle
def save_paraview(self,fname=None):
"""
Save as JSON file for use in Paraview.
Parameters
----------
fname : file, str, or pathlib.Path, optional.
fname : file, str, or pathlib.Path, optional
Filename to store results. If not given, the filename will
consist of the name of the colormap and extension '.json'.
"""
if fname is None:
fhandle = None
else:
try:
fhandle = open(fname,'w')
except TypeError:
fhandle = fname
colors = []
for i,c in enumerate(np.round(self.colors,6).tolist()):
colors+=[i]+c
@ -254,8 +275,7 @@ class Colormap(mpl.colors.ListedColormap):
'RGBPoints':colors
}]
with open(self.name.replace(' ','_')+'.json', 'w') if fhandle is None else fhandle as f:
json.dump(out, f,indent=4)
json.dump(out,self._get_file_handle(fname,'json'),indent=4)
def save_ASCII(self,fname=None):
@ -264,24 +284,14 @@ class Colormap(mpl.colors.ListedColormap):
Parameters
----------
fname : file, str, or pathlib.Path, optional.
fname : file, str, or pathlib.Path, optional
Filename to store results. If not given, the filename will
consist of the name of the colormap and extension '.txt'.
"""
if fname is None:
fhandle = None
else:
try:
fhandle = open(fname,'w')
except TypeError:
fhandle = fname
labels = {'RGBA':4} if self.colors.shape[1] == 4 else {'RGB': 3}
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:
t.save(f)
t.save(self._get_file_handle(fname,'txt'))
def save_GOM(self,fname=None):
@ -290,26 +300,19 @@ class Colormap(mpl.colors.ListedColormap):
Parameters
----------
fname : file, str, or pathlib.Path, optional.
fname : file, str, or pathlib.Path, optional
Filename to store results. If not given, the filename will
consist of the name of the colormap and extension '.legend'.
"""
if fname is None:
fhandle = None
else:
try:
fhandle = open(fname,'w')
except TypeError:
fhandle = fname
# ToDo: test in GOM
GOM_str = '1 1 {name} 9 {name} '.format(name=self.name.replace(" ","_")) \
+ '0 1 0 3 0 0 -1 9 \\ 0 0 0 255 255 255 0 0 255 ' \
+ 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))]) \
+ '\n'
with open(self.name.replace(' ','_')+'.legend', 'w') if fhandle is None else fhandle as f:
f.write(GOM_str)
self._get_file_handle(fname,'legend').write(GOM_str)
def save_gmsh(self,fname=None):
@ -318,24 +321,16 @@ class Colormap(mpl.colors.ListedColormap):
Parameters
----------
fname : file, str, or pathlib.Path, optional.
fname : file, str, or pathlib.Path, optional
Filename to store results. If not given, the filename will
consist of the name of the colormap and extension '.msh'.
"""
if fname is None:
fhandle = None
else:
try:
fhandle = open(fname,'w')
except TypeError:
fhandle = fname
# ToDo: test in gmsh
gmsh_str = 'View.ColorTable = {\n' \
+'\n'.join([f'{c[0]},{c[1]},{c[2]},' for c in self.colors[:,:3]*255]) \
+'\n}\n'
with open(self.name.replace(' ','_')+'.msh', 'w') if fhandle is None else fhandle as f:
f.write(gmsh_str)
self._get_file_handle(fname,'msh').write(gmsh_str)
@staticmethod

View File

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

View File

@ -1287,7 +1287,7 @@ class Result:
np.prod(shape))}
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())

View File

@ -26,7 +26,7 @@ class Table:
comments_ = [comments] if isinstance(comments,str) else comments
self.comments = [] if comments_ is None else [c for c in comments_]
self.data = pd.DataFrame(data=data)
self.shapes = { k:(v,) if isinstance(v,(np.int,int)) else v for k,v in shapes.items() }
self.shapes = { k:(v,) if isinstance(v,(np.int64,np.int32,int)) else v for k,v in shapes.items() }
self._label_uniform()
def __repr__(self):
@ -380,7 +380,7 @@ class Table:
[f'# {comment}' for comment in self.comments]
try:
fhandle = open(fname,'w')
fhandle = open(fname,'w',newline='\n')
except TypeError:
fhandle = fname

View File

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

View File

@ -65,8 +65,8 @@ end subroutine IO_init
function IO_readlines(fileName) result(fileContent)
character(len=*), intent(in) :: fileName
character(len=pStringLen), dimension(:), allocatable :: fileContent !< file content, separated per lines
character(len=pStringLen) :: line
character(len=:), allocatable :: rawData
integer :: &
@ -75,6 +75,7 @@ function IO_readlines(fileName) result(fileContent)
l
logical :: warned
rawData = IO_read(fileName)
!--------------------------------------------------------------------------------------------------
@ -112,16 +113,21 @@ end function IO_readlines
!--------------------------------------------------------------------------------------------------
!> @brief Read whole file.
!> @details ensures that the string ends with a new line (expected UNIX behavior)
!> @details ensures that the string ends with a new line (expected UNIX behavior) and rejects
! windows (CRLF) line endings
!--------------------------------------------------------------------------------------------------
function IO_read(fileName) result(fileContent)
character(len=*), intent(in) :: fileName
character(len=:), allocatable :: fileContent
integer :: &
fileLength, &
fileUnit, &
myStat
myStat, &
firstEOL
character, parameter :: CR = achar(13)
inquire(file = fileName, size=fileLength)
open(newunit=fileUnit, file=fileName, access='stream',&
@ -137,8 +143,12 @@ function IO_read(fileName) result(fileContent)
if(myStat /= 0) call IO_error(102,ext_msg=trim(fileName))
close(fileUnit)
if(fileContent(fileLength:fileLength) /= IO_EOL) fileContent = fileContent//IO_EOL ! ensure EOL@EOF
firstEOL = index(fileContent,IO_EOL)
if(scan(fileContent(firstEOL:firstEOL),CR) /= 0) call IO_error(115)
end function IO_read
@ -151,6 +161,7 @@ logical pure function IO_isBlank(string)
integer :: posNonBlank
posNonBlank = verify(string,IO_WHITESPACE)
IO_isBlank = posNonBlank == 0 .or. posNonBlank == scan(string,IO_COMMENT)
@ -170,6 +181,7 @@ pure function IO_stringPos(string)
integer :: left, right
allocate(IO_stringPos(1), source=0)
right = 0
@ -249,6 +261,7 @@ pure function IO_lc(string)
integer :: i,n
do i=1,len(string)
n = index(UPPER,string(i:i))
if(n/=0) then
@ -271,6 +284,7 @@ function IO_rmComment(line)
character(len=:), allocatable :: IO_rmComment
integer :: split
split = index(line,IO_COMMENT)
if (split == 0) then
@ -292,6 +306,7 @@ integer function IO_stringAsInt(string)
integer :: readStatus
character(len=*), parameter :: VALIDCHARS = '0123456789+- '
valid: if (verify(string,VALIDCHARS) == 0) then
read(string,*,iostat=readStatus) IO_stringAsInt
if (readStatus /= 0) call IO_error(111,ext_msg=string)
@ -313,6 +328,7 @@ real(pReal) function IO_stringAsFloat(string)
integer :: readStatus
character(len=*), parameter :: VALIDCHARS = '0123456789eE.+- '
valid: if (verify(string,VALIDCHARS) == 0) then
read(string,*,iostat=readStatus) IO_stringAsFloat
if (readStatus /= 0) call IO_error(112,ext_msg=string)
@ -331,6 +347,7 @@ logical function IO_stringAsBool(string)
character(len=*), intent(in) :: string !< string for conversion to int value
if (trim(adjustl(string)) == 'True' .or. trim(adjustl(string)) == 'true') then
IO_stringAsBool = .true.
elseif (trim(adjustl(string)) == 'False' .or. trim(adjustl(string)) == 'false') then
@ -356,6 +373,7 @@ subroutine IO_error(error_ID,el,ip,g,instance,ext_msg)
character(len=:), allocatable :: msg
character(len=pStringLen) :: formatString
select case (error_ID)
!--------------------------------------------------------------------------------------------------
@ -382,6 +400,9 @@ subroutine IO_error(error_ID,el,ip,g,instance,ext_msg)
msg = 'invalid character for logical:'
case (114)
msg = 'cannot decode base64 string:'
case (115)
msg = 'found CR. Windows file endings (CRLF) are not supported.'
!--------------------------------------------------------------------------------------------------
! lattice error messages