Example #1
0
def _get_pattern(
    notebook: str, command: str, cell_mapping: Mapping[int, str]
) -> Sequence[Tuple[str, Union[str, Callable[[Match[str]], str]]]]:
    """
    Get pattern and substitutions with which to process code quality tool's output.

    Parameters
    ----------
    notebook
        Notebook command is being run on.
    command
        Code quality tool.
    cell_mapping
        Mapping from Python file lines to Jupyter notebook cells.

    Returns
    -------
    List
        Patterns and substitutions for reported output.
    """
    standard_substitution = partial(_line_to_cell, cell_mapping=cell_mapping)

    relative_path, absolute_path = get_relative_and_absolute_paths(notebook)

    if command == "black":
        return [
            (
                rf"(?<=^error: cannot format {re.escape(relative_path)}: Cannot parse: )\d+"
                rf"|(?<=^error: cannot format {re.escape(absolute_path)}: Cannot parse: )\d+",
                standard_substitution,
            ),
            (r"(?<=line )\d+(?=\)\nOh no! )", standard_substitution),
            (r"line cell_(?=\d+:\d+\)\nOh no! )", "cell_"),
        ]

    if command == "doctest":
        return [
            (
                rf'(?<=^File "{re.escape(relative_path)}", line )\d+'
                rf'|(?<=^File "{re.escape(absolute_path)}", line )\d+',
                standard_substitution,
            ),
            (
                rf'(?<=^File "{re.escape(relative_path)}",) line'
                rf'|(?<=^File "{re.escape(absolute_path)}",) line',
                "",
            ),
        ]

    # This is the most common one and is used by flake, pylint, mypy, and more.
    return [
        (
            rf"(?<=^{re.escape(absolute_path)}:)\d+"
            rf"|(?<=^{re.escape(relative_path)}:)\d+",
            standard_substitution,
        )
    ]
Example #2
0
def _get_nb_to_py_mapping(
    root_dirs: Sequence[str], files: Optional[str], exclude: Optional[str]
) -> Dict[str, TemporaryFile]:
    """
    Get mapping between notebooks and temporary Python files.

    Parameters
    ----------
    root_dirs
        All the notebooks/directories passed in via the command-line.
    files
        Pattern of files to include.
    exclude
        Pattern of files to exclude.

    Returns
    -------
    Dict[str, Tuple[int, str]]
        Mapping between notebooks and temporary Python files.

    Raises
    ------
    FileNotFoundError
        If notebook isn't found.
    """
    nb_to_py_mapping: Dict[str, TemporaryFile] = {}
    for notebook in _get_all_notebooks(root_dirs, files, exclude):
        if not os.path.exists(notebook):
            _clean_up_tmp_files(nb_to_py_mapping)
            raise FileNotFoundError(
                f"{BOLD}No such file or directory: {notebook}{RESET}\n"
            )

        nb_to_py_mapping[notebook] = TemporaryFile(
            *tempfile.mkstemp(
                dir=os.path.dirname(notebook),
                prefix=remove_suffix(os.path.basename(notebook), ".ipynb"),
                suffix=".py",
            )
        )
        relative_path, _ = get_relative_and_absolute_paths(
            nb_to_py_mapping[notebook].file
        )
        nb_to_py_mapping[notebook] = nb_to_py_mapping[notebook]._replace(
            file=relative_path
        )
    return nb_to_py_mapping