Ejemplo n.º 1
0
def test_codestr2rst():
    """Test the correct translation of a code block into rst"""
    output = sg.codestr2rst('print("hello world")')
    reference = """
.. code-block:: python

    print("hello world")"""
    assert_equal(reference, output)
Ejemplo n.º 2
0
def test_codestr2rst():
    """Test the correct translation of a code block into rst"""
    output = sg.codestr2rst('print("hello world")')
    reference = """
.. code-block:: python

    print("hello world")"""
    assert_equal(reference, output)
Ejemplo n.º 3
0
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))
Ejemplo n.º 4
0
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))
Ejemplo n.º 5
0
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))