changeset 319:c58cd7e48db7

begin to switch settable to TextLineList
author catherine@dellzilla
date Thu, 11 Feb 2010 13:07:05 -0500
parents f44ad8de0d17
children b9f19255d4b7
files cmd2.py flatten_lines.py
diffstat 2 files changed, 97 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Thu Feb 11 11:25:35 2010 -0500
+++ b/cmd2.py	Thu Feb 11 13:07:05 2010 -0500
@@ -271,6 +271,53 @@
             exc.pstr = instring
             raise exc    
 
+class TextLineList(list):
+    '''A list that "wants" to consist of separate lines of text.
+    Splits multi-line strings and flattens nested lists to 
+    achieve that.
+    Also omits blank lines, strips leading/trailing whitespace.
+    
+    >>> tll = TextLineList(['my', 'dog\\nhas', '', [' fleas', 'and\\nticks']])
+    >>> tll
+    ['my', 'dog', 'has', 'fleas', 'and', 'ticks']
+    >>> tll.append(['and', ['spiders', 'and']])
+    >>> tll
+    ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and']
+    >>> tll += 'fish'
+    >>> tll
+    ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and', 'fish']
+    '''
+    def flattened(self, texts):
+        result = []
+        if isinstance(texts, basestring):
+            result.extend(texts.splitlines())
+        else:
+            for text in texts:
+                result.extend(self.flattened(text))
+        result = [r.strip() for r in result if r.strip()]
+        return result            
+    def flatten(self):
+        list.__init__(self, self.flattened(self))
+    def __init__(self, values):
+        list.__init__(self, values)
+        self.flatten()
+    def append(self, value):
+        list.append(self, value)
+        self.flatten()
+    def extend(self, values):
+        list.extend(self, values)
+        self.flatten()
+    def __setitem__(self, idx, value):
+        list.__setitem__(self, idx, value)
+        self.flatten()
+    def __iadd__(self, value):
+        if isinstance(value, basestring):
+            self.append(value)
+        else:
+            list.__iadd__(self, value)
+            self.flatten()
+        return self
+        
 def replace_with_file_contents(fname):
     if fname:
         try:
@@ -336,7 +383,7 @@
     feedback_to_output = False          # Do include nonessentials in >, | output
     quiet = False                       # Do not suppress nonessential output
     debug = True
-    settable = '''
+    settable = TextLineList('''
         prompt
         colors                Colorized output (*nix only)
         continuation_prompt
@@ -349,7 +396,7 @@
         echo                  Echo command issued into output
         timing                Report execution times
         abbrev                Accept abbreviated commands
-        '''.splitlines()
+        ''')
     
     def poutput(self, msg):
         if msg:
--- a/flatten_lines.py	Thu Feb 11 11:25:35 2010 -0500
+++ b/flatten_lines.py	Thu Feb 11 13:07:05 2010 -0500
@@ -1,19 +1,50 @@
 import doctest
-def flatten(texts):
-    '''
-    >>> flatten([['cow', 'cat'], '', 'fish', ['bird']])
-    ['cow', 'cat', 'fish', 'bird']
+
+class TextLineList(list):
+    '''A list that "wants" to consist of separate lines of text.
+    Splits multi-line strings and flattens nested lists to 
+    achieve that.
+    Also omits blank lines, strips leading/trailing whitespace.
+    
+    >>> tll = TextLineList(['my', 'dog\\nhas', '', [' fleas', 'and\\nticks']])
+    >>> tll
+    ['my', 'dog', 'has', 'fleas', 'and', 'ticks']
+    >>> tll.append(['and', ['spiders', 'and']])
+    >>> tll
+    ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and']
+    >>> tll += 'fish'
+    >>> tll
+    ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and', 'fish']
     '''
-    result = []
-    if isinstance(texts, basestring):
-        result.extend(texts.splitlines())
-    else:
-        for text in texts:
-            result.extend(flatten(text))
-    result = [r.strip() for r in result if r.strip()]
-    return result
-doctest.testmod()
-
-class LineList(list):
-    def __add__(self, newmember):
-           
+    def flattened(self, texts):
+        result = []
+        if isinstance(texts, basestring):
+            result.extend(texts.splitlines())
+        else:
+            for text in texts:
+                result.extend(self.flattened(text))
+        result = [r.strip() for r in result if r.strip()]
+        return result            
+    def flatten(self):
+        list.__init__(self, self.flattened(self))
+    def __init__(self, values):
+        list.__init__(self, values)
+        self.flatten()
+    def append(self, value):
+        list.append(self, value)
+        self.flatten()
+    def extend(self, values):
+        list.extend(self, values)
+        self.flatten()
+    def __setitem__(self, idx, value):
+        list.__setitem__(self, idx, value)
+        self.flatten()
+    def __iadd__(self, value):
+        if isinstance(value, basestring):
+            self.append(value)
+        else:
+            list.__iadd__(self, value)
+            self.flatten()
+        return self
+        
+doctest.testmod()
\ No newline at end of file