Show More
@@ -1,111 +1,93 b'' | |||
|
1 | """ | |
|
2 | Module that re-groups preprocessor that would be applied to ipynb files | |
|
3 | before going through the templating machinery. | |
|
4 | ||
|
5 | It exposes a convenient class to inherit from to access configurability. | |
|
6 | """ | |
|
7 | #----------------------------------------------------------------------------- | |
|
8 | # Copyright (c) 2013, the IPython Development Team. | |
|
9 | # | |
|
10 | # Distributed under the terms of the Modified BSD License. | |
|
11 | # | |
|
12 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
13 | #----------------------------------------------------------------------------- | |
|
1 | """Base class for preprocessors""" | |
|
14 | 2 | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | # Imports | |
|
17 | #----------------------------------------------------------------------------- | |
|
3 | # Copyright (c) IPython Development Team. | |
|
4 | # Distributed under the terms of the Modified BSD License. | |
|
18 | 5 | |
|
19 | 6 | from ..utils.base import NbConvertBase |
|
20 | 7 | from IPython.utils.traitlets import Bool |
|
21 | 8 | |
|
22 | #----------------------------------------------------------------------------- | |
|
23 | # Classes and Functions | |
|
24 | #----------------------------------------------------------------------------- | |
|
25 | 9 | |
|
26 | 10 | class Preprocessor(NbConvertBase): |
|
27 | 11 | """ A configurable preprocessor |
|
28 | 12 | |
|
29 | 13 | Inherit from this class if you wish to have configurability for your |
|
30 | 14 | preprocessor. |
|
31 | 15 | |
|
32 | 16 | Any configurable traitlets this class exposed will be configurable in |
|
33 | profiles using c.SubClassName.atribute=value | |
|
17 | profiles using c.SubClassName.attribute = value | |
|
34 | 18 | |
|
35 | 19 | you can overwrite :meth:`preprocess_cell` to apply a transformation |
|
36 | 20 | independently on each cell or :meth:`preprocess` if you prefer your own |
|
37 | 21 | logic. See corresponding docstring for informations. |
|
38 | 22 | |
|
39 | 23 | Disabled by default and can be enabled via the config by |
|
40 | 24 | 'c.YourPreprocessorName.enabled = True' |
|
41 | 25 | """ |
|
42 | 26 | |
|
43 | 27 | enabled = Bool(False, config=True) |
|
44 | 28 | |
|
45 | 29 | def __init__(self, **kw): |
|
46 | 30 | """ |
|
47 | 31 | Public constructor |
|
48 | 32 | |
|
49 | 33 | Parameters |
|
50 | 34 | ---------- |
|
51 | 35 | config : Config |
|
52 | 36 | Configuration file structure |
|
53 | 37 | **kw : misc |
|
54 | 38 | Additional arguments |
|
55 | 39 | """ |
|
56 | 40 | |
|
57 | 41 | super(Preprocessor, self).__init__(**kw) |
|
58 | 42 | |
|
59 | 43 | |
|
60 | 44 | def __call__(self, nb, resources): |
|
61 | 45 | if self.enabled: |
|
46 | self.log.debug("Applying preprocessor: %s", self.__class__.__name__) | |
|
62 | 47 | return self.preprocess(nb,resources) |
|
63 | 48 | else: |
|
64 | 49 | return nb, resources |
|
65 | 50 | |
|
66 | 51 | |
|
67 | 52 | def preprocess(self, nb, resources): |
|
68 | 53 | """ |
|
69 | 54 | Preprocessing to apply on each notebook. |
|
70 | 55 | |
|
71 |
|
|
|
56 | Must return modified nb, resources. | |
|
57 | ||
|
72 | 58 | If you wish to apply your preprocessing to each cell, you might want |
|
73 |
to over |
|
|
59 | to override preprocess_cell method instead. | |
|
74 | 60 | |
|
75 | 61 | Parameters |
|
76 | 62 | ---------- |
|
77 | 63 | nb : NotebookNode |
|
78 | 64 | Notebook being converted |
|
79 | 65 | resources : dictionary |
|
80 | 66 | Additional resources used in the conversion process. Allows |
|
81 | 67 | preprocessors to pass variables into the Jinja engine. |
|
82 | 68 | """ |
|
83 | self.log.debug("Applying preprocess: %s", self.__class__.__name__) | |
|
84 | try : | |
|
85 | for worksheet in nb.worksheets: | |
|
86 | for index, cell in enumerate(worksheet.cells): | |
|
87 | worksheet.cells[index], resources = self.preprocess_cell(cell, resources, index) | |
|
88 | return nb, resources | |
|
89 | except NotImplementedError: | |
|
90 | raise NotImplementedError('should be implemented by subclass') | |
|
69 | for worksheet in nb.worksheets: | |
|
70 | for index, cell in enumerate(worksheet.cells): | |
|
71 | worksheet.cells[index], resources = self.preprocess_cell(cell, resources, index) | |
|
72 | return nb, resources | |
|
91 | 73 | |
|
92 | 74 | |
|
93 | 75 | def preprocess_cell(self, cell, resources, index): |
|
94 | 76 | """ |
|
95 |
Over |
|
|
96 |
|
|
|
77 | Override if you want to apply some preprocessing to each cell. | |
|
78 | Must return modified cell and resource dictionary. | |
|
97 | 79 | |
|
98 | 80 | Parameters |
|
99 | 81 | ---------- |
|
100 | 82 | cell : NotebookNode cell |
|
101 | 83 | Notebook cell being processed |
|
102 | 84 | resources : dictionary |
|
103 | 85 | Additional resources used in the conversion process. Allows |
|
104 | 86 | preprocessors to pass variables into the Jinja engine. |
|
105 | 87 | index : int |
|
106 | 88 | Index of the cell being processed |
|
107 | 89 | """ |
|
108 | 90 | |
|
109 | 91 | raise NotImplementedError('should be implemented by subclass') |
|
110 | 92 | return cell, resources |
|
111 | 93 |
@@ -1,85 +1,77 b'' | |||
|
1 | """Module that allows latex output notebooks to be conditioned before | |
|
2 | they are converted. Exposes a decorator (@cell_preprocessor) in | |
|
3 | addition to the coalesce_streams pre-proccessor. | |
|
4 | """ | |
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
1 | """Preprocessor for merging consecutive stream outputs for easier handling.""" | |
|
2 | ||
|
3 | # Copyright (c) IPython Development Team. | |
|
8 | 4 | # Distributed under the terms of the Modified BSD License. |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | 5 | |
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | 6 | import re |
|
17 | 7 | |
|
18 | #----------------------------------------------------------------------------- | |
|
19 | # Functions | |
|
20 | #----------------------------------------------------------------------------- | |
|
21 | 8 | def cell_preprocessor(function): |
|
22 | 9 | """ |
|
23 | 10 | Wrap a function to be executed on all cells of a notebook |
|
24 | 11 | |
|
25 | 12 | The wrapped function should have these parameters: |
|
26 | 13 | |
|
27 | 14 | cell : NotebookNode cell |
|
28 | 15 | Notebook cell being processed |
|
29 | 16 | resources : dictionary |
|
30 | 17 | Additional resources used in the conversion process. Allows |
|
31 | 18 | preprocessors to pass variables into the Jinja engine. |
|
32 | 19 | index : int |
|
33 | 20 | Index of the cell being processed |
|
34 | 21 | """ |
|
35 | 22 | |
|
36 | 23 | def wrappedfunc(nb, resources): |
|
37 | for worksheet in nb.worksheets : | |
|
24 | from IPython.config import Application | |
|
25 | if Application.initialized(): | |
|
26 | Application.instance().log.debug( | |
|
27 | "Applying preprocessor: %s", function.__name__ | |
|
28 | ) | |
|
29 | for worksheet in nb.worksheets: | |
|
38 | 30 | for index, cell in enumerate(worksheet.cells): |
|
39 | 31 | worksheet.cells[index], resources = function(cell, resources, index) |
|
40 | 32 | return nb, resources |
|
41 | 33 | return wrappedfunc |
|
42 | 34 | |
|
43 | 35 | cr_pat = re.compile(r'.*\r(?=[^\n])') |
|
44 | 36 | |
|
45 | 37 | @cell_preprocessor |
|
46 | 38 | def coalesce_streams(cell, resources, index): |
|
47 | 39 | """ |
|
48 | 40 | Merge consecutive sequences of stream output into single stream |
|
49 | 41 | to prevent extra newlines inserted at flush calls |
|
50 | 42 | |
|
51 | 43 | Parameters |
|
52 | 44 | ---------- |
|
53 | 45 | cell : NotebookNode cell |
|
54 | 46 | Notebook cell being processed |
|
55 | 47 | resources : dictionary |
|
56 | 48 | Additional resources used in the conversion process. Allows |
|
57 | 49 | transformers to pass variables into the Jinja engine. |
|
58 | 50 | index : int |
|
59 | 51 | Index of the cell being processed |
|
60 | 52 | """ |
|
61 | 53 | |
|
62 | 54 | outputs = cell.get('outputs', []) |
|
63 | 55 | if not outputs: |
|
64 | 56 | return cell, resources |
|
65 | 57 | |
|
66 | 58 | last = outputs[0] |
|
67 | 59 | new_outputs = [last] |
|
68 | 60 | for output in outputs[1:]: |
|
69 | 61 | if (output.output_type == 'stream' and |
|
70 | 62 | last.output_type == 'stream' and |
|
71 | 63 | last.stream == output.stream |
|
72 | 64 | ): |
|
73 | 65 | last.text += output.text |
|
74 | 66 | |
|
75 | 67 | else: |
|
76 | 68 | new_outputs.append(output) |
|
77 | 69 | last = output |
|
78 | 70 | |
|
79 | 71 | # process \r characters |
|
80 | 72 | for output in new_outputs: |
|
81 | 73 | if output.output_type == 'stream': |
|
82 | 74 | output.text = cr_pat.sub('', output.text) |
|
83 | 75 | |
|
84 | 76 | cell.outputs = new_outputs |
|
85 | 77 | return cell, resources |
General Comments 0
You need to be logged in to leave comments.
Login now