From 4d24c369f4141dbeafa76c26f83cd8e43928f0cd Mon Sep 17 00:00:00 2001
From: dave <dave@dtu.dk>
Date: Wed, 3 Feb 2016 15:35:54 +0100
Subject: [PATCH] PY2 compat: open with utf-8 enc, str(os.getcwd)

---
 wetb/fast/fast_io.py                        | 4 ++--
 wetb/hawc2/ascii2bin/ascii2bin.py           | 6 +++---
 wetb/hawc2/at_time_file.py                  | 2 +-
 wetb/hawc2/cmp_test_cases.py                | 4 ++--
 wetb/hawc2/htc_file.py                      | 4 ++--
 wetb/hawc2/log_file.py                      | 2 +-
 wetb/hawc2/sel_file.py                      | 4 ++--
 wetb/hawc2/shear_file.py                    | 2 +-
 wetb/hawc2/simulation.py                    | 6 +++---
 wetb/prepost/Simulations.py                 | 2 +-
 wetb/prepost/dlcdefs.py                     | 2 +-
 wetb/utils/cython_compile/cython_compile.py | 6 +++---
 12 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/wetb/fast/fast_io.py b/wetb/fast/fast_io.py
index 7d331df..78e4859 100644
--- a/wetb/fast/fast_io.py
+++ b/wetb/fast/fast_io.py
@@ -37,7 +37,7 @@ def load_output(filename):
     """
 
     assert os.path.isfile(filename), "File, %s, does not exists" % filename
-    with open(filename, 'r') as f:
+    with open(filename, 'r', encoding='utf-8') as f:
         try:
             f.readline()
         except UnicodeDecodeError:
@@ -45,7 +45,7 @@ def load_output(filename):
     return load_ascii_output(filename)
 
 def load_ascii_output(filename):
-    with open(filename) as f:
+    with open(filename, encoding='utf-8') as f:
         info = {}
         info['name'] = os.path.splitext(os.path.basename(filename))[0]
         try:
diff --git a/wetb/hawc2/ascii2bin/ascii2bin.py b/wetb/hawc2/ascii2bin/ascii2bin.py
index 11a0971..f8280ae 100644
--- a/wetb/hawc2/ascii2bin/ascii2bin.py
+++ b/wetb/hawc2/ascii2bin/ascii2bin.py
@@ -24,7 +24,7 @@ class TextUI(object):
 sys.path.append(".")
 
 def size_from_file(selfilename):
-    with open(selfilename) as f:
+    with open(selfilename, encoding='utf-8') as f:
         info = f.readlines()[8].split()
         scans = int(info[0])
         no_sensors = int(info[1])
@@ -39,7 +39,7 @@ def ascii2bin(ascii_selfilename, bin_selfilename=None, ui=TextUI()):
 
 
     # Read, convert and write sel file
-    with open(ascii_selfilename) as f:
+    with open(ascii_selfilename, encoding='utf-8') as f:
         lines = f.readlines()
 
     if "BINARY" in lines[8]:
@@ -54,7 +54,7 @@ def ascii2bin(ascii_selfilename, bin_selfilename=None, ui=TextUI()):
         scale_factors = pandas_dat_ascii2bin(ascii_datfilename, bin_selfilename.replace('.sel', '.dat'), ui)
         for sf in scale_factors:
             lines.append("  %.5E\n" % sf)
-        with open(bin_selfilename, 'w') as f:
+        with open(bin_selfilename, 'w', encoding='utf-8') as f:
             f.writelines(lines)
         if ui is not None:
             ui.show_message("Finish converting %s to %s" % (ascii_selfilename, bin_selfilename))
diff --git a/wetb/hawc2/at_time_file.py b/wetb/hawc2/at_time_file.py
index bc1bb4f..38cbff6 100644
--- a/wetb/hawc2/at_time_file.py
+++ b/wetb/hawc2/at_time_file.py
@@ -27,7 +27,7 @@ class AtTimeFile(object):
     -5.34743208242399
     """
     def __init__(self, filename):
-        with open(filename) as fid:
+        with open(filename, encoding='utf-8') as fid:
             lines = fid.readlines()
         self.attribute_names = lines[2].lower().replace("#", "").split()
         data = np.array([[float(l) for l in lines[i].split() ] for i in range(3, len(lines))])
