~ubuntu-branches/ubuntu/wily/spyder/wily-proposed

« back to all changes in this revision

Viewing changes to spyderlib/plugins/inspector.py

  • Committer: Package Import Robot
  • Author(s): Ghislain Antony Vaillant, Ghislain Antony Vaillant, Picca Frédéric-Emmanuel
  • Date: 2014-10-19 11:42:57 UTC
  • mfrom: (1.2.8)
  • Revision ID: package-import@ubuntu.com-20141019114257-st1rz4fmmscgphhm
Tags: 2.3.1+dfsg-1
* Team upload

[Ghislain Antony Vaillant]
* New upstream release. (Closes: #765963)
* Bump Standards-Version to 3.9.6 (no changes required).
* d/control: fix pedantic lintian warning regarding capitalization in
  packages' description.

[Picca Frédéric-Emmanuel]
* Update the homepage now that upstream moved to bitbucket.
* debian/copyright
  - updated for 2.3.1 version
* debian/control
  + Recommends: python-pandas and python3-pandas

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
                                QActionGroup, QFontDialog, QWidget, QComboBox,
12
12
                                QLineEdit, QMessageBox)
13
13
from spyderlib.qt.QtCore import SIGNAL, QUrl, QThread
 
14
from spyderlib.qt.QtWebKit import QWebPage
14
15
 
15
16
import re
16
17
import os.path as osp
19
20
 
20
21
# Local imports
21
22
from spyderlib import dependencies
22
 
from spyderlib.baseconfig import get_conf_path, _
 
23
from spyderlib.baseconfig import get_conf_path, get_module_source_path, _
23
24
from spyderlib.ipythonconfig import IPYTHON_QT_INSTALLED
24
25
from spyderlib.config import CONF
25
26
from spyderlib.guiconfig import get_color_scheme, get_font, set_font
280
281
    
281
282
    Parameters
282
283
    ----------
283
 
    doc : dict
284
 
        A dict containing the doc string components to be rendered.
 
284
    doc : str or dict
 
285
        A string containing a raw rst text or a dict containing
 
286
        the doc string components to be rendered.
285
287
        See spyderlib.utils.dochelpers.getdoc for description.
 
288
    context : dict
 
289
        A dict containing the substitution variables for the
 
290
        layout template
286
291
    html_text_no_doc : unicode
287
292
        Text to be rendered if doc string cannot be extracted.
288
293
    math_option : bool
