PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/gedlab-khmer-filter-abund/pymodules/python2.7/lib/python/ipython-2.2.0-py2.7.egg/IPython/nbconvert/tests/base.py

https://gitlab.com/pooja043/Globus_Docker_4
Python | 164 lines | 128 code | 11 blank | 25 comment | 2 complexity | 4c8e516e4c2174d80a8237d62e785d70 MD5 | raw file
  1. """
  2. Contains base test class for nbconvert
  3. """
  4. #-----------------------------------------------------------------------------
  5. #Copyright (c) 2013, the IPython Development Team.
  6. #
  7. #Distributed under the terms of the Modified BSD License.
  8. #
  9. #The full license is in the file COPYING.txt, distributed with this software.
  10. #-----------------------------------------------------------------------------
  11. #-----------------------------------------------------------------------------
  12. # Imports
  13. #-----------------------------------------------------------------------------
  14. import io
  15. import os
  16. import glob
  17. import shutil
  18. import unittest
  19. import IPython
  20. from IPython.nbformat import current
  21. from IPython.utils.tempdir import TemporaryWorkingDirectory
  22. from IPython.utils.path import get_ipython_package_dir
  23. from IPython.utils.process import get_output_error_code
  24. from IPython.testing.tools import get_ipython_cmd
  25. # a trailing space allows for simpler concatenation with the other arguments
  26. ipy_cmd = get_ipython_cmd(as_string=True) + " "
  27. #-----------------------------------------------------------------------------
  28. # Classes and functions
  29. #-----------------------------------------------------------------------------
  30. class TestsBase(unittest.TestCase):
  31. """Base tests class. Contains useful fuzzy comparison and nbconvert
  32. functions."""
  33. def fuzzy_compare(self, a, b, newlines_are_spaces=True, tabs_are_spaces=True,
  34. fuzzy_spacing=True, ignore_spaces=False,
  35. ignore_newlines=False, case_sensitive=False, leave_padding=False):
  36. """
  37. Performs a fuzzy comparison of two strings. A fuzzy comparison is a
  38. comparison that ignores insignificant differences in the two comparands.
  39. The significance of certain differences can be specified via the keyword
  40. parameters of this method.
  41. """
  42. if not leave_padding:
  43. a = a.strip()
  44. b = b.strip()
  45. if ignore_newlines:
  46. a = a.replace('\n', '')
  47. b = b.replace('\n', '')
  48. if newlines_are_spaces:
  49. a = a.replace('\n', ' ')
  50. b = b.replace('\n', ' ')
  51. if tabs_are_spaces:
  52. a = a.replace('\t', ' ')
  53. b = b.replace('\t', ' ')
  54. if ignore_spaces:
  55. a = a.replace(' ', '')
  56. b = b.replace(' ', '')
  57. if fuzzy_spacing:
  58. a = self.recursive_replace(a, ' ', ' ')
  59. b = self.recursive_replace(b, ' ', ' ')
  60. if not case_sensitive:
  61. a = a.lower()
  62. b = b.lower()
  63. self.assertEqual(a, b)
  64. def recursive_replace(self, text, search, replacement):
  65. """
  66. Performs a recursive replacement operation. Replaces all instances
  67. of a search string in a text string with a replacement string until
  68. the search string no longer exists. Recursion is needed because the
  69. replacement string may generate additional search strings.
  70. For example:
  71. Replace "ii" with "i" in the string "Hiiii" yields "Hii"
  72. Another replacement cds "Hi" (the desired output)
  73. Parameters
  74. ----------
  75. text : string
  76. Text to replace in.
  77. search : string
  78. String to search for within "text"
  79. replacement : string
  80. String to replace "search" with
  81. """
  82. while search in text:
  83. text = text.replace(search, replacement)
  84. return text
  85. def create_temp_cwd(self, copy_filenames=None):
  86. temp_dir = TemporaryWorkingDirectory()
  87. #Copy the files if requested.
  88. if copy_filenames is not None:
  89. self.copy_files_to(copy_filenames, dest=temp_dir.name)
  90. #Return directory handler
  91. return temp_dir
  92. def create_empty_notebook(self, path):
  93. nb = current.new_notebook()
  94. nb.worksheets.append(current.new_worksheet())
  95. with io.open(path, 'w', encoding='utf-8') as f:
  96. current.write(nb, f, 'json')
  97. def copy_files_to(self, copy_filenames, dest='.'):
  98. "Copy test files into the destination directory"
  99. if not os.path.isdir(dest):
  100. os.makedirs(dest)
  101. files_path = self._get_files_path()
  102. for pattern in copy_filenames:
  103. for match in glob.glob(os.path.join(files_path, pattern)):
  104. shutil.copyfile(match, os.path.join(dest, os.path.basename(match)))
  105. def _get_files_path(self):
  106. #Get the relative path to this module in the IPython directory.
  107. names = self.__module__.split('.')[1:-1]
  108. names.append('files')
  109. #Build a path using the IPython directory and the relative path we just
  110. #found.
  111. path = get_ipython_package_dir()
  112. for name in names:
  113. path = os.path.join(path, name)
  114. return path
  115. def call(self, parameters, ignore_return_code=False):
  116. """
  117. Execute a, IPython shell command, listening for both Errors and non-zero
  118. return codes.
  119. Parameters
  120. ----------
  121. parameters : str
  122. List of parameters to pass to IPython.
  123. ignore_return_code : optional bool (default False)
  124. Throw an OSError if the return code
  125. """
  126. stdout, stderr, retcode = get_output_error_code(ipy_cmd + parameters)
  127. if not (retcode == 0 or ignore_return_code):
  128. raise OSError(stderr)
  129. return stdout, stderr