#!/usr/bin/python3

"""
@file
@ingroup ngcdcsSeqPython
@copyright
  SPDX-FileCopyrightText: 2026 European Southern Observatory (ESO)
  SPDX-License-Identifier: LGPL-3.0-only

"""

import os
import sys
import math

svar = {}
time_r = {}
time_p = {}
seq_idx = "1"
seq_dbg = 0

##
# Send debug message
#
def DEBUG(msg):
    global seq_dbg
    if (seq_dbg):
        sys.stdout.write("* "+msg+"\n")
        sys.stdout.flush()
    
##
# Initialize: get parameters, subroutine execution times and
#             pattern execution times
#
def Init():
    global seq_idx
    global seq_dbg
    global svar
    global time_r
    global time_p

    if (len(sys.argv) > 1):
        seq_idx = sys.argv[1]
        
    if (len(sys.argv) > 2):
        seq_dbg = int(sys.argv[2])

    
    #
    # We are ready
    #
    sys.stdout.write("!\n")
    sys.stdout.flush()

    #
    # Read parameters
    #
    s = ""
    while (s != "!" and s != "*"):
        s = sys.stdin.readline().strip()
        if (s == "*"):
            return 0
        if (s[0] == "#"):
            continue
        if (s != "!"):
            l = len(s)
            n = s.find(' ')
            if (n < 0):
                sys.stderr.write("parameter syntax error\n")
                return 1
            name = s[0:n]
            value = s[n+1:]
            svar[name] = value

    #
    # Read subroutine execution times
    #
    s = ""
    while (s != "!" and s != "*"):
        s = sys.stdin.readline().strip()
        if (s == "*"):
            return 0
        if (s[0] == "#"):
            continue
        if (s != "!"):
            l = len(s)
            n = s.find(' ')
            if (n < 0):
                sys.stderr.write("parameter syntax error\n")
                return 1
            name = s[0:n]
            value = float(s[n+1:])
            time_r[name] = value

    #
    # Read pattern execution times
    #
    s = ""
    while (s != "!" and s != "*"):
        s = sys.stdin.readline().strip()
        if (s == "*"):
            return 0
        if (s[0] == "#"):
            continue
        if (s != "!"):
            l = len(s)
            n = s.find(' ')
            if (n < 0):
                sys.stderr.write("parameter syntax error\n")
                return 1
            name = s[0:n]
            value = float(s[n+1:])
            time_p[name] = value

    return 0

##
# Receive and execute application script
#
def Script():
    global seq_idx
    global svar
    global time_r
    global time_p
    global thisScript

    #
    # Receive application script
    #
    s = ""
    thisScript = ""
    while (s.strip() != "!"):
        s = sys.stdin.readline()
        if (s.strip() != "!"):
            thisScript = thisScript + s

    #
    # Execute the script
    #
    try:
        #eval(compile(thisScript, '<string>', 'exec'))
        exec(thisScript)
    except:
        sys.stderr.write(str(sys.exc_info()[1])+"\n")
        return 1
        
    return 0

#
# Initialize
#
status = Init()

if (status == 0):
    #
    # Run script
    #
    status = Script()    

if (status == 0):
    #
    # Return updated parameters to the calling application
    #
    for x in svar.items():
        n = str(x[0])
        v = str(x[1])
        sys.stdout.write(n+" "+v+"\n")
    sys.stdout.write("!\n")
    sys.stdout.flush()

#
# Exit and return execution status
#
sys.exit(status)

#
# ___oOo___








