Exemple #1
0
def test_iterchildren_match(
        advanced_data_regression: AdvancedDataRegressionFixture,
        absolute: bool):
    repo_path = PathPlus(__file__).parent.parent
    with in_directory(repo_path.parent):

        assert repo_path.is_dir()

        if not absolute:
            repo_path = repo_path.relative_to(repo_path.parent)

        if (repo_path / "build").is_dir():
            shutil.rmtree(repo_path / "build")

        children = list(repo_path.iterchildren(match="**/*.py"))
        assert children

        child_paths = sorted(
            p.relative_to(repo_path).as_posix() for p in children)

        for exclude_filename in {
                ".coverage", "pathtype_demo.py", "dist", "htmlcov", "conda",
                ".idea", "mutdef.py"
        }:
            if exclude_filename in child_paths:
                child_paths.remove(exclude_filename)

        advanced_data_regression.check(child_paths,
                                       basename="test_iterchildren_match")
Exemple #2
0
def test_iterchildren(advanced_data_regression: AdvancedDataRegressionFixture):
    repo_path = PathPlus(__file__).parent.parent
    assert repo_path.is_dir()

    children = list((repo_path / "domdf_python_tools").iterchildren())
    assert children
    advanced_data_regression.check(
        sorted(p.relative_to(repo_path).as_posix() for p in children))
Exemple #3
0
def test_mkdir_with_unknown_drive():
    for d in "ZYXWVUTSRQPONMLKJIHGFEDCBA":
        p = PathPlus(d + ":\\")
        if not p.is_dir():
            break
    else:
        pytest.skip("cannot find a drive that doesn't exist")
    with pytest.raises(OSError):
        (p / "child" / "path").mkdir(parents=True)
Exemple #4
0
def test_mkdir_exist_ok(BASE):
    p = PathPlus(BASE, "dirB")
    st_ctime_first = p.stat().st_ctime
    assert (p.exists())
    assert (p.is_dir())
    with pytest.raises(FileExistsError) as cm:
        p.mkdir()
    assert (cm.value.errno == errno.EEXIST)
    p.mkdir(exist_ok=True)
    assert (p.exists())
    assert (p.stat().st_ctime == st_ctime_first)
Exemple #5
0
def test_iterchildren_exclusions():
    repo_path = PathPlus(__file__).parent.parent
    assert repo_path.is_dir()

    if (repo_path / "build").is_dir():
        shutil.rmtree(repo_path / "build")

    children = list(repo_path.iterchildren())
    assert children
    for directory in children:
        directory = directory.relative_to(repo_path)
        # print(directory)
        assert directory.parts[0] not in paths.unwanted_dirs
Exemple #6
0
def test_mkdir_parents(BASE):
    # Creating a chain of directories.
    p = PathPlus(BASE, "newdirB", "newdirC")
    assert not (p.exists())
    with pytest.raises(OSError) as cm:
        p.mkdir()
    assert (cm.value.errno == errno.ENOENT)
    p.mkdir(parents=True)
    assert (p.exists())
    assert (p.is_dir())
    with pytest.raises(OSError) as cm:
        p.mkdir(parents=True)
    assert (cm.value.errno == errno.EEXIST)
    # Test `mode` arg.
    mode = stat.S_IMODE(p.stat().st_mode)  # Default mode.
    p = PathPlus(BASE, "newdirD", "newdirE")
    p.mkdir(0o555, parents=True)
    assert (p.exists())
    assert (p.is_dir())
    if os.name != "nt":
        # The directory's permissions follow the mode argument.
        assert (stat.S_IMODE(p.stat().st_mode) == 0o7555 & mode)
    # The parent's permissions follow the default process settings.
    assert (stat.S_IMODE(p.parent.stat().st_mode) == mode)
    "    # Change to `notebook` for an interactive view\n    \n":
    '',
    "The output should look similar to this:":
    ".. clearpage::\nThe output should look similar to this:",
}

notebooks = [
    "Displaying_TIC",
    "Displaying_Multiple_IC",
    "Displaying_Mass_Spec",
    "Displaying_Detected_Peaks",
    "Display_User_Interaction",
]

demo_rst_dir = PathPlus("doc-source/demo_rst").resolve()
if not demo_rst_dir.is_dir():
    demo_rst_dir.mkdir()

images_dir = PathPlus("doc-source/examples/graphics").resolve()
if not images_dir.is_dir():
    images_dir.mkdir()

for notebook in notebooks:
    # Convert the notebook to RST format

    markdown_content = PathPlus(f"demo/jupyter/{notebook}.ipynb").read_text()

    markdown_content = markdown_content.replace("<!---->", '|')

    (body, resources) = rst_exporter.from_notebook_node(
        nbformat.reads(markdown_content, as_version=4))
Exemple #8
0
def main(
    filename: Iterable[PathLike],
    config_file: PathLike,
    exclude: "Optional[List[str]]",
    colour: "ColourTrilean" = None,
    verbose: bool = False,
    show_traceback: bool = False,
    show_diff: bool = False,
):
    """
	Reformat the given Python source files.
	"""

    # stdlib
    import fnmatch
    import re

    # 3rd party
    from domdf_python_tools.paths import PathPlus

    # this package
    from formate.config import load_toml
    from formate.utils import SyntaxTracebackHandler, syntaxerror_for_file

    retv = 0

    try:
        config = load_toml(config_file)
    except FileNotFoundError:
        raise click.UsageError(f"Config file '{config_file}' not found")

    for path in filename:
        for pattern in exclude or []:
            if re.match(fnmatch.translate(pattern), str(path)):
                continue

        path = PathPlus(path)

        if path.suffix not in {".py", ".pyi", ''} or path.is_dir():
            if verbose >= 2:
                click.echo(
                    f"Skipping {path} as it doesn't appear to be a Python file"
                )

            continue

        r = Reformatter(path, config=config)

        with handle_tracebacks(show_traceback, cls=SyntaxTracebackHandler):
            with syntaxerror_for_file(path):
                ret_for_file = r.run()

        if ret_for_file:
            if verbose:
                click.echo(f"Reformatting {path}")
            if show_diff:
                click.echo(r.get_diff(), color=resolve_color_default(colour))

            r.to_file()

        elif verbose >= 2:
            click.echo(f"Checking {path}")

        retv |= ret_for_file

    sys.exit(retv)