Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex


22.2.1 shlex Objects

Ashlex instance has the following methods:

get_token()
Return a token. If tokens have been stacked usingpush_token(), pop a token off the stack. Otherwise, read onefrom the input stream. If reading encounters an immediateend-of-file,self.eof is returned (the empty string ('')in non-POSIX mode, andNone in POSIX mode).

push_token(str)
Push the argument onto the token stack.

read_token()
Read a raw token. Ignore the pushback stack, and do not interpret sourcerequests. (This is not ordinarily a useful entry point, and isdocumented here only for the sake of completeness.)

sourcehook(filename)
Whenshlex detects a source request (seesource below) this method is given the following token asargument, and expected to return a tuple consisting of a filename andan open file-like object.

Normally, this method first strips any quotes off the argument. Ifthe result is an absolute pathname, or there was no previous sourcerequest in effect, or the previous source was a stream(such assys.stdin), the result is left alone. Otherwise, if theresult is a relative pathname, the directory part of the name of thefile 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 returnedas the first component of the tuple, withopen() 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 directorysearch paths, addition of file extensions, and other namespace hacks.There is no corresponding `close' hook, but a shlex instance will calltheclose() method of the sourced input stream when itreturns EOF.

For more explicit control of source stacking, use thepush_source() andpop_source() methods.

push_source(stream[, filename])
Push an input source stream onto the input stack. If the filenameargument is specified it will later be available for use in errormessages. This is the same method used internally by thesourcehook method.New in version 2.1.

pop_source()
Pop the last-pushed input source from the input stack.This is the same method used internally when the lexer reachesEOF on a stacked input stream.New in version 2.1.

error_leader([file[, line]])
This method generates an error message leader in the format of aUnix C compiler error label; the format is'"%s", line %d: ',where the "%s" is replaced with the name of the current sourcefile and the "%d" with the current input line number (theoptional arguments can be used to override these).

This convenience is provided to encourageshlex users togenerate error messages in the standard, parseable format understoodby Emacs and otherUnix tools.

Instances ofshlex subclasses have some public instancevariables which either control lexical analysis or can be used fordebugging:

commenters
The string of characters that are recognized as comment beginners.All characters from the comment beginner to end of line are ignored.Includes just "#" by default.

wordchars
The string of characters that will accumulate into multi-charactertokens. By default, includes all ASCII alphanumerics andunderscore.

whitespace
Characters that will be considered whitespace and skipped. Whitespacebounds tokens. By default, includes space, tab, linefeed andcarriage-return.

escape
Characters that will be considered as escape. This will be only usedin POSIX mode, and includes just "\" by default.New in version 2.3.

quotes
Characters that will be considered string quotes. The tokenaccumulates until the same quote is encountered again (thus, differentquote types protect each other as in the shell.) By default, includesASCII single and double quotes.

escapedquotes
Characters inquotes that will interpret escape charactersdefined inescape. This is only used in POSIX mode, andincludes just """ by default.New in version 2.3.

whitespace_split
IfTrue, tokens will only be split in whitespaces. This is useful, forexample, for parsing command lines withshlex, getting tokensin a similar way to shell arguments.New in version 2.3.

infile
The name of the current input file, as initially set at classinstantiation time or stacked by later source requests. It maybe useful to examine this when constructing error messages.

instream
The input stream from which thisshlex instance is readingcharacters.

source
This member isNone by default. If you assign a string to it,that string will be recognized as a lexical-level inclusion requestsimilar to the "source" keyword in various shells. That is, theimmediately following token will opened as a filename and input takenfrom that stream until EOF, at which point theclose()method of that stream will be called and the input source will againbecome the original input stream. Source requests may be stacked anynumber of levels deep.

debug
If this member is numeric and1 or more, ashlexinstance will print verbose progress output on its behavior. If youneed to use this, you can read the module source code to learn thedetails.

lineno
Source line number (count of newlines seen so far plus one).

token
The token buffer. It may be useful to examine this when catchingexceptions.

eof
Token used to determine end of file. This will be set to the emptystring (''), in non-POSIX mode, and toNone inPOSIX mode.New in version 2.3.


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp