Commits

Jonathan Eunice committed 601336a

adding text insert method

Comments (0)

Files changed (4)

 
     say.set(encoding='rot-13')
     
-Knock yourself out with `all the exciting opportunites <http://docs.python.org/library/codecs.html#standard-encodings>`_!
+Knock yourself out with `all the exciting opportunites
+<http://docs.python.org/library/codecs.html#standard-encodings>`_!
 If you really want the formatted text returned just as it is written to files,
 use the ``encoded`` option. Set to ``True`` and it returns text in the output
 encoding. Or set to an actual encoding name, and that will be the return encoding.
             callframe = callframe or inspect.currentframe().f_back
         for line in lines:
             self.append(line, callframe, interpolate)
-            
+    
+    def insert(self, i, data, callframe=None, interpolate=True, dedent=True):
+        lines = self._data_to_lines(data)
+        if dedent:
+            lines = self._dedent_lines(lines)
+        if interpolate:
+            callframe = callframe or inspect.currentframe().f_back
+            lines = [ fmt(line, _callframe=callframe) for line in lines ]
+        for line in reversed(lines):
+            self._lines.insert(i, line)
+
     def __getitem__(self, n):
         return self._lines[n]
 
         If called as a class method, creates a new text. By default, interpolates
         any {} expressions just as say and str.format do, but that can be turned off.
         """
-        t = Text() if self == Text else self  # enable Text.read_from() constructor
         lines = open(filepath).read().splitlines()
-        t.extend(lines, inspect.currentframe().f_back, interpolate=interpolate)
-        return t            
+        self.extend(lines, inspect.currentframe().f_back, interpolate=interpolate)
+        return self          
     
     def write_to(self, filepath, append=False, encoding='utf-8'):
         mode = "a" if append else "w"
 
 setup(
     name='say',
-    version=verno("0.878"),
+    version=verno("0.887"),
     author='Jonathan Eunice',
     author_email='jonathan.eunice@gmail.com',
     description='Super-simple templated printing. E.g.: say("Hello, {whoever}!", indent=1)',

test/test_text.py

     t.append('{x}', interpolate=False)
     assert t.text == '212\n{x}'
     
+def test_insert():
+    x = 'Thunderdome'
+    
+    t = Text('this\nis\n{x}')
+    t.insert(2,'not')
+    assert t.text == 'this\nis\nnot\nThunderdome'
+    
+    t1 = Text('this\nis\n{x}')
+    t1.insert(2,'not\nyet')
+    assert t1.text == 'this\nis\nnot\nyet\nThunderdome'
+    
+    t3 = Text('this\nis\n{x}')
+    t3.insert(0,'and')
+    assert t3.text == 'and\nthis\nis\nThunderdome'
+    
+    t4 = Text('this\nis\n{x}')
+    t4.insert(-1,'a')
+    assert t4.text == 'this\nis\na\nThunderdome'
+
+    t5 = Text('this\nis\n{x}')
+    t5.insert(len(t5), 'is it not?')
+    assert t5.text == 'this\nis\nThunderdome\nis it not?'
+
+    y = 'is it not?'
+    t6 = Text('this\nis\n{x}')
+    t6.insert(len(t6), '{y}')
+    assert t6.text == 'this\nis\nThunderdome\nis it not?'
+    
 def test_len():
     
     t = Text('1\n2\n\3\n')