3 import os
, cStringIO
, warnings
5 import config
, pycompat
8 # Locator methods implement a open method similar to the builtin open
9 # function by searching for a file according to a specific rule.
14 # locates files in the current directory
16 def opener(self
, filename
, formats
, mode
):
17 return lambda: builtinopen(filename
, mode
)
19 locator_classes
["local"] = local
23 # locates files within the pyx data tree
25 def opener(self
, filename
, formats
, mode
):
26 extension
= os
.path
.splitext(filename
)[1][1:]
33 return lambda: builtinopen(os
.path
.join(os
.path
.dirname(__file__
), "data", extension
, filename
), mode
)
36 data
= pkgutil
.get_data("pyx", "data/%s/%s" % (extension
, filename
))
42 return lambda: cStringIO
.StringIO(data
)
44 locator_classes
["internal"] = internal
48 # locates files by searching recursively in a list of directories
51 self
.dirs
= config
.getlist("locator", "recursivedir")
52 self
.full_filenames
= {}
54 def opener(self
, filename
, formats
, mode
):
55 if filename
in self
.full_filenames
:
56 return lambda: builtinopen(self
.full_filenames
[filename
], mode
)
58 dir = self
.dirs
.pop(0)
59 for item
in os
.listdir(dir):
60 full_item
= os
.path
.join(dir, item
)
61 if os
.path
.isdir(full_item
):
62 self
.dirs
.insert(0, full_item
)
64 self
.full_filenames
[item
] = full_item
65 if filename
in self
.full_filenames
:
66 return lambda: builtinopen(self
.full_filenames
[filename
], mode
)
68 locator_classes
["recursivedir"] = recursivedir
72 # locates files by searching a list of ls-R files
75 self
.ls_Rs
= config
.getlist("locator", "ls-R")
76 self
.full_filenames
= {}
78 def opener(self
, filename
, formats
, mode
):
79 while self
.ls_Rs
and filename
not in self
.full_filenames
:
80 lsr
= self
.ls_Rs
.pop(0)
81 base_dir
= os
.path
.dirname(lsr
)
84 for line
in builtinopen(lsr
):
86 if first
and line
.startswith("%"):
89 if line
.endswith(":"):
90 dir = os
.path
.join(base_dir
, line
[:-1])
92 self
.full_filenames
[line
] = os
.path
.join(dir, line
)
93 if filename
in self
.full_filenames
:
96 return builtinopen(self
.full_filenames
[filename
], mode
)
98 warnings
.warn("'%s' should be available at '%s' according to the ls-R file, "
99 "but the file is not available at this location; "
100 "update your ls-R file" % (filename
, self
.full_filenames
[filename
]))
103 locator_classes
["ls-R"] = ls_R
108 def opener(self
, filename
, formats
, mode
):
110 for format
in formats
:
111 full_filename
= pykpathsea
.find_file(filename
, format
)
116 return lambda: builtinopen(full_filename
, mode
)
118 locator_classes
["pykpathsea"] = pykpathsea
122 # # locate files by libkpathsea using ctypes
124 # def opener(self, filename, formats, mode):
127 # locator_classes["libpathsea"] = libkpathsea
132 def opener(self
, filename
, formats
, mode
):
133 for format
in formats
:
135 full_filenames
= os
.popen('kpsewhich --format="%s" "%s"' % (format
, filename
)).read()
142 full_filename
= full_filenames
.split("\n")[0]
145 return builtinopen(full_filename
, mode
)
147 warnings
.warn("'%s' should be available at '%s' according to kpsewhich, "
148 "but the file is not available at this location; "
149 "update your kpsewhich database" % (filename
, full_filename
))
152 locator_classes
["kpsewhich"] = kpsewhich
157 def opener(self
, filename
, formats
, mode
):
159 full_filenames
= os
.popen("locate \"%s\"" % filename
).read()
162 if not full_filenames
:
164 full_filename
= full_filenames
.split("\n")[0]
167 return builtinopen(full_filenames
, mode
)
169 warnings
.warn("'%s' should be available at '%s' according to the locate database, "
170 "but the file is not available at this location; "
171 "update your locate database" % (filename
, self
.full_filenames
[filename
]))
174 locator_classes
["locate"] = locate
177 methods
= [locator_classes
[method
]()
178 for method
in config
.getlist("locator", "methods", "local internal pykpathsea kpsewhich locate")]
182 def open(filename
, formats
, mode
="r"):
183 formats
= tuple(formats
)
184 if (filename
, formats
) in openers
:
185 return openers
[(filename
, formats
)]()
186 for method
in methods
:
187 opener
= method
.opener(filename
, formats
, mode
)
194 openers
[(filename
, formats
)] = opener
196 raise IOError("Could not locate the file '%s'." % filename
)
202 any_glyph
= "bitmap font"
218 mpsupport
= "MetaPost support"
225 pict
= "graphics/figure"
227 texdoc
= "TeX system documentation"
229 texsource
= "TeX system sources"
230 tex_ps_header
= "PostScript header"
231 troff_font
= "Troff fonts"
232 type1
= "type1 fonts"
234 dvips_config
= "dvips config"
236 truetype
= "truetype fonts"
237 type42
= "type42 fonts"
238 web2c
= "web2c files"
239 program_text
= "other text files"
240 program_binary
= "other binary files"
241 miscfonts
= "misc fonts"
246 subfont_definition
= "subfont definition files"
247 opentype
= "opentype fonts"
248 pdftex_config
= "pdftex config"
250 texmfscripts
= "texmfscripts"
252 font_feature
= "font feature files"
253 cid_maps
= "cid maps"