Moved plot commands from hooke_cli -> hooke.ui.gui.plotcommands stub.
[hooke.git] / hooke / hooke_cli.py
index 03385226c4be3696988be671fad0e8a90e542c29..51e56a8c9268373bb9f2d7a6ade59989febc2b98 100644 (file)
@@ -1,52 +1,3 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-'''
-hooke_cli.py
-
-Command line module of Hooke.
-
-Copyright (C) 2006 Massimo Sandal (University of Bologna, Italy).
-
-This program is released under the GNU General Public License version 2.
-'''
-
-from .libhooke import HOOKE_VERSION, WX_GOOD
-
-import wxversion
-wxversion.select(WX_GOOD)
-import wx
-
-from wx.lib.newevent import NewEvent
-from matplotlib.numerix import * #FIXME
-
-import xml.dom.minidom
-import sys, os, os.path, glob, shutil
-import Queue
-import cmd
-import time
-
-global __version__
-global __codename__
-global __releasedate__
-__version__ = HOOKE_VERSION[0]
-__codename__ = HOOKE_VERSION[1]
-__releasedate__ = HOOKE_VERSION[2]
-
-from matplotlib import __version__ as mpl_version
-from wx import __version__ as wx_version
-from wxmpl import __version__ as wxmpl_version
-from scipy import __version__ as scipy_version
-from numpy import __version__ as numpy_version
-from sys import version as python_version
-import platform
-
-from .libhooke import PlaylistXML
-from . import curve as lhc
-from . import libinput as linp
-from . import liboutlet as lout
-
-
 class HookeCli(cmd.Cmd, object):
 
     def __init__(self,frame,list_of_events,events_from_gui,config,drivers):
@@ -339,467 +290,3 @@ Syntax: previous, p
         self.do_previous(args)
 
 
