-#!/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):
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
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']
-
- 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
-
-
-
-if __name__ == '__main__':
- mycli=HookeCli(0)
- mycli.cmdloop()