Browse Source

General improvements

Piotr Czajkowski 7 years ago
parent
commit
45ebe131cc
4 changed files with 55 additions and 32 deletions
  1. 6 6
      memoQResource.py
  2. 4 1
      memoQSecurity.py
  3. 38 19
      memoQTM.py
  4. 7 6
      memoQTMTest.py

+ 6 - 6
memoQResource.py

@@ -21,6 +21,12 @@ class MemoQResource(object):
         self.__type = None
         self.__type = None
         self.info = None
         self.info = None
 
 
+    def __repr__(self):
+        if self.info != None:
+            return "{} - {} ({})".format(self.info.Name, self.info.Guid, self.get_type())
+        else:
+            return "No resource!"
+
     def get_type(self):
     def get_type(self):
         """Returns resource type."""
         """Returns resource type."""
         return self.__type
         return self.__type
@@ -36,12 +42,6 @@ class MemoQResource(object):
         if self.valid_type(value):
         if self.valid_type(value):
             self.__type = self.types[value]
             self.__type = self.types[value]
 
 
-    def __repr__(self):
-        if self.info != None:
-            return "{} - {} ({})".format(self.info.Name, self.info.Guid, self.get_type())
-        else:
-            return "No resource!"
-
     def set_active_resource(self, guid=None, resource_type=None):
     def set_active_resource(self, guid=None, resource_type=None):
         """Populates info basing on resource type and guid."""
         """Populates info basing on resource type and guid."""
         if resource_type != None and guid != None:
         if resource_type != None and guid != None:

+ 4 - 1
memoQSecurity.py

@@ -51,11 +51,14 @@ class MemoQSecurity(object):
                 "/memoqservices/security?wsdl"
                 "/memoqservices/security?wsdl"
             self.client = Client(apiURL)
             self.client = Client(apiURL)
 
 
-        self.user = None
+        self.user = User()
         self.users = []
         self.users = []
         self.groups = None
         self.groups = None
         self.subvendors = None
         self.subvendors = None
 
 
+    def __repr__(self):
+        return "{}".format(self.user)
+
     def set_active_user(self, guid):
     def set_active_user(self, guid):
         """Sets user of given guid as active."""
         """Sets user of given guid as active."""
         self.user = User(self.client.service.GetUser(guid))
         self.user = User(self.client.service.GetUser(guid))

+ 38 - 19
memoQTM.py

@@ -4,6 +4,25 @@ import os
 import json
 import json
 
 
 
 
+class TM(object):
+    """Wrapper for TM."""
+
+    def __init__(self, info=None):
+        self.info = info
+
+    def __repr__(self):
+        if self.info != None:
+            return "{} - {}\n{} - {}".format(
+                self.info.Name, self.info.Guid,
+                self.info.SourceLanguageCode, self.info.TargetLanguageCode)
+        else:
+            return "No TM!"
+
+    def get_guid(self):
+        """Returns guid of active TM."""
+        return self.info.Guid
+
+
 class MemoQTM(object):
 class MemoQTM(object):
     """Client for memoQ Translation memory API."""
     """Client for memoQ Translation memory API."""
 
 
@@ -15,12 +34,10 @@ class MemoQTM(object):
             apiURL = self.config["api_base_url"] + "/memoqservices/tm?wsdl"
             apiURL = self.config["api_base_url"] + "/memoqservices/tm?wsdl"
             self.client = Client(apiURL)
             self.client = Client(apiURL)
 
 
-        self.__guid = None
-        self.info = None
+        self.tm = TM()
 
 
-    def get_guid(self):
-        """Returns guid of active TM."""
-        return self.__guid
+    def __repr__(self):
+        return "{}".format(self.tm)
 
 
     def get_tm_details(self, guid):
     def get_tm_details(self, guid):
         """Returns TM Info for TM of given guid or none if no TM or connection problems."""
         """Returns TM Info for TM of given guid or none if no TM or connection problems."""
