def processRequest(request): output = "" logger.debug("request: {}".format(request)) up = urlparse(request["s3uri"], allow_fragments=False) accountid = request["accountId"] jobid = request["jobId"] bucketName = up.netloc objectkey = up.path.lstrip('/') # choose the base path for iterating within the translated files for the specific job basePrefixPath = objectkey + accountid + "-TranslateText-" + jobid + "/" languageCode = request["langCode"] logger.debug("Base Prefix Path:{}".format(basePrefixPath)) # Filter only the translated XML files for processing objs = S3Helper().getFilteredFileNames(bucketName, basePrefixPath, "xml") for obj in objs: try: content = S3Helper().readFromS3(bucketName, obj) logger.debug(content) #Convert the XML file to Dictionary object data_dict = xmltodict.parse(content) #Generate the Json content from the dictionary data_dict = data_dict["all"] flatten_dict = { k: (data_dict[k]["item"] if (isinstance(v, dict) and len(v.keys()) == 1 and "item" in v.keys()) else v) for (k, v) in data_dict.items() } json_data = json.dumps(flatten_dict, ensure_ascii=False).encode('utf-8') logger.debug(json_data) newObjectKey = "output/{}.json".format(FileHelper.getFileName(obj)) #Write the JSON object to the S3 output folder within the bucket S3Helper().writeToS3(json_data, bucketName, newObjectKey) output = "Output Object: {}/{}".format(bucketName, newObjectKey) logger.debug(output) except ValueError: logger.error("Error occured loading the json file:{}".format(obj)) except ClientError as e: logger.error("An error occured with S3 bucket operations: %s" % e) except: e = sys.exc_info()[0] logger.error("Error occured processing the xmlfile: %s" % e) objs = S3Helper().getFilteredFileNames(bucketName, "xmlin/", "xml") if (request["delete_xmls"] and request["delete_xmls"] == "true"): for obj in objs: try: logger.debug("Deleting temp xml files {}".format(obj)) S3Helper().deleteObject(bucketName, obj) except ClientError as e: logger.error("An error occured with S3 bucket operations: %s" % e) except: e = sys.exc_info()[0] logger.error("Error occured processing the xmlfile: %s" % e)
def processRequest(request): output = "" logger.info("request: {}".format(request)) bucketName = request["bucketName"] sourceLanguageCode = request["sourceLanguage"] targetLanguageCode = request["targetLanguage"] access_role = request["access_role"] triggerFile = request["trigger_file"] try: # Filter only the JSON files for processing objs = S3Helper().getFilteredFileNames(bucketName, "input/", "json") for obj in objs: try: content = S3Helper().readFromS3(bucketName, obj) logger.debug(content) jsonDocument = json.loads(content) print(jsonDocument) # Convert the JSON document into XML outputXML = json2xml.Json2xml(jsonDocument, attr_type=False).to_xml() logger.debug(outputXML) newObjectKey = "xmlin/{}.xml".format( FileHelper.getFileName(obj)) # Store the XML in the S3 location for Translation S3Helper().writeToS3(str(outputXML), bucketName, newObjectKey) output = "Output Object: {}/{}".format(bucketName, newObjectKey) logger.debug(output) # Rename the JSON files to prevent reprocessing S3Helper().renameObject(bucketName, obj, "{}.processed".format(obj)) except ValueError: logger.error( "Error occured loading the json file:{}".format(obj)) except ClientError as e: logger.error("An error occured with S3 Bucket Operation: %s" % e) # Start the translation batch job using Amazon Translate startTranslationJob(bucketName, sourceLanguageCode, targetLanguageCode, access_role) S3Helper().deleteObject(bucketName, "input/{}".format(triggerFile)) except ClientError as e: logger.error("An error occured with S3 Bucket Operation: %s" % e)