Skip to content
This repository has been archived by the owner on Nov 5, 2020. It is now read-only.

Commit

Permalink
fix: use UTF-8 encoding by default when dealing with text files (close
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Mar 23, 2020
1 parent e15e014 commit bcf8cd9
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
1.0.5
- fix: use UTF-8 encoding by default when dealing with text files (#267)
1.0.4
- fix: minor issue with converting pos_y feature in sessions from
before 0.7.6
Expand Down
2 changes: 1 addition & 1 deletion shapeout/gui/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def OnBatch(self, e=None):

head = ["data file", "Title"] + head

with io.open(self.out_tsv_file, "w") as fd:
with io.open(self.out_tsv_file, "w", encoding='utf-8') as fd:
header = u"\t".join([ h for h in head ])
fd.write("# "+header+"\n")

Expand Down
2 changes: 1 addition & 1 deletion shapeout/gui/confparms.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def GetConfigurationKeys(cfgfilename, capitalize=True):
Load the configuration file and return the list of variables
in the order they appear.
"""
with io.open(cfgfilename, 'r') as f:
with io.open(cfgfilename, 'r', encoding='utf-8') as f:
code = f.readlines()

cfglist = list()
Expand Down
2 changes: 1 addition & 1 deletion shapeout/gui/controls_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def OnApply(self, e=None):
"",
]

with io.open(outf, "w") as fd:
with io.open(outf, "w", encoding='utf-8') as fd:
fd.writelines([ll + "\r\n" for ll in citreq])
fd.writelines(result["Full Summary"].replace("\n", "\r\n"))

Expand Down
2 changes: 1 addition & 1 deletion shapeout/gui/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,5 @@ def export_statistics_tsv(parent):
if path.lower().endswith(".tsv") is not True:
path = path+".tsv"
parent.config.set_path(os.path.dirname(path), "TSV")
with io.open(path, 'w') as fd:
with io.open(path, 'w', encoding='utf-8') as fd:
fd.writelines(exp)
2 changes: 1 addition & 1 deletion shapeout/gui/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def check_release(
except:
msg = "Timeout or wrong URL."
try:
with open("check_update_error.log", "w") as fe:
with open("check_update_error.log", "w", encoding='utf-8') as fe:
fe.writelines(str(traceback.format_exc()))
except:
pass
Expand Down
2 changes: 1 addition & 1 deletion shapeout/meta_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def get_event_count(fname):
avif = mdir / (mid + "_imaq.avi")
if logf.exists():
# 1. The MX_log.ini file "Events" tag
with logf.open() as fd:
with logf.open(encoding='utf-8') as fd:
logd = fd.readlines()
for l in logd:
if l.strip().startswith("Events:"):
Expand Down
12 changes: 6 additions & 6 deletions shapeout/session/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def compatibilitize_session(tempdir, hash_update=True, search_path="."):
index.index_save(tempdir, index_dict)

for cc in change_configs:
with cc.open() as fd:
with cc.open(encoding='utf-8') as fd:
data = fd.read()

if version < LooseVersion("0.7.1"):
Expand Down Expand Up @@ -307,20 +307,20 @@ def compatibilitize_session(tempdir, hash_update=True, search_path="."):
pattern.format(old),
pattern.format(new))

with cc.open("w") as fd:
with cc.open("w", encoding='utf-8') as fd:
fd.write(data)

# Change polygon filters as well
pfile = tempdir / "PolygonFilters.poly"
if pfile.exists():
with pfile.open() as fd:
with pfile.open(encoding='utf-8') as fd:
datap = fd.read()

if version < LooseVersion("0.7.6"):
datap = compatibilitize_polygon(pdata=datap,
version=version)

with pfile.open("w") as fd:
with pfile.open("w", encoding='utf-8') as fd:
fd.write(datap)
# load polygon filters here, because they are needed when computing
# the hashes (RTDCBase.apply_filter is called for hierarchies)
Expand Down Expand Up @@ -397,11 +397,11 @@ def convert_polygon(infile, outfile=None, version=None):
suffix=".poly")
outfile = pathlib.Path(outfile)

with infile.open() as fd:
with infile.open(encoding='utf-8') as fd:
pdata = fd.read()
pdata = compatibilitize_polygon(pdata=pdata, version=version)

with outfile.open("w") as fd:
with outfile.open("w", encoding='utf-8') as fd:
fd.write(pdata)

return outfile
Expand Down
6 changes: 3 additions & 3 deletions shapeout/session/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def index_load(index_file):

if index_file.is_dir():
index_file = index_file / "index.txt"
with index_file.open() as f:
with index_file.open(encoding='utf-8') as f:
code = f.readlines()

for line in code:
Expand Down Expand Up @@ -165,7 +165,7 @@ def index_save(index_file, index_dict, save_version=version):

for i in range(len(out)):
out[i] = out[i]+"\n"
with index_file.open("w") as f:
with index_file.open("w", encoding='utf-8') as f:
f.writelines(out)


Expand Down Expand Up @@ -200,7 +200,7 @@ def index_version(index_file):
if index_file.is_dir():
index_file = index_file / "index.txt"
# Obtain version of session
with index_file.open("r") as fd:
with index_file.open("r", encoding='utf-8') as fd:
data = fd.readlines()

for line in data:
Expand Down
6 changes: 3 additions & 3 deletions shapeout/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ def __init__(self, name=NAME, defaults=DEFAULTS, directory=None):
# create file if not existent
if not fname.exists():
# Create the file
fname.open("w").close()
fname.open("w", encoding='utf-8').close()

self.cfgfile = fname
self.defaults = defaults
self.working_directories = {}

def load(self):
"""Loads the settings file returning a dictionary"""
with self.cfgfile.open() as fop:
with self.cfgfile.open(encoding='utf-8') as fop:
fc = fop.readlines()
cdict = {}
for line in fc:
Expand Down Expand Up @@ -115,7 +115,7 @@ def save(self, cdict):
sval = str(sval).decode("utf-8")
outlist.append(u"{} = {}\n".format(sk, sval))

with self.cfgfile.open('w') as fop:
with self.cfgfile.open('w', encoding='utf-8') as fop:
fop.writelines(outlist)

def set_bool(self, key, value):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gui_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_batch(self):
batch.data_files = [tdms_path]
batch.OnBatch()

with io.open(batch.out_tsv_file) as fd:
with io.open(batch.out_tsv_file, encoding='utf-8') as fd:
data = fd.readlines()

header = [d.strip().lower() for d in data[0].strip("# ").split("\t")]
Expand Down

0 comments on commit bcf8cd9

Please sign in to comment.