#!/usr/bin/env python

# GRAPHICAL interface for setup of QMX calculations
#
# Torsten Kerber, ENS LYON: 2011, 07, 11
#
# This work is supported by Award No. UK-C0017, made by King Abdullah
# University of Science and Technology (KAUST)

import StringIO

def writeData(stream, definitions):
    stream.write("#!/usr/bin/env python\n")
    keys=['import']

    arrImports=[]
    for definition in definitions:
        strClass=definition.getValue('class')
        strImport=definition.getValue('import')
        
        if (strClass is not '' and strImport is not ''):
            s='from '+strImport+' import '+strClass+'\n'
            if not s in arrImports:
                stream.write(s)
                arrImports.append(s)

    stream.write("\n")
    for name in ['high-level', 'low-level', 'hybrid']:
        for definition in definitions:
            if definition.system.lower() != name:
                continue
            strSystem=definition.system.lower().replace("-", "_")
            strClass=definition.getValue('class')
            strOptions=definition.getValue('class.options')

            if (strClass is not ''):
                stream.write(strSystem+'='+strClass+'('+strOptions+')\n')

    stream.write("\n")
        
    extraOptions = ''
    for name in ['cluster', 'system', 'embed']:
        for definition in definitions:
            if definition.system.lower() != name:
                continue
            strSystem=definition.system.lower()
            strClass=definition.getValue('class')
            strOptions=definition.getValue('class.options')

            if (strClass is not ''):
                if name is 'embed':
                    if strOptions is not '':
                        strOptions += ', '
                    strOptions += extraOptions
                stream.write(strSystem+'='+strClass+"("+strOptions+")\n")
                
            if name is 'cluster' and definition.name is 'CLUSTER':
                stream.write(strSystem+'=['+strOptions+']'+'\n')
                extraOptions='cluster_pos=False'
                
            if name is 'embed':
                strOptions=definition.getValue('set_calculator')
                stream.write(strSystem+'.set_calculator('+strOptions+')\n')

    stream.write("\n")
    for definition in definitions:
        if (definition.system.lower() == 'job'):
            strSystem=definition.system.lower()
            strClass=definition.getValue('class')
            strOptions=definition.getValue('class.options')
            if (strOptions is not ''):
                strOptions = ', ' + strOptions
            
            if (strClass is not ''):
                stream.write(strSystem+'='+strClass+'(embed'+strOptions+')\n')

            strSystem=definition.system.lower()
            strMethod=definition.getValue('method')
            strOptions=definition.getValue('method.options')
            stream.write(strSystem+'.'+strMethod+'('+strOptions+')\n')
