Merged Rolf Schmidt's illysam branch
[hooke.git] / hooke / ui / gui / driver.py
index 7962d6aece34246622585208b9bb1ed2cd9e273d..b5b4809dd177a0ea52135d00abac8d161b0e7d6c 100644 (file)
-# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
-#
-# This file is part of Hooke.
-#
-# Hooke is free software: you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation, either
-# version 3 of the License, or (at your option) any later version.
-#
-# Hooke is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with Hooke.  If not, see
-# <http://www.gnu.org/licenses/>.
+#!/usr/bin/env python
 
+'''
+driver.py
 
-    def _generate_vectors(self):
-        """
-        Here we parse the data and generate the raw vectors. This
-        method has only to do with the peculiar file format here, so
-        it's of no big interest (I just wrote it to present a
-        functional driver).
+Base class for file format drivers.
 
-        Only thing to remember, it can be nice to call methods that
-        are used only "internally" by the driver (or by plugins) with
-        a "_" prefix, so to have a visual remark. But it's just an
-        advice.
-        """
-        vectors={'PLOT1':[[],[],[],[]] , 'PLOT2':[[],[],[],[]]}
-        positions={'X1':0,'Y1':1,'X2':2,'Y2':3}
-        whatplot=''
-        pos=0
-        for item in self.data:
-            try:
-                num=float(item)
-                vectors[whatplot][pos].append(num)
-            except ValueError:
-                if item[:-1]=='PLOT1':
-                    whatplot=item[:-1]
-                elif item[:-1]=='PLOT2':
-                    whatplot=item[:-1]
-                elif item[0]=='X' or item[0]=='Y':
-                    pos=positions[item[:-1]]
-                else:
-                    pass
+Copyright 2006 by Massimo Sandal (University of Bologna, Italy).
 
-        return vectors
+This program is released under the GNU General Public License version 2.
+'''
 
-    def default_plots(self):
-        """
-        THIS METHOD MUST BE DEFINED.
-        RETURNS: [ lhc.PlotObject ] or [ lhc.PlotObject, lhc.PlotObject]
-
-        This is the method that returns the plots to Hooke.
-        It must return a list with *one* or *two* PlotObjects.
-
-        See the curve.py source code to see how PlotObjects are defined and work in detail.
-        """
-        gen_vectors=self._generate_vectors()
+import lib.plot
 
-        #Here we create the main plot PlotObject and initialize its vectors.
-        main_plot=lhc.PlotObject()
-        main_plot.vectors=[]
-        #The same for the other plot.
-        other_plot=lhc.PlotObject()
-        other_plot.vectors=[]
+class Driver(object):
+    '''
+    Base class for file format drivers.
 
-        """
-        Now we fill the plot vectors with our data.
-                                                           set 1                           set 2
-        The "correct" shape of the vector is [ [[x1,x2,x3...],[y1,y2,y3...]] , [[x1,x2,x3...],[y1,y2,y3...]] ], so we have to put stuff in this way into it.
+    To be overridden
+    '''
+    def __init__(self):
+        self.experiment = ''
+        self.filetype = ''
 
-        The add_set() method takes care of this , just use plot.add_set(x,y).
-        """
-        main_plot.add_set(gen_vectors['PLOT1'][0],gen_vectors['PLOT1'][1])
-        main_plot.add_set(gen_vectors['PLOT1'][2],gen_vectors['PLOT1'][3])
+    def is_me(self):
+        '''
+        This method must read the file and return True if the filetype can be managed by the driver, False if not.
+        '''
+        return False
 
-        other_plot.add_set(gen_vectors['PLOT2'][0],gen_vectors['PLOT2'][1])
-        other_plot.add_set(gen_vectors['PLOT2'][2],gen_vectors['PLOT2'][3])
+    def close_all(self):
+        '''
+        This method must close all the open files of the driver, explicitly.
+        '''
+        return None
 
-        """
-        normalize_vectors() trims the vectors, so that if two x/y couples are of different lengths, the latest
-        points are trimmed (otherwise we have a python error). Always a good idea to run it, to avoid crashes.
-        """
-        main_plot.normalize_vectors()
-        other_plot.normalize_vectors()
-
-        """
-        Here we define:
-        - units: [string, string], define the measure units of X and Y axes
-        - destination: 0/1 , defines where to plot the plot (0=top, 1=bottom), default=0
-        - title: string , the plot title.
+    def default_plots(self):
+        plot = lib.plot.Plot()
+        plot.curves.append([0])
+        plot.curves.append([0])
+        
+        return [plot]
 
-        for each plot.
-        Again, see curve.py comments for details.
-        """
-        main_plot.units=['unit of x','unit of y']
-        main_plot.destination=0
-        main_plot.title=self.filename+' main'
 
-        other_plot.units=['unit of x','unit of y']
-        other_plot.destination=1
-        other_plot.title=self.filename+' other'
 
-        return [main_plot, other_plot]