#!/opt/anaconda/bin/python

import atexit
import sys
import string
import ellip_triggers

sys.path.append('/opt/anaconda/bin/')
import cioppy
ciop = cioppy.Cioppy()

trigger = ellip_triggers.Trigger.init()

# define the exit codes
SUCCESS = 0
ERR_RUNTIME = 10

# add a trap to exit gracefully
def clean_exit(exit_code):

    log_level = 'INFO'
    if exit_code != SUCCESS:
        log_level = 'ERROR'

    msg = {SUCCESS: 'Processing successfully concluded',
           ERR_RUNTIME: 'Failed to run process'
          }

    ciop.log(log_level, msg[exit_code])
      
def main():
        
    for entry in sys.stdin:
        
        # Trigger creates a data item for the catalogue entry
        data_item = trigger.create_data_item_from_single_reference(entry)
                
        trigger.pipe(data_item)
        

try:
    main()
except SystemExit as e:
    if e.args[0]:
        clean_exit(e.args[0])
    raise
else:
    atexit.register(clean_exit, 0)