diff --git a/wetb/hawc2/cmp_test_cases.py b/wetb/hawc2/cmp_test_cases.py
index b1d4f1a..1745aa4 100644
--- a/wetb/hawc2/cmp_test_cases.py
+++ b/wetb/hawc2/cmp_test_cases.py
@@ -32,9 +32,9 @@ class CompareTestCases(unittest.TestCase):
 
 
     def compare_lines(self, ref_file, test_file, skip_first=0):
-        with open(ref_file) as ref:
+        with open(ref_file, encoding='utf-8') as ref:
             ref_lines = ref.readlines()
-        with open(test_file) as test:
+        with open(test_file, encoding='utf-8') as test:
             test_lines = test.readlines()
         self.assertEqual(len(ref_lines), len(test_lines), "\nNumber of lines differs in: '%s' and '%s'" % (ref_file, test_file))
         for i, (ref_l, test_l) in enumerate(zip(ref_lines[skip_first:], test_lines[skip_first:])):
diff --git a/wetb/hawc2/htc_file.py b/wetb/hawc2/htc_file.py
index 54a742a..7893858 100644
--- a/wetb/hawc2/htc_file.py
+++ b/wetb/hawc2/htc_file.py
@@ -59,7 +59,7 @@ class HTCFile(HTCContents, HTCDefaults):
     def readlines(self, filename):
         self.htc_inputfiles.append(filename)
         htc_lines = []
-        with open(filename) as fid:
+        with open(filename, encoding='utf-8') as fid:
             lines = fid.readlines()
         for l in lines:
             if l.lower().lstrip().startswith('continue_in_file'):
@@ -85,7 +85,7 @@ class HTCFile(HTCContents, HTCDefaults):
         # exist_ok does not exist in Python27
         if not os.path.exists(os.path.dirname(filename)):
             os.makedirs(os.path.dirname(filename))#, exist_ok=True)
-        with open(filename, 'w') as fid:
+        with open(filename, 'w', encoding='utf-8') as fid:
             fid.write(str(self))
 
     def set_name(self, name, folder="htc"):
diff --git a/wetb/hawc2/log_file.py b/wetb/hawc2/log_file.py
index d8cde89..fec5908 100644
--- a/wetb/hawc2/log_file.py
+++ b/wetb/hawc2/log_file.py
@@ -70,7 +70,7 @@ class LogFile(object):
         # exist_ok does not exist in Python27
         if not os.path.exists(os.path.dirname(self.filename)):
             os.makedirs(os.path.dirname(self.filename))#, exist_ok=True)
-        with open(self.filename, 'w'):
+        with open(self.filename, 'w', encoding='utf-8'):
             pass
         self.reset()
 
diff --git a/wetb/hawc2/sel_file.py b/wetb/hawc2/sel_file.py
index a01c018..891bc13 100644
--- a/wetb/hawc2/sel_file.py
+++ b/wetb/hawc2/sel_file.py
@@ -48,7 +48,7 @@ class SelFile(object):
     def __init__(self, sel_filename):
         if not os.path.isfile(sel_filename) or os.path.splitext(sel_filename)[1] != ".sel":
             raise Warning("%s cannot be found or is not a legal *.sel file" % os.path.realpath(sel_filename))
-        with open(sel_filename) as f:
+        with open(sel_filename, encoding='utf-8') as f:
             lines = f.readlines()
 
 
@@ -139,6 +139,6 @@ def save(sel_filename, version, time, scans, no_sensors, duration, sensors, scal
         for sf in scale_factors:
             lines.append("  %.5E" % sf)
 
-    with open(sel_filename, 'w') as f:
+    with open(sel_filename, 'w', encoding='utf-8') as f:
         f.write("\n".join(lines))
 
diff --git a/wetb/hawc2/shear_file.py b/wetb/hawc2/shear_file.py
index 04a768b..4cccd75 100644
--- a/wetb/hawc2/shear_file.py
+++ b/wetb/hawc2/shear_file.py
@@ -54,7 +54,7 @@ def save(filename, y_coordinates, z_coordinates, u=None, v=None, w=None):
     # exist_ok does not exist in Python27
     if not os.path.exists(os.path.dirname(filename)):
         os.makedirs(os.path.dirname(filename))#, exist_ok=True)
-    with open(filename, 'w') as fid:
+    with open(filename, 'w', encoding='utf-8') as fid:
         fid.write(" # autogenerated shear file\n")
         fid.write("  %d %d\n" % (len(y_coordinates), len(z_coordinates)))
         for i, l in enumerate(['v', 'u', 'w']):
diff --git a/wetb/hawc2/simulation.py b/wetb/hawc2/simulation.py
index f4d38e7..151f29b 100644
--- a/wetb/hawc2/simulation.py
+++ b/wetb/hawc2/simulation.py
@@ -116,7 +116,7 @@ class Simulation(object):
         additional_files_file = os.path.join(self.modelpath, 'additional_files.txt')
         additional_files = {}
         if os.path.isfile(additional_files_file):
-            with open(additional_files_file) as fid:
+            with open(additional_files_file, encoding='utf-8') as fid:
                 additional_files = json.load(fid)
         return additional_files
 
@@ -124,7 +124,7 @@ class Simulation(object):
         additional_files = self.additional_files()
         additional_files['input'] = additional_files.get('input', []) + [file]
         additional_files_file = os.path.join(self.modelpath, 'additional_files.txt')
-        with open(additional_files_file, 'w') as fid:
+        with open(additional_files_file, 'w', encoding='utf-8') as fid:
                 json.dump(additional_files, fid)
 
     def prepare_simulation(self):
@@ -304,7 +304,7 @@ class SimulationThread(Thread):
         p.set_nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)
         self.process.communicate()
         errorcode = self.process.returncode
