#!/usr/bin/env python

# Mike Zelazny
# Get a list of all Klystrons

import subprocess
import string

def KlystronList():
    args = [ 'aidalist', 'KLYS%' ]
    process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    KLYS = process.communicate()
    args = [ 'aidalist', 'SBST%' ]
    process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    SBST = process.communicate()
    result = []
    nKLYS = 0
    prevLOCA = ''
    for klys in string.split(KLYS[0]):
        nColons = string.count(klys,':')
        if 2 == nColons:
            nKLYS += 1
            if nKLYS > 1: # remove KLYS:ACTIVE:MASK from list
                LOCA = klys[6:9]
                if prevLOCA != LOCA:
                    for sbst in string.split(SBST[0]):
                        nColons = string.count(sbst,':')
                        if 2 == nColons:
                            sLOCA = sbst[6:9]
                            if sLOCA == LOCA:
                                result.append(sbst)
                result.append(klys)
                prevLOCA = LOCA
    return result
