changeset 26:bb3fb24b6f5f

settables ironed out
author devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
date Wed, 19 Dec 2007 16:10:01 -0500
parents c99853267a44
children ca6f34be3397
files cmd2.py sqlpyPlus.py
diffstat 2 files changed, 31 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Wed Dec 19 15:37:45 2007 -0500
+++ b/cmd2.py	Wed Dec 19 16:10:01 2007 -0500
@@ -20,8 +20,8 @@
     shortcuts = {'?': 'help', '!': 'shell', '@': 'load'}   
     excludeFromHistory = '''run r list l history hi ed li eof'''.split()   
     defaultExtension = 'txt'
-    settable = []
-    terminators = r';\n'
+    settable = ['prompt', 'continuationPrompt']
+    terminators = ';\n'
     def do_cmdenvironment(self, args):
 	self.stdout.write("""
 	Commands are %(casesensitive)scase-sensitive.
@@ -94,7 +94,7 @@
 	return True
     do_eof = do_EOF
     
-    statementEndPattern = re.compile(r'[%s]\s*$' % self.terminators)	
+    statementEndPattern = re.compile(r'[%s]\s*$' % terminators)	
     def statementHasEnded(self, lines):
 	return bool(self.statementEndPattern.search(lines))
 	       
@@ -117,7 +117,7 @@
 	    line = '%s %s' % (shortcut, line[1:])
         i, n = 0, len(line)
         while i < n and line[i] in self.identchars: i = i+1
-        cmd, arg = line[:i], line[i:].strip()
+        cmd, arg = line[:i], line[i:].strip().strip(self.terminators)
         return cmd, arg, line
     
     def showParam(self, param):
@@ -133,22 +133,6 @@
         else:
             for param in self.settable:
                 self.showParam(param)
-		
-    def cast(self, current, new):
-        typ = type(current)
-        if typ == bool:
-            new = new.lower()            
-            try:
-                if (new=='on') or (new[0] in ('y','t')):
-                    return True
-                return False
-            except TypeError:
-                None
-        try:
-            return typ(new)
-        except:
-            print "Problem setting parameter (now %s) to %s; incorrect type?" % (current, new)
-            return current
     
     def do_set(self, arg):
         'Sets a parameter'        
@@ -158,10 +142,10 @@
 	    if paramName not in self.settable:
 		raise NotSettableError	    		
 	    currentVal = getattr(self, paramName)
-	    val = self.cast(currentVal, val.strip(self.terminators))
+	    val = cast(currentVal, val.strip(self.terminators))
 	    setattr(self, paramName, val)
-	    self.stdout.write(paramName, ' - was: %s\nnow: %s\n' % (currentVal, val))
-        except ValueError, AttributeError, NotSettableError:
+	    self.stdout.write('%s - was: %s\nnow: %s\n' % (paramName, currentVal, val))
+        except (ValueError, AttributeError, NotSettableError):
             self.do_show(arg)
 		
     def do_shell(self, arg):
@@ -206,15 +190,6 @@
     do_hi = do_history
     do_l = do_list
     do_li = do_list
-    
-    def breakupStatements(self, txt):
-	"""takes text that may include multiple statements and 
-	breaks it into a list of individual statements."""
-	result = ['']
-	for line in txt.splitlines():
-	    result[-1] += line
-	    if self.statementHasEnded(result[-1]):
-		result.append('')
 	
     def do_load(self, fname):
         """Runs command(s) from a file."""
@@ -277,6 +252,30 @@
 class NotSettableError(Exception):
     None    
 	
+def cast(current, new):
+    """Tries to force a new value into the same type as the current."""
+    typ = type(current)
+    print type(new)
+    if typ == bool:
+	new = new.lower()    
+	print new
+	try:
+	    print new == 'on'
+	    if (new=='on') or (new[0] in ('y','t')):
+		return True
+	    try:
+		return bool(int(new))
+	    except ValueError:
+		pass
+	    return False
+	except TypeError:
+	    pass
+    try:
+	return typ(new)
+    except:
+	print "Problem setting parameter (now %s) to %s; incorrect type?" % (current, new)
+	return current
+	
 class Statekeeper(object):
     def __init__(self, obj, attribs):
 	self.obj = obj
--- a/sqlpyPlus.py	Wed Dec 19 15:37:45 2007 -0500
+++ b/sqlpyPlus.py	Wed Dec 19 16:10:01 2007 -0500
@@ -547,57 +547,6 @@
             traceback.print_exc(file=sys.stdout)
         self.sqlBuffer.append(self.query)
 
-    def showParam(self, param):
-        param = param.strip().lower()
-        if param in self.settable:
-            val = getattr(self, param)
-            print '%s: %s' % (param, str(getattr(self, param)))
-
-    def do_show(self, arg):
-        'Shows value of a (sqlpython, not ORACLE) parameter'
-        arg = arg.strip().lower()
-        if arg:
-            self.showParam(arg)
-        else:
-            for param in self.settable:
-                self.showParam(param)
-
-    def cast(self, current, new):
-        typ = type(current)
-        if typ == bool:
-            new = new.lower()            
-            try:
-                if (new=='on') or (new[0] in ('y','t')):
-                    return True
-                return False
-            except TypeError:
-                None
-        try:
-            return typ(new)
-        except:
-            print "Problem setting parameter (now %s) to %s; incorrect type?" % (current, new)
-            return current
-    
-    def do_set(self, arg):
-        'Sets a (sqlpython, not ORACLE) parameter'        
-        try:
-            paramName, val = arg.split(None, 1)
-        except Exception:
-            self.do_show(arg)
-            return
-        paramName = paramName.lower()
-        try:
-            current = getattr(self, paramName)
-            if callable(current):
-                raise NotSettableError
-        except (AttributeError, NotSettableError):
-            self.fail('set %s' % arg)
-            return
-        val = self.cast(current, val.strip(';'))
-        print paramName, ' - was: ', current
-        setattr(self, paramName.lower(), val)
-        print 'now: ', val
-
     pullflags = flagReader.FlagSet([flagReader.Flag('full')])	    
     def do_pull(self, arg):
         """Displays source code.