Moved plot commands from hooke_cli -> hooke.ui.gui.plotcommands stub.
[hooke.git] / hooke / hooke_cli.py
index 886563b735a8328119f15a9c3583a446c1465370..51e56a8c9268373bb9f2d7a6ade59989febc2b98 100644 (file)
@@ -1,51 +1,3 @@
-#!/usr/bin/env python
-
-'''
-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 libhookecurve 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):
@@ -205,166 +157,6 @@ Syntax: set [variable] [value]
         self.config[key]=value
         self.do_plot(0)
 
-#PLAYLIST MANAGEMENT AND NAVIGATION
-#------------------------------------
-
-    def help_loadlist(self):
-        print '''
-LOADLIST
-Loads a file playlist
------------
-Syntax: loadlist [playlist file]
-        '''
-    def do_loadlist(self, args):
-        #checking for args: if nothing is given as input, we warn and exit.
-        while len(args)==0:
-            args=linp.safeinput('File to load?')
-
-        arglist=args.split()
-        play_to_load=arglist[0]
-
-        #We assume a Hooke playlist has the extension .hkp
-        if play_to_load[-4:] != '.hkp':
-            play_to_load+='.hkp'
-
-        try:
-            playxml=PlaylistXML()
-            self.current_list, self.playlist_generics=playxml.load(play_to_load)
-            self.current_playxml=playxml
-        except IOError:
-            print 'File not found.', play_to_load
-            return
-
-        print 'Loaded %s curves from %s' \
-            % (len(self.current_list), play_to_load)
-
-        if 'pointer' in self.playlist_generics.keys():
-            self.pointer=int(self.playlist_generics['pointer'])
-        else:
-            #if no pointer is found, set the current curve as the first curve of the loaded playlist
-            self.pointer=0
-        print 'Starting at curve ',self.pointer
-
-        self.current=self.current_list[self.pointer]
-
-        #resets saved/notes saved state
-        self.playlist_saved=0
-        self.playlist_name=''
-        self.notes_saved=0
-
-        self.do_plot(0)
-
-
-    def help_genlist(self):
-        print '''
-GENLIST
-Generates a file playlist.
-Note it doesn't *save* it: see savelist for this.
-
-If [input files] is a directory, it will use all files in the directory for playlist.
-So:
-genlist dir
-genlist dir/
-genlist dir/*.*
-
-are all equivalent syntax.
-------------
-Syntax: genlist [input files]
-
-'''
-    def do_genlist(self,args):
-        #args list is: input path, output name
-        if len(args)==0:
-            args=linp.safeinput('Input files?')
-
-        arglist=args.split()
-        list_path=arglist[0]
-
-        #if it's a directory, is like /directory/*.*
-        #FIXME: probably a bit kludgy.
-        if os.path.isdir(list_path):
-            if platform.system == 'Windows':
-                SLASH="\\"
-            else:
-                SLASH="/"
-            if list_path[-1] == SLASH:
-                list_path=list_path+'*.*'
-            else:
-                list_path=list_path+SLASH+'*.*'
-
-        #expanding correctly the input list with the glob module :)
-        list_files=glob.glob(list_path)
-        list_files.sort()
-
-        self.current_list=[]
-        for item in list_files:
-            try:
-                if os.path.isfile(item):
-                    self.current_list.append(lhc.HookeCurve(os.path.abspath(item)))
-            except:
-                pass
-
-        self.pointer=0
-        if len(self.current_list)>0:
-            self.current=self.current_list[self.pointer]
-        else:
-            print 'Empty list!'
-            return
-
-        #resets saved/notes saved state
-        self.playlist_saved=0
-        self.playlist_name=''
-        self.notes_saved=0
-
-        self.do_plot(0)
-
-
-    def do_savelist(self,args):
-        '''
-        SAVELIST
-        Saves the current file playlist on disk.
-        ------------
-        Syntax: savelist [filename]
-        '''
-        while len(args)==0:
-            args=linp.safeinput('Output file?',['savedlist.txt'])
-
-        output_filename=args
-
-        self.playlist_generics['pointer']=self.pointer
-
-        #autocomplete filename if not specified
-        if output_filename[-4:] != '.hkp':
-            output_filename+='.hkp'
-
-        playxml=PlaylistXML()
-        playxml.export(self.current_list, self.playlist_generics)
-        playxml.save(output_filename)
-
-        #remembers we have saved playlist
-        self.playlist_saved=1
-
-    def help_addtolist(self):
-        print '''
-ADDTOLIST
-Adds a file to the current playlist
---------------
-Syntax: addtolist [filename]
-'''
-    def do_addtolist(self,args):
-        #args list is: input path
-        if len(args)==0:
-            print 'You must give the input filename you want to add'
-            self.help_addtolist()
-            return
-
-        filenames=glob.glob(args)
-
-        for filename in filenames:
-            self.current_list.append(lhc.HookeCurve(os.path.abspath(filename)))
-        #we need to save playlist
-        self.playlist_saved=0
-
     def help_printlist(self):
         print '''
 PRINTLIST
@@ -498,484 +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
-
-        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:
-                self.notes_filename=raw_input('Notebook filename? ')
-                title_line='Notes taken at '+time.asctime()+'\n'
-                f=open(self.notes_filename,'w')
-                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']
-
-    def help_exit(self):
-        print '''
-EXIT, QUIT
-Exits the program cleanly.
-------
-Syntax: exit
-Syntax: quit
-'''
-    def do_exit(self,args):
-        we_exit='N'
-
-        if (not self.playlist_saved) or (not self.notes_saved):
-            we_exit=linp.safeinput('You did not save your playlist and/or notes. Exit?',['n'])
-        else:
-            we_exit=linp.safeinput('Exit?',['y'])
-
-        if we_exit[0].upper()=='Y':
-            wx.CallAfter(self.frame.Close)
-            sys.exit(0)
-        else:
-            return
-
-    def help_quit(self):
-        self.help_exit()
-    def do_quit(self,args):
-        self.do_exit(args)
-
-
-if __name__ == '__main__':
-    mycli=HookeCli(0)
-    mycli.cmdloop()