def test_extract_intro(): with tempfile.NamedTemporaryFile('wb') as f: f.write('\n'.join(CONTENT).encode('utf-8')) f.flush() result = sg.extract_intro(f.name) assert_false('Docstring' in result) assert_equal( result, 'This is the description of the example which goes on and on, Óscar') assert_false('second paragraph' in result)
def test_extract_intro(): with tempfile.NamedTemporaryFile('wb', delete=False) as f: f.write('\n'.join(CONTENT).encode('utf-8')) try: result = sg.extract_intro(f.name) finally: os.remove(f.name) assert 'Docstring' not in result assert result == 'This is the description of the example which goes on and on, Óscar' # noqa assert 'second paragraph' not in result
def test_extract_intro(): with tempfile.NamedTemporaryFile('w') as f: f.write('\n'.join([ '"""' 'Docstring header', '================', '', 'This is the description of the example', 'which goes on and on', '', '', 'And this is a second paragraph', '"""', '', '# and now comes the module code', 'x, y = 1, 2' ])) f.flush() result = sg.extract_intro(f.name) assert_false('Docstring' in result) assert_equal( result, 'This is the description of the example which goes on and on') assert_false('second paragraph' in result)
def test_extract_intro(): with tempfile.NamedTemporaryFile('w') as f: f.write('\n'.join(['"""' 'Docstring header', '================', '', 'This is the description of the example', 'which goes on and on', '', '', 'And this is a second paragraph', '"""', '', '# and now comes the module code', 'x, y = 1, 2'])) f.flush() result = sg.extract_intro(f.name) assert_false('Docstring' in result) assert_equal( result, 'This is the description of the example which goes on and on') assert_false('second paragraph' in result)
def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs): """Generate the gallery reStructuredText for an example directory""" fhindex = GALLERY_HEADER if not os.path.exists(target_dir): os.makedirs(target_dir) tagged_examples = example_groups(src_dir) tagged_examples = order_examples(tagged_examples) computation_times = [] build_target_dir = os.path.relpath(target_dir, gallery_conf['src_dir']) seen = set() tmp_dir = tempfile.mkdtemp() for tag, examples in tagged_examples.items(): sorted_listdir = examples entries_text = [] iterator = sphinx_compatibility.status_iterator( sorted_listdir, 'Generating gallery for %s ' % tag, length=len(sorted_listdir)) for fname in iterator: write_example(os.path.join(src_dir, fname), tmp_dir) amount_of_code, time_elapsed = generate_file_rst( fname, target_dir, tmp_dir, gallery_conf) if fname not in seen: seen.add(fname) computation_times.append((time_elapsed, fname)) new_fname = os.path.join(src_dir, fname) intro = extract_intro(new_fname) this_entry = _thumbnail_div(build_target_dir, fname, intro) + textwrap.dedent(""" .. toctree:: :hidden: /%s """) % os.path.join(build_target_dir, fname[:-3]).replace(os.sep, '/') # noqa: E501 entries_text.append((amount_of_code, this_entry)) if gallery_conf['backreferences_dir']: write_backreferences(seen_backrefs, gallery_conf, target_dir, fname, intro) # sort to have the smallest entries in the beginning entries_text.sort() fhindex += textwrap.dedent(""" {tag} {tag_underline} .. container:: gallery_images """.format(tag=tag, tag_underline='-' * len(tag))) for _, entry_text in entries_text: fhindex += '\n '.join(entry_text.split('\n')) # clear at the end of the section fhindex += """.. raw:: html\n <div style='clear:both'></div>\n\n""" # Tidy up the temp directory shutil.rmtree(tmp_dir) return fhindex, computation_times
def test_extract_intro(): result = sg.extract_intro('<string>', '\n'.join(CONTENT[1:9])) assert 'Docstring' not in result assert result == 'This is the description of the example which goes on and on, Óscar' # noqa assert 'second paragraph' not in result