[chronojump] chronojump-importer: normalizes the paths read from the database.



commit 2462c162bd4f1745ae96f99e87f6769e6128719e
Author: Carles Pina i Estany <carles pina cat>
Date:   Mon Oct 10 19:21:42 2016 +0200

    chronojump-importer: normalizes the paths read from the database.
    
    The issue was that we could be in a Linux machine and the database could
    have been created on a Windows machine. The paths in the Chronojump's
    database depends on the machine that are created. Now it normalizes them
    to the current machine.

 src/chronojump-importer/chronojump_importer.py     |   18 +++++++++++++++++-
 .../chronojump_importer_test.py                    |   15 +++++++++++++++
 2 files changed, 32 insertions(+), 1 deletions(-)
---
diff --git a/src/chronojump-importer/chronojump_importer.py b/src/chronojump-importer/chronojump_importer.py
index 5a4115d..d5cd1e9 100755
--- a/src/chronojump-importer/chronojump_importer.py
+++ b/src/chronojump-importer/chronojump_importer.py
@@ -593,6 +593,22 @@ class ImportSession:
     def _encoder_url(session_id, signal_or_curve):
         return os.path.join("encoder", "data", str(session_id), signal_or_curve)
 
+    @staticmethod
+    def _normalize_path(path):
+        """
+        The path that it is read from the database might use Windows separators but
+        we might be on a Linux system (or OS-X). This function should replace the directory
+        separators to the system's ones.
+
+        It assumes that the "/" and "\" characters are only used to separate directories.
+        """
+        if os.sep == "/":
+            # We are on Linux, OS-X or some other system with "/" separators.
+            # If the path had "\" then replace them to "/".
+            return path.replace("\\", "/")
+        elif os.sep == "\\":
+            return path.replace("/", "\\")
+
     def _import_encoder_files(self, encoder_table):
         if self.source_base_directory is None:
             # We are skipping to copy the Encoding files. This is used in unit tests.
@@ -602,7 +618,7 @@ class ImportSession:
             # Gets information from row
             person_id = row.get("personID")
             original_filename = row.get("filename")
-            original_url = row.get("url")
+            original_url = self._normalize_path(row.get("url"))
             session_id = row.get("sessionID")
             signal_or_curve = row.get("signalOrCurve")
 
diff --git a/src/chronojump-importer/chronojump_importer_test.py 
b/src/chronojump-importer/chronojump_importer_test.py
index a5502b1..92f38ad 100755
--- a/src/chronojump-importer/chronojump_importer_test.py
+++ b/src/chronojump-importer/chronojump_importer_test.py
@@ -111,6 +111,21 @@ class TestImporter(unittest.TestCase):
         new_filename = chronojump_importer.ImportSession._encoder_filename(10, "19-test.txt")
         self.assertEqual("10-test.txt", new_filename)
 
+    def test_normalize_path(self):
+        original_os_sep = os.sep
+
+        # I don't think that unittest.mock can mock a non-call function so
+        # here it changes os.sep and leave it as it was later on.
+        os.sep = "/"
+        self.assertEqual("test/directory", 
chronojump_importer.ImportSession._normalize_path("test\\directory"))
+        self.assertEqual("test/directory", 
chronojump_importer.ImportSession._normalize_path("test/directory"))
+
+        os.sep = "\\"
+        self.assertEqual("test\\directory", 
chronojump_importer.ImportSession._normalize_path("test\\directory"))
+        self.assertEqual("test\\directory", 
chronojump_importer.ImportSession._normalize_path("test/directory"))
+
+        os.sep = original_os_sep
+
     def test_encoder_url(self):
         new_url = chronojump_importer.ImportSession._encoder_url(11, "signal")
         self.assertEqual(os.path.join("encoder", "data", "11", "signal"), new_url)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]