Example #1
0
def insert_docs(buf, iter, obj, bold_tag):
    """Insert documentation about obj into a gtk.TextBuffer

    @param buf: the buffer to insert the documentation into
    @param iter: the location to insert the documentation
    @param obj: the object to get documentation about
    @param bold_tag: the tag to use for bold text, such as headings

    """
    
    # If the routine is an instance, we get help on the type instead
    if is_data_object(obj):
        obj = type(obj)
        
    name = getattr(obj, '__name__', None)
    document = pydoc.text.document(obj, name)

    # pydoc.text.document represents boldface with overstrikes, we need to
    # reverse engineer this and find the spans of bold text
    pos = 0
    while True:
        m = BOLD_RE.search(document, pos)
        if m is None:
            # Strip the trailing newline; this isn't very justifiable in general terms,
            # but matches what we need in Reinteract
            if document.endswith("\n"):
                buf.insert(iter, document[pos:-1])
            else:
                buf.insert(iter, document[pos:])
            break

        buf.insert(iter, document[pos:m.start()])
        insert_with_tag(buf, iter, STRIP_BOLD_RE.sub(lambda m: m.group(1), m.group()), bold_tag)
        pos = m.end()
Example #2
0
def format_docs(obj, callback):
    """Gets the documentation for a given object, and format it simply
      with a distinction between bold and normal

    @param obj: the object to get documentation about
    @param callback: callback called for each segment of text. Passed two
      arguments; the text of the segment and a boolean that is True if the
      text should be formatted in bold

    """
    
    # If the routine is an instance, we get help on the type instead
    if is_data_object(obj):
        obj = type(obj)
        
    name = getattr(obj, '__name__', None)
    document = textdocshort.document(obj, name)

    # pydoc.text.document represents boldface with overstrikes, we need to
    # reverse engineer this and find the spans of bold text
    pos = 0
    while True:
        m = BOLD_RE.search(document, pos)
        if m is None:
            # Strip the trailing newline; this isn't very justifiable in general terms,
            # but matches what we need in Reinteract
            if document.endswith("\n"):
                callback(document[pos:-1], False)
            else:
                callback(document[pos:], False)
            break

        callback(document[pos:m.start()], False)
        callback(STRIP_BOLD_RE.sub(lambda m: m.group(1), m.group()), True)
        pos = m.end()
Example #3
0
    def set_target(self, target):
        """Set the object that the popup is showing documentation about"""
        
        if target is self.__target:
            return

        self.__target = target
        buf = self.__view.get_buffer()
        buf.delete(buf.get_start_iter(), buf.get_end_iter())

        if target != None:
            if data_format.is_data_object(target):
                data_format.insert_formatted(buf, buf.get_start_iter(), target, self.__heading_type_tag, self.__inline_type_tag, self.__value_tag)
            else:
                doc_format.insert_docs(buf, buf.get_start_iter(), target, self.__bold_tag)

            buf.place_cursor(buf.get_start_iter())

        self.__scrollbar.get_adjustment().set_value(0.)
    def __update_doc_popup(self):
        if not self.showing:
            self.__doc_popup.popdown()
            return

        model, iter = self.__tree.get_selection().get_selected()
        if not iter:
            self.__doc_popup.popdown()
            return

        obj = model.get_value(iter, 2)

        # Long term it would be nice to preview the value of the
        # object, but it's distracting to show the class docs on int
        # for every integer constant, etc, which is what the DocPopup
        # does currently.
        if (obj == None or is_data_object(obj)):
            self.__doc_popup.popdown()
            return
        
        self.__doc_popup.set_target(obj)
        self.__doc_popup.popup()
    def __update_doc_popup(self):
        if not self.showing:
            self.__doc_popup.popdown()
            return

        model, iter = self.__tree.get_selection().get_selected()
        if not iter:
            self.__doc_popup.popdown()
            return

        obj = model.get_value(iter, 2)

        # Long term it would be nice to preview the value of the
        # object, but it's distracting to show the class docs on int
        # for every integer constant, etc, which is what the DocPopup
        # does currently.
        if (obj is None or is_data_object(obj)):
            self.__doc_popup.popdown()
            return
        
        self.__doc_popup.set_target(obj)
        self.__doc_popup.popup()
Example #6
0
    def set_target(self, target):
        """Set the object that the popup is showing documentation about"""

        if target is self.__target:
            return

        self.__target = target
        buf = self.__view.get_buffer()
        buf.delete(buf.get_start_iter(), buf.get_end_iter())

        if target is not None:
            if data_format.is_data_object(target):
                data_format.insert_formatted(buf, buf.get_start_iter(), target,
                                             self.__heading_type_tag,
                                             self.__inline_type_tag,
                                             self.__value_tag)
            else:
                doc_format.insert_docs(buf, buf.get_start_iter(), target,
                                       self.__bold_tag)

            buf.place_cursor(buf.get_start_iter())

        self.__scrollbar.get_adjustment().set_value(0.)