#!/usr/bin/python

import os
import sys
import string
import ConfigParser

#Find the matplotlib data if installed with CompClustShell.
if sys.winver == 'py2exe':
  cfg = ConfigParser.ConfigParser()
  cfg.read('compClust.ini')
  if cfg.has_option('matplotlib', 'data'):
    os.environ['MATPLOTLIBDATA'] = cfg.get('matplotlib', 'data')

from IPython.Shell import IPythonShellEmbed
#from IPython import hooks is used to get py2exe to actually
#include the hooks module when making CompClustTk.exe. Without
#it, the program crashes. Only remove if py2exe no longer has
#this problem.
from IPython import hooks

from compClust.mlx import datasets
from compClust.mlx import views
from compClust.mlx import labelings
from compClust.mlx import wrapper
from compClust.iplot import IPlotTk as IPlot

try:
  from compClust.mlx import pcaGinzu
except ImportError, msg:
  print 'INFO: pcaGinzu failed to load, requires rpy & R'
  print 'INFO: Import Error: %s' % (msg)

def ipythonShell():
    
  msg = []
  msg.append('')
  msg.append('Welcome to the CompClust Interactive Analysis Shell')
  msg.append('')
  msg.append('USEFUL VARIABLES:')
  msg.append('       Datasets module: datasets')
  msg.append('          Views module: views')
  msg.append('      Labelings module: labelings')
  msg.append('     Clustering module: wrapper')
  msg.append('       pcaGinzu module: pcaGinzu')
  msg.append('pcaGinzuScoring module: pcaGinzuScoring')
  msg.append('')
  msg.append('EXAMPLE USE: Log2 transform of a data set')
  msg.append('')
  msg.append('  import math                                #Import math module')
  msg.append('  def log2(n):                               #Define log2(n) function')
  msg.append('     return math.log(n,2)')
  msg.append('')
  msg.append('  log2ds = views.FunctionView(dataSet, log2) #Apply log2 function to each ')
  msg.append('                                             # element in the data set')
  msg.append('')
  msg.append('HELP:')
  msg.append('     ? - Putting a \'?\' after a variable/object/function will provide more')
  msg.append('         information on that item (i.e. views.FunctionView?)')
  msg.append('  help - Typing help will provide you with more information as well') 
  msg.append('')
  msg.append('QUIT: Ctrl+D on Linux & Windows')
  msg.append('')
               
  msg = string.join(msg, os.linesep)

  ipshell = IPythonShellEmbed(banner=msg)
  #ipshell(local_ns=self.data, global_ns=vars())
  ipshell()


if __name__ == '__main__':
  ipythonShell()
