##// END OF EJS Templates
pylint
Matthias BUSSONNIER -
Show More
@@ -1,224 +1,223 b''
1 """Base classes for the notebook conversion pipeline.
1 """Base classes for the notebook conversion pipeline.
2
2
3 This module defines ConverterTemplate, a highly configurable converter
3 This module defines ConverterTemplate, a highly configurable converter
4 that uses Jinja2 to convert notebook files into different format.
4 that uses Jinja2 to convert notebook files into different format.
5
5
6 You can register both pre-transformers that will act on the notebook format
6 You can register both pre-transformers that will act on the notebook format
7 befor conversion and jinja filter that would then be availlable in the templates
7 befor conversion and jinja filter that would then be availlable in the templates
8 """
8 """
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10 # Copyright (c) 2013, the IPython Development Team.
10 # Copyright (c) 2013, the IPython Development Team.
11 #
11 #
12 # Distributed under the terms of the Modified BSD License.
12 # Distributed under the terms of the Modified BSD License.
13 #
13 #
14 # The full license is in the file COPYING.txt, distributed with this software.
14 # The full license is in the file COPYING.txt, distributed with this software.
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 # Imports
18 # Imports
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 from __future__ import print_function, absolute_import
21 from __future__ import print_function, absolute_import
22
22
23 # Stdlib imports
23 # Stdlib imports
24 import io
24 import io
25
25
26 # IPython imports
26 # IPython imports
27 from IPython.utils.traitlets import MetaHasTraits
27 from IPython.utils.traitlets import MetaHasTraits
28 from IPython.utils.traitlets import (Unicode, List, Bool)
28 from IPython.utils.traitlets import (Unicode, List, Bool)
29 from IPython.config.configurable import Configurable
29 from IPython.config.configurable import Configurable
30 from IPython.nbformat import current as nbformat
30 from IPython.nbformat import current as nbformat
31
31
32 # other libs/dependencies
32 # other libs/dependencies
33 from jinja2 import Environment, FileSystemLoader
33 from jinja2 import Environment, FileSystemLoader
34
34
35
35
36 # local import (pre-transformers)
36 # local import (pre-transformers)
37 import converters.transformers as trans
37 import converters.transformers as trans
38
38
39 # some jinja filters
39 # some jinja filters
40 from converters.jinja_filters import (python_comment, indent,
40 from converters.jinja_filters import (python_comment, indent,
41 rm_fake, remove_ansi, markdown, highlight,
41 rm_fake, remove_ansi, markdown, highlight,
42 ansi2html, markdown2latex, escape_tex, FilterDataType)
42 ansi2html, markdown2latex, escape_tex, FilterDataType)
43
43
44 from converters.utils import markdown2rst
44 from converters.utils import markdown2rst
45 from converters.config import GlobalConfigurable
46
45
47
46
48
47
49 # define differents environemnt with different
48 # define differents environemnt with different
50 # delimiters not to conflict with languages inside
49 # delimiters not to conflict with languages inside
51
50
52 env = Environment(
51 env = Environment(
53 loader=FileSystemLoader([
52 loader=FileSystemLoader([
54 './templates/',
53 './templates/',
55 './templates/skeleton/',
54 './templates/skeleton/',
56 ]),
55 ]),
57 extensions=['jinja2.ext.loopcontrols']
56 extensions=['jinja2.ext.loopcontrols']
58 )
57 )
59
58
60 texenv = Environment(
59 texenv = Environment(
61 loader=FileSystemLoader([
60 loader=FileSystemLoader([
62 './templates/tex/',
61 './templates/tex/',
63 './templates/skeleton/tex/',
62 './templates/skeleton/tex/',
64 ]),
63 ]),
65 extensions=['jinja2.ext.loopcontrols']
64 extensions=['jinja2.ext.loopcontrols']
66 )
65 )
67
66
68
67
69 texenv.block_start_string = '((*'
68 texenv.block_start_string = '((*'
70 texenv.block_end_string = '*))'
69 texenv.block_end_string = '*))'
71
70
72 texenv.variable_start_string = '((('
71 texenv.variable_start_string = '((('
73 texenv.variable_end_string = ')))'
72 texenv.variable_end_string = ')))'
74
73
75 texenv.comment_start_string = '((='
74 texenv.comment_start_string = '((='
76 texenv.comment_end_string = '=))'
75 texenv.comment_end_string = '=))'
77
76
78 texenv.filters['escape_tex'] = escape_tex
77 texenv.filters['escape_tex'] = escape_tex
79
78
80 #-----------------------------------------------------------------------------
79 #-----------------------------------------------------------------------------
81 # Class declarations
80 # Class declarations
82 #-----------------------------------------------------------------------------
81 #-----------------------------------------------------------------------------
83 class ConversionException(Exception):
82 class ConversionException(Exception):
84 pass
83 pass
85
84
86 class ConverterTemplate(Configurable):
85 class ConverterTemplate(Configurable):
87 """ A Jinja2 base converter templates
86 """ A Jinja2 base converter templates
88
87
89 Preprocess the ipynb files, feed it throug jinja templates,
88 Preprocess the ipynb files, feed it throug jinja templates,
90 and spit an converted files and a data object with other data
89 and spit an converted files and a data object with other data
91
90
92 shoudl be mostly configurable
91 shoudl be mostly configurable
93 """
92 """
94
93
95 pre_transformer_order = List(['haspyout_transformer'],
94 pre_transformer_order = List(['haspyout_transformer'],
96 config=True,
95 config=True,
97 help= """
96 help= """
98 An ordered list of pre transformer to apply to the ipynb
97 An ordered list of pre transformer to apply to the ipynb
99 file befor running through templates
98 file befor running through templates
100 """
99 """
101 )
100 )
102
101
103 tex_environement = Bool(False,
102 tex_environement = Bool(False,
104 config=True,
103 config=True,
105 help=""" is this a tex environment or not """)
104 help=""" is this a tex environment or not """)
106
105
107 template_file = Unicode('',
106 template_file = Unicode('',
108 config=True,
107 config=True,
109 help=""" Name of the template file to use """ )
108 help=""" Name of the template file to use """ )
110 #-------------------------------------------------------------------------
109 #-------------------------------------------------------------------------
111 # Instance-level attributes that are set in the constructor for this
110 # Instance-level attributes that are set in the constructor for this
112 # class.
111 # class.
113 #-------------------------------------------------------------------------
112 #-------------------------------------------------------------------------
114
113
115
114
116 preprocessors = []
115 preprocessors = []
117
116
118 def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw):
117 def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw):
119 """ Init a new converter.
118 """ Init a new converter.
120
119
121 config: the Configurable config object to pass around.
120 config: the Configurable config object to pass around.
122
121
123 preprocessors: dict of **availlable** key/value function to run on
122 preprocessors: dict of **availlable** key/value function to run on
124 ipynb json data before conversion to extract/inline file.
123 ipynb json data before conversion to extract/inline file.
125 See `transformer.py` and `ConfigurableTransformers`
124 See `transformer.py` and `ConfigurableTransformers`
126
125
127 set the order in which the transformers should apply
126 set the order in which the transformers should apply
128 with the `pre_transformer_order` trait of this class
127 with the `pre_transformer_order` trait of this class
129
128
130 transformers registerd by this key will take precedence on
129 transformers registerd by this key will take precedence on
131 default one.
130 default one.
132
131
133
132
134 jinja_filters: dict of supplementary jinja filter that should be made
133 jinja_filters: dict of supplementary jinja filter that should be made
135 availlable in template. If those are of Configurable Class type,
134 availlable in template. If those are of Configurable Class type,
136 they will be instanciated with the config object as argument.
135 they will be instanciated with the config object as argument.
137
136
138 user defined filter will overwrite the one availlable by default.
137 user defined filter will overwrite the one availlable by default.
139 """
138 """
140 super(ConverterTemplate, self).__init__(config=config, **kw)
139 super(ConverterTemplate, self).__init__(config=config, **kw)
141
140
142 # variable parameters depending on the pype of jinja environement
141 # variable parameters depending on the pype of jinja environement
143 self.env = texenv if self.tex_environement else env
142 self.env = texenv if self.tex_environement else env
144 self.ext = '.tplx' if self.tex_environement else '.tpl'
143 self.ext = '.tplx' if self.tex_environement else '.tpl'
145
144
146 for name in self.pre_transformer_order:
145 for name in self.pre_transformer_order:
147 # get the user-defined transformer first
146 # get the user-defined transformer first
148 transformer = getattr(preprocessors, name, getattr(trans, name, None))
147 transformer = getattr(preprocessors, name, getattr(trans, name, None))
149 if isinstance(transformer, MetaHasTraits):
148 if isinstance(transformer, MetaHasTraits):
150 transformer = transformer(config=config)
149 transformer = transformer(config=config)
151 self.preprocessors.append(transformer)
150 self.preprocessors.append(transformer)
152
151
153 ## for compat, remove later
152 ## for compat, remove later
154 self.preprocessors.append(trans.coalesce_streams)
153 self.preprocessors.append(trans.coalesce_streams)
155 self.preprocessors.append(trans.ExtractFigureTransformer(config=config))
154 self.preprocessors.append(trans.ExtractFigureTransformer(config=config))
156 self.preprocessors.append(trans.RevealHelpTransformer(config=config))
155 self.preprocessors.append(trans.RevealHelpTransformer(config=config))
157 self.preprocessors.append(trans.CSSHtmlHeaderTransformer(config=config))
156 self.preprocessors.append(trans.CSSHtmlHeaderTransformer(config=config))
158
157
159 ##
158 ##
160 self.env.filters['filter_data_type'] = FilterDataType(config=config)
159 self.env.filters['filter_data_type'] = FilterDataType(config=config)
161 self.env.filters['pycomment'] = python_comment
160 self.env.filters['pycomment'] = python_comment
162 self.env.filters['indent'] = indent
161 self.env.filters['indent'] = indent
163 self.env.filters['rm_fake'] = rm_fake
162 self.env.filters['rm_fake'] = rm_fake
164 self.env.filters['rm_ansi'] = remove_ansi
163 self.env.filters['rm_ansi'] = remove_ansi
165 self.env.filters['markdown'] = markdown
164 self.env.filters['markdown'] = markdown
166 self.env.filters['highlight'] = highlight
165 self.env.filters['highlight'] = highlight
167 self.env.filters['ansi2html'] = ansi2html
166 self.env.filters['ansi2html'] = ansi2html
168 self.env.filters['markdown2latex'] = markdown2latex
167 self.env.filters['markdown2latex'] = markdown2latex
169 self.env.filters['markdown2rst'] = markdown2rst
168 self.env.filters['markdown2rst'] = markdown2rst
170
169
171 ## user filter will overwrite
170 ## user filter will overwrite
172 for key, filtr in jinja_filters.iteritems():
171 for key, filtr in jinja_filters.iteritems():
173 if isinstance(filtr, MetaHasTraits):
172 if isinstance(filtr, MetaHasTraits):
174 self.env.filters[key] = filtr(config=config)
173 self.env.filters[key] = filtr(config=config)
175 else :
174 else :
176 self.env.filters[key] = filtr
175 self.env.filters[key] = filtr
177
176
178 self.template = self.env.get_template(self.template_file+self.ext)
177 self.template = self.env.get_template(self.template_file+self.ext)
179
178
180
179
181 def process(self, nb):
180 def process(self, nb):
182 """
181 """
183 preprocess the notebook json for easier use with the templates.
182 preprocess the notebook json for easier use with the templates.
184 will call all the `preprocessor`s in order before returning it.
183 will call all the `preprocessor`s in order before returning it.
185 """
184 """
186
185
187 # dict of 'resources' that could be made by the preprocessors
186 # dict of 'resources' that could be made by the preprocessors
188 # like key/value data to extract files from ipynb like in latex conversion
187 # like key/value data to extract files from ipynb like in latex conversion
189 resources = {}
188 resources = {}
190
189
191 for preprocessor in self.preprocessors:
190 for preprocessor in self.preprocessors:
192 nb, resources = preprocessor(nb, resources)
191 nb, resources = preprocessor(nb, resources)
193
192
194 return nb, resources
193 return nb, resources
195
194
196 def convert(self, nb):
195 def convert(self, nb):
197 """ convert the ipynb file
196 """ convert the ipynb file
198
197
199 return both the converted ipynb file and a dict containing potential
198 return both the converted ipynb file and a dict containing potential
200 other resources
199 other resources
201 """
200 """
202 nb, resources = self.process(nb)
201 nb, resources = self.process(nb)
203 return self.template.render(nb=nb, resources=resources), resources
202 return self.template.render(nb=nb, resources=resources), resources
204
203
205
204
206 def from_filename(self, filename):
205 def from_filename(self, filename):
207 """read and convert a notebook from a file name"""
206 """read and convert a notebook from a file name"""
208 with io.open(filename) as f:
207 with io.open(filename) as f:
209 return self.convert(nbformat.read(f, 'json'))
208 return self.convert(nbformat.read(f, 'json'))
210
209
211 def from_file(self, filelike):
210 def from_file(self, filelike):
212 """read and convert a notebook from a filelike object
211 """read and convert a notebook from a filelike object
213
212
214 filelike object will just be "read" and should be json format..
213 filelike object will just be "read" and should be json format..
215 """
214 """
216 return self.convert(nbformat.read(filelike, 'json'))
215 return self.convert(nbformat.read(filelike, 'json'))
217
216
218 def from_json(self, json):
217 def from_json(self, json):
219 """ not implemented
218 """ not implemented
220
219
221 Should convert from a json object
220 Should convert from a json object
222 """
221 """
223 raise NotImplementedError('not implemented (yet?)')
222 raise NotImplementedError('not implemented (yet?)')
224
223
1 NO CONTENT: modified file
NO CONTENT: modified file
General Comments 0
You need to be logged in to leave comments. Login now