Show More
@@ -1,110 +1,110 b'' | |||||
1 | """ |
|
1 | """ | |
2 | Module that re-groups transformer that would be applied to ipynb files |
|
2 | Module that re-groups transformer that would be applied to ipynb files | |
3 | before going through the templating machinery. |
|
3 | before going through the templating machinery. | |
4 |
|
4 | |||
5 | It exposes a convenient class to inherit from to access configurability. |
|
5 | It exposes a convenient class to inherit from to access configurability. | |
6 | """ |
|
6 | """ | |
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (c) 2013, the IPython Development Team. |
|
8 | # Copyright (c) 2013, the IPython Development Team. | |
9 | # |
|
9 | # | |
10 | # Distributed under the terms of the Modified BSD License. |
|
10 | # Distributed under the terms of the Modified BSD License. | |
11 | # |
|
11 | # | |
12 | # The full license is in the file COPYING.txt, distributed with this software. |
|
12 | # The full license is in the file COPYING.txt, distributed with this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | from ..utils.base import NbConvertBase |
|
19 | from ..utils.base import NbConvertBase | |
20 | from IPython.utils.traitlets import Bool |
|
20 | from IPython.utils.traitlets import Bool | |
21 |
|
21 | |||
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 | # Classes and Functions |
|
23 | # Classes and Functions | |
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 |
|
25 | |||
26 | class Transformer(NbConvertBase): |
|
26 | class Transformer(NbConvertBase): | |
27 | """ A configurable transformer |
|
27 | """ A configurable transformer | |
28 |
|
28 | |||
29 | Inherit from this class if you wish to have configurability for your |
|
29 | Inherit from this class if you wish to have configurability for your | |
30 | transformer. |
|
30 | transformer. | |
31 |
|
31 | |||
32 | Any configurable traitlets this class exposed will be configurable in profiles |
|
32 | Any configurable traitlets this class exposed will be configurable in profiles | |
33 | using c.SubClassName.atribute=value |
|
33 | using c.SubClassName.atribute=value | |
34 |
|
34 | |||
35 | you can overwrite transform_cell to apply a transformation independently on each cell |
|
35 | you can overwrite `transform_cell` to apply a transformation independently on each cell | |
36 |
or |
|
36 | or `call` if you prefer your own logic. See corresponding docstring for informations. | |
37 |
|
37 | |||
38 | Disabled by default and can be enabled via the config by |
|
38 | Disabled by default and can be enabled via the config by | |
39 | 'c.YourTransformerName.enabled = True' |
|
39 | 'c.YourTransformerName.enabled = True' | |
40 | """ |
|
40 | """ | |
41 |
|
41 | |||
42 | enabled = Bool(False, config=True) |
|
42 | enabled = Bool(False, config=True) | |
43 |
|
43 | |||
44 | def __init__(self, **kw): |
|
44 | def __init__(self, **kw): | |
45 | """ |
|
45 | """ | |
46 | Public constructor |
|
46 | Public constructor | |
47 |
|
47 | |||
48 | Parameters |
|
48 | Parameters | |
49 | ---------- |
|
49 | ---------- | |
50 | config : Config |
|
50 | config : Config | |
51 | Configuration file structure |
|
51 | Configuration file structure | |
52 | **kw : misc |
|
52 | **kw : misc | |
53 | Additional arguments |
|
53 | Additional arguments | |
54 | """ |
|
54 | """ | |
55 |
|
55 | |||
56 | super(Transformer, self).__init__(**kw) |
|
56 | super(Transformer, self).__init__(**kw) | |
57 |
|
57 | |||
58 |
|
58 | |||
59 | def __call__(self, nb, resources): |
|
59 | def __call__(self, nb, resources): | |
60 | if self.enabled: |
|
60 | if self.enabled: | |
61 | return self.call(nb,resources) |
|
61 | return self.call(nb,resources) | |
62 | else: |
|
62 | else: | |
63 | return nb, resources |
|
63 | return nb, resources | |
64 |
|
64 | |||
65 |
|
65 | |||
66 | def call(self, nb, resources): |
|
66 | def call(self, nb, resources): | |
67 | """ |
|
67 | """ | |
68 | Transformation to apply on each notebook. |
|
68 | Transformation to apply on each notebook. | |
69 |
|
69 | |||
70 | You should return modified nb, resources. |
|
70 | You should return modified nb, resources. | |
71 | If you wish to apply your transform on each cell, you might want to |
|
71 | If you wish to apply your transform on each cell, you might want to | |
72 | overwrite transform_cell method instead. |
|
72 | overwrite transform_cell method instead. | |
73 |
|
73 | |||
74 | Parameters |
|
74 | Parameters | |
75 | ---------- |
|
75 | ---------- | |
76 | nb : NotebookNode |
|
76 | nb : NotebookNode | |
77 | Notebook being converted |
|
77 | Notebook being converted | |
78 | resources : dictionary |
|
78 | resources : dictionary | |
79 | Additional resources used in the conversion process. Allows |
|
79 | Additional resources used in the conversion process. Allows | |
80 | transformers to pass variables into the Jinja engine. |
|
80 | transformers to pass variables into the Jinja engine. | |
81 | """ |
|
81 | """ | |
82 | self.log.debug("Applying transform: %s", self.__class__.__name__) |
|
82 | self.log.debug("Applying transform: %s", self.__class__.__name__) | |
83 | try : |
|
83 | try : | |
84 | for worksheet in nb.worksheets: |
|
84 | for worksheet in nb.worksheets: | |
85 | for index, cell in enumerate(worksheet.cells): |
|
85 | for index, cell in enumerate(worksheet.cells): | |
86 | worksheet.cells[index], resources = self.transform_cell(cell, resources, index) |
|
86 | worksheet.cells[index], resources = self.transform_cell(cell, resources, index) | |
87 | return nb, resources |
|
87 | return nb, resources | |
88 | except NotImplementedError: |
|
88 | except NotImplementedError: | |
89 | raise NotImplementedError('should be implemented by subclass') |
|
89 | raise NotImplementedError('should be implemented by subclass') | |
90 |
|
90 | |||
91 |
|
91 | |||
92 | def transform_cell(self, cell, resources, index): |
|
92 | def transform_cell(self, cell, resources, index): | |
93 | """ |
|
93 | """ | |
94 | Overwrite if you want to apply a transformation on each cell. You |
|
94 | Overwrite if you want to apply a transformation on each cell. You | |
95 | should return modified cell and resource dictionary. |
|
95 | should return modified cell and resource dictionary. | |
96 |
|
96 | |||
97 | Parameters |
|
97 | Parameters | |
98 | ---------- |
|
98 | ---------- | |
99 | cell : NotebookNode cell |
|
99 | cell : NotebookNode cell | |
100 | Notebook cell being processed |
|
100 | Notebook cell being processed | |
101 | resources : dictionary |
|
101 | resources : dictionary | |
102 | Additional resources used in the conversion process. Allows |
|
102 | Additional resources used in the conversion process. Allows | |
103 | transformers to pass variables into the Jinja engine. |
|
103 | transformers to pass variables into the Jinja engine. | |
104 | index : int |
|
104 | index : int | |
105 | Index of the cell being processed |
|
105 | Index of the cell being processed | |
106 | """ |
|
106 | """ | |
107 |
|
107 | |||
108 | raise NotImplementedError('should be implemented by subclass') |
|
108 | raise NotImplementedError('should be implemented by subclass') | |
109 | return cell, resources |
|
109 | return cell, resources | |
110 |
|
110 |
General Comments 0
You need to be logged in to leave comments.
Login now