#!/usr/bin/env jython

# A test script for testing new jython code snippets

# import time
import sys
import os
import math
# import string

import stilts

# testCode class
class testCode(object):

    # Constructor
    def __init__(self, argv):
        super(testCode, self).__init__(argv)

    def connectMysql(self, argv):

        if len(sys.argv) != 6:
            print "USAGE: ippjython testCode.py (dbHost) (dbUser) (dbPass) (dbName) (filename)"
            os._exit(2)

        # open config and grab database parameters
        self.dbHost = sys.argv[1]
        self.dbUser = sys.argv[2]
        self.dbPass = sys.argv[3]
        self.dbName = sys.argv[4]

        self.filename = sys.argv[5]

        # url for mysql / stilts
        self.url = "jdbc:mysql://"+self.dbHost+"/"+self.dbName+"?autoReconnect=true&user="+self.dbUser+"&password="+self.dbPass

        ## no need to set up the connection for this test
        ## try:
        ##     self.con = DriverManager.getConnection(self.url)
        ## except:
        ##     raise

    '''
    Accepts a regular expression filter so not all tables need to be imported
    '''
    def importIppTables(self):

        if len(sys.argv) != 2:
            print "USAGE: ippjython testCode.py (filename)"
            os._exit(2)

        self.filename = sys.argv[1]

        try:
            tables = stilts.treads(self.filename)
        except:
            print "STILTS could not import from " + filename
            os._exit(3)

        for table in tables:

            print "import table " + table.name

            # need to generate an index on the IPP_IDET column
            # table = stilts.tpipe(table, cmd='addcol table_index $0')
            try:
                table = stilts.tpipe(table, cmd='explodeall')
            except Exception,e:
                # print the error, wait a few secs then try again
                print str(e)
                return False

            try:
                myColumns = table.columns()
            except Exception,e:
                # print the error, wait a few secs then try again
                print str(e)
                return False

            print "got myColumns"

            try:
                for column in myColumns:
                    print "got column " + column.name
            except Exception,e:
                # print the error, wait a few secs then try again
                print str(e)
                return False

            print "read table " + table.name

            # drop any previous tables before import
            # self.scratchDb.dropTable(table.name)

            # limit to specific columns
            # table = stilts.tpipe(table, cmd='keepcols "' + columns + '"')

            # IPP FITS files are littered with infinities, so remove these
            ## print "Removing Infinity values from all columns"
            ## table = stilts.tpipe(table, cmd='replaceval -Infinity null *')
            ## table = stilts.tpipe(table, cmd='replaceval Infinity null *')
            ## 
            ## try:
            ##     table.write(self.url + '#' + table.name)
            ## except:
            ##     print "Problem writing table '" + table.name + "' to the database"
            ##     os._exit(4)

        return True

    def run(self, argv):

        print "starting test"

        myString = "this.is.a.test"
        print "my string: " + myString

        words = myString.split('.')
        print "split my string"

        for word in words:
            print "word: " + word

        print "words[1]: " + words[1]

        myList = ["word1", "word2", "word3"]
        if "word1" in myList:
            print "found word1"

        if 'word1' in myList:
            print "alt word1"
        
        self.importIppTables()

        # self.connectMysql(argv)
        # self.importIppTables()

        ## for x in range(0, 8):
        ##     for y in range(0, 8):
        ## 
        ##         if not ((x == 0) and (y == 1)):
        ##             print "case 1"
        ##             print "x : " + str(x)
        ##             print "y : " + str(y)
        ##         else:
        ##             print "case 2"
        ##             print "x : " + str(x)
        ##             print "y : " + str(y)

'''
Start of program.
'''
try:
    testcode = testCode(sys.argv)
    testcode.run(sys.argv)
except: pass

        # XXX a test of fits header reading:
        ## XX myFits = Fits(self.logger, self.config, sys.argv[2])
        ## XX 
        ## XX for ix in range(7, -1, -1):
        ## XX     for iy in range(7, -1, -1):
        ## XX         if (ix == 0) and (iy == 0): continue
        ## XX         if (ix == 7) and (iy == 0): continue
        ## XX         if (ix == 0) and (iy == 7): continue
        ## XX         if (ix == 7) and (iy == 7): continue
        ## XX         extname = "XY%d%d.hdr" % (ix, iy)
        ## XX 
        ## XX         header = myFits.findAndReadHeader(extname, False)
        ## XX         print "read " + extname
        ## XX 
        ## XX os._exit(3)

        ## the above works in loader.init()
