Show More
@@ -1,36 +1,35 b'' | |||
|
1 | #!/usr/bin/env python | |
|
2 | 1 |
|
|
3 | 2 | Basic post processor |
|
4 | 3 | """ |
|
5 | 4 | #----------------------------------------------------------------------------- |
|
6 | 5 | #Copyright (c) 2013, the IPython Development Team. |
|
7 | 6 | # |
|
8 | 7 | #Distributed under the terms of the Modified BSD License. |
|
9 | 8 | # |
|
10 | 9 | #The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 10 | #----------------------------------------------------------------------------- |
|
12 | 11 | |
|
13 | 12 | #----------------------------------------------------------------------------- |
|
14 | 13 | # Imports |
|
15 | 14 | #----------------------------------------------------------------------------- |
|
16 | 15 | |
|
17 | 16 | from ..utils.base import NbConvertBase |
|
18 | 17 | |
|
19 | 18 | |
|
20 | 19 | #----------------------------------------------------------------------------- |
|
21 | 20 | # Classes |
|
22 | 21 | #----------------------------------------------------------------------------- |
|
23 | 22 | class PostProcessorBase(NbConvertBase): |
|
24 | 23 | |
|
25 | 24 | def __call__(self, input): |
|
26 | 25 | """ |
|
27 | 26 | See def call() ... |
|
28 | 27 | """ |
|
29 | 28 | self.call(input) |
|
30 | 29 | |
|
31 | 30 | |
|
32 | 31 | def call(self, input): |
|
33 | 32 | """ |
|
34 | 33 | Post-process output from a writer. |
|
35 | 34 | """ |
|
36 | 35 | raise NotImplementedError('call') |
@@ -1,178 +1,177 b'' | |||
|
1 | #!/usr/bin/env python | |
|
2 | 1 |
|
|
3 | 2 | Contains base test class for nbconvert |
|
4 | 3 | """ |
|
5 | 4 | #----------------------------------------------------------------------------- |
|
6 | 5 | #Copyright (c) 2013, the IPython Development Team. |
|
7 | 6 | # |
|
8 | 7 | #Distributed under the terms of the Modified BSD License. |
|
9 | 8 | # |
|
10 | 9 | #The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 10 | #----------------------------------------------------------------------------- |
|
12 | 11 | |
|
13 | 12 | #----------------------------------------------------------------------------- |
|
14 | 13 | # Imports |
|
15 | 14 | #----------------------------------------------------------------------------- |
|
16 | 15 | |
|
17 | 16 | import subprocess |
|
18 | 17 | import os |
|
19 | 18 | import glob |
|
20 | 19 | import shutil |
|
21 | 20 | import sys |
|
22 | 21 | |
|
23 | 22 | import IPython |
|
24 | 23 | from IPython.utils.tempdir import TemporaryDirectory |
|
25 | 24 | from IPython.utils import py3compat |
|
26 | 25 | |
|
27 | 26 | #----------------------------------------------------------------------------- |
|
28 | 27 | # Classes and functions |
|
29 | 28 | #----------------------------------------------------------------------------- |
|
30 | 29 | |
|
31 | 30 | class TemporaryWorkingDirectory(TemporaryDirectory): |
|
32 | 31 | """ |
|
33 | 32 | Creates a temporary directory and sets the cwd to that directory. |
|
34 | 33 | Automatically reverts to previous cwd upon cleanup. |
|
35 | 34 | Usage example: |
|
36 | 35 | |
|
37 | 36 | with TemporaryWorakingDirectory() as tmpdir: |
|
38 | 37 | ... |
|
39 | 38 | """ |
|
40 | 39 | |
|
41 | 40 | def __init__(self, **kw): |
|
42 | 41 | """ |
|
43 | 42 | Constructor |
|
44 | 43 | """ |
|
45 | 44 | super(TemporaryWorkingDirectory, self).__init__(**kw) |
|
46 | 45 | |
|
47 | 46 | #Change cwd to new temp dir. Remember old cwd. |
|
48 | 47 | self.old_wd = os.getcwd() |
|
49 | 48 | os.chdir(self.name) |
|
50 | 49 | |
|
51 | 50 | |
|
52 | 51 | def cleanup(self): |
|
53 | 52 | """ |
|
54 | 53 | Destructor |
|
55 | 54 | """ |
|
56 | 55 | |
|
57 | 56 | #Revert to old cwd. |
|
58 | 57 | os.chdir(self.old_wd) |
|
59 | 58 | |
|
60 | 59 | #Cleanup |
|
61 | 60 | super(TemporaryWorkingDirectory, self).cleanup() |
|
62 | 61 | |
|
63 | 62 | |
|
64 | 63 | class TestsBase(object): |
|
65 | 64 | """Base tests class. Contains usefull fuzzy comparison and nbconvert |
|
66 | 65 | functions.""" |
|
67 | 66 | |
|
68 | 67 | |
|
69 | 68 | def fuzzy_compare(self, a, b, newlines_are_spaces=True, tabs_are_spaces=True, |
|
70 | 69 | fuzzy_spacing=True, ignore_spaces=False, |
|
71 | 70 | ignore_newlines=False, case_sensitive=False): |
|
72 | 71 | """ |
|
73 | 72 | Performs a fuzzy comparison of two strings. A fuzzy comparison is a |
|
74 | 73 | comparison that ignores insignificant differences in the two comparands. |
|
75 | 74 | The significance of certain differences can be specified via the keyword |
|
76 | 75 | parameters of this method. |
|
77 | 76 | """ |
|
78 | 77 | |
|
79 | 78 | if newlines_are_spaces: |
|
80 | 79 | a = a.replace('\n', ' ') |
|
81 | 80 | b = b.replace('\n', ' ') |
|
82 | 81 | |
|
83 | 82 | if tabs_are_spaces: |
|
84 | 83 | a = a.replace('\t', ' ') |
|
85 | 84 | b = b.replace('\t', ' ') |
|
86 | 85 | |
|
87 | 86 | if ignore_spaces: |
|
88 | 87 | a = a.replace(' ', '') |
|
89 | 88 | b = b.replace(' ', '') |
|
90 | 89 | |
|
91 | 90 | if fuzzy_spacing: |
|
92 | 91 | a = self.recursive_replace(a, ' ', ' ') |
|
93 | 92 | b = self.recursive_replace(b, ' ', ' ') |
|
94 | 93 | |
|
95 | 94 | if ignore_newlines: |
|
96 | 95 | a = a.replace('\n', '') |
|
97 | 96 | b = b.replace('\n', '') |
|
98 | 97 | |
|
99 | 98 | if not case_sensitive: |
|
100 | 99 | a = a.lower() |
|
101 | 100 | b = b.lower() |
|
102 | 101 | |
|
103 | 102 | return a == b |
|
104 | 103 | |
|
105 | 104 | |
|
106 | 105 | def recursive_replace(self, text, search, replacement): |
|
107 | 106 | """ |
|
108 | 107 | Performs a recursive replacement operation. Replaces all instances |
|
109 | 108 | of a search string in a text string with a replacement string until |
|
110 | 109 | the search string no longer exists. Recursion is needed because the |
|
111 | 110 | replacement string may generate additional search strings. |
|
112 | 111 | |
|
113 | 112 | For example: |
|
114 | 113 | Replace "ii" with "i" in the string "Hiiii" yields "Hii" |
|
115 | 114 | Another replacement yields "Hi" (the desired output) |
|
116 | 115 | |
|
117 | 116 | Parameters: |
|
118 | 117 | ----------- |
|
119 | 118 | text : string |
|
120 | 119 | Text to replace in. |
|
121 | 120 | search : string |
|
122 | 121 | String to search for within "text" |
|
123 | 122 | replacement : string |
|
124 | 123 | String to replace "search" with |
|
125 | 124 | """ |
|
126 | 125 | while search in text: |
|
127 | 126 | text = text.replace(search, replacement) |
|
128 | 127 | return text |
|
129 | 128 | |
|
130 | 129 | |
|
131 | 130 | def create_temp_cwd(self, copy_filenames=None): |
|
132 | 131 | temp_dir = TemporaryWorkingDirectory() |
|
133 | 132 | |
|
134 | 133 | #Copy the files if requested. |
|
135 | 134 | if not copy_filenames is None: |
|
136 | 135 | self.copy_files_to(copy_filenames) |
|
137 | 136 | |
|
138 | 137 | #Return directory handler |
|
139 | 138 | return temp_dir |
|
140 | 139 | |
|
141 | 140 | |
|
142 | 141 | def copy_files_to(self, copy_filenames=None, destination=None): |
|
143 | 142 | |
|
144 | 143 | #Copy test files into the destination directory. |
|
145 | 144 | if copy_filenames: |
|
146 | 145 | for pattern in copy_filenames: |
|
147 | 146 | for match in glob.glob(os.path.join(self._get_files_path(), pattern)): |
|
148 | 147 | if destination is None: |
|
149 | 148 | shutil.copyfile(match, os.path.basename(match)) |
|
150 | 149 | else: |
|
151 | 150 | if not os.path.isdir(destination): |
|
152 | 151 | os.makedirs(destination) |
|
153 | 152 | shutil.copyfile(match, os.path.join(destination, os.path.basename(match))) |
|
154 | 153 | |
|
155 | 154 | |
|
156 | 155 | def _get_files_path(self): |
|
157 | 156 | |
|
158 | 157 | #Get the relative path to this module in the IPython directory. |
|
159 | 158 | names = self.__module__.split('.')[1:-1] |
|
160 | 159 | names.append('files') |
|
161 | 160 | |
|
162 | 161 | #Build a path using the IPython directory and the relative path we just |
|
163 | 162 | #found. |
|
164 | 163 | path = IPython.__path__[0] |
|
165 | 164 | for name in names: |
|
166 | 165 | path = os.path.join(path, name) |
|
167 | 166 | return path |
|
168 | 167 | |
|
169 | 168 | |
|
170 | 169 | def call(self, parameters): |
|
171 | 170 | output = subprocess.Popen(parameters, stdout=subprocess.PIPE).communicate()[0] |
|
172 | 171 | |
|
173 | 172 | #Convert the output to a string if running Python3 |
|
174 | 173 | if py3compat.PY3: |
|
175 | 174 | return output.decode('utf-8') |
|
176 | 175 | else: |
|
177 | 176 | return output |
|
178 | 177 | No newline at end of file |
@@ -1,57 +1,56 b'' | |||
|
1 | #!/usr/bin/env python | |
|
2 | 1 |
|
|
3 | 2 | Contains writer base class. |
|
4 | 3 | """ |
|
5 | 4 | #----------------------------------------------------------------------------- |
|
6 | 5 | #Copyright (c) 2013, the IPython Development Team. |
|
7 | 6 | # |
|
8 | 7 | #Distributed under the terms of the Modified BSD License. |
|
9 | 8 | # |
|
10 | 9 | #The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 10 | #----------------------------------------------------------------------------- |
|
12 | 11 | |
|
13 | 12 | #----------------------------------------------------------------------------- |
|
14 | 13 | # Imports |
|
15 | 14 | #----------------------------------------------------------------------------- |
|
16 | 15 | |
|
17 | 16 | from IPython.utils.traitlets import List |
|
18 | 17 | |
|
19 | 18 | from ..utils.base import NbConvertBase |
|
20 | 19 | |
|
21 | 20 | #----------------------------------------------------------------------------- |
|
22 | 21 | # Classes |
|
23 | 22 | #----------------------------------------------------------------------------- |
|
24 | 23 | |
|
25 | 24 | class WriterBase(NbConvertBase): |
|
26 | 25 | """Consumes output from nbconvert export...() methods and writes to a |
|
27 | 26 | useful location. """ |
|
28 | 27 | |
|
29 | 28 | |
|
30 | 29 | files = List([], config=True, help=""" |
|
31 | 30 | List of the files that the notebook references. Files will be |
|
32 | 31 | included with written output.""") |
|
33 | 32 | |
|
34 | 33 | |
|
35 | 34 | def __init__(self, config=None, **kw): |
|
36 | 35 | """ |
|
37 | 36 | Constructor |
|
38 | 37 | """ |
|
39 | 38 | super(WriterBase, self).__init__(config=config, **kw) |
|
40 | 39 | |
|
41 | 40 | |
|
42 | 41 | def write(self, output, resources, **kw): |
|
43 | 42 | """ |
|
44 | 43 | Consume and write Jinja output. |
|
45 | 44 | |
|
46 | 45 | Parameters |
|
47 | 46 | ---------- |
|
48 | 47 | output : string |
|
49 | 48 | Conversion results. This string contains the file contents of the |
|
50 | 49 | converted file. |
|
51 | 50 | resources : dict |
|
52 | 51 | Resources created and filled by the nbconvert conversion process. |
|
53 | 52 | Includes output from transformers, such as the extract figure |
|
54 | 53 | transformer. |
|
55 | 54 | """ |
|
56 | 55 | |
|
57 | 56 | raise NotImplementedError() |
General Comments 0
You need to be logged in to leave comments.
Login now