Skip to content

Commit

Permalink
Ensure consistent write_text usage and write unix-like line endings a…
Browse files Browse the repository at this point in the history
…lways
  • Loading branch information
tkittel committed Oct 31, 2024
1 parent cb928ac commit 894f428
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion NCrystal/_cli_cif2ncmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,4 @@ def _main_impl( args, do_quiet ):
else:
if not do_quiet:
print(f"Writing {fn}")
pathlib.Path(fn).write_text(out,encoding = 'utf8')
nc_common.write_text(fn,out)
3 changes: 2 additions & 1 deletion NCrystal/_cli_hfg2ncmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def create_argparser_for_sphinx( progname ):
def main( progname, arglist ):
import pathlib
from .hfg2ncmat import _default_debye_temp, hfg2ncmat
from ._common import write_text as nc_write_text
args = _parseArgs( _default_debye_temp(), progname, arglist )
do_stdout = args.output=='stdout'
try:
Expand All @@ -139,5 +140,5 @@ def main( progname, arglist ):
if not outfile.parent.is_dir():
raise SystemExit('Error: output directory does not exist:'
f' { outfile.parent }')
outfile.write_text(ncmat,encoding='utf8')
nc_write_text(outfile,ncmat)
print(f"Wrote: {outfile}")
3 changes: 2 additions & 1 deletion NCrystal/_cli_mcstasunion.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ def main( progname, args ):
raise SystemExit(f'File already exists (use --force to overwrite): {p}')
if not p.parent.is_dir():
raise SystemExit(f'Output directory does not exist: {p.parent}')
p.write_text(code,encoding = 'utf8' )
from ._common import write_text
write_text(p,code)
print(f"Wrote: {p}")
5 changes: 3 additions & 2 deletions NCrystal/_cli_ncmat2hkl.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ def _main_impl( args, do_quiet, override_prec ):
for line in content_iter:
sys.stdout.write(line + '\n')
else:
args.output.write_text( '\n'.join( content_iter ) + '\n',
encoding = 'utf8' )
from ._common import write_text
write_text( args.output,
( '\n'.join( content_iter ) + '\n' ) )
if not do_quiet:
import pathlib
print(f"Wrote {pathlib.Path(args.output).name}")
12 changes: 12 additions & 0 deletions NCrystal/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,15 @@ def _calc_md5hexdigest( str_or_bytes, / ):
else:
data = str_or_bytes
return hashlib.md5( data ).hexdigest()

def write_text( path, content ):
"""Like path.write_text(content) but forcing some global settings
to ensure consistent NCrystal behaviour on any platform. Specifically
encoding='utf8' and newline='\n'"""
#The newline parameter was only added for pathlib.Path.write_text in python
#3.10, so we do instead:
import pathlib
with pathlib.Path(path).open( 'wt',
encoding = 'utf8',
newline='\n' ) as fh:
fh.write(content)
6 changes: 5 additions & 1 deletion NCrystal/_ncmat2cpp_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,11 @@ def linepattern(strdata):
else:
import pathlib
of=pathlib.Path(outfile)
of.write_text(out,encoding='utf8')
if run_standalone:
of.write_text(out,encoding='utf8')
else:
from ._common import write_text
write_text(of,out)
print('Wrote: %s'%of)
return out

Expand Down
4 changes: 2 additions & 2 deletions NCrystal/_ncmatimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,8 @@ def write( self, path, cfg_params ):
import pathlib
p = pathlib.Path( path )
assert p.name.endswith('.ncmat') and len(p.name) > 6
p.write_text( self.create_ncmat( cfg_params = cfg_params ),
encoding = 'utf8' )
_nc_common.write_text( p,
self.create_ncmat( cfg_params = cfg_params ) )
return p

def load(self,cfg_params,force ):
Expand Down
2 changes: 1 addition & 1 deletion NCrystal/cifutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ def notfound():
_nc_common.print(f"Adding {fn} to local file cache in $NCRYSTAL_ONLINEDB_CACHEDIR")
p.mkdir(parents=True, exist_ok=True)
pfn_tmp = p / f'{fn}_tmp_{os.getpid()}'
pfn_tmp.write_text(text_data,encoding='utf8')
_nc_common.write_text(pfn_tmp,text_data)
pfn_tmp.replace(pfn)
return notfound()
#retrieve data from local cache:
Expand Down
3 changes: 2 additions & 1 deletion scripts/ncrystal_endf2ncmat
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ NCMAT v2

def genstdheaders():
def do_write(fn,suggestedcmdopts,content):
pathlib.Path(fn).write_text(content,encoding='utf8')
nccommon = ncImportSubModule(NC,'_common')
nccommon.write_text(fn,content)
print(f'Wrote {fn}')
progname=os.path.basename(sys.argv[0])
print( f' -> Suggested conversion cmd: {progname} {suggestedcmdopts}')
Expand Down
2 changes: 1 addition & 1 deletion tests/scripts/fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
## ##
################################################################################

import NCTestUtils.enable_fpe
import NCTestUtils.enable_fpe # noqa F401
from NCTestUtils.loadlib import Lib
from NCTestUtils.common import ( explicit_unicode_str, is_windows )
import pathlib
Expand Down

0 comments on commit 894f428

Please sign in to comment.