def test_identify_names2(tmpdir): """Test more name identification.""" code_str = b""" ''' Title ----- This is an example. ''' # -*- coding: utf-8 -*- # \xc3\x9f from a.b import c import d as e print(c) e.HelloWorld().f.g """ expected = { 'c': { 'name': 'c', 'module': 'a.b', 'module_short': 'a.b', 'is_class': False, }, 'e.HelloWorld': { 'name': 'HelloWorld', 'module': 'd', 'module_short': 'd', 'is_class': False, } } fname = tmpdir.join("indentify_names.py") fname.write(code_str, 'wb') _, script_blocks = split_code_and_text_blocks(fname.strpath) res = sg.identify_names(script_blocks) assert expected == res code_str = b""" ''' Title ----- This example uses :func:`h.i`. ''' """ + code_str.split(b"'''")[-1] expected['h.i'] = { u'module': u'h', u'module_short': u'h', u'name': u'i', 'is_class': False } fname = tmpdir.join("indentify_names.py") fname.write(code_str, 'wb') _, script_blocks = split_code_and_text_blocks(fname.strpath) res = sg.identify_names(script_blocks) assert expected == res
def test_identify_names(unicode_sample): """Test name identification.""" expected = { 'os.path.join': { 'name': 'join', 'module': 'os.path', 'module_short': 'os.path', 'is_class': False, }, 'br.identify_names': { 'name': 'identify_names', 'module': 'sphinx_gallery.back_references', 'module_short': 'sphinx_gallery.back_references', 'is_class': False, }, 'identify_names': { 'name': 'identify_names', 'module': 'sphinx_gallery.back_references', 'module_short': 'sphinx_gallery.back_references', 'is_class': False, }, } _, script_blocks = split_code_and_text_blocks(unicode_sample) res = sg.identify_names(script_blocks) assert expected == res
def _create_tutorial_section(fname, src_dir, target_dir): if not os.path.exists(target_dir): os.makedirs(target_dir) src_file = os.path.normpath(os.path.join(src_dir, fname)) # Check if the same tutorial script has been already run md5_file = os.path.join(target_dir, f"{fname}.md5") if _md5sum_is_current(src_file, md5_file): return file_conf, script_blocks = parser.split_code_and_text_blocks(src_file) # Remove *.py suffix base_image_name = os.path.splitext(fname)[0] image_path_template = os.path.join(target_dir, base_image_name + "_{0:02}.png") script_vars = { "execute_script": True, "image_path_iterator": scrapers.ImagePathIterator(image_path_template), "src_file": src_file, "memory_delta": [], } tutorial_globals = {"__doc__": "", "__name__": "__main__"} compiler = codeop.Compile() content_rst = "" for block_label, block_content, line_no in script_blocks: if block_label == "code": # Run code and save output images code_output = genrst.execute_code_block( compiler=compiler, block=(block_label, block_content, line_no), example_globals=tutorial_globals, script_vars=script_vars, gallery_conf={ "abort_on_example_error": True, "src_dir": ".", "execute_script": True, "show_memory": False, "image_scrapers": (scrapers.matplotlib_scraper, ), }) content_rst += genrst.codestr2rst(block_content, lineno=None) + "\n" content_rst += code_output else: content_rst += block_content + "\n\n" with open(os.path.join(target_dir, f"{base_image_name}.rst"), "w") as file: file.write(content_rst) # Write checksum of file to avoid unnecessary rerun with open(md5_file, "w") as file: file.write(genrst.get_md5sum(src_file))
def _create_tutorial_section(fname, src_dir, target_dir): if not os.path.exists(target_dir): os.makedirs(target_dir) src_file = os.path.normpath(os.path.join(src_dir, fname)) # Check if the same tutorial script has been already run md5_file = os.path.join(target_dir, f"{fname}.md5") if _md5sum_is_current(src_file, md5_file): return file_conf, script_blocks = parser.split_code_and_text_blocks(src_file) # Remove *.py suffix base_image_name = os.path.splitext(fname)[0] image_path_template = os.path.join(target_dir, base_image_name+"_{0:02}.png") block_vars = {'execute_script': True, 'fig_count': 0, 'image_path': image_path_template, 'src_file': src_file} tutorial_globals = { "__doc__": "", "__name__": "__main__" } compiler = codeop.Compile() content_rst = "" for block_label, block_content, line_no in script_blocks: if block_label == 'code': # Run code and save output images code_output, rtime = genrst.execute_code_block( compiler=compiler, src_file=src_file, code_block=block_content, lineno=line_no, example_globals=tutorial_globals, block_vars=block_vars, gallery_conf = {"abort_on_example_error": True, "src_dir":"."} ) content_rst += genrst.codestr2rst( block_content, lineno=None ) + "\n" content_rst += code_output else: content_rst += block_content + "\n\n" with open(os.path.join(target_dir, f"{base_image_name}.rst"), "w") as file: file.write(content_rst) # Write checksum of file to avoid unnecessary rerun with open(md5_file, "w") as file: file.write(genrst.get_md5sum(src_file))
====================================== This demonstrates how Sphinx-Gallery identifies function names to figure out which functions are called in the script and to which module do they belong. """ # Code source: Óscar Nájera # License: BSD 3 clause import os # noqa, analysis:ignore import matplotlib.pyplot as plt from sphinx_gallery.backreferences import identify_names from sphinx_gallery.py_source_parser import split_code_and_text_blocks filename = os.__file__.replace('.pyc', '.py') _, script_blocks = split_code_and_text_blocks(filename) names = identify_names(script_blocks) figheight = len(names) + .5 fontsize = 12.5 ############################################################################### # Sphinx-Gallery examines both the executed code itself, as well as the # documentation blocks (such as this one, or the top-level one), # to find backreferences. This means that by writing :obj:`numpy.sin` # and :obj:`numpy.exp` here, a backreference will be created even though # they are not explicitly used in the code. This is useful in particular when # functions return classes -- if you add them to the documented blocks of # examples that use them, they will be shown in the backreferences. # # Also note that global variables of the script have intersphinx references
def _create_tutorial_section(fname, src_dir, target_dir): if not os.path.exists(target_dir): os.makedirs(target_dir) src_file = os.path.normpath(os.path.join(src_dir, fname)) # Check if the same tutorial script has been already run md5_file = os.path.join(target_dir, f"{fname}.md5") if _md5sum_is_current(src_file, md5_file): return file_conf, script_blocks = parser.split_code_and_text_blocks(src_file) # Remove *.py suffix base_image_name = os.path.splitext(fname)[0] # Locate file in tutorial target directory abs_base_image_name = os.path.join(os.getcwd(), "tutorial", "target", base_image_name) image_path_template = abs_base_image_name + "_{0:02}.png" fake_main = module_from_spec(spec_from_loader('__main__', None)) script_vars = { "execute_script": True, "image_path_iterator": scrapers.ImagePathIterator(image_path_template), "src_file": src_file, "memory_delta": [], "fake_main": fake_main } tutorial_globals = fake_main.__dict__ tutorial_globals.update({ "__doc__": "", }) gallery_conf = copy.deepcopy(DEFAULT_GALLERY_CONF) gallery_conf.update({ "abort_on_example_error": True, "src_dir": os.getcwd(), "execute_script": True, "inspect_global_variables": False, "call_memory": (lambda func: (0., func())), "image_scrapers": (scrapers.matplotlib_scraper, ), }) compiler = codeop.Compile() content_rst = "" for block_label, block_content, line_no in script_blocks: if block_label == "code": # Run code and save output images code_output = genrst.execute_code_block( compiler=compiler, block=(block_label, block_content, line_no), example_globals=tutorial_globals, script_vars=script_vars, gallery_conf=gallery_conf) content_rst += genrst.codestr2rst(block_content, lineno=None) + "\n" content_rst += code_output else: content_rst += block_content + "\n\n" with open(os.path.join(target_dir, f"{base_image_name}.rst"), "w") as file: file.write(content_rst) # Write checksum of file to avoid unnecessary rerun with open(md5_file, "w") as file: file.write(genrst.get_md5sum(src_file))
def test_identify_names2(tmpdir): """Test more name identification.""" code_str = b""" ''' Title ----- This is an example. ''' # -*- coding: utf-8 -*- # \xc3\x9f from a.b import c import d as e import h.i print(c) e.HelloWorld().f.g h.i.j() """ expected = { 'c': [{ 'name': 'c', 'module': 'a.b', 'module_short': 'a.b', 'is_class': False, }], 'e.HelloWorld': [{ 'name': 'HelloWorld', 'module': 'd', 'module_short': 'd', 'is_class': False, }], 'h.i.j': [{ 'name': 'j', 'module': 'h.i', 'module_short': 'h.i', 'is_class': False, }], } fname = tmpdir.join("identify_names.py") fname.write(code_str, 'wb') _, script_blocks = split_code_and_text_blocks(fname.strpath) res = sg.identify_names(script_blocks) assert expected == res code_str = b""" ''' Title ----- This example uses :func:`k.l` and :meth:`~m.n`. ''' """ + code_str.split(b"'''")[-1] expected['k.l'] = [{ u'module': u'k', u'module_short': u'k', u'name': u'l', 'is_class': False }] expected['m.n'] = [{ u'module': u'm', u'module_short': u'm', u'name': u'n', 'is_class': False }] fname = tmpdir.join("identify_names.py") fname.write(code_str, 'wb') _, script_blocks = split_code_and_text_blocks(fname.strpath) res = sg.identify_names(script_blocks) assert expected == res