in translate_json/translate_job_event_handler.py [0:0]
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)