Example #1
0
    def update_source_translation(self, project_slug, resource_slug,
                                  path_to_pofile):
        """
        Update the source translation for a give resource

        @param project_slug
            the project slug
        @param resource_slug
            the resource slug
        @param path_to_pofile
            the path to the pofile which will be uploaded

        @return dictionary with info
            Info may include keys
                strings_added
                strings_updated
                redirect

        @raises `TransifexAPIException`
        @raises `IOError`
        """
        url = '%s/project/%s/resource/%s/content/' % (
            self._base_api_url, project_slug, resource_slug
        )
        content = open(path_to_pofile, 'r').read()
        headers = {'content-type': 'application/json'}
        data = {'content': content}
        response = requests.put(
             url, data=json.dumps(data), auth=self._auth, headers=headers,
        )

        if response.status_code != requests.codes['OK']:
            raise TransifexAPIException(response)
        else:
            return json.loads(codecs.decode(response.content, 'utf-8'))
Example #2
0
    def list_languages(self, project_slug, resource_slug):
        """
        List all the languages available for a given resource in a project
        
        @param project_slug
            The project slug
        @param resource_slug
            The resource slug
            
        @returns list
            The language codes which this resource has translations 
           
        @raises `TransifexAPIException`
        """
        url = '%s/project/%s/resource/%s/' % (self._base_api_url, project_slug,
                                              resource_slug)
        response = requests.get(url, auth=self._auth, params={'details': ''})

        if response.status_code != requests.codes['OK']:
            raise TransifexAPIException(response)

        content = json.loads(response.content)
        languages = [
            language['code'] for language in content['available_languages']
        ]
        return languages
Example #3
0
 def get_translation(self, project_slug, resource_slug, language_code,
                     path_to_pofile):
     """
     Returns the requested translation, if it exists. The translation is
     returned as a serialized string, unless the GET parameter file is
     specified.
     
     @param project_slug
         The project slug
     @param resource_slug 
         The resource slug
     @param language_code
         The language_code of the file.
         This should be the *Transifex* language code
     @param path_to_pofile
         The path to the pofile which will be saved
         
     @return None
         
     @raises `TransifexAPIException`
     @raises `IOError`
     """
     url = '%s/project/%s/resource/%s/translation/%s/' % (
         self._base_api_url, project_slug, resource_slug, language_code)
     output_path = path_to_pofile
     query = {'file': ''}
     response = requests.get(url, auth=self._auth, params=query)
     if response.status_code != requests.codes['OK']:
         raise TransifexAPIException(response)
     else:
         handle = open(output_path, 'w')
         for line in response.iter_content():
             handle.write(line)
         handle.close()
Example #4
0
    def put_translation_string(self,
                               project_slug,
                               resource_slug,
                               language_code,
                               entity,
                               translation,
                               reviewed=False,
                               user=None,
                               context=None):

        url = '%s/project/%s/resource/%s/translation/%s/string/%s' % (
            self._base_api_url, project_slug, resource_slug, language_code,
            translation_hash(entity, context))

        data = {"reviewed": reviewed, "user": user, "translation": translation}

        headers = {'content-type': 'application/json'}

        response = requests.put(url,
                                auth=self._auth,
                                data=json.dumps(data),
                                headers=headers)
        if response.status_code != requests.codes['OK']:
            raise TransifexAPIException(response)
        else:
            return True
Example #5
0
    def list_translation_strings(self,
                                 project_slug,
                                 resource_slug,
                                 language_code,
                                 key=None,
                                 context=None):

        url = '%s/project/%s/resource/%s/translation/%s/strings' % (
            self._base_api_url, project_slug, resource_slug, language_code)

        params = {
            "key": key,
            "context": context,
        }

        headers = {'content-type': 'application/json'}

        response = requests.get(url,
                                auth=self._auth,
                                headers=headers,
                                params=params)
        if response.status_code != requests.codes['OK']:
            raise TransifexAPIException(response)
        else:
            return json.loads(response.content)
Example #6
0
    def new_resource(self,
                     project_slug,
                     path_to_pofile,
                     resource_slug=None,
                     resource_name=None):
        """
        Creates a new resource with the specified slug from the given file.
        
        @param project_slug
            the project slug
        @param path_to_pofile
            the path to the pofile which will be uploaded
        @param resource_slug (optional)
            the resource slug, defaults to a sluggified version of the filename
        @param resource_name (optional)
            the resource name, defaults to the resource name
            
        @return None
        
        @raises `TransifexAPIException`
        @raises `IOError`
        """
        url = '%s/project/%s/resources/' % (self._base_api_url, project_slug)
        content = open(path_to_pofile, 'r').read()

        __, filename = os.path.split(path_to_pofile)
        if resource_slug is None:
            resource_slug = slugify(filename)
        else:
            if resource_slug != slugify(resource_slug):
                raise InvalidSlugException('%r is not a valid slug' %
                                           (resource_slug))

        if resource_name is None:
            resource_name = resource_slug

        headers = {'content-type': 'application/json'}
        data = {
            'name': resource_name,
            'slug': resource_slug,
            'content': content,
            'i18n_type': 'PO'
        }
        #        slug
        #        name
        #        accept_translations
        #        source_language
        #        mimetype
        #        content (in case of sending the content as one string)
        #        category

        response = requests.post(
            url,
            data=json.dumps(data),
            auth=self._auth,
            headers=headers,
        )
        if response.status_code != requests.codes['CREATED']:
            raise TransifexAPIException(response)
