How to use the jupytext.formats.JupytextFormatError 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_cli.py View on Github external
def test_error_not_notebook_ext_input(tmpdir, capsys):
    tmp_file = str(tmpdir.join('notebook.ext'))
    with open(tmp_file, 'w') as fp:
        fp.write('\n')

    with pytest.raises(JupytextFormatError, match="Extension '.ext' is not a notebook extension. Please use one of"):
        jupytext([tmp_file, '--to', 'py'])
github mwouts / jupytext / tests / test_cli.py View on Github external
def test_error_not_notebook_ext_output(tmp_ipynb, tmpdir):
    with pytest.raises(JupytextFormatError, match="Extension '.ext' is not a notebook extension. Please use one of"):
        jupytext([tmp_ipynb, '-o', str(tmpdir.join('not.ext'))])
github mwouts / jupytext / jupytext / formats.py View on Github external
def get_format_implementation(ext, format_name=None):
    """Return the implementation for the desired format"""
    # remove pre-extension if any
    ext = '.' + ext.split('.')[-1]

    formats_for_extension = []
    for fmt in JUPYTEXT_FORMATS:
        if fmt.extension == ext:
            if fmt.format_name == format_name or not format_name:
                return fmt
            formats_for_extension.append(fmt.format_name)

    if formats_for_extension:
        if ext in ['.md', '.markdown'] and format_name == 'pandoc':
            raise JupytextFormatError('Please install pandoc>=2.7.2')

        raise JupytextFormatError("Format '{}' is not associated to extension '{}'. "
                                  "Please choose one of: {}.".format(format_name, ext,
                                                                     ', '.join(formats_for_extension)))
    raise JupytextFormatError("No format associated to extension '{}'".format(ext))
github mwouts / jupytext / jupytext / formats.py View on Github external
def validate_one_format(jupytext_format):
    """Validate extension and options for the given format"""
    if not isinstance(jupytext_format, dict):
        raise JupytextFormatError('Jupytext format should be a dictionary')

    for key in jupytext_format:
        if key not in _VALID_FORMAT_INFO + _VALID_FORMAT_OPTIONS:
            raise JupytextFormatError("Unknown format option '{}' - should be one of '{}'".format(
                key, "', '".join(_VALID_FORMAT_OPTIONS)))
        value = jupytext_format[key]
        if key in _BINARY_FORMAT_OPTIONS:
            if not isinstance(value, bool):
                raise JupytextFormatError("Format option '{}' should be a bool, not '{}'".format(key, str(value)))

    if 'extension' not in jupytext_format:
        raise JupytextFormatError('Missing format extension')
    ext = jupytext_format['extension']
    if ext not in NOTEBOOK_EXTENSIONS + ['.auto']:
        raise JupytextFormatError("Extension '{}' is not a notebook extension. Please use one of '{}'.".format(
            ext, "', '".join(NOTEBOOK_EXTENSIONS + ['.auto'])))
github mwouts / jupytext / jupytext / formats.py View on Github external
fmt = get_format_implementation(ext, format_name)
    current = fmt.current_version_number

    # Missing version, still generated by jupytext?
    if notebook.metadata and not version:
        version = current

    # Same version? OK
    if version == fmt.current_version_number:
        return

    # Version larger than minimum readable version
    if (fmt.min_readable_version_number or current) <= version <= current:
        return

    raise JupytextFormatError("File {} is in format/version={}/{} (current version is {}). "
                              "It would not be safe to override the source of {} with that file. "
                              "Please remove one or the other file."
                              .format(os.path.basename(source_path),
                                      format_name, version, current,
                                      os.path.basename(outputs_path)))
github mwouts / jupytext / jupytext / formats.py View on Github external
def validate_one_format(jupytext_format):
    """Validate extension and options for the given format"""
    if not isinstance(jupytext_format, dict):
        raise JupytextFormatError('Jupytext format should be a dictionary')

    for key in jupytext_format:
        if key not in _VALID_FORMAT_INFO + _VALID_FORMAT_OPTIONS:
            raise JupytextFormatError("Unknown format option '{}' - should be one of '{}'".format(
                key, "', '".join(_VALID_FORMAT_OPTIONS)))
        value = jupytext_format[key]
        if key in _BINARY_FORMAT_OPTIONS:
            if not isinstance(value, bool):
                raise JupytextFormatError("Format option '{}' should be a bool, not '{}'".format(key, str(value)))

    if 'extension' not in jupytext_format:
        raise JupytextFormatError('Missing format extension')
    ext = jupytext_format['extension']
    if ext not in NOTEBOOK_EXTENSIONS + ['.auto']:
        raise JupytextFormatError("Extension '{}' is not a notebook extension. Please use one of '{}'.".format(
            ext, "', '".join(NOTEBOOK_EXTENSIONS + ['.auto'])))

    return jupytext_format
github mwouts / jupytext / jupytext / formats.py View on Github external
DeprecationWarning)
            fmt['format_name'] = 'nomarker'
    else:
        ext = jupytext_format

    if ext.rfind('.') > 0:
        fmt['suffix'], ext = os.path.splitext(ext)

    if not ext.startswith('.'):
        ext = '.' + ext

    if ext == '.auto':
        ext = auto_ext_from_metadata(metadata) if metadata is not None else '.auto'
        if not ext:
            if auto_ext_requires_language_info:
                raise JupytextFormatError("No language information in this notebook. Please replace 'auto' with "
                                          "an actual script extension.")
            ext = '.auto'

    fmt['extension'] = ext
    if update:
        fmt.update(update)
    return validate_one_format(fmt)
github mwouts / jupytext / jupytext / formats.py View on Github external
def validate_one_format(jupytext_format):
    """Validate extension and options for the given format"""
    if not isinstance(jupytext_format, dict):
        raise JupytextFormatError('Jupytext format should be a dictionary')

    for key in jupytext_format:
        if key not in _VALID_FORMAT_INFO + _VALID_FORMAT_OPTIONS:
            raise JupytextFormatError("Unknown format option '{}' - should be one of '{}'".format(
                key, "', '".join(_VALID_FORMAT_OPTIONS)))
        value = jupytext_format[key]
        if key in _BINARY_FORMAT_OPTIONS:
            if not isinstance(value, bool):
                raise JupytextFormatError("Format option '{}' should be a bool, not '{}'".format(key, str(value)))

    if 'extension' not in jupytext_format:
        raise JupytextFormatError('Missing format extension')
    ext = jupytext_format['extension']
    if ext not in NOTEBOOK_EXTENSIONS + ['.auto']:
        raise JupytextFormatError("Extension '{}' is not a notebook extension. Please use one of '{}'.".format(
            ext, "', '".join(NOTEBOOK_EXTENSIONS + ['.auto'])))

    return jupytext_format