-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
sphinxthread.py
91 lines (83 loc) · 3.47 KB
/
sphinxthread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
"""SphinxThread used in richtext help plugin."""
# Third party imports
from qtpy.QtCore import QThread, Signal
# Local Imports
from spyder.config.base import _
from spyder.py3compat import to_text_string
from spyder.plugins.help.utils.sphinxify import (CSS_PATH, generate_context,
sphinxify)
class SphinxThread(QThread):
"""
A worker thread for handling rich text rendering.
Parameters
----------
doc : str or dict
A string containing a raw rst text or a dict containing
the doc string components to be rendered.
See spyder.utils.dochelpers.getdoc for description.
context : dict
A dict containing the substitution variables for the
layout template
html_text_no_doc : unicode
Text to be rendered if doc string cannot be extracted.
math_option : bool
Use LaTeX math rendering.
"""
# Signals
error_msg = Signal(str)
html_ready = Signal(str)
def __init__(self, parent, html_text_no_doc='', css_path=CSS_PATH):
super(SphinxThread, self).__init__(parent)
self.doc = None
self.context = None
self.html_text_no_doc = html_text_no_doc
self.math_option = False
self.css_path = css_path
def render(self, doc, context=None, math_option=False, img_path='',
css_path=CSS_PATH):
"""Start thread to render a given documentation"""
# If the thread is already running wait for it to finish before
# starting it again.
if self.wait():
self.doc = doc
self.context = context
self.math_option = math_option
self.img_path = img_path
self.css_path = css_path
# This causes run() to be executed in separate thread
self.start()
def run(self):
html_text = self.html_text_no_doc
doc = self.doc
if doc is not None:
if type(doc) is dict and 'docstring' in doc.keys():
try:
context = generate_context(name=doc['name'],
argspec=doc['argspec'],
note=doc['note'],
math=self.math_option,
img_path=self.img_path,
css_path=self.css_path)
html_text = sphinxify(doc['docstring'], context)
if doc['docstring'] == '':
if any([doc['name'], doc['argspec'], doc['note']]):
msg = _("No further documentation available")
html_text += '<div class="hr"></div>'
else:
msg = _("No documentation available")
html_text += '<div id="doc-warning">%s</div>' % msg
except Exception as error:
self.error_msg.emit(to_text_string(error))
return
elif self.context is not None:
try:
html_text = sphinxify(doc, self.context)
except Exception as error:
self.error_msg.emit(to_text_string(error))
return
self.html_ready.emit(html_text)