forked from nasa/cumulus-message-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
executable file
·49 lines (45 loc) · 1.66 KB
/
__main__.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
#!/usr/bin/env python
# coding=utf-8
import json
import sys
from message_adapter import message_adapter
# use input with both python 2 & 3
try:
import __builtin__
input = getattr(__builtin__, 'raw_input')
except (ImportError, AttributeError):
pass
if __name__ == '__main__':
functionName = sys.argv[1]
allInput = json.loads(input())
if 'schemas' in allInput:
schemas = allInput['schemas']
else:
schemas = None
transformer = message_adapter.message_adapter(schemas)
exitCode = 1
event = allInput['event']
try:
context = allInput.get('context')
if (functionName == 'loadRemoteEvent'):
result = transformer.loadRemoteEvent(event)
elif (functionName == 'loadAndUpdateRemoteEvent'):
result = transformer.loadAndUpdateRemoteEvent(event, context)
elif (functionName == 'loadNestedEvent'):
result = transformer.loadNestedEvent(event, context)
elif (functionName == 'createNextEvent'):
handlerResponse = allInput['handler_response']
if 'message_config' in allInput:
messageConfig = allInput['message_config']
else:
messageConfig = None
result = transformer.createNextEvent(handlerResponse, event, messageConfig)
if (result is not None and len(result) > 0):
sys.stdout.write(json.dumps(result))
sys.stdout.flush()
exitCode = 0
except LookupError as le:
sys.stderr.write("Lookup error: " + str(le))
except:
sys.stderr.write("Unexpected error:"+ str(sys.exc_info()[0])+ ". " + str(sys.exc_info()[1]))
sys.exit(exitCode)