#!/usr/bin/python
from NodesDict import *
import doNode
import argparse
import datetime



#This is a template for how to a run a command on the whole hierarchy of nodes from a starting point downwards.
#The doNode() function does the work
#It calls my_node() defined here, as apropriate:


topName=""

#This makes the .xml for setting up an alarm

def main():
    global topName
    parser = argparse.ArgumentParser()
    parser.add_argument("top", help="Top node (eg B_SYS_HV)")
    parser.add_argument("topname", help="Top node name - a layer name in quotes (eg \"High voltage\") or\"\" to skip it")
    parser.add_argument("confname", help="Name of Alarm configuration - almost certainly HallB")
    parser.add_argument("-v", "--verbosity",   help="increase verbosity", nargs='?', const=0, default=0)
    args        = parser.parse_args()
    topName     = args.topname
    configName  = args.confname
    global verbose
    verbose = args.verbosity

    now = datetime.datetime.now()
    print "<!--"
    print "   Alarm config generated by makeAlarmConfig.py on "+now.strftime("%Y-%m-%d %H:%M")
    print "-->"
    print "<config name=\""+configName+"\">"
    
    #This is the function that works recursively through the tree and calls do_unto_element start_node end_node
    doNode.doNode(args.top,do_node=my_node, v=verbose)

    print"</config>"

def my_node(node,depth,mode='elem',result=None):
    #This must have the args defined here, and must have a section for each of the 3 modes:
    
    #mode='init': Do something before porcessing a node
    #mode='elem': Called for each element in the node. Can return a value which will be added to the result[] list.
    #mode='end':  Called at the end to process the result[] list for the node

    shortname=node.split("_")[len(node.split("_"))-1];
    parent=node.replace("_"+shortname,"")

    if mode == 'init':                                        #if start of node
        global topName
        if(depth==0)and (len(topName)>1):
            print '  '*depth+"  <component name=\""+topName+"\">"
        else:
            if(depth>0):
                print '  '*depth+"  <component name=\""+shortname+"\">"
        return

    if mode == 'end':  #ifend of node
        if(depth>0):
            print '  '*depth+"   </component>"
        return

    
    if mode == 'elem':                                        #if element
        print '  '*depth+"   <pv name=\""+node+"\">"
        print '  '*depth+"      <description>High Voltage alarm for "+node+"</description>"
        print '  '*depth+"      <latching>true</latching>"
        print '  '*depth+"      <annunciating>true</annunciating>"
        print '  '*depth+"      <delay>2.0</delay>"
        print '  '*depth+"      <count>0</count>"
        print '  '*depth+"      <guidance>"
        print '  '*depth+"         <title>Guidance</title>"
        print '  '*depth+"         <details>Try to reset the voltage channel using the GUI. If problem persists, contact the expert.</details>"
        print '  '*depth+"      </guidance>"
        print '  '*depth+"      <display>"
        print '  '*depth+"         <title>Open HV GUI</title>"
        print '  '*depth+"         <details>/CLAS12_Share/apps/clasTreeApp/HVMonitor.opi \"P="+parent+",E="+node+"\"</details>"
        print '  '*depth+"      </display>"
        print '  '*depth+"   </pv>"
        
        return
    
if __name__ == "__main__": main()

