| 1 | # -*- coding: iso-8859-1 -*- |
| 2 | """A lexical analyzer class for simple shell-like syntaxes.""" |
| 3 | |
| 4 | # Module and documentation by Eric S. Raymond, 21 Dec 1998 |
| 5 | # Input stacking and error message cleanup added by ESR, March 2000 |
| 6 | # push_source() and pop_source() made explicit by ESR, January 2001. |
| 7 | # Posix compliance, split(), string arguments, and |
| 8 | # iterator interface by Gustavo Niemeyer, April 2003. |
| 9 | |
| 10 | import os.path |
| 11 | import sys |
| 12 | from collections import deque |
| 13 | |
| 14 | try: |
| 15 | from cStringIO import StringIO |
| 16 | except ImportError: |
| 17 | from StringIO import StringIO |
| 18 | |
| 19 | __all__ = ["shlex", "split"] |
| 20 | |
| 21 | class shlex: |
| 22 | "A lexical analyzer class for simple shell-like syntaxes." |
| 23 | def __init__(self, instream=None, infile=None, posix=False): |
| 24 | if isinstance(instream, basestring): |
| 25 | instream = StringIO(instream) |
| 26 | if instream is not None: |
| 27 | self.instream = instream |
| 28 | self.infile = infile |
| 29 | else: |
| 30 | self.instream = sys.stdin |
| 31 | self.infile = None |
| 32 | self.posix = posix |
| 33 | if posix: |
| 34 | self.eof = None |
| 35 | else: |
| 36 | self.eof = '' |
| 37 | self.commenters = '#' |
| 38 | self.wordchars = ('abcdfeghijklmnopqrstuvwxyz' |
| 39 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_') |
| 40 | if self.posix: |
| 41 |