Skip to content

Commit

Permalink
MAINT: restore default line length (#577)
Browse files Browse the repository at this point in the history
* MAINT: restore default line length

This larger limit was supposed to be for flake8 only

* MAINT: drop TODO
  • Loading branch information
agoose77 committed Jan 15, 2024
1 parent 0d110f2 commit 6476744
Show file tree
Hide file tree
Showing 30 changed files with 323 additions and 113 deletions.
4 changes: 3 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ class MystNbConfigDirective(_ConfigBase):
required_arguments = 1
option_spec = {
"sphinx": directives.flag,
"section": lambda x: directives.choice(x, ["config", "read", "execute", "render"]),
"section": lambda x: directives.choice(
x, ["config", "read", "execute", "render"]
),
}

def run(self):
Expand Down
20 changes: 15 additions & 5 deletions myst_nb/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def quickstart(args: list[str] | None = None):
# write conf.py
(path / "conf.py").write_text(generate_conf_py(), encoding="utf-8")
# write index.md
(path / "index.md").write_text(generate_index(["notebook1", "notebook2"]), encoding="utf-8")
(path / "index.md").write_text(
generate_index(["notebook1", "notebook2"]), encoding="utf-8"
)
# write notebook1.ipynb
(path / "notebook1.ipynb").write_text(generate_jupyter_notebook(), encoding="utf-8")
# write notebook2.md
Expand All @@ -40,8 +42,12 @@ def quickstart(args: list[str] | None = None):

def create_quickstart_cli():
cli = argparse.ArgumentParser(description="Create a basic myst_nb project.")
cli.add_argument("path", metavar="PATH", type=str, help="Directory to output the project.")
cli.add_argument("-o", "--overwrite", action="store_true", help="Overwrite existing files.")
cli.add_argument(
"path", metavar="PATH", type=str, help="Directory to output the project."
)
cli.add_argument(
"-o", "--overwrite", action="store_true", help="Overwrite existing files."
)
cli.add_argument("-v", "--verbose", action="store_true", help="Increase verbosity.")
return cli

Expand Down Expand Up @@ -160,14 +166,18 @@ def create_md_to_nb_cli():
cli = argparse.ArgumentParser(
description="Convert a text-based notebook to a Jupyter notebook."
)
cli.add_argument("inpath", metavar="PATH_IN", type=str, help="Path to Markdown file.")
cli.add_argument(
"inpath", metavar="PATH_IN", type=str, help="Path to Markdown file."
)
cli.add_argument(
"outpath",
metavar="PATH_OUT",
nargs="?",
type=str,
help="Path to output to.",
)
cli.add_argument("-o", "--overwrite", action="store_true", help="Overwrite existing files.")
cli.add_argument(
"-o", "--overwrite", action="store_true", help="Overwrite existing files."
)
cli.add_argument("-v", "--verbose", action="store_true", help="Increase verbosity.")
return cli
29 changes: 21 additions & 8 deletions myst_nb/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def custom_formats_converter(value: dict) -> Dict[str, Tuple[str, dict, bool]]:
if isinstance(reader, str):
output[suffix] = (reader, {}, False)
elif not isinstance(reader, Sequence):
raise TypeError(f"`nb_custom_formats` values must be a string or sequence: {reader}")
raise TypeError(
f"`nb_custom_formats` values must be a string or sequence: {reader}"
)
elif len(reader) == 2:
output[suffix] = (reader[0], reader[1], False)
elif len(reader) == 3:
Expand All @@ -38,12 +40,18 @@ def custom_formats_converter(value: dict) -> Dict[str, Tuple[str, dict, bool]]:
f"2 or 3: {reader}"
)
if not isinstance(output[suffix][0], str):
raise TypeError(f"`nb_custom_formats` values[0] must be a string: {output[suffix][0]}")
raise TypeError(
f"`nb_custom_formats` values[0] must be a string: {output[suffix][0]}"
)
# TODO check can be loaded as a python object?
if not isinstance(output[suffix][1], dict):
raise TypeError(f"`nb_custom_formats` values[1] must be a dict: {output[suffix][1]}")
raise TypeError(
f"`nb_custom_formats` values[1] must be a dict: {output[suffix][1]}"
)
if not isinstance(output[suffix][2], bool):
raise TypeError(f"`nb_custom_formats` values[2] must be a bool: {output[suffix][2]}")
raise TypeError(
f"`nb_custom_formats` values[2] must be a bool: {output[suffix][2]}"
)
return output


Expand Down Expand Up @@ -256,11 +264,12 @@ def __post_init__(self):
default=False,
metadata={
"validator": instance_of(bool),
"help": "Raise an exception on failed execution, " "rather than emitting a warning",
"help": "Raise an exception on failed execution, "
"rather than emitting a warning",
"sections": (Section.global_lvl, Section.file_lvl, Section.execute),
},
)
execution_show_tb: bool = dc.field( # TODO implement
execution_show_tb: bool = dc.field(
default=False,
metadata={
"validator": instance_of(bool),
Expand Down Expand Up @@ -380,7 +389,9 @@ def __post_init__(self):
default=(),
metadata={
"validator": deep_iterable(
has_items(instance_of(str), instance_of(str), optional(instance_of(int))),
has_items(
instance_of(str), instance_of(str), optional(instance_of(int))
),
),
"help": "Overrides for the base render priority of mime types: "
"list of (builder name, mime type, priority)",
Expand All @@ -390,7 +401,9 @@ def __post_init__(self):
},
repr=False,
)
output_stderr: Literal["show", "remove", "remove-warn", "warn", "error", "severe"] = dc.field(
output_stderr: Literal[
"show", "remove", "remove-warn", "warn", "error", "severe"
] = dc.field(
default="show",
metadata={
"validator": in_(
Expand Down
8 changes: 6 additions & 2 deletions myst_nb/core/execute/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def __enter__(self) -> NotebookClientBase:
"""Enter the context manager."""
self.start_client()
# extract glue data from the notebook
self._glue_data = extract_glue_data(self.notebook, self._source_map, self.logger)
self._glue_data = extract_glue_data(
self.notebook, self._source_map, self.logger
)
return self

@final
Expand Down Expand Up @@ -152,7 +154,9 @@ def nb_source_code_lexer(self) -> str | None:
lexer = (metadata.get("kernelspec") or {}).get("language", None)
return lexer

def code_cell_outputs(self, cell_index: int) -> tuple[int | None, list[NotebookNode]]:
def code_cell_outputs(
self, cell_index: int
) -> tuple[int | None, list[NotebookNode]]:
"""Get the outputs of a cell.
:returns: a tuple of the execution_count and the outputs
Expand Down
4 changes: 3 additions & 1 deletion myst_nb/core/execute/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def start_client(self):
return

if self.path is None:
raise ValueError("Input source must exist as file, if execution_mode is 'cache'")
raise ValueError(
"Input source must exist as file, if execution_mode is 'cache'"
)

# attempt to execute the notebook
read_fmt = self._kwargs.get("read_fmt", None)
Expand Down
4 changes: 3 additions & 1 deletion myst_nb/core/execute/direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def start_client(self):
cwd_context = TemporaryDirectory()
else:
if self.path is None:
raise ValueError("Input source must exist as file, if execution_in_temp=False")
raise ValueError(
"Input source must exist as file, if execution_in_temp=False"
)
cwd_context = nullcontext(str(self.path.parent))

# execute in the context of the current working directory
Expand Down
16 changes: 12 additions & 4 deletions myst_nb/core/execute/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def start_client(self):
resources = {"metadata": {"path": self._tmp_path}}
else:
if self.path is None:
raise ValueError("Input source must exist as file, if execution_in_temp=False")
raise ValueError(
"Input source must exist as file, if execution_in_temp=False"
)
resources = {"metadata": {"path": str(self.path.parent)}}

self.logger.info("Starting inline execution client")
Expand All @@ -66,7 +68,9 @@ def start_client(self):
msg_id = self._client.kc.kernel_info()
info_msg = self._client.wait_for_reply(msg_id)
if info_msg is not None and "language_info" in info_msg["content"]:
self.notebook.metadata["language_info"] = info_msg["content"]["language_info"]
self.notebook.metadata["language_info"] = info_msg["content"][
"language_info"
]
else:
self.logger.warning("Failed to retrieve language info from kernel")

Expand All @@ -92,7 +96,9 @@ def close_client(self, exc_type, exc_val, exc_tb):
"runtime": _exec_time,
"method": self.nb_config.execution_mode,
"succeeded": False if self._cell_error else True,
"error": f"{self._cell_error.__class__.__name__}" if self._cell_error else None,
"error": f"{self._cell_error.__class__.__name__}"
if self._cell_error
else None,
"traceback": self._exc_string,
}
if not self._cell_error:
Expand All @@ -105,7 +111,9 @@ def close_client(self, exc_type, exc_val, exc_tb):
if self._tmp_path:
shutil.rmtree(self._tmp_path, ignore_errors=True)

def code_cell_outputs(self, cell_index: int) -> tuple[int | None, list[NotebookNode]]:
def code_cell_outputs(
self, cell_index: int
) -> tuple[int | None, list[NotebookNode]]:
cells = self.notebook.get("cells", [])

# ensure all cells up to and including the requested cell have been executed
Expand Down
4 changes: 3 additions & 1 deletion myst_nb/core/lexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
}


def _token_from_lexer_state(bold: bool, faint: bool, fg_color: str | None, bg_color: str | None):
def _token_from_lexer_state(
bold: bool, faint: bool, fg_color: str | None, bg_color: str | None
):
"""Construct a token given the current lexer state.
We can only emit one token even though we have a multiple-tuple state.
Expand Down
4 changes: 3 additions & 1 deletion myst_nb/core/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ class DocutilsDocLogger(logging.LoggerAdapter):
]

def __init__(self, document: nodes.document, type_name: str = DEFAULT_LOG_TYPE):
self.logger: logging.Logger = logging.getLogger(f"{type_name}-{document.source}")
self.logger: logging.Logger = logging.getLogger(
f"{type_name}-{document.source}"
)
# docutils handles the level of output logging
self.logger.setLevel(logging.DEBUG)
if not self.logger.handlers:
Expand Down
28 changes: 21 additions & 7 deletions myst_nb/core/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ def is_myst_markdown_notebook(text: str | Iterator[str]) -> bool:
if "file_format" in metadata and metadata["file_format"] == "mystnb":
return True
if (
metadata.get("jupytext", {}).get("text_representation", {}).get("format_name", None)
metadata.get("jupytext", {})
.get("text_representation", {})
.get("format_name", None)
!= "myst"
):
return False
Expand All @@ -162,7 +164,9 @@ def myst_nb_reader_plugin(uri: str) -> nbf.NotebookNode:
Used as plugin for jupyter-cache.
"""
return read_myst_markdown_notebook(Path(uri).read_text("utf8"), add_source_map=True, path=uri)
return read_myst_markdown_notebook(
Path(uri).read_text("utf8"), add_source_map=True, path=uri
)


def read_myst_markdown_notebook(
Expand All @@ -189,7 +193,9 @@ def read_myst_markdown_notebook(
"""
config = config or MdParserConfig()
# parse markdown file up to the block level (i.e. don't worry about inline text)
inline_config = dc.replace(config, disable_syntax=(list(config.disable_syntax) + ["inline"]))
inline_config = dc.replace(
config, disable_syntax=(list(config.disable_syntax) + ["inline"])
)
parser = create_md_parser(inline_config, RendererHTML)
tokens = parser.parse(text + "\n")
lines = text.splitlines()
Expand Down Expand Up @@ -227,7 +233,9 @@ def _flush_markdown(start_line, token, md_metadata):
meta = nbf.from_dict(md_metadata)
if md_source:
source_map.append(start_line)
notebook.cells.append(nbf_version.new_markdown_cell(source=md_source, metadata=meta))
notebook.cells.append(
nbf_version.new_markdown_cell(source=md_source, metadata=meta)
)

# iterate through the tokens to identify notebook cells
nesting_level = 0
Expand All @@ -247,7 +255,9 @@ def _flush_markdown(start_line, token, md_metadata):
options, body_lines = _read_fenced_cell(token, len(notebook.cells), "Code")
# Parse :load: or load: tags and populate body with contents of file
if "load" in options:
body_lines = _load_code_from_file(path, options["load"], token, body_lines)
body_lines = _load_code_from_file(
path, options["load"], token, body_lines
)
meta = nbf.from_dict(options)
source_map.append(token_map[0] + 1)
notebook.cells.append(
Expand Down Expand Up @@ -333,13 +343,17 @@ def _read_cell_metadata(token, cell_index):
)
if not isinstance(metadata, dict):
raise MystMetadataParsingError(
"Markdown cell {} at line {} is not a dict".format(cell_index, token.map[0] + 1)
"Markdown cell {} at line {} is not a dict".format(
cell_index, token.map[0] + 1
)
)

return metadata


def _load_code_from_file(nb_path: None | str | Path, file_name: str, token, body_lines: list[str]):
def _load_code_from_file(
nb_path: None | str | Path, file_name: str, token, body_lines: list[str]
):
"""load source code from a file."""
if nb_path is None:
raise _LoadFileParsingError("path to notebook not supplied for :load:")
Expand Down
Loading

0 comments on commit 6476744

Please sign in to comment.