Show More
@@ -51,11 +51,11 b' class BaseExporter(LoggingConfigurable):' | |||||
51 | reading a notebook from different sources. |
|
51 | reading a notebook from different sources. | |
52 |
|
52 | |||
53 | """ |
|
53 | """ | |
54 |
|
54 | |||
55 | # finish the docstring |
|
55 | # finish the docstring | |
56 |
|
56 | |||
57 | file_extension = Unicode( |
|
57 | file_extension = Unicode( | |
58 |
'txt', config=True, |
|
58 | 'txt', config=True, | |
59 | help="Extension of the file that should be written to disk" |
|
59 | help="Extension of the file that should be written to disk" | |
60 | ) |
|
60 | ) | |
61 |
|
61 | |||
@@ -73,14 +73,14 b' class BaseExporter(LoggingConfigurable):' | |||||
73 | nbtransformers.LatexTransformer, |
|
73 | nbtransformers.LatexTransformer, | |
74 | nbtransformers.SphinxTransformer], |
|
74 | nbtransformers.SphinxTransformer], | |
75 | config=True, |
|
75 | config=True, | |
76 |
help="""List of transformers available by default, by name, namespace, |
|
76 | help="""List of transformers available by default, by name, namespace, | |
77 | instance, or type.""") |
|
77 | instance, or type.""") | |
78 |
|
78 | |||
79 |
|
79 | |||
80 | def __init__(self, config=None, **kw): |
|
80 | def __init__(self, config=None, **kw): | |
81 | """ |
|
81 | """ | |
82 | Public constructor |
|
82 | Public constructor | |
83 |
|
83 | |||
84 | Parameters |
|
84 | Parameters | |
85 | ---------- |
|
85 | ---------- | |
86 | config : config |
|
86 | config : config | |
@@ -88,7 +88,7 b' class BaseExporter(LoggingConfigurable):' | |||||
88 | """ |
|
88 | """ | |
89 | if not config: |
|
89 | if not config: | |
90 | config = self.default_config |
|
90 | config = self.default_config | |
91 |
|
91 | |||
92 | super(BaseExporter, self).__init__(config=config, **kw) |
|
92 | super(BaseExporter, self).__init__(config=config, **kw) | |
93 |
|
93 | |||
94 | #Init |
|
94 | #Init | |
@@ -98,7 +98,7 b' class BaseExporter(LoggingConfigurable):' | |||||
98 | @property |
|
98 | @property | |
99 | def default_config(self): |
|
99 | def default_config(self): | |
100 | return Config() |
|
100 | return Config() | |
101 |
|
101 | |||
102 | def _config_changed(self, name, old, new): |
|
102 | def _config_changed(self, name, old, new): | |
103 | """When setting config, make sure to start with our default_config""" |
|
103 | """When setting config, make sure to start with our default_config""" | |
104 | c = self.default_config |
|
104 | c = self.default_config | |
@@ -112,12 +112,12 b' class BaseExporter(LoggingConfigurable):' | |||||
112 | def from_notebook_node(self, nb, resources=None): |
|
112 | def from_notebook_node(self, nb, resources=None): | |
113 | """ |
|
113 | """ | |
114 | Convert a notebook from a notebook node instance. |
|
114 | Convert a notebook from a notebook node instance. | |
115 |
|
115 | |||
116 | Parameters |
|
116 | Parameters | |
117 | ---------- |
|
117 | ---------- | |
118 | nb : Notebook node |
|
118 | nb : Notebook node | |
119 |
resources : dict (**kw) |
|
119 | resources : dict (**kw) | |
120 |
of additional resources that can be accessed read/write by |
|
120 | of additional resources that can be accessed read/write by | |
121 | transformers. |
|
121 | transformers. | |
122 | """ |
|
122 | """ | |
123 | nb_copy = copy.deepcopy(nb) |
|
123 | nb_copy = copy.deepcopy(nb) | |
@@ -132,7 +132,7 b' class BaseExporter(LoggingConfigurable):' | |||||
132 | def from_filename(self, filename, resources=None, **kw): |
|
132 | def from_filename(self, filename, resources=None, **kw): | |
133 | """ |
|
133 | """ | |
134 | Convert a notebook from a notebook file. |
|
134 | Convert a notebook from a notebook file. | |
135 |
|
135 | |||
136 | Parameters |
|
136 | Parameters | |
137 | ---------- |
|
137 | ---------- | |
138 | filename : str |
|
138 | filename : str | |
@@ -150,7 +150,7 b' class BaseExporter(LoggingConfigurable):' | |||||
150 |
|
150 | |||
151 | modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename)) |
|
151 | modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename)) | |
152 | resources['metadata']['modified_date'] = modified_date.strftime("%B %d, %Y") |
|
152 | resources['metadata']['modified_date'] = modified_date.strftime("%B %d, %Y") | |
153 |
|
153 | |||
154 | with io.open(filename) as f: |
|
154 | with io.open(filename) as f: | |
155 | return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources, **kw) |
|
155 | return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources, **kw) | |
156 |
|
156 | |||
@@ -158,7 +158,7 b' class BaseExporter(LoggingConfigurable):' | |||||
158 | def from_file(self, file_stream, resources=None, **kw): |
|
158 | def from_file(self, file_stream, resources=None, **kw): | |
159 | """ |
|
159 | """ | |
160 | Convert a notebook from a notebook file. |
|
160 | Convert a notebook from a notebook file. | |
161 |
|
161 | |||
162 | Parameters |
|
162 | Parameters | |
163 | ---------- |
|
163 | ---------- | |
164 | file_stream : file-like object |
|
164 | file_stream : file-like object | |
@@ -174,7 +174,7 b' class BaseExporter(LoggingConfigurable):' | |||||
174 | passed into the Jinja templating engine. Transformers are also |
|
174 | passed into the Jinja templating engine. Transformers are also | |
175 | capable of passing additional information to the Jinja |
|
175 | capable of passing additional information to the Jinja | |
176 | templating engine. |
|
176 | templating engine. | |
177 |
|
177 | |||
178 | Parameters |
|
178 | Parameters | |
179 | ---------- |
|
179 | ---------- | |
180 | transformer : transformer |
|
180 | transformer : transformer | |
@@ -190,7 +190,7 b' class BaseExporter(LoggingConfigurable):' | |||||
190 | #this register_transformer method |
|
190 | #this register_transformer method | |
191 | transformer_cls = import_item(transformer) |
|
191 | transformer_cls = import_item(transformer) | |
192 | return self.register_transformer(transformer_cls, enabled) |
|
192 | return self.register_transformer(transformer_cls, enabled) | |
193 |
|
193 | |||
194 | if constructed and hasattr(transformer, '__call__'): |
|
194 | if constructed and hasattr(transformer, '__call__'): | |
195 | #Transformer is a function, no need to construct it. |
|
195 | #Transformer is a function, no need to construct it. | |
196 | #Register and return the transformer. |
|
196 | #Register and return the transformer. | |
@@ -200,7 +200,7 b' class BaseExporter(LoggingConfigurable):' | |||||
200 | return transformer |
|
200 | return transformer | |
201 |
|
201 | |||
202 | elif isclass and isinstance(transformer, MetaHasTraits): |
|
202 | elif isclass and isinstance(transformer, MetaHasTraits): | |
203 |
#Transformer is configurable. Make sure to pass in new default for |
|
203 | #Transformer is configurable. Make sure to pass in new default for | |
204 | #the enabled flag if one was specified. |
|
204 | #the enabled flag if one was specified. | |
205 | self.register_transformer(transformer(parent=self), enabled) |
|
205 | self.register_transformer(transformer(parent=self), enabled) | |
206 |
|
206 | |||
@@ -209,8 +209,8 b' class BaseExporter(LoggingConfigurable):' | |||||
209 | self.register_transformer(transformer(), enabled) |
|
209 | self.register_transformer(transformer(), enabled) | |
210 |
|
210 | |||
211 | else: |
|
211 | else: | |
212 |
#Transformer is an instance of something without a __call__ |
|
212 | #Transformer is an instance of something without a __call__ | |
213 |
#attribute. |
|
213 | #attribute. | |
214 | raise TypeError('transformer') |
|
214 | raise TypeError('transformer') | |
215 |
|
215 | |||
216 |
|
216 | |||
@@ -231,7 +231,7 b' class BaseExporter(LoggingConfigurable):' | |||||
231 | if self.transformers: |
|
231 | if self.transformers: | |
232 | for transformer in self.transformers: |
|
232 | for transformer in self.transformers: | |
233 | self.register_transformer(transformer, enabled=True) |
|
233 | self.register_transformer(transformer, enabled=True) | |
234 |
|
234 | |||
235 |
|
235 | |||
236 | def _init_resources(self, resources): |
|
236 | def _init_resources(self, resources): | |
237 |
|
237 | |||
@@ -241,7 +241,7 b' class BaseExporter(LoggingConfigurable):' | |||||
241 | if not isinstance(resources, ResourcesDict): |
|
241 | if not isinstance(resources, ResourcesDict): | |
242 | new_resources = ResourcesDict() |
|
242 | new_resources = ResourcesDict() | |
243 | new_resources.update(resources) |
|
243 | new_resources.update(resources) | |
244 |
resources = new_resources |
|
244 | resources = new_resources | |
245 |
|
245 | |||
246 | #Make sure the metadata extension exists in resources |
|
246 | #Make sure the metadata extension exists in resources | |
247 | if 'metadata' in resources: |
|
247 | if 'metadata' in resources: | |
@@ -249,19 +249,19 b' class BaseExporter(LoggingConfigurable):' | |||||
249 | resources['metadata'] = ResourcesDict(resources['metadata']) |
|
249 | resources['metadata'] = ResourcesDict(resources['metadata']) | |
250 | else: |
|
250 | else: | |
251 | resources['metadata'] = ResourcesDict() |
|
251 | resources['metadata'] = ResourcesDict() | |
252 |
if not resources['metadata']['name']: |
|
252 | if not resources['metadata']['name']: | |
253 | resources['metadata']['name'] = 'Notebook' |
|
253 | resources['metadata']['name'] = 'Notebook' | |
254 |
|
254 | |||
255 | #Set the output extension |
|
255 | #Set the output extension | |
256 | resources['output_extension'] = self.file_extension |
|
256 | resources['output_extension'] = self.file_extension | |
257 | return resources |
|
257 | return resources | |
258 |
|
258 | |||
259 |
|
259 | |||
260 | def _transform(self, nb, resources): |
|
260 | def _transform(self, nb, resources): | |
261 | """ |
|
261 | """ | |
262 | Preprocess the notebook before passing it into the Jinja engine. |
|
262 | Preprocess the notebook before passing it into the Jinja engine. | |
263 |
To preprocess the notebook is to apply all of the |
|
263 | To preprocess the notebook is to apply all of the | |
264 |
|
264 | |||
265 | Parameters |
|
265 | Parameters | |
266 | ---------- |
|
266 | ---------- | |
267 | nb : notebook node |
|
267 | nb : notebook node | |
@@ -269,8 +269,8 b' class BaseExporter(LoggingConfigurable):' | |||||
269 | resources : a dict of additional resources that |
|
269 | resources : a dict of additional resources that | |
270 | can be accessed read/write by transformers |
|
270 | can be accessed read/write by transformers | |
271 | """ |
|
271 | """ | |
272 |
|
272 | |||
273 |
# Do a copy.deepcopy first, |
|
273 | # Do a copy.deepcopy first, | |
274 | # we are never safe enough with what the transformers could do. |
|
274 | # we are never safe enough with what the transformers could do. | |
275 | nbc = copy.deepcopy(nb) |
|
275 | nbc = copy.deepcopy(nb) | |
276 | resc = copy.deepcopy(resources) |
|
276 | resc = copy.deepcopy(resources) |
General Comments 0
You need to be logged in to leave comments.
Login now