we probably don't need a shell
[PyX.git] / config.py
blob6f3ae940a60319eb030360d11f2caf68d6876403
1 # -*- encoding: utf-8 -*-
4 # Copyright (C) 2003-2011 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2003-2011 André Wobst <wobsta@users.sourceforge.net>
7 # This file is part of PyX (http://pyx.sourceforge.net/).
9 # PyX is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # PyX is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with PyX; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 import configparser, os.path, warnings
24 import os, io, warnings, pkgutil
26 from . import pycompat
28 builtinopen = open
30 try:
31 import pykpathsea as pykpathsea_module
32 has_pykpathsea = True
33 except ImportError:
34 has_pykpathsea = False
37 # Locators implement an open method which returns a list of functions
38 # by searching for a file according to a specific rule. Each of the functions
39 # returned can be called (multiple times) and return an open file. The
40 # opening of the file might fail with a IOError which indicates, that the
41 # file could not be found at the given location.
42 # names is a list of kpsewhich format names to be used for searching where as
43 # extensions is a list of file extensions to be tried (including the dot). Note
44 # that the list of file extenions should include an empty string to not add
45 # an extension at all.
47 locator_classes = {}
49 class local:
50 """locates files in the current directory"""
52 def openers(self, filename, names, extensions):
53 return [lambda extension=extension: builtinopen(filename+extension, "rb") for extension in extensions]
55 locator_classes["local"] = local
58 class internal:
59 """locates files within the PyX data tree"""
61 def openers(self, filename, names, extensions):
62 for extension in extensions:
63 full_filename = filename+extension
64 dir = os.path.splitext(full_filename)[1][1:]
65 try:
66 data = pkgutil.get_data("pyx", "data/%s/%s" % (dir, full_filename))
67 except IOError:
68 pass
69 else:
70 if data:
71 return [lambda: io.BytesIO(data)]
72 return []
74 locator_classes["internal"] = internal
77 class recursivedir:
78 """locates files by searching recursively in a list of directories"""
80 def __init__(self):
81 self.dirs = getlist("filelocator", "recursivedir")
82 self.full_filenames = {}
84 def openers(self, filename, names, extensions):
85 for extension in extensions:
86 if filename+extension in self.full_filenames:
87 return [lambda: builtinopen(self.full_filenames[filename], "rb")]
88 while self.dirs:
89 dir = self.dirs.pop(0)
90 for item in os.listdir(dir):
91 full_item = os.path.join(dir, item)
92 if os.path.isdir(full_item):
93 self.dirs.insert(0, full_item)
94 else:
95 self.full_filenames[item] = full_item
96 for extension in extensions:
97 if filename+extension in self.full_filenames:
98 return [lambda: builtinopen(self.full_filenames[filename], "rb")]
99 return []
101 locator_classes["recursivedir"] = recursivedir
104 class ls_R:
105 """locates files by searching a list of ls-R files"""
107 def __init__(self):
108 self.ls_Rs = getlist("filelocator", "ls-R")
109 self.full_filenames = {}
111 def openers(self, filename, names, extensions):
112 while self.ls_Rs and not any([filename+extension in self.full_filenames for extension in extensions]):
113 lsr = self.ls_Rs.pop(0)
114 base_dir = os.path.dirname(lsr)
115 dir = None
116 first = True
117 with builtinopen(lsr, "r", encoding="ascii", errors="surrogateescape") as lsrfile:
118 for line in lsrfile:
119 line = line.rstrip()
120 if first and line.startswith("%"):
121 continue
122 first = False
123 if line.endswith(":"):
124 dir = os.path.join(base_dir, line[:-1])
125 elif line:
126 self.full_filenames[line] = os.path.join(dir, line)
127 for extension in extensions:
128 if filename+extension in self.full_filenames:
129 def _opener():
130 try:
131 return builtinopen(self.full_filenames[filename+extension], "rb")
132 except IOError:
133 warnings.warn("'%s' should be available at '%s' according to the ls-R file, "
134 "but the file is not available at this location; "
135 "update your ls-R file" % (filename, self.full_filenames[filename]))
136 return [_opener]
137 return []
139 locator_classes["ls-R"] = ls_R
142 class pykpathsea:
143 """locate files by pykpathsea (a C extension module wrapping libkpathsea)"""
145 def openers(self, filename, names, extensions):
146 if not has_pykpathsea:
147 return []
148 for name in names:
149 full_filename = pykpathsea_module.find_file(filename, name)
150 if full_filename:
151 break
152 else:
153 return []
154 def _opener():
155 try:
156 return builtinopen(full_filename, "rb")
157 except IOError:
158 warnings.warn("'%s' should be available at '%s' according to libkpathsea, "
159 "but the file is not available at this location; "
160 "update your kpsewhich database" % (filename, full_filename))
161 return [_opener]
163 locator_classes["pykpathsea"] = pykpathsea
166 # class libkpathsea:
167 # """locate files by libkpathsea using ctypes"""
169 # def openers(self, filename, names, extensions):
170 # raise NotImplemented
172 # locator_classes["libpathsea"] = libkpathsea
175 class kpsewhich:
176 """locate files using the kpsewhich executable"""
178 def openers(self, filename, names, extensions):
179 for name in names:
180 try:
181 with pycompat.popen('kpsewhich --format="%s" "%s"' % (name, filename)) as output:
182 full_filenames = output.read()
183 except OSError:
184 return []
185 if full_filenames:
186 break
187 else:
188 return []
189 full_filename = full_filenames.decode("ascii").split("\n")[0].rstrip("\r")
191 # Detect Cygwin kpsewhich on Windows Python
192 if os.name == "nt" and full_filename.startswith("/"):
193 full_filename = pycompat.popen('cygpath -w "%s"' % full_filename).read().strip()
195 def _opener():
196 try:
197 return builtinopen(full_filename, "rb")
198 except IOError:
199 warnings.warn("'%s' should be available at '%s' according to kpsewhich, "
200 "but the file is not available at this location; "
201 "update your kpsewhich database" % (filename, full_filename))
202 return [_opener]
204 locator_classes["kpsewhich"] = kpsewhich
207 class locate:
208 """locate files using a locate executable"""
210 def openers(self, filename, names, extensions):
211 for extension in extensions:
212 full_filenames = pycompat.popen("locate \"%s\"" % (filename+extension)).read()
213 if full_filenames:
214 break
215 else:
216 return []
217 full_filename = full_filenames.split("\n")[0].rstrip("\r")
218 def _opener():
219 try:
220 return builtinopen(full_filenames, "rb")
221 except IOError:
222 warnings.warn("'%s' should be available at '%s' according to the locate, "
223 "but the file is not available at this location; "
224 "update your locate database" % (filename, self.full_filenames[filename]))
225 return [_opener]
227 locator_classes["locate"] = locate
231 class _marker: pass
233 config = configparser.ConfigParser()
234 config.read_string(locator_classes["internal"]().openers("pyxrc", [], [""])[0]().read().decode("utf-8"), source="(internal pyxrc)")
235 if os.name == "nt":
236 user_pyxrc = os.path.join(os.environ['APPDATA'], "pyxrc")
237 else:
238 user_pyxrc = os.path.expanduser("~/.pyxrc")
239 config.read(user_pyxrc, encoding="utf-8")
241 def get(section, option, default=_marker):
242 if default is _marker:
243 return config.get(section, option)
244 else:
245 try:
246 return config.get(section, option)
247 except configparser.Error:
248 return default
250 def getint(section, option, default=_marker):
251 if default is _marker:
252 return config.getint(section, option)
253 else:
254 try:
255 return config.getint(section, option)
256 except configparser.Error:
257 return default
259 def getfloat(section, option, default=_marker):
260 if default is _marker:
261 return config.getfloat(section, option)
262 else:
263 try:
264 return config.getfloat(section, option)
265 except configparser.Error:
266 return default
268 def getboolean(section, option, default=_marker):
269 if default is _marker:
270 return config.getboolean(section, option)
271 else:
272 try:
273 return config.getboolean(section, option)
274 except configparser.Error:
275 return default
277 def getlist(section, option, default=_marker):
278 if default is _marker:
279 l = config.get(section, option).split()
280 else:
281 try:
282 l = config.get(section, option).split()
283 except configparser.Error:
284 return default
285 if space:
286 l = [item.replace(space, " ") for item in l]
287 return l
290 space = get("general", "space", "SPACE")
291 formatWarnings = get("general", "warnings", "default")
292 if formatWarnings not in ["default", "short", "shortest"]:
293 raise RuntimeError("invalid config value for option 'warnings' in section 'general'")
294 if formatWarnings != "default":
295 def formatwarning(message, category, filename, lineno, line=None):
296 if formatWarnings == "short":
297 return "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message)
298 else:
299 return "%s\n" % message
300 warnings.formatwarning = formatwarning
303 methods = [locator_classes[method]()
304 for method in getlist("filelocator", "methods", ["local", "internal", "pykpathsea", "kpsewhich"])]
305 opener_cache = {}
308 def open(filename, formats, ascii=False):
309 """returns an open file searched according the list of formats"""
311 # When using an empty list of formats, the names list is empty
312 # and the extensions list contains an empty string only. For that
313 # case some locators (notably local and internal) return an open
314 # function for the requested file whereas other locators might not
315 # return anything (like pykpathsea and kpsewhich).
316 # This is useful for files not to be searched in the latex
317 # installation at all (like lfs files).
318 extensions = set([""])
319 for format in formats:
320 for extension in format.extensions:
321 extensions.add(extension)
322 names = tuple([format.name for format in formats])
323 if (filename, names) in opener_cache:
324 file = opener_cache[(filename, names)]()
325 else:
326 for method in methods:
327 openers = method.openers(filename, names, extensions)
328 for opener in openers:
329 try:
330 file = opener()
331 except EnvironmentError:
332 file = None
333 if file:
334 opener_cache[(filename, names)] = opener
335 break
336 # break two loops here
337 else:
338 continue
339 break
340 else:
341 raise IOError("Could not locate the file '%s'." % filename)
342 if ascii:
343 return io.TextIOWrapper(file, encoding="ascii", errors="surrogateescape")
344 else:
345 return file
348 class format:
349 def __init__(self, name, extensions):
350 self.name = name
351 self.extensions = extensions
353 format.tfm = format("tfm", [".tfm"])
354 format.afm = format("afm", [".afm"])
355 format.fontmap = format("map", [])
356 format.pict = format("graphic/figure", [".eps", ".epsi"])
357 format.tex_ps_header = format("PostScript header", [".pro"]) # contains also: enc files
358 format.type1 = format("type1 fonts", [".pfa", ".pfb"])
359 format.vf = format("vf", [".vf"])
360 format.dvips_config = format("dvips config", [])