[Solved] Python replace line in buffer

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

[Solved] Python replace line in buffer

Postby taupist » Sun Nov 24, 2013 5:29 am

I'm trying to replace a line of text in a buffer, can anyone see what I'm doing wrong?

I have a textview with a buffer "texbuff", I load a line of text from "texbuff" into a textinput called "ocredit". When I correct the text in "ocredit" I want to replace the corresponding line in "texbuff" and the textview.

Thx

Code: Select all
     def ocredit(self, old_line):
         m = self.counter
         self.ocredit = self.builder.get_object("ocredit")
         self.ocredit.modify_font(pango.FontDescription("Monospace 11"))
         buff = self.texbuff
         text = self.getbuffer(buff)
         text = text.splitlines()
         #FIXME
         o = StringIO()
         #FIXME Try: here!
         try:
             for line in text:
                 # Search for old_line
                 if str(old_line) in line:
                     new_line = self.ocredit.get_text() + '\n'
                     line.replace(old_line, new_line)
                     o.write(new_line)
          #FIXME Except: pass here!
         except:
             pass
         try:
             #FIXME Text iters here?
             #print(reversed(range(len(text))))
             if m <= range(len(text)):
                 #FIXME old_line is here
                 old_line = text[m]
                 self.ocredit.set_text(text[m])
                 self.counter += 1
         except:
             self.builder.get_object("hbox4").hide()
             return


My function now looks like this.

Code: Select all
    def ocredit(self, old_line):
        m = self.counter
        self.ocredit = self.builder.get_object("ocredit")
        self.ocredit.modify_font(pango.FontDescription("Monospace 11"))
        text = self.getbuffer(self.texbuff)
        text = text.splitlines()
        if self.old_line != '':
            try:
                new_line = self.ocredit.get_text()
                if self.old_line != new_line:
                    text = [line.replace(self.old_line,
                            new_line) for line in text]
                    n = 0
                    newtext = []
                    for line in text:
                        if n <= range(len(text)):
                            line = text[n] + '\n'
                        newtext.append(line)
                        n = n + 1
                newtext = '%s' % ''.join(map(str, newtext))
                self.texbuff.set_text(newtext)
            except:
                pass
        try:
            #FIXME Text iters here?
            #print(reversed(range(len(text))))
            if m <= range(len(text)):
                self.old_line = text[m]
                #print(old_line)
                self.ocredit.set_text(text[m])
                self.counter += 1
        except:
            self.builder.get_object("hbox4").hide()
            return
Last edited by taupist on Tue Nov 26, 2013 4:41 am, edited 2 times in total.
User avatar
taupist
 
Posts: 20
Joined: Sat Jan 14, 2006 6:24 pm
Location: Idaho, USA

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests