-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommons.py
63 lines (51 loc) · 1.89 KB
/
commons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -----------------------------------------------------------
# Storage file for global variables and configs used across all scripts
#
# Copyright (c) 2021 Morgan Davies, UK
# Released under GNU GPL v3 License
# -----------------------------------------------------------
import os
import sys
import json
from dotenv import load_dotenv
load_dotenv()
def respond(messageType, code, message, content=None):
"""respond() : Print/return informational JSON message and sometimes terminate execution
:param messageType: Type of message (ERROR, SUCCESS, etc)
:param code: Exit code to terminate execution with
:param message: Short message to be sent describing the event
:param content: Optional excess message content (e.g. JSON data) used by the website or other scripts
"""
jsonMessage = json.dumps(
obj={
"TYPE": messageType,
"MESSAGE": message,
"CONTENT": json.dumps(content),
"CODE": code
},
indent=2
)
print(jsonMessage)
# Replace response file and exit
with open(os.getenv("RESPONSE_FILE_PATH"), "w") as logfile:
logfile.write(jsonMessage)
sys.exit(code)
def parseObjectName(fileName):
"""parseObjectName() : Produces a single word identifier for an image
:param fileName: Full path to an S3 or local file
:return: The identifier to be assigned
"""
objectName = fileName
if "/" in fileName:
objectName = fileName.split("/")[-1]
objectName = parseImageObject(objectName)
return objectName
def parseImageObject(objectName):
"""parseImageObject() : Ensures that an object name is an image and appends jpg if it isn't
:param objectName: Objectname to be checked and modified
:return: The new objectName
"""
if "jpg" not in objectName and "png" not in objectName:
return f"{objectName}.jpg"
else:
return objectName