-        with open(self.modelpath + self.sim.stdout_filename) as fid:
+        with open(self.modelpath + self.sim.stdout_filename, encoding='utf-8') as fid:
             stdout = fid.read()
         self.res = errorcode, stdout
 
diff --git a/wetb/prepost/Simulations.py b/wetb/prepost/Simulations.py
index 5d5b2a0..5594417 100755
--- a/wetb/prepost/Simulations.py
+++ b/wetb/prepost/Simulations.py
@@ -433,7 +433,7 @@ def run_local(cases, silent=False, check_log=True):
     """
 
     # remember the current working directory
-    cwd = os.getcwd()
+    cwd = str(os.getcwd())
     nr = len(cases)
     if not silent:
         print('')
diff --git a/wetb/prepost/dlcdefs.py b/wetb/prepost/dlcdefs.py
index 21dd670..2d439c1 100644
--- a/wetb/prepost/dlcdefs.py
+++ b/wetb/prepost/dlcdefs.py
@@ -32,7 +32,7 @@ def configure_dirs(verbose=False):
     Automatically configure required directories to launch simulations
     """
 
-    P_RUN = os.getcwd()
+    P_RUN = str(os.getcwd())
     p_run_root = os.sep.join(P_RUN.split(os.sep)[:-2])
     # MODEL SOURCES, exchanche file sources
     P_SOURCE = P_RUN
diff --git a/wetb/utils/cython_compile/cython_compile.py b/wetb/utils/cython_compile/cython_compile.py
index 02213ae..f05fe5d 100644
--- a/wetb/utils/cython_compile/cython_compile.py
+++ b/wetb/utils/cython_compile/cython_compile.py
@@ -42,7 +42,7 @@ def wrap(f, autodeclare, *args, **kwargs):
     # Generate name: "c:\documents\project\mymodule.py" -> mymodule_myfunction
     # When called from ipython notebooks, filename is an object e.g: "<ipython-input-12-e897f9fefc0c>"
     # therefore <,>,- is removed to make it a legal python module name
-    name = os.path.relpath(inspect.getabsfile(f), os.getcwd()).replace(".py", "")
+    name = os.path.relpath(inspect.getabsfile(f), str(os.getcwd())).replace(".py", "")
     name = name.replace("<", "").replace(">", "").replace("-", "")
     name = "%s_%s" % (name, f.__name__)
     if name.startswith(".."):
@@ -70,7 +70,7 @@ def wrap(f, autodeclare, *args, **kwargs):
 
         # Write pyrex code lines to .pyx file
         pyx_filename = name.replace("\\", "/") + ".pyx"
-        with open(pyx_filename, 'w') as fid:
+        with open(pyx_filename, 'w', encoding='utf-8') as fid:
             fid.writelines(pyx_lines)
 
         # compile, import compiled module_name and delete temporary files
@@ -151,7 +151,7 @@ def cython_import(import_module_name, compiler=None):
         fid.close()
 
         # compile, import compiled module and delete temporary files
-        module_relname = os.path.relpath(eval(module_name).__file__, os.getcwd()).replace(os.path.sep, ".")[:-3]
+        module_relname = os.path.relpath(eval(module_name).__file__, str(os.getcwd())).replace(os.path.sep, ".")[:-3]
         return compile_and_cleanup(import_module_name, pyx_filename, module_relname, compiler)
     return eval(module_name)
 
-- 
GitLab