How to use the jupytext.paired_paths.paired_paths function in jupytext

To help you get started, we’ve selected a few jupytext examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mwouts / jupytext / tests / test_paired_paths.py View on Github external
def test_simple_pair():
    formats = long_form_multiple_formats('ipynb,py')
    expected_paths = ['notebook.ipynb', 'notebook.py']
    compare(paired_paths('notebook.ipynb', 'ipynb', formats), list(zip(expected_paths, formats)))
    compare(paired_paths('notebook.py', 'py', formats), list(zip(expected_paths, formats)))
github mwouts / jupytext / tests / test_paired_paths.py View on Github external
def test_prefix_on_root_174():
    short_formats = 'ipynb,python//py:percent'

    formats = long_form_multiple_formats(short_formats)
    assert short_form_multiple_formats(formats) == short_formats

    expected_paths = ['/Untitled.ipynb', '/python/Untitled.py']
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))
github mwouts / jupytext / tests / test_paired_paths.py View on Github external
def test_many_and_suffix():
    formats = long_form_multiple_formats('ipynb,.pct.py,_lgt.py')
    expected_paths = ['notebook.ipynb', 'notebook.pct.py', 'notebook_lgt.py']
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    with pytest.raises(InconsistentPath):
        paired_paths('wrong_suffix.py', 'py', formats)
github mwouts / jupytext / tests / test_paired_paths.py View on Github external
'parent/script_folder/NOTEBOOK_NAME_in_light_format.py']
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    # without the parent folder
    expected_paths = [path[7:] for path in expected_paths]
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    # Not the expected parent folder
    with pytest.raises(InconsistentPath):
        paired_paths('script_folder_incorrect/NOTEBOOK_NAME_in_percent_format.py', formats[1], formats)

    # Not the expected suffix
    with pytest.raises(InconsistentPath):
        paired_paths('parent/script_folder/NOTEBOOK_NAME_in_LIGHT_format.py', formats[2], formats)

    # Not the expected extension
    with pytest.raises(InconsistentPath):
        paired_paths('notebook_folder/notebook_prefix_NOTEBOOK_NAME_notebook_suffix.py', formats[0], formats)
github mwouts / jupytext / tests / test_paired_paths.py View on Github external
def test_prefix_and_suffix():
    short_formats = 'notebook_folder/notebook_prefix_/_notebook_suffix.ipynb,' \
                    'script_folder//_in_percent_format.py:percent,' \
                    'script_folder//_in_light_format.py'

    formats = long_form_multiple_formats(short_formats)
    assert short_form_multiple_formats(formats) == short_formats

    expected_paths = ['parent/notebook_folder/notebook_prefix_NOTEBOOK_NAME_notebook_suffix.ipynb',
                      'parent/script_folder/NOTEBOOK_NAME_in_percent_format.py',
                      'parent/script_folder/NOTEBOOK_NAME_in_light_format.py']
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    # without the parent folder
    expected_paths = [path[7:] for path in expected_paths]
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    # Not the expected parent folder
    with pytest.raises(InconsistentPath):
        paired_paths('script_folder_incorrect/NOTEBOOK_NAME_in_percent_format.py', formats[1], formats)

    # Not the expected suffix
    with pytest.raises(InconsistentPath):
        paired_paths('parent/script_folder/NOTEBOOK_NAME_in_LIGHT_format.py', formats[2], formats)

    # Not the expected extension
    with pytest.raises(InconsistentPath):
github mwouts / jupytext / tests / test_cli.py View on Github external
def test_paired_paths(nb_file, tmpdir, capsys):
    tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
    nb = read(nb_file)
    nb.metadata.setdefault('jupytext', {})['formats'] = 'ipynb,_light.py,_percent.py:percent'
    write(nb, tmp_ipynb)

    jupytext(['--paired-paths', tmp_ipynb])

    out, err = capsys.readouterr()
    assert not err

    formats = nb.metadata.get('jupytext', {}).get('formats')
    assert set(out.splitlines()).union([tmp_ipynb]) == set(
        [path for path, _ in paired_paths(tmp_ipynb, 'ipynb', formats)])
github mwouts / jupytext / jupytext / contentsmanager.py View on Github external
jupytext_formats = long_form_multiple_formats(jupytext_formats, nbk.metadata,
                                                          auto_ext_requires_language_info=False)

            # Compute paired notebooks from formats
            alt_paths = [(path, fmt)]
            if jupytext_formats:
                try:
                    _, fmt = find_base_path_and_format(path, jupytext_formats)
                    alt_paths = paired_paths(path, fmt, jupytext_formats)
                    self.update_paired_notebooks(path, fmt, jupytext_formats)
                except InconsistentPath as err:
                    self.log.info("Unable to read paired notebook: %s", str(err))
            else:
                if path in self.paired_notebooks:
                    fmt, formats = self.paired_notebooks.get(path)
                    alt_paths = paired_paths(path, fmt, formats)

            if len(alt_paths) > 1 and ext == '.ipynb':
                # Apply default options (like saving and reloading would do)
                jupytext_metadata = model['content']['metadata'].get('jupytext', {})
                self.set_default_format_options(jupytext_metadata, read=True)
                if jupytext_metadata:
                    model['content']['metadata']['jupytext'] = jupytext_metadata

            org_model = model
            fmt_inputs = fmt
            path_inputs = path_outputs = path
            model_outputs = None

            # Source format is the most recent non ipynb format found on disk
            if path.endswith('.ipynb'):
                source_timestamps = {}
github mwouts / jupytext / jupytext / contentsmanager.py View on Github external
def update_paired_notebooks(self, path, fmt, formats):
            """Update the list of paired notebooks to include/update the current pair"""
            if not formats:
                self.drop_paired_notebook(path)
                return

            new_paired_paths = paired_paths(path, fmt, formats)
            for alt_path, _ in new_paired_paths:
                self.drop_paired_notebook(alt_path)

            long_formats = long_form_multiple_formats(formats)
            if len(long_formats) == 1 and set(long_formats[0]) <= {'extension'}:
                return

            short_formats = short_form_multiple_formats(formats)
            for alt_path, alt_fmt in new_paired_paths:
                self.paired_notebooks[alt_path] = short_form_one_format(alt_fmt), short_formats
github mwouts / jupytext / jupytext / contentsmanager.py View on Github external
def trust_notebook(self, path):
            """Trust the current notebook"""
            if path.endswith('.ipynb') or path not in self.paired_notebooks:
                super(JupytextContentsManager, self).trust_notebook(path)
                return

            fmt, formats = self.paired_notebooks[path]
            for alt_path, alt_fmt in paired_paths(path, fmt, formats):
                if alt_fmt['extension'] == '.ipynb':
                    super(JupytextContentsManager, self).trust_notebook(alt_path)
github mwouts / jupytext / jupytext / contentsmanager.py View on Github external
else:
                self.set_default_format_options(fmt, read=True)
                with mock.patch('nbformat.reads', _jupytext_reads(fmt)):
                    model = self._notebook_model(path, content=content)

            if not load_alternative_format:
                return model

            if not content:
                # Modification time of a paired notebook, in this context - Jupyter is checking timestamp
                # before saving - is the most recent among all representations #118
                if path not in self.paired_notebooks:
                    return model

                fmt, formats = self.paired_notebooks.get(path)
                for alt_path, _ in paired_paths(path, fmt, formats):
                    if alt_path != path and self.exists(alt_path):
                        alt_model = self._notebook_model(alt_path, content=False)
                        if alt_model['last_modified'] > model['last_modified']:
                            model['last_modified'] = alt_model['last_modified']

                return model

            # We will now read a second file if this is a paired notebooks.
            nbk = model['content']
            jupytext_formats = nbk.metadata.get('jupytext', {}).get('formats') or self.default_formats(path)
            jupytext_formats = long_form_multiple_formats(jupytext_formats, nbk.metadata,
                                                          auto_ext_requires_language_info=False)

            # Compute paired notebooks from formats
            alt_paths = [(path, fmt)]
            if jupytext_formats: