From 58f639ef8d2ea55b78791d8fe7839d7161bbf384 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 12 May 2010 08:29:00 -0400 Subject: [PATCH] Moved system commands from hooke_cli to plugin.system. --- hooke/hooke_cli.py | 91 ------------------------------------- hooke/playlist.py | 2 +- hooke/plugin/__init__.py | 1 + hooke/plugin/curvetools.py | 31 +++++++++++++ hooke/plugin/cut.py | 3 +- hooke/plugin/playlist.py | 6 +-- hooke/plugin/system.py | 93 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 131 insertions(+), 96 deletions(-) create mode 100644 hooke/plugin/system.py diff --git a/hooke/hooke_cli.py b/hooke/hooke_cli.py index 0338522..568aa48 100644 --- a/hooke/hooke_cli.py +++ b/hooke/hooke_cli.py @@ -690,63 +690,6 @@ Syntax copylog [directory] 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 @@ -764,40 +707,6 @@ 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__': diff --git a/hooke/playlist.py b/hooke/playlist.py index d629023..60475ac 100644 --- a/hooke/playlist.py +++ b/hooke/playlist.py @@ -1,4 +1,4 @@ -"""The playlist module provides :class:`Playlist` its subclass +"""The `playlist` module provides a :class:`Playlist` and its subclass :class:`FilePlaylist` for manipulating lists of :class:`hooke.curve.Curve`\s. """ diff --git a/hooke/plugin/__init__.py b/hooke/plugin/__init__.py index abf1bdb..6014a66 100644 --- a/hooke/plugin/__init__.py +++ b/hooke/plugin/__init__.py @@ -30,6 +30,7 @@ PLUGIN_MODULES = [ # ('review', True), # ('showconvoluted', True), # ('superimpose', True), + ('system', True), # ('tutorial', True), # ('viewer', True), ] diff --git a/hooke/plugin/curvetools.py b/hooke/plugin/curvetools.py index 1060ce5..63513ba 100755 --- a/hooke/plugin/curvetools.py +++ b/hooke/plugin/curvetools.py @@ -91,3 +91,34 @@ class curvetoolsCommands: +class InfoCommand (Command): + """Execute a system command and report the output. + """ + def __init__(self): + super(SystemCommand, self).__init__( + name='system', + arguments=[ + Argument( + name='command', type='string', optional=False, count=-1, + help=""" +Command line to execute. +""".strip()) +], + help=self.__doc__) + + def _run(self, hooke, inqueue, outqueue, params): + os.system(params['command']) + + 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 diff --git a/hooke/plugin/cut.py b/hooke/plugin/cut.py index 68dd2a0..369cfcc 100644 --- a/hooke/plugin/cut.py +++ b/hooke/plugin/cut.py @@ -1,4 +1,5 @@ -"""Defines :class:`CutPlugin` and :class:`CutCommand`. +"""The `cut` module provides :class:`CutPlugin` and +:class:`CutCommand`. """ from ..command import Command, Argument, Failure diff --git a/hooke/plugin/playlist.py b/hooke/plugin/playlist.py index 060312c..7474bf5 100644 --- a/hooke/plugin/playlist.py +++ b/hooke/plugin/playlist.py @@ -1,6 +1,6 @@ -"""Defines :class:`PlaylistPlugin` several associated -:class:`hooke.plugin.Command`\s for handling :mod:`hooke.playlist` -classes. +"""The `playlist` module provides :class:`PlaylistPlugin` several +associated :class:`hooke.command.Command`\s for handling +:mod:`hooke.playlist` classes. """ import glob diff --git a/hooke/plugin/system.py b/hooke/plugin/system.py new file mode 100644 index 0000000..5a56ded --- /dev/null +++ b/hooke/plugin/system.py @@ -0,0 +1,93 @@ +"""The `system` module provides :class:`SystemPlugin` and several +associated :class:`hooke.command.Command`\s for interacting with the +operating system and execution environment. +""" + +import os +import os.path +import subprocess +import sys + +from ..command import Command, Argument +from ..plugin import Builtin + + +class SystemPlugin (Builtin): + def __init__(self): + super(SystemPlugin, self).__init__(name='system') + + def commands(self): + return [ListDirectoryCommand(), GetWorkingDirectoryCommand(), + ChangeDirectoryCommand(), SystemCommand()] + + +class ListDirectoryCommand (Command): + """List the files in a directory. + """ + def __init__(self): + super(ListDirectoryCommand, self).__init__( + name='ls', aliases=['dir'], + arguments=[ + Argument( + name='path', type='path', default='.', + help=""" +Path to the directory whose contents get listed. Defaults to the +current working directory. +""".strip()), +], + help=self.__doc__) + + def _run(self, hooke, inqueue, outqueue, params): + outqueue.put('\n'.join(sorted(os.listdir(params['path'])))) + +class GetWorkingDirectoryCommand (Command): + """Get the current working directory. + """ + def __init__(self): + super(GetWorkingDirectoryCommand, self).__init__( + name='cwd', aliases=['pwd'], help=self.__doc__) + + def _run(self, hooke, inqueue, outqueue, params): + outqueue.put(os.getcwd()) + +class ChangeDirectoryCommand (Command): + """Change the current working directory. + """ + def __init__(self): + super(ChangeDirectoryCommand, self).__init__( + name='cd', + arguments=[ + Argument( + name='path', type='path', default='~', + help=""" +Path of the directory to change into. Default to the user's home +directory. +""".strip()), +], + help=self.__doc__) + + def _run(self, hooke, inqueue, outqueue, params): + os.chdir(os.path.expanduser(params['path'])) + +class SystemCommand (Command): + """Execute a system command and report the output. + """ + def __init__(self): + super(SystemCommand, self).__init__( + name='system', + arguments=[ + Argument( + name='command', type='string', optional=False, count=-1, + help=""" +Command line to execute. +""".strip()), +], + help=self.__doc__) + + def _run(self, hooke, inqueue, outqueue, params): + p = subprocess.Popen( + params['command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout,stderr = p.communicate() + returncode = p.wait() + outqueue.put(stdout) + outqueue.put(stderr) -- 2.26.2