291
296
    """
292
297
    def __init__(self, html_text_no_doc=''):
293
298
        super(SphinxThread, self).__init__()
 
299
        self.doc = None
 
300
        self.context = None
294
301
        self.html_text_no_doc = html_text_no_doc
295
 
        self.doc = {}
296
302
        self.math_option = False
297
303
        
298
 
    def render(self, doc, math_option=False):
299
 
        """Start thread to render given doc string dictionary"""
 
304
    def render(self, doc, context=None, math_option=False):
 
305
        """Start thread to render a given documentation"""
300
306
        # If the thread is already running wait for it to finish before
301
307
        # starting it again.
302
308
        if self.wait():
303
309
            self.doc = doc
 
310
            self.context = context
304
311
            self.math_option = math_option
305
312
            # This causes run() to be executed in separate thread
306
313
            self.start()
308
315
    def run(self):
309
316
        html_text = self.html_text_no_doc
310
317
        doc = self.doc
311
 
        if doc is not None and doc['docstring'] != '':
312
 
            try:
313
 
                context = generate_context(name=doc['name'],
314
 
                                           argspec=doc['argspec'],
315
 
                                           note=doc['note'],
316
 
                                           math=self.math_option)
317
 
                html_text = sphinxify(doc['docstring'], context)
318
 
            except Exception as error:
319
 
                self.emit(SIGNAL('error_msg(QString)'),
320
 
                          to_text_string(error))
321
 
                return
 
318
        if doc is not None:
 
319
            if type(doc) is dict and 'docstring' in doc.keys() \
 
320
              and doc['docstring'] != '':
 
321
                try:
 
322
                    context = generate_context(name=doc['name'],
 
323
                                               argspec=doc['argspec'],
 
324
                                               note=doc['note'],
 
325
                                               math=self.math_option)
 
326
                    html_text = sphinxify(doc['docstring'], context)
 
327
                except Exception as error:
 
328
                    self.emit(SIGNAL('error_msg(QString)'),
 
329
                              to_text_string(error))
 
330
                    return
 
331
            elif self.context is not None:
 
332
                try:
 
333
                    html_text = sphinxify(doc, self.context)
 
334
                except Exception as error:
 
335
                    self.emit(SIGNAL('error_msg(QString)'),
 
336
                              to_text_string(error))
 
337
                    return
322
338
        self.emit(SIGNAL('html_ready(QString)'), html_text)
323
339
 
324
340
 
400
416
        layout_edit.addWidget(self.object_edit)
401
417
        self.combo.setMaxCount(self.get_option('max_history_entries'))
402
418
        self.combo.addItems( self.load_history() )
 
419
        self.combo.setItemText(0, '')
403
420
        self.connect(self.combo, SIGNAL("valid(bool)"),
404
421
                     lambda valid: self.force_refresh())
405
422
        
474
491
                         self._on_sphinx_thread_html_ready)
475
492
            self.connect(self._sphinx_thread, SIGNAL('error_msg(QString)'),
476
493
                         self._on_sphinx_thread_error_msg)
 
494
        
 
495
        # Render internal links
 
496
        view = self.rich_text.webview
 
497
        view.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
 
498
        view.linkClicked.connect(self.handle_link_clicks)
477
499
 
478
500
        self._starting_up = True
479
501
            
601
623
                self.switch_to_rich_text()
602
624
            else:
603
625
                self.switch_to_plain_text()
604
 
    
605
 
    def show_intro_message(self):
606
 
        intro_message = _("Here you can get help of any object by pressing "
607
 
                          "%s in front of it, either on the Editor or the "
608
 
                          "Console.%s"
609
 
                          "Help can also be shown automatically after writing "
610
 
                          "a left parenthesis next to an object. You can "
611
 
                          "activate this behavior in %s.")
612
 
        prefs = _("Preferences > Object Inspector")
613
 
        if self.is_rich_text_mode():
614
 
            title = _("Usage")
615
 
            intro_message = intro_message % ("<b>Ctrl+I</b>", "<br><br>",
616
 
                                             "<i>"+prefs+"</i>")
617
 
            self.set_rich_text_html(usage(title, intro_message),
618
 
                                    QUrl.fromLocalFile(CSS_PATH))
619
 
        else:
620
 
            install_sphinx = "\n\n%s" % _("Please consider installing Sphinx "
621
 
                                          "to get documentation rendered in "
622
 
                                          "rich text.")
623
 
            intro_message = intro_message % ("Ctrl+I", "\n\n", prefs)
624
 
            intro_message += install_sphinx
625
 
            self.set_plain_text(intro_message, is_code=False)
626
626
        
627
627
    #------ Public API (related to rich/plain text widgets) --------------------
628
628
    @property
719
719
        """Set rich text"""
720
720
        self.rich_text.set_html(html_text, base_url)
721
721
        self.save_text([self.rich_text.set_html, html_text, base_url])
 
722
    
 
723
    def show_intro_message(self):
 
724
        intro_message = _("Here you can get help of any object by pressing "
 
725
                          "%s in front of it, either on the Editor or the "
 
726
                          "Console.%s"
 
727
                          "Help can also be shown automatically after writing "
 
728
                          "a left parenthesis next to an object. You can "
 
729
                          "activate this behavior in %s.")
 
730
        prefs = _("Preferences > Object Inspector")
 
731
        if self.is_rich_text_mode():
 
732
            title = _("Usage")
 
733
            tutorial_message = _("New to Spyder? Read our")
 
734
            tutorial = _("tutorial")
 
735
            intro_message = intro_message % ("<b>Ctrl+I</b>", "<br><br>",
 
736
                                             "<i>"+prefs+"</i>")
 
737
            self.set_rich_text_html(usage(title, intro_message,
 
738
                                          tutorial_message, tutorial),
 
739
                                    QUrl.fromLocalFile(CSS_PATH))
 
740
        else:
 
741
            install_sphinx = "\n\n%s" % _("Please consider installing Sphinx "
 
742
                                          "to get documentation rendered in "
 
743
                                          "rich text.")
 
744
            intro_message = intro_message % ("Ctrl+I", "\n\n", prefs)
 
745
            intro_message += install_sphinx
 
746
            self.set_plain_text(intro_message, is_code=False)
 
747
    
 
748
    def show_rich_text(self, text, collapse=False, img_path=''):
 
749
        """Show text in rich mode"""
 
750
        self.visibility_changed(True)
 
751
        self.raise_()
 
752
        self.switch_to_rich_text()
 
753
        context = generate_context(collapse=collapse, img_path=img_path)
 
754
        self.render_sphinx_doc(text, context)
 
755
    
 
756
    def show_plain_text(self, text):
 
757
        """Show text in plain mode"""
 
758
        self.visibility_changed(True)
 
759
        self.raise_()
 
760
        self.switch_to_plain_text()
 
761
        self.set_plain_text(text, is_code=False)
 
762
    
 
763
    def show_tutorial(self):
 
764
        tutorial_path = get_module_source_path('spyderlib.utils.inspector')
 
765
        img_path = osp.join(tutorial_path, 'static', 'images')
 
766
        tutorial = osp.join(tutorial_path, 'tutorial.rst')
 
767
        text = open(tutorial).read()
 
768
        if sphinxify is not None:
 
769
            self.show_rich_text(text, collapse=True, img_path=img_path)
 
770
        else:
 
771
            self.show_plain_text(text)
 
772
 
 
773
    def handle_link_clicks(self, url):
 
774
        url = to_text_string(url.toString())
 
775
        if url == "spy://tutorial":
 
776
            self.show_tutorial()
 
777
        elif url.startswith('http'):
 
778
            programs.start_file(url)
 
779
        else:
 
780
            self.rich_text.webview.load(QUrl(url))
722
781
        
723
782
    #------ Public API ---------------------------------------------------------
724
783
    def set_external_console(self, external_console):
876
935
                self.shell = self.internal_shell
877
936
        return self.shell
878
937
        
879
 
    def render_sphinx_doc(self, doc):
 
938
    def render_sphinx_doc(self, doc, context=None):
880
939
        """Transform doc string dictionary to HTML and show it"""
881
940
        # Math rendering option could have changed
882
 
        self._sphinx_thread.render(doc, self.get_option('math'))
 
941
        self._sphinx_thread.render(doc, context, self.get_option('math'))
883
942
        
884
943
    def _on_sphinx_thread_html_ready(self, html_text):
885
944
        """Set our sphinx documentation based on thread result"""