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