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 (optional) 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 slug @return None @raises `TransifexAPIException` @raises `IOError` """ 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) ) self.new_resource_from_content(project_slug, content, resource_slug, resource_name)
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)
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)
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)
def _get_proj_slug(self): """ Get the Transifex project slug """ if self._noprojprefix: proj_name = self._reponame else: proj_name = "%s%s" % (_Const.TRANSIFEX_PROJ_PREFIX, self._reponame) project_slug = slugify(proj_name) if not self._transifex.project_exists(project_slug): raise TransUpdateError("ERROR: project does not exist: '%s'" % project_slug) return project_slug
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)
def _download_from_transifex(self, download_path, file_list, tx_res_list, tx_lang_list): """ Download translated resource files and return completion stats""" stats_all = {} project_slug = self._get_proj_slug() for i, file in enumerate(file_list): lang = tx_lang_list[i] resource_slug = slugify(tx_res_list[i]) create_path(os.path.dirname(file)) print "Downloading %s" % file self._transifex.get_translation(project_slug, resource_slug, lang, file) stats = self._transifex.get_statistics(project_slug, resource_slug, lang) stats_all[file[len(download_path) + 1:]] = stats return stats_all