Uploaded Test files

This commit is contained in:
Batuhan Berk Başoğlu 2020-11-12 11:05:57 -05:00
parent f584ad9d97
commit 2e81cb7d99
16627 changed files with 2065359 additions and 102444 deletions

View file

@ -0,0 +1,29 @@
"""Global configuration class."""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from traitlets import List
from traitlets.config.configurable import LoggingConfigurable
from traitlets import Unicode
class NbConvertBase(LoggingConfigurable):
"""Global configurable class for shared config
Useful for display data priority that might be used by many transformers
"""
display_data_priority = List(['text/html', 'application/pdf', 'text/latex', 'image/svg+xml', 'image/png', 'image/jpeg', 'text/markdown', 'text/plain'],
help= """
An ordered list of preferred output type, the first
encountered will usually be used when converting discarding
the others.
"""
).tag(config=True)
default_language = Unicode('ipython',
help='Deprecated default highlight language as of 5.0, please use language_info metadata instead'
).tag(config=True)
def __init__(self, **kw):
super().__init__(**kw)

View file

@ -0,0 +1,16 @@
"""NbConvert specific exceptions"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
class ConversionException(Exception):
"""An exception raised by the conversion process."""
pass

View file

@ -0,0 +1,47 @@
# coding: utf-8
"""io-related utilities"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import codecs
import sys
def unicode_std_stream(stream='stdout'):
u"""Get a wrapper to write unicode to stdout/stderr as UTF-8.
This ignores environment variables and default encodings, to reliably write
unicode to stdout or stderr.
::
unicode_std_stream().write(u'ł@e¶ŧ←')
"""
assert stream in ('stdout', 'stderr')
stream = getattr(sys, stream)
try:
stream_b = stream.buffer
except AttributeError:
# sys.stdout has been replaced - use it directly
return stream
return codecs.getwriter('utf-8')(stream_b)
def unicode_stdin_stream():
u"""Get a wrapper to read unicode from stdin as UTF-8.
This ignores environment variables and default encodings, to reliably read unicode from stdin.
::
totreat = unicode_stdin_stream().read()
"""
stream = sys.stdin
try:
stream_b = stream.buffer
except AttributeError:
return stream
return codecs.getreader('utf-8')(stream_b)

View file

@ -0,0 +1,6 @@
"""Deprecated as of 5.0; import from IPython.lib.lexers instead."""
from warnings import warn
warn("nbconvert.utils.lexers is deprecated as of 5.0. Use IPython.lib.lexers")
from IPython.lib.lexers import *

View file

@ -0,0 +1,137 @@
"""Utility for calling pandoc"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import print_function, absolute_import
import subprocess
import warnings
import re
from io import TextIOWrapper, BytesIO
from nbconvert.utils.version import check_version
import shutil
from .exceptions import ConversionException
_minimal_version = "1.12.1"
_maximal_version = "3.0.0"
def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'):
"""Convert an input string using pandoc.
Pandoc converts an input string `from` a format `to` a target format.
Parameters
----------
source : string
Input string, assumed to be valid format `from`.
fmt : string
The name of the input format (markdown, etc.)
to : string
The name of the output format (html, etc.)
Returns
-------
out : unicode
Output as returned by pandoc.
Raises
------
PandocMissing
If pandoc is not installed.
Any error messages generated by pandoc are printed to stderr.
"""
cmd = ['pandoc', '-f', fmt, '-t', to]
if extra_args:
cmd.extend(extra_args)
# this will raise an exception that will pop us out of here
check_pandoc_version()
# we can safely continue
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, _ = p.communicate(source.encode())
out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
return out.rstrip('\n')
def get_pandoc_version():
"""Gets the Pandoc version if Pandoc is installed.
If the minimal version is not met, it will probe Pandoc for its version, cache it and return that value.
If the minimal version is met, it will return the cached version and stop probing Pandoc
(unless `clean_cache()` is called).
Raises
------
PandocMissing
If pandoc is unavailable.
"""
global __version
if __version is None:
if not shutil.which('pandoc'):
raise PandocMissing()
out = subprocess.check_output(['pandoc', '-v'])
out_lines = out.splitlines()
version_pattern = re.compile(r"^\d+(\.\d+){1,}$")
for tok in out_lines[0].decode('ascii', 'replace').split():
if version_pattern.match(tok):
__version = tok
break
return __version
def check_pandoc_version():
"""Returns True if pandoc's version meets at least minimal version.
Raises
------
PandocMissing
If pandoc is unavailable.
"""
if check_pandoc_version._cached is not None:
return check_pandoc_version._cached
v = get_pandoc_version()
if v is None:
warnings.warn("Sorry, we cannot determine the version of pandoc.\n"
"Please consider reporting this issue and include the"
"output of pandoc --version.\nContinuing...",
RuntimeWarning, stacklevel=2)
return False
ok = check_version(v, _minimal_version, max_v=_maximal_version)
check_pandoc_version._cached = ok
if not ok:
warnings.warn( "You are using an unsupported version of pandoc (%s).\n" % v +
"Your version must be at least (%s) " % _minimal_version +
"but less than (%s).\n" % _maximal_version +
"Refer to https://pandoc.org/installing.html.\nContinuing with doubts...",
RuntimeWarning, stacklevel=2)
return ok
check_pandoc_version._cached = None
#-----------------------------------------------------------------------------
# Exception handling
#-----------------------------------------------------------------------------
class PandocMissing(ConversionException):
"""Exception raised when Pandoc is missing."""
def __init__(self, *args, **kwargs):
super().__init__( "Pandoc wasn't found.\n" +
"Please check that pandoc is installed:\n" +
"https://pandoc.org/installing.html" )
#-----------------------------------------------------------------------------
# Internal state management
#-----------------------------------------------------------------------------
def clean_cache():
global __version
__version = None
__version = None

View file

@ -0,0 +1,41 @@
# encoding: utf-8
"""Tests for utils.io"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import io as stdlib_io
import sys
import pytest
from ..io import unicode_std_stream
from io import StringIO
def test_UnicodeStdStream():
# Test wrapping a bytes-level stdout
stdoutb = stdlib_io.BytesIO()
stdout = stdlib_io.TextIOWrapper(stdoutb, encoding='ascii')
orig_stdout = sys.stdout
sys.stdout = stdout
try:
sample = u"@łe¶ŧ←"
unicode_std_stream().write(sample)
output = stdoutb.getvalue().decode('utf-8')
assert output == sample
assert not stdout.closed
finally:
sys.stdout = orig_stdout
def test_UnicodeStdStream_nowrap():
# If we replace stdout with a StringIO, it shouldn't get wrapped.
orig_stdout = sys.stdout
sys.stdout = StringIO()
try:
assert unicode_std_stream() is sys.stdout
assert not sys.stdout.closed
finally:
sys.stdout = orig_stdout

