changeset 27:ca6f34be3397

working on ed
author devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
date Wed, 19 Dec 2007 16:22:53 -0500
parents bb3fb24b6f5f
children dd1cc6630a65
files cmd2.py sqlpyPlus.py
diffstat 2 files changed, 36 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Wed Dec 19 16:10:01 2007 -0500
+++ b/cmd2.py	Wed Dec 19 16:22:53 2007 -0500
@@ -5,11 +5,11 @@
 Case-insensitive commands
 Special-character shortcut commands
 Load commands from file
+Settable environment parameters
 
 still to do:
-environment (maxrows, etc.)
 edit
-
+>
 """
 import cmd, re, os
 
@@ -20,7 +20,9 @@
     shortcuts = {'?': 'help', '!': 'shell', '@': 'load'}   
     excludeFromHistory = '''run r list l history hi ed li eof'''.split()   
     defaultExtension = 'txt'
-    settable = ['prompt', 'continuationPrompt']
+    defaultFileName = 'command.txt'
+    editor = os.environ.get('EDITOR') or ''
+    settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor']
     terminators = ';\n'
     def do_cmdenvironment(self, args):
 	self.stdout.write("""
@@ -191,8 +193,38 @@
     do_l = do_list
     do_li = do_list
 	
-    def do_load(self, fname):
+    def do_ed(self, arg):
+        'ed [N]: brings up SQL from N commands ago in text editor, and puts result in SQL buffer.'
+	if not self.editor:
+	    self.do_show('editor')
+	    return
+        buffer = self.last_matching(arg)
+        if not buffer:
+            print 'Nothing appropriate in buffer to edit.'
+            return
+        f = open(self.defaultFileName, 'w')
+        f.write(buffer)
+        f.close()
+	os.system('%s %s' % (self.editor, self.defaultFileName))
+        self.load(self.defaultFileName)
+    do_edit = do_ed
+    
+    def do_save(self, fname=None):
+	"""Saves most recent command to a file."""
+	
+	if fname is None:
+	    fname = self.defaultFileName
+        try:
+            f = open(fname, 'w')
+            f.write(self.history[-1])
+            f.close()
+        except Exception, e:
+            print 'Error saving %s: %s' % (fname, str(e))
+	    
+    def do_load(self, fname=None):
         """Runs command(s) from a file."""
+	if fname is None:
+	    fname = self.defaultFileName	
 	keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuationPrompt'))
 	try:
 	    self.stdin = open(fname, 'r')
--- a/sqlpyPlus.py	Wed Dec 19 16:10:01 2007 -0500
+++ b/sqlpyPlus.py	Wed Dec 19 16:22:53 2007 -0500
@@ -695,19 +695,6 @@
         self.onecmd_plus_hooks(runme)
     do_r = do_run
 
-    def do_ed(self, arg):
-        'ed [N]: brings up SQL from N commands ago in text editor, and puts result in SQL buffer.'
-        fname = 'sqlpython_temp.sql'
-        buffer = self.last_matching(arg)
-        if not buffer:
-            print 'Nothing appropriate in buffer to edit.'
-            return
-        f = open(fname, 'w')
-        f.write(buffer)
-        f.close()
-        editSearcher.invoke(fname)
-        self.load(fname)
-    do_edit = do_ed
     def do_get(self, fname):
         'Brings SQL commands from a file to the in-memory SQL buffer.'
         numCommandsLoaded = self.load(fname)
@@ -747,14 +734,6 @@
             self.onecmd('q')
         except KeyError:
             print 'psql command \%s not yet supported.' % abbrev        
-    def do_save(self, fname):
-        'save FILENAME: Saves most recent SQL command to disk.'
-        try:
-            f = open(fname, 'w')
-            f.write(self.sqlBuffer[-1])
-            f.close()
-        except Exception, e:
-            print 'Error saving %s: %s' % (fname, str(e))
         
     def do_print(self, arg):
         'print VARNAME: Show current value of bind variable VARNAME.'