shlex.sourcehook(filename)
When shlex
detects a source request (see source
below) this method is given the following token as argument, and expected to return a tuple consisting of a filename and an open file-like object.
Normally, this method first strips any quotes off the argument. If the result is an absolute pathname, or there was no previous source request in effect, or the previous source was a stream (such as sys.stdin
), the result is left alone. Otherwise, if the result is a relative pathname, the directory part of the name of the file immediately before it on the source inclusion stack is prepended (this behavior is like the way the C preprocessor handles #include
"file.h"
).
The result of the manipulations is treated as a filename, and returned as the first component of the tuple, with open()
called on it to yield the second component. (Note: this is the reverse of the order of arguments in instance initialization!)
This hook is exposed so that you can use it to implement directory search paths, addition of file extensions, and other namespace hacks. There is no corresponding ‘close’ hook, but a shlex instance will call the close()
method of the sourced input stream when it returns EOF.
For more explicit control of source stacking, use the push_source()
and pop_source()
methods.
Please login to continue.