View file

@ -0,0 +1,79 @@
"""Test Pandoc module"""
#-----------------------------------------------------------------------------
# Copyright (C) 2014 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
import warnings
from ...tests.utils import onlyif_cmds_exist
from nbconvert.tests.base import TestsBase
from .. import pandoc
#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
class TestPandoc(TestsBase):
"""Collection of Pandoc tests"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.original_env = os.environ.copy()
def setUp(self):
super().setUp()
pandoc.check_pandoc_version._cached = None
@onlyif_cmds_exist('pandoc')
def test_pandoc_available(self):
""" Test behaviour that pandoc functions raise PandocMissing as documented """
pandoc.clean_cache()
os.environ["PATH"] = ""
with self.assertRaises(pandoc.PandocMissing):
pandoc.get_pandoc_version()
with self.assertRaises(pandoc.PandocMissing):
pandoc.check_pandoc_version()
with self.assertRaises(pandoc.PandocMissing):
pandoc.pandoc("", "markdown", "html")
# original_env["PATH"] should contain pandoc
os.environ["PATH"] = self.original_env["PATH"]
with warnings.catch_warnings(record=True) as w:
pandoc.get_pandoc_version()
pandoc.check_pandoc_version()
pandoc.pandoc("", "markdown", "html")
self.assertEqual(w, [])
@onlyif_cmds_exist('pandoc')
def test_minimal_version(self):
original_minversion = pandoc._minimal_version
pandoc._minimal_version = "120.0"
with warnings.catch_warnings(record=True) as w:
# call it twice to verify the cached value is used
assert not pandoc.check_pandoc_version()
assert not pandoc.check_pandoc_version()
# only one warning after two calls, due to cache
self.assertEqual(len(w), 1)
# clear cache
pandoc.check_pandoc_version._cached = None
pandoc._minimal_version = pandoc.get_pandoc_version()
assert pandoc.check_pandoc_version()
def pandoc_function_raised_missing(f, *args, **kwargs):
try:
f(*args, **kwargs)
except pandoc.PandocMissing:
return True
else:
return False

View file

@ -0,0 +1,12 @@
from ..version import check_version
def test_check_version():
"""Test the behaviour of check_versionself.
This is mostly used to make sure the pandoc version is appropriate for the library.
"""
assert check_version('1.19.2.4', '1.12.1')
assert check_version('2.2.3.2', '1.12.1')
assert check_version('1.19.2.4', '1.12.1', max_v='2.0')
assert not check_version('2.2.3.2', '1.12.1', max_v='2.0')

View file

@ -0,0 +1,35 @@
# encoding: utf-8
"""
Utilities for version comparison
It is a bit ridiculous that we need these.
"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from distutils.version import LooseVersion
def check_version(v, min_v, max_v=None):
"""check version string v >= min_v and v < max_v
Parameters
----------
v : str
version of the package
min_v : str
minimal version supported
max_v : str
earliest version not supported
Note: If dev/prerelease tags result in TypeError for string-number
comparison, it is assumed that the check passes and the version dependency
is satisfied. Users on dev branches are responsible for keeping their own
packages up to date.
"""
try:
below_max = LooseVersion(v) < LooseVersion(max_v) if max_v is not None else True
return LooseVersion(v) >= LooseVersion(min_v) and below_max
except TypeError:
return True