Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
shlex.shlex Class Reference

List of all members.

Public Member Functions

def __init__
def push_token
def push_source
def pop_source
def get_token
def read_token
def sourcehook
def error_leader
def __iter__
def __next__

Public Attributes

 instream
 infile
 posix
 eof
 commenters
 wordchars
 whitespace
 whitespace_split
 quotes
 escape
 escapedquotes
 state
 pushback
 lineno
 debug
 token
 filestack
 source

Detailed Description

Definition at line 17 of file shlex.py.


Constructor & Destructor Documentation

def shlex.shlex.__init__ (   self,
  instream = None,
  infile = None,
  posix = False 
)

Definition at line 19 of file shlex.py.

00019 
00020     def __init__(self, instream=None, infile=None, posix=False):
00021         if isinstance(instream, str):
00022             instream = StringIO(instream)
00023         if instream is not None:
00024             self.instream = instream
00025             self.infile = infile
00026         else:
00027             self.instream = sys.stdin
00028             self.infile = None
00029         self.posix = posix
00030         if posix:
00031             self.eof = None
00032         else:
00033             self.eof = ''
00034         self.commenters = '#'
00035         self.wordchars = ('abcdfeghijklmnopqrstuvwxyz'
00036                           'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
00037         if self.posix:
00038             self.wordchars += ('ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'
00039                                'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ')
00040         self.whitespace = ' \t\r\n'
00041         self.whitespace_split = False
00042         self.quotes = '\'"'
00043         self.escape = '\\'
00044         self.escapedquotes = '"'
00045         self.state = ' '
00046         self.pushback = deque()
00047         self.lineno = 1
00048         self.debug = 0
00049         self.token = ''
00050         self.filestack = deque()
00051         self.source = None
00052         if self.debug:
00053             print('shlex: reading from %s, line %d' \
00054                   % (self.instream, self.lineno))

Here is the caller graph for this function:


Member Function Documentation

def shlex.shlex.__iter__ (   self)

Definition at line 261 of file shlex.py.

00261 
00262     def __iter__(self):
00263         return self

def shlex.shlex.__next__ (   self)

Definition at line 264 of file shlex.py.

00264 
00265     def __next__(self):
00266         token = self.get_token()
00267         if token == self.eof:
00268             raise StopIteration
00269         return token

Here is the call graph for this function:

def shlex.shlex.error_leader (   self,
  infile = None,
  lineno = None 
)

Definition at line 253 of file shlex.py.

00253 
00254     def error_leader(self, infile=None, lineno=None):
00255         "Emit a C-compiler-like, Emacs-friendly error-message leader."
00256         if infile is None:
00257             infile = self.infile
00258         if lineno is None:
00259             lineno = self.lineno
00260         return "\"%s\", line %d: " % (infile, lineno)

def shlex.shlex.get_token (   self)

Definition at line 84 of file shlex.py.

00084 
00085     def get_token(self):
00086         "Get a token from the input stream (or from stack if it's nonempty)"
00087         if self.pushback:
00088             tok = self.pushback.popleft()
00089             if self.debug >= 1:
00090                 print("shlex: popping token " + repr(tok))
00091             return tok
00092         # No pushback.  Get a token.
00093         raw = self.read_token()
00094         # Handle inclusions
00095         if self.source is not None:
00096             while raw == self.source:
00097                 spec = self.sourcehook(self.read_token())
00098                 if spec:
00099                     (newfile, newstream) = spec
00100                     self.push_source(newstream, newfile)
00101                 raw = self.get_token()
00102         # Maybe we got EOF instead?
00103         while raw == self.eof:
00104             if not self.filestack:
00105                 return self.eof
00106             else:
00107                 self.pop_source()
00108                 raw = self.get_token()
00109         # Neither inclusion nor EOF
00110         if self.debug >= 1:
00111             if raw != self.eof:
00112                 print("shlex: token=" + repr(raw))
00113             else:
00114                 print("shlex: token=EOF")
00115         return raw

Here is the call graph for this function:

Here is the caller graph for this function:

def shlex.shlex.pop_source (   self)

Definition at line 75 of file shlex.py.

00075 
00076     def pop_source(self):
00077         "Pop the input source stack."
00078         self.instream.close()
00079         (self.infile, self.instream, self.lineno) = self.filestack.popleft()
00080         if self.debug:
00081             print('shlex: popping to %s, line %d' \
00082                   % (self.instream, self.lineno))
00083         self.state = ' '

Here is the caller graph for this function:

def shlex.shlex.push_source (   self,
  newstream,
  newfile = None 
)

Definition at line 61 of file shlex.py.

00061 
00062     def push_source(self, newstream, newfile=None):
00063         "Push an input source onto the lexer's input source stack."
00064         if isinstance(newstream, str):
00065             newstream = StringIO(newstream)
00066         self.filestack.appendleft((self.infile, self.instream, self.lineno))
00067         self.infile = newfile
00068         self.instream = newstream
00069         self.lineno = 1
00070         if self.debug:
00071             if newfile is not None:
00072                 print('shlex: pushing to file %s' % (self.infile,))
00073             else:
00074                 print('shlex: pushing to stream %s' % (self.instream,))

Here is the caller graph for this function:

def shlex.shlex.push_token (   self,
  tok 
)

Definition at line 55 of file shlex.py.

00055 
00056     def push_token(self, tok):
00057         "Push a token onto the stack popped by the get_token method"
00058         if self.debug >= 1:
00059             print("shlex: pushing token " + repr(tok))
00060         self.pushback.appendleft(tok)

def shlex.shlex.read_token (   self)

Definition at line 116 of file shlex.py.

00116 
00117     def read_token(self):
00118         quoted = False
00119         escapedstate = ' '
00120         while True:
00121             nextchar = self.instream.read(1)
00122             if nextchar == '\n':
00123                 self.lineno = self.lineno + 1
00124             if self.debug >= 3:
00125                 print("shlex: in state", repr(self.state), \
00126                       "I see character:", repr(nextchar))
00127             if self.state is None:
00128                 self.token = ''        # past end of file
00129                 break
00130             elif self.state == ' ':
00131                 if not nextchar:
00132                     self.state = None  # end of file
00133                     break
00134                 elif nextchar in self.whitespace:
00135                     if self.debug >= 2:
00136                         print("shlex: I see whitespace in whitespace state")
00137                     if self.token or (self.posix and quoted):
00138                         break   # emit current token
00139                     else:
00140                         continue
00141                 elif nextchar in self.commenters:
00142                     self.instream.readline()
00143                     self.lineno = self.lineno + 1
00144                 elif self.posix and nextchar in self.escape:
00145                     escapedstate = 'a'
00146                     self.state = nextchar
00147                 elif nextchar in self.wordchars:
00148                     self.token = nextchar
00149                     self.state = 'a'
00150                 elif nextchar in self.quotes:
00151                     if not self.posix:
00152                         self.token = nextchar
00153                     self.state = nextchar
00154                 elif self.whitespace_split:
00155                     self.token = nextchar
00156                     self.state = 'a'
00157                 else:
00158                     self.token = nextchar
00159                     if self.token or (self.posix and quoted):
00160                         break   # emit current token
00161                     else:
00162                         continue
00163             elif self.state in self.quotes:
00164                 quoted = True
00165                 if not nextchar:      # end of file
00166                     if self.debug >= 2:
00167                         print("shlex: I see EOF in quotes state")
00168                     # XXX what error should be raised here?
00169                     raise ValueError("No closing quotation")
00170                 if nextchar == self.state:
00171                     if not self.posix:
00172                         self.token = self.token + nextchar
00173                         self.state = ' '
00174                         break
00175                     else:
00176                         self.state = 'a'
00177                 elif self.posix and nextchar in self.escape and \
00178                      self.state in self.escapedquotes:
00179                     escapedstate = self.state
00180                     self.state = nextchar
00181                 else:
00182                     self.token = self.token + nextchar
00183             elif self.state in self.escape:
00184                 if not nextchar:      # end of file
00185                     if self.debug >= 2:
00186                         print("shlex: I see EOF in escape state")
00187                     # XXX what error should be raised here?
00188                     raise ValueError("No escaped character")
00189                 # In posix shells, only the quote itself or the escape
00190                 # character may be escaped within quotes.
00191                 if escapedstate in self.quotes and \
00192                    nextchar != self.state and nextchar != escapedstate:
00193                     self.token = self.token + self.state
00194                 self.token = self.token + nextchar
00195                 self.state = escapedstate
00196             elif self.state == 'a':
00197                 if not nextchar:
00198                     self.state = None   # end of file
00199                     break
00200                 elif nextchar in self.whitespace:
00201                     if self.debug >= 2:
00202                         print("shlex: I see whitespace in word state")
00203                     self.state = ' '
00204                     if self.token or (self.posix and quoted):
00205                         break   # emit current token
00206                     else:
00207                         continue
00208                 elif nextchar in self.commenters:
00209                     self.instream.readline()
00210                     self.lineno = self.lineno + 1
00211                     if self.posix:
00212                         self.state = ' '
00213                         if self.token or (self.posix and quoted):
00214                             break   # emit current token
00215                         else:
00216                             continue
00217                 elif self.posix and nextchar in self.quotes:
00218                     self.state = nextchar
00219                 elif self.posix and nextchar in self.escape:
00220                     escapedstate = 'a'
00221                     self.state = nextchar
00222                 elif nextchar in self.wordchars or nextchar in self.quotes \
00223                     or self.whitespace_split:
00224                     self.token = self.token + nextchar
00225                 else:
00226                     self.pushback.appendleft(nextchar)
00227                     if self.debug >= 2:
00228                         print("shlex: I see punctuation in word state")
00229                     self.state = ' '
00230                     if self.token:
00231                         break   # emit current token
00232                     else:
00233                         continue
00234         result = self.token
00235         self.token = ''
00236         if self.posix and not quoted and result == '':
00237             result = None
00238         if self.debug > 1:
00239             if result:
00240                 print("shlex: raw token=" + repr(result))
00241             else:
00242                 print("shlex: raw token=EOF")
00243         return result

Here is the caller graph for this function:

def shlex.shlex.sourcehook (   self,
  newfile 
)

Definition at line 244 of file shlex.py.

00244 
00245     def sourcehook(self, newfile):
00246         "Hook called on a filename to be sourced."
00247         if newfile[0] == '"':
00248             newfile = newfile[1:-1]
00249         # This implements cpp-like semantics for relative-path inclusion.
00250         if isinstance(self.infile, str) and not os.path.isabs(newfile):
00251             newfile = os.path.join(os.path.dirname(self.infile), newfile)
00252         return (newfile, open(newfile, "r"))

Here is the caller graph for this function:


Member Data Documentation

Definition at line 33 of file shlex.py.

Definition at line 47 of file shlex.py.

Definition at line 30 of file shlex.py.

Definition at line 42 of file shlex.py.

Definition at line 43 of file shlex.py.

Definition at line 49 of file shlex.py.

Definition at line 24 of file shlex.py.

Definition at line 23 of file shlex.py.

Definition at line 46 of file shlex.py.

Definition at line 28 of file shlex.py.

Definition at line 45 of file shlex.py.

Definition at line 41 of file shlex.py.

Definition at line 50 of file shlex.py.

Definition at line 44 of file shlex.py.

Definition at line 48 of file shlex.py.

Definition at line 39 of file shlex.py.

Definition at line 40 of file shlex.py.

Definition at line 34 of file shlex.py.


The documentation for this class was generated from the following file: