Repository: mwouts/jupytext
Pull request #503 (2020-05-08 00:03:58)

Simplify format options on the CLI
Jupytext can be used to set format options on the command line (#130) with e.g. 

    jupytext --opt split_at_heading=true notebook.Rmd

codecov[bot]: # [Codecov](https://codecov.io/gh/mwouts/jupytext/pull/503?src=pr&el=h1) Report
> Merging [#503](https://codecov.io/gh/mwouts/jupytext/pull/503?src=pr&el=desc) into [master](https://codecov.io/gh/mwouts/jupytext/commit/adcfa2008daad10c9e67201385826cb9678f793c&el=desc) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/mwouts/jupytext/pull/503/graphs/tree.svg?width=650&height=150&src=pr&token=a1pNjsiILi)](https://codecov.io/gh/mwouts/jupytext/pull/503?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #503   +/-   ##
=======================================
  Coverage   98.75%   98.75%           
=======================================
  Files          83       83           
  Lines        8098     8110   +12     
=======================================
+ Hits         7997     8009   +12     
  Misses        101      101           
```


| [Impacted Files](https://codecov.io/gh/mwouts/jupytext/pull/503?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [jupytext/cli.py](https://codecov.io/gh/mwouts/jupytext/pull/503/diff?src=pr&el=tree#diff-anVweXRleHQvY2xpLnB5) | `95.45% <100.00%> (+0.01%)` | :arrow_up: |
| [jupytext/paired\_paths.py](https://codecov.io/gh/mwouts/jupytext/pull/503/diff?src=pr&el=tree#diff-anVweXRleHQvcGFpcmVkX3BhdGhzLnB5) | `97.50% <100.00%> (ø)` | |
| [tests/test\_cli.py](https://codecov.io/gh/mwouts/jupytext/pull/503/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9jbGkucHk=) | `99.86% <100.00%> (+<0.01%)` | :arrow_up: |
| [tests/test\_read\_simple\_markdown.py](https://codecov.io/gh/mwouts/jupytext/pull/503/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9yZWFkX3NpbXBsZV9tYXJrZG93bi5weQ==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/mwouts/jupytext/pull/503?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/mwouts/jupytext/pull/503?src=pr&el=footer). Last update [adcfa20...cc8ebc0](https://codecov.io/gh/mwouts/jupytext/pull/503?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/jupytext/cli.py b/jupytext/cli.py
index 8e7becb8..8ab52b0d 100644
--- a/jupytext/cli.py
+++ b/jupytext/cli.py
@@ -347,12 +347,13 @@ def jupytext(args=None):
         and not args.pipe
         and not args.check
         and not args.update_metadata
+        and not args.format_options
         and not args.set_kernel
         and not args.execute
     ):
         raise ValueError(
             "Please provide one of --to, --output, --set-formats, --sync, --pipe, "
-            "--check, --update_metadata, --set-kernel or --execute"
+            "--check, --update-metadata, --format-options, --set-kernel or --execute"
         )
 
     if args.output and len(args.notebooks) != 1:
@@ -487,6 +488,14 @@ def jupytext_single_file(nb_file, args, log):
         log("[jupytext] Setting kernel {}".format(kernelspec.get("name")))
         args.update_metadata["kernelspec"] = kernelspec
 
+    # Are we updating a text file that has a metadata filter? #212
+    if args.update_metadata or args.format_options:
+        if (
+            notebook.metadata.get("jupytext", {}).get("notebook_metadata_filter")
+            == "-all"
+        ):
+            notebook.metadata.get("jupytext", {}).pop("notebook_metadata_filter")
+
     # Update the metadata
     if args.update_metadata:
         log(
@@ -494,13 +503,6 @@ def jupytext_single_file(nb_file, args, log):
                 json.dumps(args.update_metadata)
             )
         )
-        # Are we updating a text file that has a metadata filter? #212
-        if (
-            notebook.metadata.get("jupytext", {}).get("notebook_metadata_filter")
-            == "-all"
-        ):
-            notebook.metadata.get("jupytext", {}).pop("notebook_metadata_filter")
-        recursive_update(notebook.metadata, args.update_metadata)
 
         if (
             "kernelspec" in args.update_metadata
@@ -508,6 +510,8 @@ def jupytext_single_file(nb_file, args, log):
         ):
             notebook.metadata["jupytext"].pop("main_language")
 
+        recursive_update(notebook.metadata, args.update_metadata)
+
     # Read paired notebooks, except if the pair is being created
     if args.sync:
         set_prefix_and_suffix(fmt, notebook, nb_file)
diff --git a/jupytext/paired_paths.py b/jupytext/paired_paths.py
index f97561ad..8c77d610 100644
--- a/jupytext/paired_paths.py
+++ b/jupytext/paired_paths.py
@@ -13,7 +13,7 @@ class InconsistentPath(ValueError):
 
 def base_path(main_path, fmt):
     """Given a path and options for a format (ext, suffix, prefix), return the corresponding base path"""
-    if not fmt:
+    if not fmt or (isinstance(fmt, dict) and "extension" not in fmt):
         base, ext = os.path.splitext(main_path)
         if ext not in NOTEBOOK_EXTENSIONS:
             raise InconsistentPath(


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 4c012079..a595b6d8 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -1260,3 +1260,22 @@ def test_create_header_with_set_formats_and_set_kernel(format_name, tmpdir):
     nb = read(tmp_nb)
     assert nb["metadata"]["jupytext"]["formats"] == format_name
     assert "kernelspec" in nb["metadata"]
+
+
+def test_set_option_split_at_heading(tmpdir):
+    tmp_rmd = tmpdir.join("notebook.Rmd")
+    tmp_rmd.write(
+        """A paragraph
+
+# H1 Header
+"""
+    )
+
+    jupytext([str(tmp_rmd), "--opt", "split_at_heading=true"])
+    assert "split_at_heading: true" in tmp_rmd.read()
+    nb = read(str(tmp_rmd))
+
+    nb_expected = new_notebook(
+        cells=[new_markdown_cell("A paragraph"), new_markdown_cell("# H1 Header")]
+    )
+    compare_notebooks(nb, nb_expected)
diff --git a/tests/test_read_simple_markdown.py b/tests/test_read_simple_markdown.py
index 7e030320..ad096693 100644
--- a/tests/test_read_simple_markdown.py
+++ b/tests/test_read_simple_markdown.py
@@ -265,6 +265,25 @@ def test_split_on_header_after_two_blank_lines(
     compare(markdown2, markdown)
 
 
+def test_split_at_heading_in_metadata(
+    markdown="""---
+jupyter:
+  jupytext:
+    split_at_heading: true
+---
+
+A paragraph
+
+# H1 Header
+""",
+    nb_expected=new_notebook(
+        cells=[new_markdown_cell("A paragraph"), new_markdown_cell("# H1 Header")]
+    ),
+):
+    nb = jupytext.reads(markdown, ".md")
+    compare_notebooks(nb, nb_expected)
+
+
 def test_code_cell_with_metadata(
     markdown="""```python tags=["parameters"]
 a = 1