Package fliesclient :: Package flieslib :: Module docservice
[hide private]
[frames] | no frames]

Source Code for Module fliesclient.flieslib.docservice

  1  #vim:set et sts=4 sw=4:  
  2  #  
  3  # Flies Python Client 
  4  # 
  5  # Copyright (c) 2010 Jian Ni <jni@redhat.com> 
  6  # Copyright (c) 2010 Red Hat, Inc. 
  7  # 
  8  # This library is free software; you can redistribute it and/or 
  9  # modify it under the terms of the GNU Lesser General Public 
 10  # License as published by the Free Software Foundation; either 
 11  # version 2 of the License, or (at your option) any later version. 
 12  # 
 13  # This library is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU Lesser General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU Lesser General Public 
 19  # License along with this program; if not, write to the 
 20  # Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 21  # Boston, MA  02111-1307  USA 
 22   
 23   
 24  __all__ = ( 
 25          "DocumentService", 
 26     ) 
 27   
 28  import os 
 29  import json 
 30  import sys 
 31  from rest.client import RestClient 
 32  from error import * 
 33   
34 -class DocumentService:
35 - def __init__(self, projects):
36 self.projects = projects
37
38 - def get_file_list(self, projectid, iterationid):
39 res, content = self.projects.restclient.request_get('/projects/p/%s/iterations/i/%s/r'%(projectid, iterationid)) 40 41 if res['status'] == '200' or res['status'] == '304': 42 list = json.loads(content) 43 filelist = [file.get('name') for file in list] 44 return filelist
45
46 - def commit_translation(self, projectid, iterationid, resources):
47 """ 48 Push the json object to flies server 49 @param projectid: id of project 50 @param iterationid: id of iteration 51 @param resources: json object of the content that want to commit to flies server 52 @return: True 53 @raise UnAuthorizedException: 54 @raise BadRequestBodyException: 55 @raise SameNameDocumentException: 56 """ 57 if projectid and iterationid: 58 try: 59 self.projects.iterations.get(projectid, iterationid) 60 except NoSuchProjectException as e: 61 print "%s :%s"%(e.expr, e.msg) 62 sys.exit() 63 64 headers = {} 65 headers['X-Auth-User'] = self.projects.username 66 headers['X-Auth-Token'] = self.projects.apikey 67 68 res, content = self.projects.restclient.request_post('/projects/p/%s/iterations/i/%s/r'%(projectid,iterationid), args=resources, headers=headers) 69 70 if res['status'] == '201': 71 return True 72 elif res['status'] == '401': 73 raise UnAuthorizedException('Error 401', 'UnAuthorized Operation') 74 elif res['status'] == '400': 75 raise BadRequestBodyException('Error 400', 'Unable to read request body.') 76 elif res['status'] == '409': 77 raise SameNameDocumentException('Error 409', 'A document with same name already exists.')
78
79 - def retrieve_translation(self, lang, projectid, iterationid, file):
80 """ 81 Get translation content of file from Flies server 82 @param lang: language 83 @param projectid: Id of project 84 @param iterationid: Id of iteration 85 @param file: name of document 86 @return: translation content of document 87 @raise UnAvaliableResourceException: 88 @raise UnAuthorizedException: 89 """ 90 if projectid and iterationid: 91 try: 92 self.projects.iterations.get(projectid, iterationid) 93 except NoSuchProjectException as e: 94 print "%s :%s"%(e.expr, e.msg) 95 96 97 res, content = self.projects.restclient.request_get('/projects/p/%s/iterations/i/%s/r/%s/translations/%s'%(projectid, iterationid, file, lang)) 98 99 if res['status'] == '200' or res['status'] == '304': 100 return content 101 elif res['status'] == '404': 102 raise UnAvaliableResourceException('Error 404', 'The requested resource is not available') 103 elif res['status'] == '401': 104 raise UnAuthorizedException('Error 401', 'UnAuthorized Operation')
105
106 - def update_translation(self, projectid, iterationid):
107 pass
108