Commits

Anonymous committed e73245a

Attrs retrieving methods added

  • Participants
  • Parent commits 252d17a

Comments (0)

Files changed (2)

File src/pytaus.py

         super(Exception, self).__init__(message)
         self.data = data
 
-class TausLang(object):
-    
+class TausObject(object):
+
     def __init__(self, id, name):
         self.id = id
         self.name = name
         
     def __repr__(self):
         return self.name
-        
+
+class TausLang(TausObject):
+    
+    def __init__(self, id, name):
+        TausObject.__init__(self, id, name)
+
+class TausAttr(TausObject):
+    
+    def __init__(self, id, name):
+        TausObject.__init__(self, id, name)
+    
 class Taus:
     URL = "http://www.tausdata.org/api/"
     POST = 1
     def authenticate(self, login, password):
         self.auth_key = None
         self.conn.setopt(pycurl.USERPWD, "%s:%s" % (login, password))
-        self._perform_action(Taus.POST, 'auth_key', {'action': 'login'}, self._on_receive_authentication)
+        self._perform_action(Taus.POST, 'auth_key', {'action': 'login'}, self._on_receive_data)
         data = self._parse_result(self._temp_data, allowed_codes=[201])
         self.auth_key = data['auth_key']['id']
 
-    def _on_receive_authentication(self, data):
+    def _on_receive_data(self, data):
         self._temp_data += data  
         
     def set_lang(self, source_lang, target_lang):
             'source_lang': self.source_lang,
             'target_lang': self.target_lang}
 
-        self._perform_action(Taus.GET, 'segment', params, self._on_receive_segment)
+        self._perform_action(Taus.GET, 'segment', params, self._on_receive_data)
 
         return self._parse_result(self._temp_data, allowed_codes = [200])
     
     def langs(self, language=""):
         params = {
                   'action' : 'lang',
-                  'language' : language
                   }
-        self._perform_action(Taus.GET, 'lang', params, self._on_receive_langs)
+        
+        if language:
+            params['language'] = language
+            
+        self._perform_action(Taus.GET, 'lang', params, self._on_receive_data)
         
         return [ TausLang(item['id'], item['name']) for item in self._parse_result(self._temp_data)['lang'] ]
+    
+    def get_lang(self, lang_id):
+        action = str('lang/%(lang_id)s' % locals())
+        self._perform_action(Taus.GET, action, {}, self._on_receive_data)
+        item = self._parse_result(self._temp_data)['lang']
+        
+        return TausLang(item['id'], item['name'])
 
-    def _on_receive_langs(self, data):
-        self._temp_data += data
+    def attr_names(self):
+        self._perform_action(Taus.GET, 'attr', {}, self._on_receive_data)
+        result = self._parse_result(self._temp_data)
 
-    def _on_receive_segment(self, data):
-        self._temp_data += data        
+        return result['attr']
+    
+    def attr(self, attr_name, for_param=None):
+        action = str('attr/%(attr_name)s' % locals())
+        
+        params = {}        
+        if for_param:
+            params['for'] = for_param
 
+        self._perform_action(Taus.GET, action, params, self._on_receive_data)
+        items = self._parse_result(self._temp_data)[attr_name]
+        
+        return [ TausAttr(item['id'], item['name']) for item in items ]
+        
     def _parse_result(self, data, allowed_codes = [200]):
         decoded = self.json_decoder.decode(data)
         if allowed_codes.count(decoded['status']) == 0:

File tests/taustest.py

 import pytaus
 
 class TausTestCase(unittest.TestCase):
-    LOGIN = 'login@domain'
-    PASSWORD = 'password'
+    LOGIN = 'grzgrzlac@gmail.com'#'login@domain'
+    PASSWORD = 'malkav!an'
 
     def setUp(self):
         self.tested = pytaus.Taus()
         tested = self.tested.langs()
         self.assertTrue(tested, 'Langs is null')
         self.assertTrue(len(tested) > 0, 'Langs length is 0')
+    
+    def test_langs2(self):
+        self.tested.authenticate(TausTestCase.LOGIN, TausTestCase.PASSWORD)
+        tested = self.tested.langs('en')
+        self.assertTrue(tested, 'Langs is null')
+        self.assertTrue(len(tested) > 0, 'Langs length is 0')
+    
+    def test_get_lang(self):
+        self.tested.authenticate(TausTestCase.LOGIN, TausTestCase.PASSWORD)
+        tested = self.tested.get_lang('pl-pl')
+        self.assertTrue(tested, 'Lang is null')
+        self.assertTrue(tested.id and tested.name, 'Properties of retrieved lang are empty')
+        
+    def test_attr_names(self):
+        self.tested.authenticate(TausTestCase.LOGIN, TausTestCase.PASSWORD)
+        tested = self.tested.attr_names()
+        self.assertTrue(tested, 'Attr names is null')
+        self.assertTrue(len(tested) > 0, 'Attr names length is 0')
+        
+    def test_attr(self):
+        self.tested.authenticate(TausTestCase.LOGIN, TausTestCase.PASSWORD)
+        
+        attr_value = self.tested.attr_names()[0]
+        tested = self.tested.attr(attr_value)
+        self.assertTrue(tested, 'Attr of %(attr_value)s is null' % locals())
+        self.assertTrue(len(tested) > 0, 'Attr of %(attr_value)s  length is 0' % locals())
+
+    def test_attr2(self):
+        self.tested.authenticate(TausTestCase.LOGIN, TausTestCase.PASSWORD)
+        
+        attr_value = self.tested.attr_names()[0]
+        tested = self.tested.attr(attr_value, for_param='upload')
+        self.assertTrue(tested, 'Attr of %(attr_value)s is null' % locals())
+        self.assertTrue(len(tested) > 0, 'Attr of %(attr_value)s  length is 0' % locals())        
 
 class TausExceptionTestCase(unittest.TestCase):