def import_test(): #example for import and last_modified commands uploadDataImport = UploadData(FILE_PATH, FILE_NAME_IMPORT, FILE_TYPE_IMPORT) uploadDataImport.addDirective(SmartlingDirective("placeholder_format_custom", "\[.+?\]")) useSandbox = False example = SmartlingApiExample(useSandbox, uploadDataImport, "it-IT", FILE_NAME_RENAMED) example.test_import(FILE_NAME_TO_IMPORT)
def _importTranslationFile(self, path, fileName, uriPath, locale, directives=None, overwrite=False): smartlingLocale = self._getSmartlingLocale(locale) # Test if English source exists try: self.api.getStatus(uriPath + fileName, smartlingLocale) except IOError: logging.error("Failed to import translation as English source has not been uploaded: %s", uriPath + fileName) raise ValueError("No English source: {0}".format(uriPath + fileName)) absFile = os.path.join(path, fileName) if self.args.run: logging.debug("Importing translation (%s): %s", smartlingLocale, absFile) if not path.endswith(os.path.sep): path += os.path.sep uploadData = UploadData(path, fileName, self._getFileType(fileName), uriPath) uploadData.uri = uriPath + fileName if directives: for directive, value in directives: uploadData.addDirective(SmartlingDirective(directive, value)) stats = self.api.importFile(uploadData, smartlingLocale, overwrite) logging.info("Imported translation (%s): %s -> %s (Word count = %s)", smartlingLocale, absFile, uriPath + fileName, stats.wordCount) else: logging.info("Import translation (noop): %s -> %s", absFile, uriPath + fileName)
def ascii_test(): #test simple file uploadDataASCII = UploadData(FILE_PATH, FILE_NAME, FILE_TYPE) uploadDataASCII.addDirective(SmartlingDirective("placeholder_format_custom", "\[.+?\]")) useSandbox = False example = SmartlingApiExample(useSandbox, uploadDataASCII, "it-IT", FILE_NAME_RENAMED) example.test()
def test_addDirective(self): ud = UploadData("path", "name", "type") assert_equal(len(ud.directives), 0) ud.addDirective(SmartlingDirective("name", "value")) assert_equal(len(ud.directives), 1) assert_equal(ud.directives[0].name, "name") ud.addDirective(SmartlingDirective("name2", "value2")) assert_equal(len(ud.directives), 2) assert_equal(ud.directives[1].value, "value2")
def test_commandUpload_Directives(self): ud = UploadData("path", "name", "type") ud.setApproveContent("true") ud.setCallbackUrl("smartling.com") ud.addDirective(SmartlingDirective("placeholder_format_custom", "\[.+?\]")) ud.addDirective(SmartlingDirective("placeholder_format", "IOS")) api = FileApiBase("host", "apiKey", "projectId") api.uploadMultipart = self.mock_uploadMultipart params = api.commandUpload(ud) assert_equal(len(params), 7) assert_equal(params["smartling.placeholder_format_custom"], "\[.+?\]") assert_equal(params["smartling.placeholder_format"], "IOS")
def test_commandUpload_Directives(self): ud = UploadData("path", "name", "type") ud.setApproveContent("true") ud.setCallbackUrl("smartling.com") ud.addDirective( SmartlingDirective("placeholder_format_custom", "\[.+?\]")) ud.addDirective(SmartlingDirective("placeholder_format", "IOS")) api = FileApiBase("host", "apiKey", "projectId") api.uploadMultipart = self.mock_uploadMultipart params = api.commandUpload(ud) assert_equal(len(params), 7) assert_equal(params["smartling.placeholder_format_custom"], "\[.+?\]") assert_equal(params["smartling.placeholder_format"], "IOS")
def upload_source_file_to_smartling(path, file_name, file_type, fields_to_translate, approve, slapi): """ Upload a source file of type file_type to Smartling. """ path = path + '/' # sdk requires TODO check this upload_data = UploadData(path, file_name, file_type, file_name) upload_data.setUri(file_name) if approve: upload_data.setApproveContent('true') else: upload_data.setApproveContent('false') upload_data.addDirective(SmartlingDirective('translate_paths', ','.join(fields_to_translate))) upload_data.addDirective(SmartlingDirective('string_format_paths', 'html:body')) upload_data.addDirective(SmartlingDirective('source_key_paths', 'title')) response, http_response_code = slapi.upload(upload_data) if http_response_code == 200: response_data = response.data logging.debug('Uploaded to Smartling: %s', file_name) logging.debug('Overwritten: %s, String count: %s, Word count: %s', response_data.overWritten, response_data.stringCount, response_data.wordCount) else: raise SmartlingError('Error in Smartling API upload call', http_response_code, response)
def _uploadSourceFile(self, path, fileName, uriPath, directives=None): absFile = os.path.join(path, fileName) logging.debug("Uploading: %s", absFile) if not path.endswith(os.path.sep): path += os.path.sep if self.args.run: uploadData = UploadData(path, fileName, self._getFileType(fileName), uriPath) if directives: for directive, value in directives: uploadData.addDirective(SmartlingDirective(directive, value)) stats = self.api.uploadFile(uploadData) logging.info("Uploaded: %s -> %s (Word count = %s New source = %s)", absFile, uriPath + fileName, stats.wordCount, "No" if stats.overWritten else "Yes") else: logging.info("Upload (noop): %s -> %s", os.path.join(path, fileName), uriPath + fileName)
def upload_file(fapi, file_name, conf): """ Uploads a file to smartling """ if not conf.has_key('file-type'): raise SmarterlingError("%s doesn't have a file-type" % file_name) print("Uploading %s to smartling" % file_name) data = UploadData( os.path.dirname(file_name)+os.sep, os.path.basename(file_name), conf.get('file-type')) data.setUri(file_uri(file_name, conf)) if conf.has_key('approve-content'): data.setApproveContent("true" if conf.get('approve-content', True) else "false") if conf.has_key('callback-url'): data.setCallbackUrl(conf.get('callback-url')) for name, value in conf.get('directives', {}).items(): data.addDirective(SmartlingDirective(name, value)) (response, code) = fapi.upload(data) if code!=200: print(repr(response)) raise SmarterlingError("Error uploading file: %s" % file_name) else: print("Uploaded %s, wordCount: %s, stringCount: %s" % (file_name, response.data.wordCount, response.data.stringCount))
self.printMarker("delete from server goes here") print self.fapi.delete(self.new_name) self.printMarker("doing list again to see if it's deleted") print self.fapi.list() FILE_NAME = "java.properties" FILE_NAME_UTF16= "javaUTF16.properties" FILE_TYPE = "javaProperties" FILE_PATH = "../resources/" FILE_NAME_RENAMED = "java.properties.renamed" CALLBACK_URL = "http://yourdomain.com/callback" #test simple file uploadDataASCII = UploadData(FILE_PATH, FILE_NAME, FILE_TYPE) uploadDataASCII.addDirective(SmartlingDirective("placeholder_format_custom","\[.+?\]")) useSandbox = False example = SmartlingApiExample (useSandbox, uploadDataASCII, "ru-RU", FILE_NAME_RENAMED) example.test() #add charset and approveContent parameters uploadDataUtf16 = UploadData(FILE_PATH, FILE_NAME_UTF16, FILE_TYPE) uploadDataUtf16.setApproveContent("true") uploadDataUtf16.setCallbackUrl(CALLBACK_URL) useSandbox = True example = SmartlingApiExample (useSandbox, uploadDataUtf16, "ru-RU", FILE_NAME_RENAMED) example.test()