import sys
sys.path.append("wrappers")
from modelverse import *
import os
import json

import time
import traceback

def file_service(port):
    filename = service_get(port)
    content = service_get(port)
    try:
        with open(filename, 'w') as f:
            f.write(content)
        service_set(port, "Success")
    except:
        service_set(port, "Failure: " + traceback.format_exc())
        raise

try:
    init(sys.argv[1])
    login("file", "file")

    service_register("files", file_service)

    try:
        if sys.version_info[0] < 3:
            while raw_input() != "STOP":
                pass
        else:
            while input() != "STOP":
                pass
    except EOFError:
        import time
        while 1:
            time.sleep(1.0)
    finally:
        service_stop()
except KeyboardInterrupt:
    pass
