Show More
@@ -0,0 +1,57 | |||||
|
1 | #!/usr/bin/env python | |||
|
2 | """ | |||
|
3 | Contains writer base class. | |||
|
4 | """ | |||
|
5 | #----------------------------------------------------------------------------- | |||
|
6 | #Copyright (c) 2013, the IPython Development Team. | |||
|
7 | # | |||
|
8 | #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 | ||||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | # Imports | |||
|
15 | #----------------------------------------------------------------------------- | |||
|
16 | ||||
|
17 | from IPython.utils.traitlets import List | |||
|
18 | ||||
|
19 | from ..utils.config import GlobalConfigurable | |||
|
20 | ||||
|
21 | #----------------------------------------------------------------------------- | |||
|
22 | # Classes | |||
|
23 | #----------------------------------------------------------------------------- | |||
|
24 | ||||
|
25 | class WriterBase(GlobalConfigurable): | |||
|
26 | """Consumes output from nbconvert export...() methods and writes to a | |||
|
27 | useful location. """ | |||
|
28 | ||||
|
29 | ||||
|
30 | files = List([], config=True, help=""" | |||
|
31 | List of the files that the notebook references. Files will be | |||
|
32 | included with written output.""") | |||
|
33 | ||||
|
34 | ||||
|
35 | def __init__(self, config=None, **kw): | |||
|
36 | """ | |||
|
37 | Constructor | |||
|
38 | """ | |||
|
39 | super(WriterBase, self).__init__(config=config, **kw) | |||
|
40 | ||||
|
41 | ||||
|
42 | def write(self, output, resources, **kw): | |||
|
43 | """ | |||
|
44 | Consume and write Jinja output. | |||
|
45 | ||||
|
46 | Parameters | |||
|
47 | ---------- | |||
|
48 | output : string | |||
|
49 | Conversion results. This string contains the file contents of the | |||
|
50 | converted file. | |||
|
51 | resources : dict | |||
|
52 | Resources created and filled by the nbconvert conversion process. | |||
|
53 | Includes output from transformers, such as the extract figure | |||
|
54 | transformer. | |||
|
55 | """ | |||
|
56 | ||||
|
57 | raise NotImplementedError() |
@@ -0,0 +1,43 | |||||
|
1 | #!/usr/bin/env python | |||
|
2 | """ | |||
|
3 | Contains debug writer. | |||
|
4 | """ | |||
|
5 | #----------------------------------------------------------------------------- | |||
|
6 | #Copyright (c) 2013, the IPython Development Team. | |||
|
7 | # | |||
|
8 | #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 | ||||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | # Imports | |||
|
15 | #----------------------------------------------------------------------------- | |||
|
16 | ||||
|
17 | from .base import WriterBase | |||
|
18 | from pprint import pprint | |||
|
19 | ||||
|
20 | #----------------------------------------------------------------------------- | |||
|
21 | # Classes | |||
|
22 | #----------------------------------------------------------------------------- | |||
|
23 | ||||
|
24 | class DebugWriter(WriterBase): | |||
|
25 | """Consumes output from nbconvert export...() methods and writes usefull | |||
|
26 | debugging information to the stdout. The information includes a list of | |||
|
27 | resources that were extracted from the notebook(s) during export.""" | |||
|
28 | ||||
|
29 | ||||
|
30 | def write(self, output, resources, **kw): | |||
|
31 | """ | |||
|
32 | Consume and write Jinja output. | |||
|
33 | ||||
|
34 | See base for more... | |||
|
35 | """ | |||
|
36 | ||||
|
37 | if 'figures' in resources: | |||
|
38 | print("Figures extracted from %s" % notebook_name) | |||
|
39 | print('-' * 80) | |||
|
40 | pprint.pprint(resources['figures'], indent=2, width=70) | |||
|
41 | else: | |||
|
42 | print("No figures extracted from %s" % notebook_name) | |||
|
43 | print('=' * 80) |
@@ -0,0 +1,98 | |||||
|
1 | #!/usr/bin/env python | |||
|
2 | """ | |||
|
3 | Contains writer for writing nbconvert output to filesystem. | |||
|
4 | """ | |||
|
5 | #----------------------------------------------------------------------------- | |||
|
6 | #Copyright (c) 2013, the IPython Development Team. | |||
|
7 | # | |||
|
8 | #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 | ||||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | # Imports | |||
|
15 | #----------------------------------------------------------------------------- | |||
|
16 | ||||
|
17 | import io | |||
|
18 | import os | |||
|
19 | import shutil | |||
|
20 | import glob | |||
|
21 | ||||
|
22 | from IPython.utils.traitlets import Unicode | |||
|
23 | ||||
|
24 | from .base import WriterBase | |||
|
25 | ||||
|
26 | #----------------------------------------------------------------------------- | |||
|
27 | # Classes | |||
|
28 | #----------------------------------------------------------------------------- | |||
|
29 | ||||
|
30 | class FilesWriter(WriterBase): | |||
|
31 | """Consumes nbconvert output and produces files.""" | |||
|
32 | ||||
|
33 | ||||
|
34 | build_directory = Unicode("nbconvert_build", config=True, | |||
|
35 | help="""Directory to write output to. Leave blank | |||
|
36 | to output to the current directory""") | |||
|
37 | ||||
|
38 | ||||
|
39 | #Make sure that the output directory exists. | |||
|
40 | def _build_directory_changed(self, name, old, new): | |||
|
41 | if new and not os.path.isdir(new): | |||
|
42 | os.makedirs(new) | |||
|
43 | ||||
|
44 | ||||
|
45 | def __init__(self, **kw): | |||
|
46 | super(FilesWriter, self).__init__(**kw) | |||
|
47 | self._build_directory_changed('build_directory', self.build_directory, | |||
|
48 | self.build_directory) | |||
|
49 | ||||
|
50 | ||||
|
51 | def write(self, output, resources, notebook_name=None, **kw): | |||
|
52 | """ | |||
|
53 | Consume and write Jinja output to the file system. Output directory | |||
|
54 | is set via the 'build_directory' variable of this instance (a | |||
|
55 | configurable). | |||
|
56 | ||||
|
57 | See base for more... | |||
|
58 | """ | |||
|
59 | ||||
|
60 | #Pull the extension from the resources dict. | |||
|
61 | output_extension = resources['output_extension'] | |||
|
62 | ||||
|
63 | #Write all of the extracted resources to the destination directory. | |||
|
64 | #NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG | |||
|
65 | #TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS... | |||
|
66 | for filename, data in resources.get('figures', {}).items(): | |||
|
67 | ||||
|
68 | #Determine where to write the file to | |||
|
69 | dest = os.path.join(self.build_directory, filename) | |||
|
70 | ||||
|
71 | #Write file | |||
|
72 | with io.open(dest, 'wb') as f: | |||
|
73 | f.write(data) | |||
|
74 | ||||
|
75 | #Copy referenced files to output directory | |||
|
76 | if self.build_directory: | |||
|
77 | for filename in self.files: | |||
|
78 | ||||
|
79 | #Copy files that match search pattern | |||
|
80 | for matching_filename in glob.glob(filename): | |||
|
81 | ||||
|
82 | #Make sure folder exists. | |||
|
83 | dest = os.path.join(self.build_directory, filename) | |||
|
84 | path = os.path.dirname(dest) | |||
|
85 | if not os.path.isdir(path): | |||
|
86 | os.makedirs(path) | |||
|
87 | ||||
|
88 | #Copy | |||
|
89 | shutil.copyfile(matching_filename, dest) | |||
|
90 | ||||
|
91 | #Determine where to write conversion results. | |||
|
92 | dest = notebook_name + '.' + output_extension | |||
|
93 | if self.build_directory: | |||
|
94 | dest = os.path.join(self.build_directory, dest) | |||
|
95 | ||||
|
96 | #Write conversion results. | |||
|
97 | with io.open(dest, 'w') as f: | |||
|
98 | f.write(output) |
@@ -0,0 +1,37 | |||||
|
1 | #!/usr/bin/env python | |||
|
2 | """ | |||
|
3 | Contains Stdout writer | |||
|
4 | """ | |||
|
5 | #----------------------------------------------------------------------------- | |||
|
6 | #Copyright (c) 2013, the IPython Development Team. | |||
|
7 | # | |||
|
8 | #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 | ||||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | # Imports | |||
|
15 | #----------------------------------------------------------------------------- | |||
|
16 | ||||
|
17 | from .base import WriterBase | |||
|
18 | ||||
|
19 | #----------------------------------------------------------------------------- | |||
|
20 | # Classes | |||
|
21 | #----------------------------------------------------------------------------- | |||
|
22 | ||||
|
23 | class StdoutWriter(WriterBase): | |||
|
24 | """Consumes output from nbconvert export...() methods and writes to the | |||
|
25 | stdout stream. Allows for quick debuging of nbconvert output. Using the | |||
|
26 | debug flag makes the writer pretty-print the figures contained within the | |||
|
27 | notebook.""" | |||
|
28 | ||||
|
29 | ||||
|
30 | def write(self, output, resources, **kw): | |||
|
31 | """ | |||
|
32 | Consume and write Jinja output. | |||
|
33 | ||||
|
34 | See base for more... | |||
|
35 | """ | |||
|
36 | ||||
|
37 | print(output) |
General Comments 0
You need to be logged in to leave comments.
Login now