-#PLOT INTERACTION COMMANDS
-#-------------------------------
-    def help_plot(self):
-        print '''
-PLOT
-Plots the current force curve
--------
-Syntax: plot
-        '''
-    def do_plot(self,args):
-        if self.current.identify(self.drivers) == False:
-            return
-        self.plots=self.current.curve.default_plots()
-        try:
-            self.plots=self.current.curve.default_plots()
-        except Exception, e:
-            print 'Unexpected error occurred in do_plot().'
-            print e
-            return
-
-        #apply the plotmanip functions eventually present
-        nplots=len(self.plots)
-        c=0
-        while c<nplots:
-            for function in self.plotmanip: #FIXME: something strange happens about self.plotmanip[0]
-                self.plots[c]=function(self.plots[c], self.current)
-
-            self.plots[c].xaxes=self.config['xaxes'] #FIXME: in the future, xaxes and yaxes should be set per-plot
-            self.plots[c].yaxes=self.config['yaxes']
-
-            c+=1
-
-        self._send_plot(self.plots)
-
-    def _delta(self, set=1):
-        '''
-        calculates the difference between two clicked points
-        '''
-        print 'Click two points'
-        points=self._measure_N_points(N=2, whatset=set)
-        dx=abs(points[0].graph_coords[0]-points[1].graph_coords[0])
-        dy=abs(points[0].graph_coords[1]-points[1].graph_coords[1])
-        unitx=self.plots[points[0].dest].units[0]
-        unity=self.plots[points[0].dest].units[1]
-        return dx,unitx,dy,unity
-
-    def do_delta(self,args):
-        '''
-        DELTA
-
-        Measures the delta X and delta Y between two points.
-        ----
-        Syntax: delta
-        '''
-        dx,unitx,dy,unity=self._delta()
-        print str(dx)+' '+unitx
-        print str(dy)+' '+unity
-
-    def _point(self, set=1):
-        '''calculates the coordinates of a single clicked point'''
-
-        print 'Click one point'
-        point=self._measure_N_points(N=1, whatset=set)
-
-        x=point[0].graph_coords[0]
-        y=point[0].graph_coords[1]
-        unitx=self.plots[point[0].dest].units[0]
-        unity=self.plots[point[0].dest].units[1]
-        return x,unitx,y,unity
-
-    def do_point(self,args):
-        '''
-        POINT
-
-        Returns the coordinates of a point on the graph.
-        ----
-        Syntax: point
-        '''
-        x,unitx,y,unity=self._point()
-        print str(x)+' '+unitx
-        print str(y)+' '+unity
-        to_dump='point '+self.current.path+' '+str(x)+' '+unitx+', '+str(y)+' '+unity
-        self.outlet.push(to_dump)
-
-
-    def do_close(self,args=None):
-        '''
-        CLOSE
-        Closes one of the two plots. If no arguments are given, the bottom plot is closed.
-        ------
-        Syntax: close [top,bottom]
-        '''
-        if args=='top':
-            to_close=0
-        elif args=='bottom':
-            to_close=1
-        else:
-            to_close=1
-
-        close_plot=self.list_of_events['close_plot']
-        wx.PostEvent(self.frame, close_plot(to_close=to_close))
-
-    def do_show(self,args=None):
-        '''
-        SHOW
-        Shows both plots.
-        '''
-        show_plots=self.list_of_events['show_plots']
-        wx.PostEvent(self.frame, show_plots())
-
-
-
-    #PLOT EXPORT AND MANIPULATION COMMANDS
-    def help_export(self):
-        print '''
-EXPORT
-Saves the current plot as an image file
----------------
-Syntax: export [filename] {plot to export}
-
-The supported formats are PNG and EPS; the file extension of the filename is automatically recognized
-and correctly exported. Resolution is (for now) fixed at 150 dpi.
-
-If you have a multiple plot, the optional plot to export argument tells Hooke which plot you want to export. If 0, the top plot is exported. If 1, the bottom plot is exported (Exporting both plots is still to implement)
-        '''
-    def do_export(self,args):
-        #FIXME: the bottom plot doesn't have the title
-
-        dest=0
-
-        if len(args)==0:
-            #FIXME: We have to go into the libinput stuff and fix it, for now here's a dummy replacement...
-            #name=linp.safeinput('Filename?',[self.current.path+'.png'])
-            name=raw_input('Filename? ')
-        else:
-            args=args.split()
-            name=args[0]
-            if len(args) > 1:
-                dest=int(args[1])
-
-        export_image=self.list_of_events['export_image']
-        wx.PostEvent(self.frame, export_image(name=name, dest=dest))
-
-
-    def help_txt(self):
-        print '''
-TXT
-Saves the current curve as a text file
-Columns are, in order:
-X1 , Y1 , X2 , Y2 , X3 , Y3 ...
-
--------------
-Syntax: txt [filename] {plot to export}
-        '''
-    def do_txt(self,args):
-
-        def transposed2(lists, defval=0):
-            '''
-            transposes a list of lists, i.e. from [[a,b,c],[x,y,z]] to [[a,x],[b,y],[c,z]] without losing
-            elements
-            (by Zoran Isailovski on the Python Cookbook online)
-            '''
-            if not lists: return []
-            return map(lambda *row: [elem or defval for elem in row], *lists)
-
-        whichplot=0
-        args=args.split()
-        if len(args)==0:
-            filename=linp.safeinput('Filename?',[self.current.path+'.txt'])
-        else:
-            filename=linp.checkalphainput(args[0],self.current.path+'.txt',[])
-            try:
-                whichplot=int(args[1])
-            except:
-                pass
-
-       try:
-            outofplot=self.plots[whichplot].vectors
-        except:
-            print "Plot index out of range."
-           return 0
-
-        columns=[]     
-        for dataset in self.plots[whichplot].vectors:
-            for i in range(0,len(dataset)):
-                columns.append([])
-                for value in dataset[i]:
-                    columns[-1].append(str(value))
-
-        rows=transposed2(columns, 'nan')
-        rows=[' , '.join(item) for item in rows]
-        text='\n'.join(rows)
-
-        txtfile=open(filename,'w+')
-        #Save units of measure in header
-        txtfile.write('X:'+self.plots[whichplot].units[0]+'\n')
-        txtfile.write('Y:'+self.plots[whichplot].units[1]+'\n')
-        txtfile.write(text)
-        txtfile.close()
-
-
-    #LOGGING, REPORTING, NOTETAKING
-
-
-    def do_note_old(self,args):
-        '''
-        NOTE_OLD
-        **deprecated**: Use note instead. Will be removed in 0.9
-
-        Writes or displays a note about the current curve.
-        If [anything] is empty, it displays the note, otherwise it adds a note.
-        The note is then saved in the playlist if you issue a savelist command
-        ---------------
-        Syntax: note_old [anything]
-
-        '''
-        if args=='':
-            print self.current_list[self.pointer].notes
-        else:
-            #bypass UnicodeDecodeError troubles
-            try:
-                args=args.decode('ascii')
-            except:
-                args=args.decode('ascii','ignore')
-                if len(args)==0:
-                    args='?'
-
-            self.current_list[self.pointer].notes=args
-        self.notes_saved=0
-
-
-    def do_note(self,args):
-        '''
-        NOTE
-
-        Writes or displays a note about the current curve.
-        If [anything] is empty, it displays the note, otherwise it adds a note.
-        The note is then saved in the playlist if you issue a savelist command.
-        ---------------
-        Syntax: note_old [anything]
-
-        '''
-        if args=='':
-            print self.current_list[self.pointer].notes
-        else:
-            if self.notes_filename == None:
-               if not os.path.exists(os.path.realpath('output')):
-                   os.mkdir('output')
-                self.notes_filename=raw_input('Notebook filename? ')
-               self.notes_filename=os.path.join(os.path.realpath('output'),self.notes_filename)
-                title_line='Notes taken at '+time.asctime()+'\n'
-                f=open(self.notes_filename,'a')
-                f.write(title_line)
-                f.close()
-
-            #bypass UnicodeDecodeError troubles
-            try:
-               args=args.decode('ascii')
-            except:
-               args=args.decode('ascii','ignore')
-               if len(args)==0:
-                   args='?'
-            self.current_list[self.pointer].notes=args
-
-            f=open(self.notes_filename,'a+')
-            note_string=(self.current.path+'  |  '+self.current.notes+'\n')
-            f.write(note_string)
-            f.close()
-
-    def help_notelog(self):
-        print '''
-NOTELOG
-Writes a log of the notes taken during the session for the current
-playlist
---------------
-Syntax notelog [filename]
-'''
-    def do_notelog(self,args):
-
-        if len(args)==0:
-            args=linp.safeinput('Notelog filename?',['notelog.txt'])
-
-        note_lines='Notes taken at '+time.asctime()+'\n'
-        for item in self.current_list:
-            if len(item.notes)>0:
-                #FIXME: log should be justified
-                #FIXME: file path should be truncated...
-                note_string=(item.path+'  |  '+item.notes+'\n')
-                note_lines+=note_string
-
-        try:
-            f=open(args,'a+')
-            f.write(note_lines)
-            f.close()
-        except IOError, (ErrorNumber, ErrorMessage):
-            print 'Error: notes cannot be saved. Catched exception:'
-            print ErrorMessage
-
-        self.notes_saved=1
-
-    def help_copylog(self):
-        print '''
-COPYLOG
-Moves the annotated curves to another directory
------------
-Syntax copylog [directory]
-        '''
-    def do_copylog(self,args):
-
-        if len(args)==0:
-            args=linp.safeinput('Destination directory?')  #TODO default
-
-        mydir=os.path.abspath(args)
-        if not os.path.isdir(mydir):
-            print 'Destination is not a directory.'
-            return
-
-        for item in self.current_list:
-            if len(item.notes)>0:
-                try:
-                    shutil.copy(item.path, mydir)
-                except (OSError, IOError):
-                    print 'Cannot copy file. '+item.path+' Perhaps you gave me a wrong directory?'
-
-#OUTLET management
-#-----------------
-    def do_outlet_show(self,args):
-        '''OUTLET_SHOW
-        ---------
-        Shows current content of outlet with index for reference
-        '''
-        self.outlet.printbuf()
-
-    def do_outlet_undo(self, args):
-        '''OUTLET_UNDO
-        ---------
-        Eliminates last entry in outlet
-        '''
-        print 'Erasing last entry'
-        self.outlet.pop()
-
-    def do_outlet_delete(self, args):
-        '''OUTLET_DELETE
-        Eliminates a particular entry from outlet
-        Syntax: outlet_delete n
-        '''
-        if len(args)==0:
-            print 'Index needed!, use outlet_show to know it'
-        else:
-            self.outlet.delete(args)
-
-#OS INTERACTION COMMANDS
-#-----------------
-    def help_dir(self):
-        print '''
-DIR, LS
-Lists the files in the directory
----------
-Syntax: dir [path]
-          ls  [path]
-        '''
-    def do_dir(self,args):
-
-        if len(args)==0:
-            args='*'
-        print glob.glob(args)
-
-    def help_ls(self):
-        self.help_dir(self)
-    def do_ls(self,args):
-        self.do_dir(args)
-
-    def help_pwd(self):
-        print '''
-PWD
-Gives the current working directory.
-------------
-Syntax: pwd
-        '''
-    def do_pwd(self,args):
-        print os.getcwd()
-
-    def help_cd(self):
-        print '''
-CD
-Changes the current working directory
------
-Syntax: cd
-        '''
-    def do_cd(self,args):
-        mypath=os.path.abspath(args)
-        try:
-            os.chdir(mypath)
-        except OSError:
-            print 'I cannot access that directory.'
-
-
-    def help_system(self):
-        print '''
-SYSTEM
-Executes a system command line and reports the output
------
-Syntax system [command line]
-        '''
-        pass
-    def do_system(self,args):
-        waste=os.system(args)
-
-    def do_debug(self,args):
-        '''
-        this is a dummy command where I put debugging things
-        '''
-        print self.config['plotmanips']
-        pass
-
-    def help_current(self):
-        print '''
-CURRENT
-Prints the current curve path.
-------
-Syntax: current
-        '''
-    def do_current(self,args):
-        print self.current.path
-
-    def do_info(self,args):
-        '''
-        INFO
-        ----
-        Returns informations about the current curve.
-        '''
-        print 'Path: ',self.current.path
-        print 'Experiment: ',self.current.curve.experiment
-        print 'Filetype: ',self.current.curve.filetype
-        for plot in self.current.curve.default_plots():
-            for set in plot.vectors:
-                lengths=[len(item) for item in set]
-                print 'Data set size: ',lengths
-
-    def do_version(self,args):
-        '''
-        VERSION
-        ------
-        Prints the current version and codename, plus library version. Useful for debugging.
-        '''
-        print 'Hooke '+__version__+' ('+__codename__+')'
-        print 'Released on: '+__releasedate__
-        print '---'
-        print 'Python version: '+python_version
-        print 'WxPython version: '+wx_version
-        print 'wxMPL version: '+wxmpl_version
-        print 'Matplotlib version: '+mpl_version
-        print 'SciPy version: '+scipy_version
-        print 'NumPy version: '+numpy_version
-        print '---'
-        print 'Platform: '+str(platform.uname())
-        print '---'
-        print 'Loaded plugins:',self.config['loaded_plugins']
-
-
-
-if __name__ == '__main__':
-    mycli=HookeCli(0)
-    mycli.cmdloop()