-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the jupytext-config helpers to a subpackage, and add tests (#1167)
* Move the jupytext-config helpers to a subpackage and add tests * Use settings_file rather than settings_path * Simplify LabConfig and increase the coverage * Locate the config using jupyter_core.paths.jupyter_config_dir()
- Loading branch information
Showing
6 changed files
with
190 additions
and
130 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
""" | ||
helper to inspect / initialize jupyterlab labconfig settings | ||
that are required to open jupytext notebooks in jupyterlab by default | ||
when these settings are not present, a double click on a jupytext | ||
notebook will cause jupyterlab to open it in an editor, i.e. as a text file | ||
""" | ||
|
||
import json | ||
import logging | ||
from pathlib import Path | ||
|
||
|
||
class LabConfig: | ||
DOCTYPES = [ | ||
"python", | ||
"markdown", | ||
"myst", | ||
"r-markdown", | ||
"quarto", | ||
"julia", | ||
"r", | ||
] | ||
|
||
def __init__(self, *, settings_file, logger=None): | ||
self.settings_file = Path(settings_file) | ||
self.logger = logger or logging.getLogger(__name__) | ||
self.config = {} | ||
|
||
def read(self): | ||
""" | ||
read the labconfig settings file | ||
""" | ||
if self.settings_file.exists(): | ||
with self.settings_file.open() as fid: | ||
self.config = json.load(fid) | ||
else: | ||
self.logger.info(f"Could not read from {self.settings_file} (not found)") | ||
|
||
return self | ||
|
||
def get_viewers(self): | ||
return self.config.setdefault( | ||
"@jupyterlab/docmanager-extension:plugin", {} | ||
).setdefault("defaultViewers", {}) | ||
|
||
def list_default_viewer(self): | ||
""" | ||
list the current labconfig settings | ||
""" | ||
self.logger.debug( | ||
f"Current @jupyterlab/docmanager-extension:plugin in {self.settings_file}" | ||
) | ||
for key, value in self.get_viewers().items(): | ||
print(f"{key}: {value}") | ||
|
||
def set_default_viewers(self, doctypes=None): | ||
if not doctypes: | ||
doctypes = self.DOCTYPES | ||
for doctype in doctypes: | ||
self.set_default_viewer(doctype) | ||
return self | ||
|
||
def set_default_viewer(self, doctype): | ||
viewers = self.get_viewers() | ||
if doctype not in viewers: | ||
viewers[doctype] = "Jupytext Notebook" | ||
|
||
def unset_default_viewers(self, doctypes=None): | ||
if not doctypes: | ||
doctypes = self.DOCTYPES | ||
for doctype in doctypes: | ||
self.unset_default_viewer(doctype) | ||
return self | ||
|
||
def unset_default_viewer(self, doctype): | ||
viewers = self.get_viewers() | ||
if doctype in viewers: | ||
del viewers[doctype] | ||
|
||
def write(self): | ||
""" | ||
write the labconfig settings file | ||
""" | ||
self.settings_file.parent.mkdir(parents=True, exist_ok=True) | ||
with self.settings_file.open("w") as fid: | ||
json.dump(self.config, fid, indent=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from jupytext.cli import system | ||
|
||
|
||
def test_jupytext_config_cli(tmp_path): | ||
settings_file = tmp_path / "default_setting_overrides.json" | ||
system("jupytext-config", "-h") | ||
system( | ||
"jupytext-config", | ||
"--settings-file", | ||
str(settings_file), | ||
"set-default-viewer", | ||
"python", | ||
"markdown", | ||
) | ||
assert "python" in (settings_file).read_text() | ||
assert "markdown" in system( | ||
"jupytext-config", "--settings-file", settings_file, "list-default-viewer" | ||
) | ||
system( | ||
"jupytext-config", | ||
"--settings-file", | ||
str(settings_file), | ||
"unset-default-viewer", | ||
"markdown", | ||
) | ||
list_viewers = system( | ||
"jupytext-config", "--settings-file", settings_file, "list-default-viewer" | ||
) | ||
assert "python" in list_viewers | ||
assert "markdown" not in list_viewers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import json | ||
|
||
import pytest | ||
|
||
from jupytext_config.labconfig import LabConfig | ||
|
||
|
||
@pytest.fixture() | ||
def sample_viewer_config(): | ||
return { | ||
"@jupyterlab/docmanager-extension:plugin": { | ||
"defaultViewers": { | ||
"markdown": "Jupytext Notebook", | ||
"myst": "Jupytext Notebook", | ||
"r-markdown": "Jupytext Notebook", | ||
"quarto": "Jupytext Notebook", | ||
"julia": "Jupytext Notebook", | ||
"python": "Jupytext Notebook", | ||
"r": "Jupytext Notebook", | ||
} | ||
} | ||
} | ||
|
||
|
||
@pytest.fixture() | ||
def sample_empty_viewer_config(): | ||
return {"@jupyterlab/docmanager-extension:plugin": {"defaultViewers": {}}} | ||
|
||
|
||
@pytest.fixture() | ||
def settings_file(tmp_path): | ||
return tmp_path / "default_setting_overrides.json" | ||
|
||
|
||
def test_read_config(settings_file, sample_viewer_config): | ||
(settings_file).write_text(json.dumps(sample_viewer_config)) | ||
labconfig = LabConfig(settings_file=settings_file).read() | ||
assert labconfig.config == sample_viewer_config | ||
|
||
|
||
def test_set_unset_default_viewers( | ||
settings_file, sample_viewer_config, sample_empty_viewer_config | ||
): | ||
labconfig = LabConfig(settings_file=settings_file) | ||
labconfig.set_default_viewers() | ||
assert labconfig.config == sample_viewer_config | ||
labconfig.unset_default_viewers() | ||
assert labconfig.config == sample_empty_viewer_config | ||
|
||
|
||
def test_write_config(settings_file, sample_viewer_config): | ||
labconfig = LabConfig(settings_file=settings_file) | ||
labconfig.set_default_viewers() | ||
labconfig.write() | ||
assert json.loads(settings_file.read_text()) == sample_viewer_config |