Browse Source

Improvement and fixes never stop

Piotr Czajkowski 7 years ago
parent
commit
c898ae473b
2 changed files with 50 additions and 29 deletions
  1. 26 26
      memoQProject.py
  2. 24 3
      memoQProjectTest.py

+ 26 - 26
memoQProject.py

@@ -265,10 +265,12 @@ class MemoQProject(object):
 
         return options
 
-    def run_statistics(self):
-        """Returns statistics for all documents and language combinations of active project,
-         none on failure."""
-        options = self.statistics_options()
+    def run_statistics(self, options=None):
+        """Returns statistics (memoQ CSV) for all documents and language
+        combinations of active project using predefined or provided options,
+         returns none on failure."""
+        if options == None:
+            options = self.statistics_options()
 
         statistics_format = self.client.factory.create(
             '{http://schemas.datacontract.org/2004/07/MemoQServices}StatisticsResultFormat')
@@ -286,28 +288,26 @@ class MemoQProject(object):
         except Exception as e:
             print(str(e))
 
-    def save_statistics(self, path):
-        """Saves statictis to given path (one file per language combination).
-        Returns path to statistics file(s)."""
-        statistics = self.run_statistics()
-        if statistics != None:
-            for stat in statistics.ResultsForTargetLangs.StatisticsResultForLang:
-                output_file = '{}_{}.csv'.format(
-                    stat.TargetLangCode, self.project.name)
-                with open(output_file, 'wb') as target:
-                    target.write(b64decode(stat.ResultData))
-                    filename = '{}_{}.csv'.format(
-                        stat.TargetLangCode, self.project.name)
-                output_file = os.path.join(path, filename)
-                with open(output_file, 'wb') as target:
-                    # Statistics are base64 and utf-16 encoded, so we need to
-                    # decode first
-                    stat_data = b64decode(stat.ResultData).decode("utf-16")
-                    # Changing delimiter to ,
-                    stat_data = stat_data.replace(";", ",")
-                    target.write(stat_data.encode("utf-8"))
-                return output_file
+    def save_statistics(self, path, statistics=None, options=None):
+        """Saves provided statistics (generates them using provided options
+        if none) to given path (one file per language combination).
+        Prints path to statistics file(s)."""
+        if statistics == None:
+            statistics = self.run_statistics(options=options)
+
+        for stat in statistics.ResultsForTargetLangs.StatisticsResultForLang:
+            filename = '{}_{}.csv'.format(
+                stat.TargetLangCode, self.project.name)
+            output_file = os.path.join(path, filename)
+            with open(output_file, 'wb') as target:
+                # Statistics are base64 and utf-16 encoded, so we need to
+                # decode first
+                stat_data = b64decode(stat.ResultData).decode("utf-16")
+                # Changing delimiter to ,
+                stat_data = stat_data.replace(";", ",")
+                target.write(stat_data.encode("utf-8"))
+            print(output_file)
 
     def delete(self):
-        """Deletes active project permamently. WARNING! Not possible to recover!"""
+        """Deletes active project permanently. WARNING! Not possible to recover!"""
         self.client.service.DeleteProject(self.project.get_project_guid())

+ 24 - 3
memoQProjectTest.py

@@ -281,6 +281,12 @@ class MemoQProjectTest(unittest.TestCase):
         statistics = test.run_statistics()
         self.assertNotEqual(statistics, None, "Statistics shouldn't be none!")
 
+        # Testing override
+        options = test.statistics_options()
+        options.IncludeLockedRows = True
+        statistics = test.run_statistics(options=options)
+        self.assertNotEqual(statistics, None, "Statistics shouldn't be none!")
+
         test.delete()
 
     def test_save_statistics(self):
@@ -296,10 +302,25 @@ class MemoQProjectTest(unittest.TestCase):
         result = test.import_document(self.config["test_file_path"])
         self.assertTrue(result, "Result should be true!")
 
-        file_path = test.save_statistics(".")
-        self.assertTrue(os.path.isfile(file_path), "File should exist!")
+        test.save_statistics(".", statistics=test.run_statistics())
+        csv_files = [x for x in os.listdir(
+            ".") if os.path.isfile(x) and ".csv" in x]
+        self.assertTrue(len(csv_files), "File should exist!")
+
+        for csv in csv_files:
+            os.remove(csv)
+
+        # Testing override
+        options = test.statistics_options()
+        options.IncludeLockedRows = True
+        test.save_statistics(".", options=options)
+        csv_files = [x for x in os.listdir(
+            ".") if os.path.isfile(x) and ".csv" in x]
+        self.assertTrue(len(csv_files), "File should exist!")
+
+        for csv in csv_files:
+            os.remove(csv)
 
-        os.remove(file_path)
         test.delete()
 
     def test_delete(self):