##// END OF EJS Templates
Cleanup.
AnneTheAgile -
Show More
@@ -1,153 +1,151 b''
1 1 """Export to PDF via latex"""
2 2
3 3 # Copyright (c) IPython Development Team.
4 4 # Distributed under the terms of the Modified BSD License.
5 5
6 6 import subprocess
7 7 import os
8 8 import sys
9 9 import shutil
10 10
11 11 from IPython.utils.traitlets import Integer, List, Bool, Instance
12 12 from IPython.utils.tempdir import TemporaryWorkingDirectory
13 13 from .latex import LatexExporter
14 14
15 15
16 16 class PDFExporter(LatexExporter):
17 17 """Writer designed to write to PDF files"""
18 18
19 19 latex_count = Integer(3, config=True,
20 20 help="How many times latex will be called."
21 21 )
22 22
23 23 latex_command = List([u"pdflatex", u"{filename}"], config=True,
24 24 help="Shell command used to compile latex."
25 25 )
26 26
27 27 bib_command = List([u"bibtex", u"{filename}"], config=True,
28 28 help="Shell command used to run bibtex."
29 29 )
30 30
31 31 verbose = Bool(False, config=True,
32 32 help="Whether to display the output of latex commands."
33 33 )
34 34
35 35 temp_file_exts = List(['.aux', '.bbl', '.blg', '.idx', '.log', '.out'], config=True,
36 36 help="File extensions of temp files to remove after running."
37 37 )
38 38
39 39 writer = Instance("IPython.nbconvert.writers.FilesWriter", args=())
40 40
41 41 def valid_on_path(self, command):
42 42 """Ensure the given command exists in the OS PATH."""
43 43 if (shutil.which(command)==None) :
44 44 raise FileNotFoundError("NBConvert requires this command to be on the System PATH: "+str(command))
45 45
46 46 def run_command(self, command_list, filename, count, log_function):
47 47 """Run command_list count times.
48 48
49 49 Parameters
50 50 ----------
51 51 command_list : list
52 52 A list of args to provide to Popen. Each element of this
53 53 list will be interpolated with the filename to convert.
54 54 filename : unicode
55 55 The name of the file to convert.
56 56 count : int
57 57 How many times to run the command.
58 58
59 59 Returns
60 60 -------
61 61 success : bool
62 62 A boolean indicating if the command was successful (True)
63 63 or failed (False).
64 64 """
65 65 command = [c.format(filename=filename) for c in command_list]
66 66
67 67 # On windows with python 2.x there is a bug in subprocess.Popen and
68 68 # unicode commands are not supported
69 69 if sys.platform == 'win32' and sys.version_info < (3,0):
70 70 #We must use cp1252 encoding for calling subprocess.Popen
71 71 #Note that sys.stdin.encoding and encoding.DEFAULT_ENCODING
72 72 # could be different (cp437 in case of dos console)
73 73 command = [c.encode('cp1252') for c in command]
74 74
75 75 self.valid_on_path(command_list[0])
76 76
77 77 times = 'time' if count == 1 else 'times'
78 78 self.log.info("Running %s %i %s: %s", command_list[0], count, times, command)
79 79 with open(os.devnull, 'rb') as null:
80 80 stdout = subprocess.PIPE if not self.verbose else None
81 81 for index in range(count):
82 82 p = subprocess.Popen(command, stdout=stdout, stdin=null)
83 83 out, err = p.communicate()
84 84 if p.returncode:
85 85 if self.verbose:
86 86 # verbose means I didn't capture stdout with PIPE,
87 87 # so it's already been displayed and `out` is None.
88 88 out = u''
89 89 else:
90 90 out = out.decode('utf-8', 'replace')
91 91 log_function(command, out)
92 92 return False # failure
93 93 return True # success
94 94
95 95 def run_latex(self, filename):
96 96 """Run pdflatex self.latex_count times."""
97 97
98 98 def log_error(command, out):
99 99 self.log.critical(u"%s failed: %s\n%s", command[0], command, out)
100 100
101 the_cmd = self.latex_command[0]
102 self.log.info("About to test shutil of Running : %s", self.latex_command[0])
103 101 return self.run_command(self.latex_command, filename,
104 self.latex_count, log_error)
102 self.latex_count, log_error)
105 103
106 104 def run_bib(self, filename):
107 105 """Run bibtex self.latex_count times."""
108 106 filename = os.path.splitext(filename)[0]
109 107
110 108 def log_error(command, out):
111 109 self.log.warn('%s had problems, most likely because there were no citations',
112 110 command[0])
113 111 self.log.debug(u"%s output: %s\n%s", command[0], command, out)
114 112
115 113 return self.run_command(self.bib_command, filename, 1, log_error)
116 114
117 115 def clean_temp_files(self, filename):
118 116 """Remove temporary files created by pdflatex/bibtex."""
119 117 self.log.info("Removing temporary LaTeX files")
120 118 filename = os.path.splitext(filename)[0]
121 119 for ext in self.temp_file_exts:
122 120 try:
123 121 os.remove(filename+ext)
124 122 except OSError:
125 123 pass
126 124
127 125 def from_notebook_node(self, nb, resources=None, **kw):
128 126 latex, resources = super(PDFExporter, self).from_notebook_node(
129 127 nb, resources=resources, **kw
130 128 )
131 129 with TemporaryWorkingDirectory() as td:
132 130 notebook_name = "notebook"
133 131 tex_file = self.writer.write(latex, resources, notebook_name=notebook_name)
134 132 self.log.info("Building PDF")
135 133 rc = self.run_latex(tex_file)
136 134 if not rc:
137 135 rc = self.run_bib(tex_file)
138 136 if not rc:
139 137 rc = self.run_latex(tex_file)
140 138
141 139 pdf_file = notebook_name + '.pdf'
142 140 if not os.path.isfile(pdf_file):
143 141 raise RuntimeError("PDF creating failed")
144 142 self.log.info('PDF successfully created')
145 143 with open(pdf_file, 'rb') as f:
146 144 pdf_data = f.read()
147 145
148 146 # convert output extension to pdf
149 147 # the writer above required it to be tex
150 148 resources['output_extension'] = 'pdf'
151 149
152 150 return pdf_data, resources
153 151
General Comments 0
You need to be logged in to leave comments. Login now