def _create(self, project_id: int, target_langs, files: dict): result = self._post('job/create', { 'project': project_id, 'targetLang': target_langs, }, files) # unsupported file count is 0 mean success. unsupported_files = result.get('unsupportedFiles', []) if len(unsupported_files) == 0: return [models.JobPart(job_parts) for job_parts in result['jobParts']] _, value = files.popitem() if isinstance(value, tuple): # If value is tuple type, this function called from createFromText. # We need to create temporary file for to raise exception. file_name, text = value file_path = os.path.join('/', 'tmp', file_name) with open(file_path, 'w+') as f: f.write(text) else: file_path = value.name raise exceptions.MemsourceUnsupportedFileException( unsupported_files, file_path, self.last_url, self.last_params )
def createJob( self, project_id: int, file_path: str, target_langs: (str, list), *, callback_url=None, **kwargs: dict ) -> Tuple[models.AsynchronousResponse, List[models.JobPart]]: """Create new Job on Memsource asynchronously. :param project_id: Project ID of target project. :param file_path: Absolute path of translation target file. :param target_langs: Translation target languages. :param callback_url: Memsource will hit this url when finished to create the job. :param kwargs: See Memsource official document http://wiki.memsource.com/wiki/Job_Asynchronous_API_v2 :return: models.AsynchronousResponse and list of models.JobPart """ with open(file_path, 'rb') as f: files = { 'file': (os.path.basename(file_path), f), } result = self._post( 'job/create', dict( kwargs, **{ 'project': project_id, 'targetLang': target_langs, 'callbackUrl': callback_url, }), files) # unsupported file count is 0 mean success. unsupported_files = result.get('unsupportedFiles', []) if len(unsupported_files) == 0: return (models.AsynchronousRequest(result['asyncRequest']), [ models.JobPart(job_parts) for job_parts in result['jobParts'] ]) raise exceptions.MemsourceUnsupportedFileException( unsupported_files, file_path, self.last_url, self.last_params)
def _create( self, project_id: int, target_langs: List[str], files: dict) -> List[models.JobPart]: """Common process of creating job. If returning JSON has `unsupportedFiles`, this method raise MemsourceUnsupportedFileException :param project_id: New job will be in this project. :param file_path: Source file of job. :param target_langs: List of translation target languages. :return: List of models.JobPart """ result = self._post('job/create', { 'project': project_id, 'targetLang': target_langs, }, files) # unsupported file count is 0 mean success. unsupported_files = result.get('unsupportedFiles', []) if len(unsupported_files) == 0: return [models.JobPart(job_parts) for job_parts in result['jobParts']] _, value = files.popitem() if isinstance(value, tuple): # If value is tuple type, this function called from createFromText. # We need to create temporary file for to raise exception. file_name, text = value file_path = os.path.join('/', 'tmp', file_name) with open(file_path, 'w+') as f: f.write(text) else: file_path = value.name raise exceptions.MemsourceUnsupportedFileException( unsupported_files, file_path, self.last_url, self.last_params )