Example #7
0
    def new_project(self,
                    slug,
                    name=None,
                    source_language_code=None,
                    outsource_project_name=None,
                    private=False,
                    repository_url=None):
        """
        Create a new project on transifex

        @param slug
            the project slug
        @param name (optional)
            the project name, defaults to the project slug
        @param source_language_code (optional)
            the source language code, defaults to 'en-gb'
        @param outsource_project_name (optional)
            the name of the project to outsource translation team management to
        @param private (optional)
            controls if this is created as a closed source or open-source
            project, defaults to `False`
        @param repository_url (optional)
            The url for the repository. This is required if private is set to
            False

        @returns None

        @raises `TransifexAPIException`
           if project was not created properly
        """
        if slug != slugify(slug):
            raise InvalidSlugException('%r is not a valid slug' % (slug))
        if name is None:
            name = slug
        if source_language_code is None:
            source_language_code = 'en-gb'

        url = '%s/projects/' % self._base_api_url
        headers = {'content-type': 'application/json'}
        data = {
            'name': name,
            'slug': slug,
            'source_language_code': source_language_code,
            'description': name,
            'private': private,
            'repository_url': repository_url
        }
        if outsource_project_name is not None:
            data['outsource'] = outsource_project_name

        response = requests.post(
            url,
            data=json.dumps(data),
            auth=self._auth,
            headers=headers,
        )

        if response.status_code != requests.codes['CREATED']:
            raise TransifexAPIException(response)
Example #8
0
    def list_projects(self):
        url = '%s/projects/' % (self._base_api_url)
        response = requests.get(url, auth=self._auth)
        response.raise_for_status()

        if response.status_code != requests.codes['OK']:
            raise TransifexAPIException(response)

        return response.json()
Example #9
0
    def get_translation_string(self, project_slug, resource_slug,
                               language_code, entity, context=None):

        url = '%s/project/%s/resource/%s/translation/%s/string/%s' % (
            self._base_api_url, project_slug, resource_slug,
            language_code, translation_hash(entity, context)
        )

        headers = {'content-type': 'application/json'}

        response = requests.get(url, auth=self._auth, headers=headers)
        if response.status_code != requests.codes['OK']:
            raise TransifexAPIException(response)
        else:
            return json.loads(codecs.decode(response.content, 'utf-8'))
Example #10
0
    def delete_resource(self, project_slug, resource_slug):
        """
        Deletes the given resource
        
        @param project_slug
            the project slug
        @param resource_slug
            the resource slug

        @return None
        
        @raises `TransifexAPIException`
        """
        url = '%s/project/%s/resource/%s/' % (self._base_api_url, project_slug,
                                              resource_slug)
        response = requests.delete(url, auth=self._auth)
        if response.status_code != requests.codes['NO_CONTENT']:
            raise TransifexAPIException(response)
Example #11
0
 def project_exists(self, project_slug):
     """
     Check if there is a project with the given slug registered with 
     Transifex
     
     @param project_slug
         The project slug
         
     @return Boolean
        True is project exists
        False if not
     """
     url = '%s/project/%s/' % (self._base_api_url, project_slug)
     response = requests.get(url, auth=self._auth)
     if response.status_code == requests.codes['OK']:
         return True
     elif response.status_code == requests.codes['NOT_FOUND']:
         return False
     else:
         raise TransifexAPIException(response)
Example #12
0
    def list_resources(self, project_slug):
        """
        List all resources in a project
        
        @param project_slug
            the project slug
            
        @returns list of dictionaries with resources info
            each dictionary may contain
                category
                i18n_type
                source_language_code
                slug
                name
           
        @raises `TransifexAPIException`
        """
        url = '%s/project/%s/resources/' % (self._base_api_url, project_slug)
        response = requests.get(url, auth=self._auth)

        if response.status_code != requests.codes['OK']:
            raise TransifexAPIException(response)

        return json.loads(response.content)
Example #13
0
    def new_project(self,
                    slug,
                    name=None,
                    source_language_code=None,
                    outsource_project_name=None):
        """
        Create a new project on transifex
        
        @param slug
            the project slug
        @param name (optional)
            the project name, defaults to the project slug
        @param source_language_code (optional)
            the source language code, defaults to 'en-gb'
        @param outsource_project_name (optional)
            the name of the project to outsource translation team management to
            
        @returns None
           
        @raises `TransifexAPIException`
           if project was not created properly
        """

        if slug != slugify(slug):
            raise InvalidSlugException('%r is not a valid slug' % (slug))
        if name is None:
            name = slug
        if source_language_code is None:
            source_language_code = 'en-gb'

        url = '%s/projects/' % self._base_api_url
        headers = {'content-type': 'application/json'}
        data = {
            'name': name,
            'slug': slug,
            'source_language_code': source_language_code,
            'description': name
        }
        if outsource_project_name is not None:
            data['outsource'] = outsource_project_name
#        description
#        long_description
#        private
#        homepage
#        feed
#        anyone_submit
#        hidden
#        bug_tracker
#        trans_instructions
#        tags
#        maintainers

        response = requests.post(
            url,
            data=json.dumps(data),
            auth=self._auth,
            headers=headers,
        )

        if response.status_code != requests.codes['CREATED']:
            raise TransifexAPIException(response)