@@ -34,27 +51,29 @@ class MemoQTM(object):
         """Sets guid and info if TM exists."""
         """Sets guid and info if TM exists."""
         tm_info = self.get_tm_details(guid)
         tm_info = self.get_tm_details(guid)
         if tm_info != None:
         if tm_info != None:
-            self.__guid = guid
-            self.info = tm_info
+            self.tm = TM(tm_info)
 
 
-    def __repr__(self):
-        if self.info != None:
-            return "{} - {}\n{} - {}".format(self.info.Name, self.info.Guid, self.info.SourceLanguageCode, self.info.TargetLanguageCode)
-        else:
-            return "No TM!"
-
-    def all_tms(self):
-        return self.client.service.ListTMs()
+    def get_all_tms(self):
+        """Returns list of all TMs on server."""
+        return [TM(x) for x in self.client.service.ListTMs()[0]]
 
 
     def download_tmx(self, path, guid=None):
     def download_tmx(self, path, guid=None):
-        """Downloads TMX export of active or given TM to given path. Returns path to TMX file or none on any failure."""
+        """Downloads TMX export of active or given TM to given path.
+        Returns path to TMX file or none on any failure."""
         if guid != None:
         if guid != None:
             self.set_active_tm(guid)
             self.set_active_tm(guid)
-        if self.info == None:
+        if self.tm.info == None:
+            return None
+
+        session_id = None
+        try:
+            session_id = self.client.service.BeginChunkedTMXExport(
+                self.tm.get_guid())
+        except Exception as e:
+            print(e)
             return None
             return None
 
 
-        session_id = self.client.service.BeginChunkedTMXExport(self.get_guid())
-        output_filename = os.path.join(path, (self.info.Name + ".tmx"))
+        output_filename = os.path.join(path, (self.tm.info.Name + ".tmx"))
         output = open(output_filename, 'wb')
         output = open(output_filename, 'wb')
         while True:
         while True:
             try:
             try:

+ 7 - 6
memoQTMTest.py

@@ -30,16 +30,17 @@ class MemoQTMTest(unittest.TestCase):
 
 
         test.set_active_tm(self.config["wrong_tm_guid"])
         test.set_active_tm(self.config["wrong_tm_guid"])
         self.assertEqual(
         self.assertEqual(
-            test.info, None, "TM Info for wrong TM guid should be none!")
+            test.tm.info, None, "TM Info for wrong TM guid should be none!")
 
 
         test.set_active_tm(self.config["valid_tm_guid"])
         test.set_active_tm(self.config["valid_tm_guid"])
         self.assertNotEqual(
         self.assertNotEqual(
-            test.info, None, "TM Info for valid TM guid shouldn't be none!")
+            test.tm.info, None, "TM Info for valid TM guid shouldn't be none!")
 
 
-    def test_all_tms(self):
-        """Test for all_tms method."""
+    def test_get_all_tms(self):
+        """Test for get_all_tms method."""
         test = memoQTM.MemoQTM()
         test = memoQTM.MemoQTM()
-        self.assertTrue(len(test.all_tms()), "List of TMs shouldn't be empty!")
+        self.assertTrue(len(test.get_all_tms()),
+                        "List of TMs shouldn't be empty!")
 
 
     def test_download_tmx(self):
     def test_download_tmx(self):
         """Test for download_tmx method."""
         """Test for download_tmx method."""
@@ -59,7 +60,7 @@ class MemoQTMTest(unittest.TestCase):
         test = memoQTM.MemoQTM()
         test = memoQTM.MemoQTM()
         test.set_active_tm(self.config["valid_tm_guid"])
         test.set_active_tm(self.config["valid_tm_guid"])
         self.assertNotEqual(
         self.assertNotEqual(
-            test.info, None, "TM Info for valid TM guid shouldn't be none!")
+            test.tm.info, None, "TM Info for valid TM guid shouldn't be none!")
 
 
         file_path = test.download_tmx(".")
         file_path = test.download_tmx(".")
         self.assertNotEqual(file_path, None,
         self.assertNotEqual(file_path, None,