##// END OF EJS Templates
Added a space between pound and comments (@minrk request)
Jonathan Frederic -
Show More
@@ -15,13 +15,13 b' Command-line interface for the NbConvert conversion utility.'
15 #Imports
15 #Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #Stdlib imports
18 # Stdlib imports
19 from __future__ import print_function
19 from __future__ import print_function
20 import sys
20 import sys
21 import os
21 import os
22 import glob
22 import glob
23
23
24 #From IPython
24 # From IPython
25 from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags
25 from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags
26 from IPython.config import catch_config_error, Configurable
26 from IPython.config import catch_config_error, Configurable
27 from IPython.utils.traitlets import (
27 from IPython.utils.traitlets import (
@@ -103,7 +103,7 b' class NbConvertApp(BaseIPythonApplication):'
103
103
104 > ipython nbconvert --config mycfg.py
104 > ipython nbconvert --config mycfg.py
105 """.format(get_export_names()))
105 """.format(get_export_names()))
106 #Writer specific variables
106 # Writer specific variables
107 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
107 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
108 help="""Instance of the writer class used to write the
108 help="""Instance of the writer class used to write the
109 results of the conversion.""")
109 results of the conversion.""")
@@ -121,7 +121,7 b' class NbConvertApp(BaseIPythonApplication):'
121 self.writer_factory = import_item(new)
121 self.writer_factory = import_item(new)
122
122
123
123
124 #Other configurable variables
124 # Other configurable variables
125 export_format = CaselessStrEnum(get_export_names(),
125 export_format = CaselessStrEnum(get_export_names(),
126 default_value="full_html",
126 default_value="full_html",
127 config=True,
127 config=True,
@@ -153,7 +153,7 b' class NbConvertApp(BaseIPythonApplication):'
153 else:
153 else:
154 patterns = self.notebooks
154 patterns = self.notebooks
155
155
156 #Use glob to replace all the notebook patterns with filenames.
156 # Use glob to replace all the notebook patterns with filenames.
157 filenames = []
157 filenames = []
158 for pattern in patterns:
158 for pattern in patterns:
159 for filename in glob.glob(pattern):
159 for filename in glob.glob(pattern):
@@ -179,18 +179,18 b' class NbConvertApp(BaseIPythonApplication):'
179 """
179 """
180 Convert the notebooks in the self.notebook traitlet
180 Convert the notebooks in the self.notebook traitlet
181 """
181 """
182 #Export each notebook
182 # Export each notebook
183 conversion_success = 0
183 conversion_success = 0
184 for notebook_filename in self.notebooks:
184 for notebook_filename in self.notebooks:
185
185
186 #Get a unique key for the notebook and set it in the resources object.
186 # Get a unique key for the notebook and set it in the resources object.
187 basename = os.path.basename(notebook_filename)
187 basename = os.path.basename(notebook_filename)
188 notebook_name = basename[:basename.rfind('.')]
188 notebook_name = basename[:basename.rfind('.')]
189 resources = {}
189 resources = {}
190 resources['unique_key'] = notebook_name
190 resources['unique_key'] = notebook_name
191 resources['output_files_dir'] = '%s_files' % notebook_name
191 resources['output_files_dir'] = '%s_files' % notebook_name
192
192
193 #Try to export
193 # Try to export
194 try:
194 try:
195 output, resources = export_by_name(self.export_format,
195 output, resources = export_by_name(self.export_format,
196 notebook_filename,
196 notebook_filename,
@@ -203,22 +203,22 b' class NbConvertApp(BaseIPythonApplication):'
203 "\n\t" + "\n\t".join(get_export_names()),
203 "\n\t" + "\n\t".join(get_export_names()),
204 file=sys.stderr)
204 file=sys.stderr)
205 sys.exit(-1)
205 sys.exit(-1)
206 #except Exception as e:
206 # except Exception as e:
207 #print("Error: could not export '%s'" % notebook_filename, file=sys.stderr)
207 # print("Error: could not export '%s'" % notebook_filename, file=sys.stderr)
208 #print(e, file=sys.stderr)
208 # print(e, file=sys.stderr)
209 else:
209 else:
210 self.writer.write(output, resources, notebook_name=notebook_name)
210 self.writer.write(output, resources, notebook_name=notebook_name)
211 conversion_success += 1
211 conversion_success += 1
212
212
213 #If nothing was converted successfully, help the user.
213 # If nothing was converted successfully, help the user.
214 if conversion_success == 0:
214 if conversion_success == 0:
215
215
216 #No notebooks were specified, show help.
216 # No notebooks were specified, show help.
217 if len(self.notebooks) == 0:
217 if len(self.notebooks) == 0:
218 self.print_help()
218 self.print_help()
219
219
220 #Notebooks were specified, but not converted successfully. Show how
220 # Notebooks were specified, but not converted successfully. Show how
221 #to access help.
221 # to access help.
222 else:
222 else:
223 print('For help, use "ipython nbconvert --help"')
223 print('For help, use "ipython nbconvert --help"')
224
224
@@ -36,7 +36,7 b' class FilesWriter(WriterBase):'
36 to output to the current directory""")
36 to output to the current directory""")
37
37
38
38
39 #Make sure that the output directory exists.
39 # Make sure that the output directory exists.
40 def _build_directory_changed(self, name, old, new):
40 def _build_directory_changed(self, name, old, new):
41 if new and not os.path.isdir(new):
41 if new and not os.path.isdir(new):
42 os.makedirs(new)
42 os.makedirs(new)
@@ -57,46 +57,46 b' class FilesWriter(WriterBase):'
57 See base for more...
57 See base for more...
58 """
58 """
59
59
60 #Pull the extension from the resources dict.
60 # Pull the extension and subdir from the resources dict.
61 output_extension = resources['output_extension']
61 output_extension = resources['output_extension']
62
62
63 #Write all of the extracted resources to the destination directory.
63 # Write all of the extracted resources to the destination directory.
64 #NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG
64 # NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG
65 #TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS...
65 # TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS...
66 for filename, data in resources.get('figures', {}).items():
66 for filename, data in resources.get('figures', {}).items():
67
67
68 #Determine where to write the file to
68 # Determine where to write the file to
69 dest = os.path.join(self.build_directory, filename)
69 dest = os.path.join(self.build_directory, filename)
70 path = os.path.dirname(dest)
70 path = os.path.dirname(dest)
71 if not os.path.isdir(path):
71 if not os.path.isdir(path):
72 os.makedirs(path)
72 os.makedirs(path)
73
73
74 #Write file
74 # Write file
75 with io.open(dest, 'wb') as f:
75 with io.open(dest, 'wb') as f:
76 f.write(data)
76 f.write(data)
77
77
78 #Copy referenced files to output directory
78 # Copy referenced files to output directory
79 if self.build_directory:
79 if self.build_directory:
80 for filename in self.files:
80 for filename in self.files:
81
81
82 #Copy files that match search pattern
82 # Copy files that match search pattern
83 for matching_filename in glob.glob(filename):
83 for matching_filename in glob.glob(filename):
84
84
85 #Make sure folder exists.
85 # Make sure folder exists.
86 dest = os.path.join(self.build_directory, filename)
86 dest = os.path.join(self.build_directory, filename)
87 path = os.path.dirname(dest)
87 path = os.path.dirname(dest)
88 if not os.path.isdir(path):
88 if not os.path.isdir(path):
89 os.makedirs(path)
89 os.makedirs(path)
90
90
91 #Copy if destination is different.
91 # Copy if destination is different.
92 if not os.path.normpath(dest) == os.path.normpath(matching_filename):
92 if not os.path.normpath(dest) == os.path.normpath(matching_filename):
93 shutil.copyfile(matching_filename, dest)
93 shutil.copyfile(matching_filename, dest)
94
94
95 #Determine where to write conversion results.
95 # Determine where to write conversion results.
96 dest = notebook_name + '.' + output_extension
96 dest = notebook_name + '.' + output_extension
97 if self.build_directory:
97 if self.build_directory:
98 dest = os.path.join(self.build_directory, dest)
98 dest = os.path.join(self.build_directory, dest)
99
99
100 #Write conversion results.
100 # Write conversion results.
101 with io.open(dest, 'w') as f:
101 with io.open(dest, 'w') as f:
102 f.write(output)
102 f.write(output)
General Comments 0
You need to be logged in to leave comments. Login now