-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabext.py
174 lines (140 loc) · 5.42 KB
/
fabext.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'''
Copyright 2013 Wenjun Deng <wdeng@wdeng.info>
This file is part of Omega Tools.
Omega Tools is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Omega Tools is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Omega Tools. If not, see <http://www.gnu.org/licenses/>.
This file provides some classes to simply building of gnuplot plots and LaTeX
documents using fabricate <https://code.google.com/p/fabricate/>.
'''
__version__ = '2013-05-30 18:21:28-04:00'
import os
from fabricate import *
# make use of default_builder in fabricate
from fabricate import _set_default_builder
_set_default_builder()
from fabricate import default_builder
class FabGnuplot:
'''Class for building gnuplot plots using fabricate. So far only supports
mp terminal.
Usage example:
Suppose there are dir1/a.gp and dir2/b.gp to be built, use:
from fabext import *
list_fig = ['dir1/a', 'dir2/b']
fg = FabGnuplot(list_fig)
fg.build_mps() # use this line to build mps figures
fg.build_eps() # use this line to build eps figures
fg.build_pdf() # use this line to build pdf figures
'''
def __init__(self, list_fig):
'''list_fig is a list of figure file names without extention,
e.g., if dir1/a.gp and dir2/b.gp need to be built,
use ['dir1/a', 'dir2/b'] as list_fig.'''
self.list_fig = list_fig
def gp2mp(self, filename):
'''filename should have no .gp extension'''
run('gnuplot_lmp', filename + '.gp')
def mp2mps(self, filename):
'''filename should have no .mp extension'''
run('mpost_mps', filename + '.mp')
def mps2eps(self, filename):
'''filename should have no .mps extension'''
run('mps2eps', filename + '.mps')
def mps2pdf(self, filename):
'''filename should have no .mps extension'''
run('mptopdf_mps', filename + '.mps')
def build_mps(self):
'''Build mps figures'''
for fig in self.list_fig:
self.gp2mp(fig)
after()
for fig in self.list_fig:
self.mp2mps(fig)
def build_eps(self):
'''Build eps figures'''
self.build_mps()
after()
for fig in self.list_fig:
self.mps2eps(fig)
def build_pdf(self):
'''Build pdf figures'''
self.build_mps()
after()
for fig in self.list_fig:
self.mps2pdf(fig)
def clean(self):
'''Remove temporary output files'''
for fig in self.list_fig:
for ext in ['.mp', '.mp~', '.mpx', '_mp.log', '.log']:
try:
os.remove(fig + ext)
except OSError, e:
default_builder.echo_delete(fig + ext, e)
else:
default_builder.echo_delete(fig + ext)
def cleanall(self):
'''Remove all output files'''
self.clean()
for fig in self.list_fig:
for ext in ['.mps', '.eps', '.pdf']:
try:
os.remove(fig + ext)
except OSError, e:
default_builder.echo_delete(fig + ext, e)
else:
default_builder.echo_delete(fig + ext)
class FabLatex:
'''Class for building LaTeX documents using fabricate. latexmk is needed.
Usage example:
Suppose there is main.tex to be built, use:
from fabext import *
fl = FabLatex(['main'])
fl.build() # use this line to call latex to build dvi document
fl.build_pdfdvi() # use this line to call latex then dvipdf to build pdf
fl.build_pdf() # use this line to use pdflatex to build pdf document
'''
def __init__(self, list_tex):
'''list_tex is a list of LaTeX file names without '.tex' extention,
e.g., if main.tex needs to be built,
use ['main'] as list_tex.'''
self.list_tex = list_tex
def _build(self, opt):
for tex in self.list_tex:
cmd = ('latexmk', ) + opt + (tex + '.tex', )
run(*cmd)
def build(self):
self._build(())
def build_pdfdvi(self):
self._build(('-pdfdvi', ))
def build_pdf(self):
self._build(('-pdf', ))
def clean(self):
'''Remove temporary output files'''
for tex in self.list_tex:
for ext in ['.aux', '.bbl', '.blg', 'Notes.bib', '.out', \
'.log', '.nav', '.snm', '.toc', '.fls', '.spl', \
'.fdb_latexmk']:
try:
os.remove(tex + ext)
except OSError, e:
default_builder.echo_delete(tex + ext, e)
else:
default_builder.echo_delete(tex + ext)
def cleanall(self):
'''Remove all output files'''
self.clean()
for tex in self.list_tex:
for ext in ['.dvi', '.ps', '.pdf']:
try:
os.remove(tex + ext)
except OSError, e:
default_builder.echo_delete(tex + ext, e)
else:
default_builder.echo_delete(tex + ext)