예제 #1
0
    def expect(expected):
        si = StringIO()
        i = buf.get_start_iter()
        while True:
            end = i.copy()
            if not end.ends_line():
                end.forward_to_line_end()
            text = reunicode.decode(buf.get_slice(i, end))

            line, _ = buf.iter_to_pos(i, adjust=ADJUST_NONE)
            if line is not None:
                chunk = buf.worksheet.get_chunk(line)
            else:
                chunk = None

            if chunk and isinstance(chunk, StatementChunk):
                if line == chunk.start:
                    si.write(">>> ")
                else:
                    si.write("... ")

            si.write(text)

            if _forward_line(i):
                si.write("\n")
            else:
                break

        result = si.getvalue()
        if not result == expected:
            raise AssertionError("\nGot:\n%s\nExpected:\n%s" % (result, expected))
예제 #2
0
    def do_insert_text(self, location, text, text_len):
        if self.__in_modification_count > 0:
            gtk.TextBuffer.do_insert_text(self, location, text, text_len)
            return

        line, offset = self.iter_to_pos(location, adjust=ADJUST_NONE)
        if line is None:
            return

        with _RevalidateIters(self, location):
            # If we get "unsafe" text from GTK+, it will be a non-BMP character.
            # Inserting this as an escape isn't entirely unexpected and is
            # the best we can do.
            self.worksheet.insert(line, offset, reunicode.decode(text[0:text_len], escape="True"))
예제 #3
0
    def do_insert_text(self, location, text, text_len):
        if self.__in_modification_count > 0:
            gtk.TextBuffer.do_insert_text(self, location, text, text_len)
            return

        line, offset = self.iter_to_pos(location, adjust=ADJUST_NONE)
        if line is None:
            return

        with _RevalidateIters(self, location):
            # If we get "unsafe" text from GTK+, it will be a non-BMP character.
            # Inserting this as an escape isn't entirely unexpected and is
            # the best we can do.
            self.worksheet.insert(
                line, offset, reunicode.decode(text[0:text_len],
                                               escape="True"))
예제 #4
0
    def load(self, filename, escape=False):
        """Load a file from disk into the worksheet. Can raise IOError if the
        file cannot be read, and reunicode.ConversionError if the file contains
        invalid characters. (reunicode.ConversionError will not be raised if
        escape is True)

        @param filename the file to load
        @param escape if true, invalid byte and character sequences in the input
           will be converted into \\x<nn> and \\u<nnnn> escape sequences.

        """
        f = open(filename)
        text = f.read()
        f.close()

        self.__do_clear()
        self.insert(0, 0, reunicode.decode(text, escape=escape))
        # A bit of a hack - we assume that if escape was passed we *did* escape.
        # this is the way that things work currently - first the GUI loads with
        # escape=False, and if that fails, prompts the user and loads with escape=True
        self.__set_filename_and_modified(filename, escape)
        self.__undo_stack.clear()
예제 #5
0
    def load(self, filename, escape=False):
        """Load a file from disk into the worksheet. Can raise IOError if the
        file cannot be read, and reunicode.ConversionError if the file contains
        invalid characters. (reunicode.ConversionError will not be raised if
        escape is True)

        @param filename the file to load
        @param escape if true, invalid byte and character sequences in the input
           will be converted into \\x<nn> and \\u<nnnn> escape sequences.

        """
        if not isinstance(filename, unicode):
            raise ValueError("filename argument must be unicode")
        f = open(filename)
        text = f.read()
        f.close()

        self.__do_clear()
        self.insert(0, 0, reunicode.decode(text, escape=escape))
        # A bit of a hack - we assume that if escape was passed we *did* escape.
        # this is the way that things work currently - first the GUI loads with
        # escape=False, and if that fails, prompts the user and loads with escape=True
        self.__set_filename_and_modified(filename, escape)
        self.__undo_stack.clear()