Python sphinx.errors.ExtensionError() Examples
The following are 28
code examples of sphinx.errors.ExtensionError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sphinx.errors
, or try the search function
.
Example #1
Source File: test_shortname.py From sphinxcontrib-disqus with MIT License | 6 votes |
def test(monkeypatch, tmpdir, tail, expected_error): """Test valid and invalid values.""" tmpdir.join('conf.py').write(BASE_CONFIG.format(py.path.local(__file__).join('..', '..'))) tmpdir.join('conf.py').write(tail, mode='a') tmpdir.join('index.rst').write('====\nMain\n====\n\n.. toctree::\n :maxdepth: 2\n.. disqus::') monkeypatch.setattr(directives, '_directives', getattr(directives, '_directives').copy()) monkeypatch.setattr(roles, '_roles', getattr(roles, '_roles').copy()) srcdir = confdir = str(tmpdir) outdir = tmpdir.join('_build', 'html') doctreedir = outdir.join('doctrees').ensure(dir=True, rec=True) app = application.Sphinx(srcdir, confdir, str(outdir), str(doctreedir), 'html') if not expected_error: app.builder.build_all() html_body = outdir.join('index.html').read() disqus_div = re.findall(r'(<div[^>]+ id="disqus_thread"[^>]*></div>)', html_body)[0] assert 'data-disqus-shortname="good"' in disqus_div return with pytest.raises(errors.ExtensionError) as exc: app.builder.build_all() assert expected_error == exc.value.args[0]
Example #2
Source File: docs_resolv.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _get_data(url): """Get data over http(s) or from a local file.""" if urllib_parse.urlparse(url).scheme in ('http', 'https'): user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11' # noqa: E501 headers = {'User-Agent': user_agent} req = urllib_request.Request(url, None, headers) resp = urllib_request.urlopen(req) encoding = resp.headers.get('content-encoding', 'plain') data = resp.read() if encoding == 'gzip': data = gzip.GzipFile(fileobj=BytesIO(data)).read() elif encoding != 'plain': raise ExtensionError('unknown encoding %r' % (encoding,)) data = data.decode('utf-8') else: with codecs.open(url, mode='r', encoding='utf-8') as fid: data = fid.read() return data
Example #3
Source File: roles.py From sphinxcontrib-django with Apache License 2.0 | 6 votes |
def setup(app): """Allow this module to be used as Sphinx extension. This is also called from the top-level ``__init__.py``. It adds the rules to allow :django:setting:`SITE_ID` to work. :type app: sphinx.application.Sphinx """ try: app.add_crossref_type( directivename="setting", rolename="setting", indextemplate="pair: %s; setting", ) except ExtensionError as e: logger.warning("Unable to register :django:setting:`..`: " + str(e))
Example #4
Source File: scrapers.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __iter__(self): """Iterate over paths. Returns ------- paths : iterable of str This enables the use of this Python pattern:: >>> for epoch in epochs: # doctest: +SKIP >>> print(epoch) # doctest: +SKIP Where ``epoch`` is given by successive outputs of :func:`mne.Epochs.next`. """ # we should really never have 1e6, let's prevent some user pain for ii in range(self._stop): yield self.next() else: raise ExtensionError('Generated over %s images' % (self._stop,))
Example #5
Source File: backreferences.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _thumbnail_div(target_dir, src_dir, fname, snippet, title, is_backref=False, check=True): """Generate RST to place a thumbnail in a gallery.""" thumb, _ = _find_image_ext( os.path.join(target_dir, 'images', 'thumb', 'sphx_glr_%s_thumb.png' % fname[:-3])) if check and not os.path.isfile(thumb): # This means we have done something wrong in creating our thumbnail! raise ExtensionError('Could not find internal sphinx-gallery thumbnail' ' file:\n%s' % (thumb,)) thumb = os.path.relpath(thumb, src_dir) full_dir = os.path.relpath(target_dir, src_dir) # Inside rst files forward slash defines paths thumb = thumb.replace(os.sep, "/") ref_name = os.path.join(full_dir, fname).replace(os.path.sep, '_') template = BACKREF_THUMBNAIL_TEMPLATE if is_backref else THUMBNAIL_TEMPLATE return template.format(snippet=escape(snippet), thumbnail=thumb, title=title, ref_name=ref_name)
Example #6
Source File: test_gen_rst.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_gen_dir_rst(gallery_conf, fakesphinxapp, ext): """Test gen_dir_rst.""" print(os.listdir(gallery_conf['examples_dir'])) fname_readme = os.path.join(gallery_conf['src_dir'], 'README.txt') with open(fname_readme, 'wb') as fid: fid.write(u"Testing\n=======\n\nÓscar here.".encode('utf-8')) fname_out = os.path.splitext(fname_readme)[0] + ext if fname_readme != fname_out: shutil.move(fname_readme, fname_out) args = (gallery_conf['src_dir'], gallery_conf['gallery_dir'], gallery_conf, []) if ext == '.bad': # not found with correct ext with pytest.raises(ExtensionError, match='does not have a README'): generate_dir_rst(*args) else: out = generate_dir_rst(*args) assert u"Óscar here" in out[0]
Example #7
Source File: test_py_source_parser.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_get_docstring_and_rest(unicode_sample, tmpdir, monkeypatch): docstring, rest, lineno, _ = sg._get_docstring_and_rest(unicode_sample) assert u'Únicode' in docstring assert u'heiß' in rest # degenerate fname = op.join(str(tmpdir), 'temp') with open(fname, 'w') as fid: fid.write('print("hello")\n') with pytest.raises(ExtensionError, match='Could not find docstring'): sg._get_docstring_and_rest(fname) with open(fname, 'w') as fid: fid.write('print hello\n') assert sg._get_docstring_and_rest(fname)[0] == sg.SYNTAX_ERROR_DOCSTRING monkeypatch.setattr(sg, 'parse_source_file', lambda x: ('', None)) with pytest.raises(ExtensionError, match='only supports modules'): sg._get_docstring_and_rest('')
Example #8
Source File: test_backreferences.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_thumbnail_div(content, tooltip, is_backref): """Test if the thumbnail div generates the correct string.""" with pytest.raises(ExtensionError, match='internal sphinx-gallery thumb'): html_div = sg._thumbnail_div('fake_dir', '', 'test_file.py', '<"test">', '<"title">') content = _sanitize_rst(content) title = 'test title' html_div = sg._thumbnail_div('fake_dir', '', 'test_file.py', content, title, is_backref=is_backref, check=False) if is_backref: extra = """ .. only:: not html * :ref:`sphx_glr_fake_dir_test_file.py`""" else: extra = '' reference = REFERENCE.format(tooltip, extra) assert html_div == reference
Example #9
Source File: custom_html_writer.py From google-resumable-media-python with Apache License 2.0 | 5 votes |
def visit_literal_block(self, node): """Visit a ``literal_block`` node. This verifies the state of each literal / code block. """ language = node.attributes.get('language', '') test_type = node.attributes.get('testnodetype', '') if test_type != 'doctest': if language.lower() in ('', 'python'): msg = _LITERAL_ERR_TEMPLATE.format( node.rawsource, language, test_type) raise errors.ExtensionError(msg) # The base classes are not new-style, so we can't use super(). return html.HTMLTranslator.visit_literal_block(self, node)
Example #10
Source File: test_execute.py From jupyter-sphinx with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_raises(doctree): source = """ .. jupyter-execute:: raise ValueError() """ with pytest.raises(ExtensionError): doctree(source) source = """ .. jupyter-execute:: :raises: raise ValueError() """ tree = doctree(source) (cell,) = tree.traverse(JupyterCellNode) (cellinput, celloutput) = cell.children assert "ValueError" in celloutput.children[0].rawsource source = """ .. jupyter-execute:: :raises: KeyError, ValueError raise ValueError() """ tree = doctree(source) (cell,) = tree.traverse(JupyterCellNode) (cellinput, celloutput) = cell.children assert "ValueError" in celloutput.children[0].rawsource
Example #11
Source File: execute.py From jupyter-sphinx with BSD 3-Clause "New" or "Revised" License | 5 votes |
def execute_cells(kernel_name, cells, execute_kwargs): """Execute Jupyter cells in the specified kernel and return the notebook.""" notebook = blank_nb(kernel_name) notebook.cells = cells # Modifies 'notebook' in-place try: executenb(notebook, **execute_kwargs) except Exception as e: raise ExtensionError("Notebook execution failed", orig_exc=e) return notebook
Example #12
Source File: utils.py From jupyter-sphinx with BSD 3-Clause "New" or "Revised" License | 5 votes |
def blank_nb(kernel_name): try: spec = get_kernel_spec(kernel_name) except NoSuchKernel as e: raise ExtensionError("Unable to find kernel", orig_exc=e) return nbformat.v4.new_notebook( metadata={ "kernelspec": { "display_name": spec.display_name, "language": spec.language, "name": kernel_name, } } )
Example #13
Source File: __init__.py From jupyter-sphinx with BSD 3-Clause "New" or "Revised" License | 5 votes |
def halt(self, node): raise ExtensionError( ( "Rendering encountered a node type that should " "have been removed before rendering: %s" % type(node) ) ) # Renders the children of a container
Example #14
Source File: google_analytics.py From tick with BSD 3-Clause "New" or "Revised" License | 5 votes |
def check_config(app): if not app.config.googleanalytics_id: raise ExtensionError("'googleanalytics_id' config value must be set " "for ga statistics to function properly.")
Example #15
Source File: scrapers.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 5 votes |
def save_figures(block, block_vars, gallery_conf): """Save all open figures of the example code-block. Parameters ---------- block : tuple A tuple containing the (label, content, line_number) of the block. block_vars : dict Dict of block variables. gallery_conf : dict Contains the configuration of Sphinx-Gallery Returns ------- images_rst : str rst code to embed the images in the document. """ image_path_iterator = block_vars['image_path_iterator'] all_rst = u'' prev_count = len(image_path_iterator) for scraper in gallery_conf['image_scrapers']: rst = scraper(block, block_vars, gallery_conf) if not isinstance(rst, str): raise ExtensionError('rst from scraper %r was not a string, ' 'got type %s:\n%r' % (scraper, type(rst), rst)) n_new = len(image_path_iterator) - prev_count for ii in range(n_new): current_path, _ = _find_image_ext( image_path_iterator.paths[prev_count + ii]) if not os.path.isfile(current_path): raise ExtensionError( 'Scraper %s did not produce expected image:' '\n%s' % (scraper, current_path)) all_rst += rst return all_rst
Example #16
Source File: scrapers.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _import_matplotlib(): """Import matplotlib safely.""" # make sure that the Agg backend is set before importing any # matplotlib import matplotlib matplotlib.use('agg') matplotlib_backend = matplotlib.get_backend().lower() filterwarnings("ignore", category=UserWarning, message='Matplotlib is currently using agg, which is a' ' non-GUI backend, so cannot show the figure.') if matplotlib_backend != 'agg': raise ExtensionError( "Sphinx-Gallery relies on the matplotlib 'agg' backend to " "render figures and write them to files. You are " "currently using the {} backend. Sphinx-Gallery will " "terminate the build now, because changing backends is " "not well supported by matplotlib. We advise you to move " "sphinx_gallery imports before any matplotlib-dependent " "import. Moving sphinx_gallery imports at the top of " "your conf.py file should fix this issue" .format(matplotlib_backend)) import matplotlib.pyplot as plt return matplotlib, plt
Example #17
Source File: conftest.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 5 votes |
def req_pil(): try: _get_image() except ExtensionError: pytest.skip('Test requires pillow')
Example #18
Source File: test_scrapers.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_iterator(): """Test ImagePathIterator.""" ipi = ImagePathIterator('foo{0}') ipi._stop = 10 with pytest.raises(ExtensionError, match='10 images'): for ii in ipi: pass
Example #19
Source File: test_full.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_junit(sphinx_app, tmpdir): out_dir = sphinx_app.outdir junit_file = op.join(out_dir, 'sphinx-gallery', 'junit-results.xml') assert op.isfile(junit_file) with codecs.open(junit_file, 'r', 'utf-8') as fid: contents = fid.read() assert contents.startswith('<?xml') assert 'errors="0" failures="0"' in contents assert 'tests="%d"' % (N_TOT,) in contents assert 'local_module' not in contents # it's not actually run as an ex assert 'expected example failure' in contents assert '<failure message' not in contents src_dir = sphinx_app.srcdir new_src_dir = op.join(str(tmpdir), 'src') shutil.copytree(src_dir, new_src_dir) del src_dir new_out_dir = op.join(new_src_dir, '_build', 'html') new_toctree_dir = op.join(new_src_dir, '_build', 'toctrees') passing_fname = op.join(new_src_dir, 'examples', 'plot_numpy_matplotlib.py') failing_fname = op.join(new_src_dir, 'examples', 'future', 'plot_future_imports_broken.py') shutil.move(passing_fname, passing_fname + '.temp') shutil.move(failing_fname, passing_fname) shutil.move(passing_fname + '.temp', failing_fname) with docutils_namespace(): app = Sphinx(new_src_dir, new_src_dir, new_out_dir, new_toctree_dir, buildername='html', status=StringIO()) # need to build within the context manager # for automodule and backrefs to work with pytest.raises(ExtensionError, match='Here is a summary of the '): app.build(False, []) junit_file = op.join(new_out_dir, 'sphinx-gallery', 'junit-results.xml') assert op.isfile(junit_file) with codecs.open(junit_file, 'r', 'utf-8') as fid: contents = fid.read() assert 'errors="0" failures="2"' in contents assert 'tests="2"' in contents # this time we only ran the two stale files assert '<failure message="RuntimeError: Forcing' in contents assert 'Passed even though it was marked to fail' in contents
Example #20
Source File: test_gen_gallery.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_examples_not_expected_to_pass(sphinx_app_wrapper): with pytest.raises(ExtensionError) as excinfo: sphinx_app_wrapper.build_sphinx_app() assert "expected to fail, but not failing" in str(excinfo.value)
Example #21
Source File: test_gen_gallery.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_failing_examples_raise_exception(sphinx_app_wrapper): example_dir = os.path.join(sphinx_app_wrapper.srcdir, 'src') with codecs.open(os.path.join(example_dir, 'plot_3.py'), 'a', encoding='utf-8') as fid: fid.write('raise SyntaxError') with pytest.raises(ExtensionError) as excinfo: sphinx_app_wrapper.build_sphinx_app() assert "Unexpected failing examples" in str(excinfo.value)
Example #22
Source File: test_gen_gallery.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_default_config(sphinx_app_wrapper): """Test the default Sphinx-Gallery configuration is loaded if only the extension is added to Sphinx""" sphinx_app = sphinx_app_wrapper.create_sphinx_app() cfg = sphinx_app.config assert cfg.project == "Sphinx-Gallery <Tests>" # no duplicate values allowed The config is present already with pytest.raises(ExtensionError) as excinfo: sphinx_app.add_config_value('sphinx_gallery_conf', 'x', True) assert 'already present' in str(excinfo.value)
Example #23
Source File: utils.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _get_image(): try: from PIL import Image except ImportError as exc: # capture the error for the modern way try: import Image except ImportError: raise ExtensionError( 'Could not import pillow, which is required ' 'to rescale images (e.g., for thumbnails): %s' % (exc,)) return Image
Example #24
Source File: gen_rst.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _get_readme(dir_, gallery_conf, raise_error=True): extensions = ['.txt'] + sorted(gallery_conf['app'].config['source_suffix']) for ext in extensions: for fname in ('README', 'readme'): fpth = os.path.join(dir_, fname + ext) if os.path.isfile(fpth): return fpth if raise_error: raise ExtensionError( "Example directory {0} does not have a README file with one " "of the expected file extensions {1}. Please write one to " "introduce your gallery.".format(dir_, extensions)) return None
Example #25
Source File: gen_rst.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 5 votes |
def extract_intro_and_title(filename, docstring): """Extract and clean the first paragraph of module-level docstring.""" # lstrip is just in case docstring has a '\n\n' at the beginning paragraphs = docstring.lstrip().split('\n\n') # remove comments and other syntax like `.. _link:` paragraphs = [p for p in paragraphs if not p.startswith('.. ') and len(p) > 0] if len(paragraphs) == 0: raise ExtensionError( "Example docstring should have a header for the example title. " "Please check the example file:\n {}\n".format(filename)) # Title is the first paragraph with any ReSTructuredText title chars # removed, i.e. lines that consist of (3 or more of the same) 7-bit # non-ASCII chars. # This conditional is not perfect but should hopefully be good enough. title_paragraph = paragraphs[0] match = re.search(r'^(?!([\W _])\1{3,})(.+)', title_paragraph, re.MULTILINE) if match is None: raise ExtensionError( 'Could not find a title in first paragraph:\n{}'.format( title_paragraph)) title = match.group(0).strip() # Use the title if no other paragraphs are provided intro_paragraph = title if len(paragraphs) < 2 else paragraphs[1] # Concatenate all lines of the first paragraph and truncate at 95 chars intro = re.sub('\n', ' ', intro_paragraph) intro = _sanitize_rst(intro) if len(intro) > 95: intro = intro[:95] + '...' return intro, title
Example #26
Source File: test_docs_resolv.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 4 votes |
def test_parse_sphinx_docopts(): data = ''' <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: './', VERSION: '2.0.2', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true, SOURCELINK_SUFFIX: '.txt' }; </script> ''' assert sg.parse_sphinx_docopts(data) == { 'URL_ROOT': './', 'VERSION': '2.0.2', 'COLLAPSE_INDEX': False, 'FILE_SUFFIX': '.html', 'HAS_SOURCE': True, 'SOURCELINK_SUFFIX': '.txt' } data_sphinx_175 = ''' <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: document.getElementById("documentation_options")\ .getAttribute('data-url_root'), VERSION: '2.0.2', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true, SOURCELINK_SUFFIX: '.txt' }; </script> ''' assert sg.parse_sphinx_docopts(data_sphinx_175) == { 'VERSION': '2.0.2', 'COLLAPSE_INDEX': False, 'FILE_SUFFIX': '.html', 'HAS_SOURCE': True, 'SOURCELINK_SUFFIX': '.txt' } with pytest.raises(ExtensionError): sg.parse_sphinx_docopts('empty input') with pytest.raises(ExtensionError): sg.parse_sphinx_docopts('DOCUMENTATION_OPTIONS = ') with pytest.raises(ExtensionError): sg.parse_sphinx_docopts('DOCUMENTATION_OPTIONS = {')
Example #27
Source File: docs_resolv.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 4 votes |
def parse_sphinx_docopts(index): """ Parse the Sphinx index for documentation options. Parameters ---------- index : str The Sphinx index page Returns ------- docopts : dict The documentation options from the page. """ pos = index.find('var DOCUMENTATION_OPTIONS') if pos < 0: raise ExtensionError( 'Documentation options could not be found in index.') pos = index.find('{', pos) if pos < 0: raise ExtensionError( 'Documentation options could not be found in index.') endpos = index.find('};', pos) if endpos < 0: raise ExtensionError( 'Documentation options could not be found in index.') block = index[pos + 1:endpos].strip() docopts = {} for line in block.splitlines(): key, value = line.split(':', 1) key = key.strip().strip('"') value = value.strip() if value[-1] == ',': value = value[:-1].rstrip() if value[0] in '"\'': value = value[1:-1] elif value == 'false': value = False elif value == 'true': value = True else: try: value = int(value) except ValueError: # In Sphinx 1.7.5, URL_ROOT is a JavaScript fragment. # Ignoring this entry since URL_ROOT is not used # elsewhere. # https://github.com/sphinx-gallery/sphinx-gallery/issues/382 continue docopts[key] = value return docopts
Example #28
Source File: gen_rst.py From sphinx-gallery with BSD 3-Clause "New" or "Revised" License | 4 votes |
def save_thumbnail(image_path_template, src_file, file_conf, gallery_conf): """Generate and Save the thumbnail image Parameters ---------- image_path_template : str holds the template where to save and how to name the image src_file : str path to source python file gallery_conf : dict Sphinx-Gallery configuration dictionary """ thumb_dir = os.path.join(os.path.dirname(image_path_template), 'thumb') if not os.path.exists(thumb_dir): os.makedirs(thumb_dir) # read specification of the figure to display as thumbnail from main text thumbnail_number = file_conf.get('thumbnail_number', None) thumbnail_path = file_conf.get('thumbnail_path', None) # thumbnail_number has priority. if thumbnail_number is None and thumbnail_path is None: # If no number AND no path, set to default thumbnail_number thumbnail_number = 1 if thumbnail_number is None: image_path = os.path.join(gallery_conf['src_dir'], thumbnail_path) else: if not isinstance(thumbnail_number, int): raise ExtensionError( 'sphinx_gallery_thumbnail_number setting is not a number, ' 'got %r' % (thumbnail_number,)) image_path = image_path_template.format(thumbnail_number) del thumbnail_number, thumbnail_path, image_path_template thumbnail_image_path, ext = _find_image_ext(image_path) base_image_name = os.path.splitext(os.path.basename(src_file))[0] thumb_file = os.path.join(thumb_dir, 'sphx_glr_%s_thumb.%s' % (base_image_name, ext)) if src_file in gallery_conf['failing_examples']: img = os.path.join(glr_path_static(), 'broken_example.png') elif os.path.exists(thumbnail_image_path): img = thumbnail_image_path elif not os.path.exists(thumb_file): # create something to replace the thumbnail img = os.path.join(glr_path_static(), 'no_image.png') img = gallery_conf.get("default_thumb_file", img) else: return if ext in ('svg', 'gif'): copyfile(img, thumb_file) else: scale_image(img, thumb_file, *gallery_conf["thumbnail_size"]) if 'thumbnails' in gallery_conf['compress_images']: optipng(thumb_file, gallery_conf['compress_images_args'])