make basic bitmap functionality work again
[PyX.git] / config.py
blob6206e0c5462547a8a4c11a8d6913d4c4cd381e7d
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: builtinopen(filename+extension, "rb") for extension in extensions]
55 locator_classes["local"] = local
58 class internal_pkgutil:
59 """locates files within the PyX data tree (via pkgutil)"""
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 class internal_open:
75 """locates files within the PyX data tree (via an open relative to the path of this file)"""
77 def openers(self, filename, names, extensions):
78 result = []
79 for extension in extensions:
80 full_filename = filename+extension
81 dir = os.path.splitext(full_filename)[1][1:]
82 result.append(lambda: builtinopen(os.path.join(os.path.dirname(__file__), "data", dir, full_filename), "rb"))
83 return result
85 try:
86 pkgutil.get_data
87 except AttributeError:
88 locator_classes["internal"] = internal_open # fallback for python < 2.6
89 else:
90 locator_classes["internal"] = internal_pkgutil
93 class recursivedir:
94 """locates files by searching recursively in a list of directories"""
96 def __init__(self):
97 self.dirs = getlist("locator", "recursivedir")
98 self.full_filenames = {}
100 def openers(self, filename, names, extensions):
101 for extension in extensions:
102 if filename+extension in self.full_filenames:
103 return [lambda: builtinopen(self.full_filenames[filename], "rb")]
104 while self.dirs:
105 dir = self.dirs.pop(0)
106 for item in os.listdir(dir):
107 full_item = os.path.join(dir, item)
108 if os.path.isdir(full_item):
109 self.dirs.insert(0, full_item)
110 else:
111 self.full_filenames[item] = full_item
112 for extension in extensions:
113 if filename+extension in self.full_filenames:
114 return [lambda: builtinopen(self.full_filenames[filename], "rb")]
115 return []
117 locator_classes["recursivedir"] = recursivedir
120 class ls_R:
121 """locates files by searching a list of ls-R files"""
123 def __init__(self):
124 self.ls_Rs = getlist("locator", "ls-R")
125 self.full_filenames = {}
127 def openers(self, filename, names, extensions):
128 while self.ls_Rs and not any([filename+extension in self.full_filenames for extension in extensions]):
129 lsr = self.ls_Rs.pop(0)
130 base_dir = os.path.dirname(lsr)
131 dir = None
132 first = True
133 for line in builtinopen(lsr, "r", encoding="ascii", errors="surrogateescape"):
134 line = line.rstrip()
135 if first and line.startswith("%"):
136 continue
137 first = False
138 if line.endswith(":"):
139 dir = os.path.join(base_dir, line[:-1])
140 elif line:
141 self.full_filenames[line] = os.path.join(dir, line)
142 for extension in extensions:
143 if filename+extension in self.full_filenames:
144 def _opener():
145 try:
146 return builtinopen(self.full_filenames[filename+extension], "rb")
147 except IOError:
148 warnings.warn("'%s' should be available at '%s' according to the ls-R file, "
149 "but the file is not available at this location; "
150 "update your ls-R file" % (filename, self.full_filenames[filename]))
151 return [_opener]
152 return []
154 locator_classes["ls-R"] = ls_R
157 class pykpathsea:
158 """locate files by pykpathsea (a C extension module wrapping libkpathsea)"""
160 def openers(self, filename, names, extensions):
161 if not has_pykpathsea:
162 return []
163 for name in names:
164 full_filename = pykpathsea_module.find_file(filename, name)
165 if full_filename:
166 break
167 else:
168 return []
169 def _opener():
170 try:
171 return builtinopen(full_filename, "rb")
172 except IOError:
173 warnings.warn("'%s' should be available at '%s' according to libkpathsea, "
174 "but the file is not available at this location; "
175 "update your kpsewhich database" % (filename, full_filename))
176 return [_opener]
178 locator_classes["pykpathsea"] = pykpathsea
181 # class libkpathsea:
182 # """locate files by libkpathsea using ctypes"""
184 # def openers(self, filename, names, extensions):
185 # raise NotImplemented
187 # locator_classes["libpathsea"] = libkpathsea
190 class kpsewhich:
191 """locate files using the kpsewhich executable"""
193 def openers(self, filename, names, extensions):
194 for name in names:
195 try:
196 full_filenames = pycompat.popen('kpsewhich --format="%s" "%s"' % (name, filename)).read()
197 except OSError:
198 return []
199 if full_filenames:
200 break
201 else:
202 return []
203 full_filename = full_filenames.decode("ascii").split("\n")[0].rstrip("\r")
205 # Detect Cygwin kpsewhich on Windows Python
206 if os.name == "nt" and full_filename.startswith("/"):
207 full_filename = pycompat.popen('cygpath -w "%s"' % full_filename).read().strip()
209 def _opener():
210 try:
211 return builtinopen(full_filename, "rb")
212 except IOError:
213 warnings.warn("'%s' should be available at '%s' according to kpsewhich, "
214 "but the file is not available at this location; "
215 "update your kpsewhich database" % (filename, full_filename))
216 return [_opener]
218 locator_classes["kpsewhich"] = kpsewhich
221 class locate:
222 """locate files using a locate executable"""
224 def openers(self, filename, names, extensions):
225 for extension in extensions:
226 full_filenames = pycompat.popen("locate \"%s\"" % (filename+extension)).read()
227 if full_filenames:
228 break
229 else:
230 return []
231 full_filename = full_filenames.split("\n")[0].rstrip("\r")
232 def _opener():
233 try:
234 return builtinopen(full_filenames, "rb")
235 except IOError:
236 warnings.warn("'%s' should be available at '%s' according to the locate, "
237 "but the file is not available at this location; "
238 "update your locate database" % (filename, self.full_filenames[filename]))
239 return [_opener]
241 locator_classes["locate"] = locate
245 class _marker: pass
247 config = configparser.ConfigParser()
248 config.read_string(locator_classes["internal"]().openers("pyxrc", [], [""])[0]().read().decode("utf-8"), source="(internal pyxrc)")
249 config.read(os.path.expanduser("~/.pyxrc"), encoding="utf-8")
251 def get(section, option, default=_marker):
252 if default is _marker:
253 return config.get(section, option)
254 else:
255 try:
256 return config.get(section, option)
257 except configparser.Error:
258 return default
260 def getint(section, option, default=_marker):
261 if default is _marker:
262 return config.getint(section, option)
263 else:
264 try:
265 return config.getint(section, option)
266 except configparser.Error:
267 return default
269 def getfloat(section, option, default=_marker):
270 if default is _marker:
271 return config.getfloat(section, option)
272 else:
273 try:
274 return config.getfloat(section, option)
275 except configparser.Error:
276 return default
278 def getboolean(section, option, default=_marker):
279 if default is _marker:
280 return config.getboolean(section, option)
281 else:
282 try:
283 return config.getboolean(section, option)
284 except configparser.Error:
285 return default
287 def getlist(section, option, default=_marker):
288 if default is _marker:
289 l = config.get(section, option).split()
290 else:
291 try:
292 l = config.get(section, option).split()
293 except configparser.Error:
294 return default
295 if space:
296 l = [item.replace(space, " ") for item in l]
297 return l
300 space = get("general", "space", None)
301 formatWarnings = get("general", "warnings", "default")
302 if formatWarnings not in ["default", "short", "shortest"]:
303 raise RuntimeError("invalid config value for option 'warnings' in section 'general'")
304 if formatWarnings != "default":
305 def formatwarning(message, category, filename, lineno, line=None):
306 if formatWarnings == "short":
307 return "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message)
308 else:
309 return "%s\n" % message
310 warnings.formatwarning = formatwarning
313 methods = [locator_classes[method]()
314 for method in getlist("filelocator", "methods", ["local", "internal", "pykpathsea", "kpsewhich"])]
315 opener_cache = {}
318 def open(filename, formats):
319 """returns an open file searched according the list of formats"""
321 # When using an empty list of formats, the names list is empty
322 # and the extensions list contains an empty string only. For that
323 # case some locators (notably local and internal) return an open
324 # function for the requested file whereas other locators might not
325 # return anything (like pykpathsea and kpsewhich).
326 # This is useful for files not to be searched in the latex
327 # installation at all (like lfs files).
328 extensions = set([""])
329 for format in formats:
330 for extension in format.extensions:
331 extensions.add(extension)
332 names = tuple([format.name for format in formats])
333 if (filename, names) in opener_cache:
334 return opener_cache[(filename, names)]()
335 for method in methods:
336 openers = method.openers(filename, names, extensions)
337 for opener in openers:
338 try:
339 file = opener()
340 except IOError:
341 file = None
342 if file:
343 opener_cache[(filename, names)] = opener
344 return file
345 raise IOError("Could not locate the file '%s'." % filename)
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", [])