Moved system commands from hooke_cli to plugin.system.
authorW. Trevor King <wking@drexel.edu>
Wed, 12 May 2010 12:29:00 +0000 (08:29 -0400)
committerW. Trevor King <wking@drexel.edu>
Wed, 12 May 2010 12:29:00 +0000 (08:29 -0400)
hooke/hooke_cli.py
hooke/playlist.py
hooke/plugin/__init__.py
hooke/plugin/curvetools.py
hooke/plugin/cut.py
hooke/plugin/playlist.py
hooke/plugin/system.py [new file with mode: 0644]

index 03385226c4be3696988be671fad0e8a90e542c29..568aa4854f3293f6a036f311fae2bceebab0dbb8 100644 (file)
@@ -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__':
index d629023f57dd6bf667a88c70c870f83a61b0ed2b..60475ac6bb7ac6777fc06962df3d18f9e9373859 100644 (file)
@@ -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.
 """
index abf1bdb39d6af441c43a31033f074d3b838c8c20..6014a66207666ff4eeabd0bccb9c302a0b3a4870 100644 (file)
@@ -30,6 +30,7 @@ PLUGIN_MODULES = [
 #    ('review', True),
 #    ('showconvoluted', True),
 #    ('superimpose', True),
+    ('system', True),
 #    ('tutorial', True),
 #    ('viewer', True),
     ]
index 1060ce52b572bacabe8e9466cef462e8c58332cc..63513ba4b640e4fc869cab183b090ed47c9712cf 100755 (executable)
@@ -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
index 68dd2a0223b2a1f55f4ef466adfaa4c331b11aa9..369cfcc3b7fa86d49df9ac776a7953e8bd5f8e41 100644 (file)
@@ -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
index 060312c4006b79db918a11683bc2abebfb26953b..7474bf534ac76fbde56a2ba8a63118a3fbc3fbb3 100644 (file)
@@ -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 (file)
index 0000000..5a56ded
--- /dev/null
@@ -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)