#! /usr/bin/python

from my_exceptions import *
from Numeric import *
import Gnuplot, Gnuplot.funcutils
import os

class Export:
    "Module to export the double_lin_reg"

    def __init__(self,output_file_name='output'):
        self.File_list=['png','svg','fig','jpeg']
        self.Export_list=self.File_list+['window','text']
        self.Export=['text','window']
        self.Output_file_name=output_file_name


    #method used to define the plot (all the different point)
    def set_plot(self,plot):
        self.Plot=plot

    #method used to define the coefficient of the double linear regression
    def set_reg(self,reg):
        self.Reg=reg

    #method used to give the number of the Export_list
    def size_of_export_list(self):
        return len(self.Export_list)

    #method used to use File_list
    def file_list(self):
        return self.File_list
    

    #method used to defined the export as a list : dangerous : There is no control
    def set_export_list(self, List):
        self.Export=List

    #method used to defined the first exportation type
    def set_export(self,export):
        if export=='all':
            self.Export=self.Export_list
            
        elif  export in self.Export_list:
            self.Export=[export]
        else:
            raise InvalidArgument,export
        
    #method used to add one exportation type
    def add_export(self,export):
        if export in self.Export_list:
            if not export in self.Export:
                self.Export+=[export]
        else:
            raise InvalidArgument,export

    #method used to configure the output file name without the extention 
    def set_output_file_name(self,file_name):
        self.Output_file_name=file_name
    
    #method used to do the exportation
    #Before use this method you MUST have set the plot and the reg with setreg(reg) and setplot(plot) 
    
    def export(self):
        for export in self.Export:
            if export =='text':
                print 'The fractal dimensions are : ',-self.Reg[0][0] ,'and', -self.Reg[1][0]
            elif export in (self.Export_list +['window']) :
                try:
                    graphic = Gnuplot.Gnuplot(debug=0)
                    graphic.title('Result of interpolation') 
                    graphic.xlabel('ln(size_of_boxes)')
                    graphic('set yrange [' + str(self.Plot[-1][1]-1) + ':' + str(self.Plot[0][1]+0.5)+']')
                    graphic.ylabel('ln(number_of_covering_contour_boxes)')
                    if export in self.File_list:
                        graphic('set terminal '+ export)
                        graphic('set output "'+ self.Output_file_name + '.' + export + '"')
                    graphic.plot(self.Plot,Gnuplot.Func(str(self.Reg[0][0]) + '*x+'+ str(self.Reg[0][1])),Gnuplot.Func(str(self.Reg[1][0]) + '*x+'+ str(self.Reg[1][1])))
                    if export == 'window':
                        raw_input('Please press return to continue...\n')
                    graphic.reset()
                except:
                    print "The " + export + "failed" 
            else:
                print 'no output!'

    #method to check if we have right to write the output file
    def check_right(self):
        for export in self.Export:
            if export in self.File_list:
                path=os.path.abspath(self.Output_file_name +"." +export)
                if os.path.exists(path):
                    if not os.access(path, os.W_OK):
                        print "You can't write on "+ path
                        return False
                else:
                    if not os.access(os.path.dirname(path), os.W_OK):
                        print "You can't write in "+ os.path.dirname(path)
                        return False
        return True

            



