A lexical analyzer class for simple shell-like syntaxes.
| 17 | __all__ = ["shlex", "split", "quote", "join"] |
| 18 | |
| 19 | class shlex: |
| 20 | "A lexical analyzer class for simple shell-like syntaxes." |
| 21 | def __init__(self, instream=None, infile=None, posix=False, |
| 22 | punctuation_chars=False): |
| 23 | if isinstance(instream, str): |
| 24 | instream = StringIO(instream) |
| 25 | if instream is not None: |
| 26 | self.instream = instream |
| 27 | self.infile = infile |
| 28 | else: |
| 29 | self.instream = sys.stdin |
| 30 | self.infile = None |
| 31 | self.posix = posix |
| 32 | if posix: |
| 33 | self.eof = None |
| 34 | else: |
| 35 | self.eof = '' |
| 36 | self.commenters = '#' |
| 37 | self.wordchars = ('abcdfeghijklmnopqrstuvwxyz' |
| 38 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_') |
| 39 | if self.posix: |
| 40 | self.wordchars += ('ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ' |
| 41 | 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ') |
| 42 | self.whitespace = ' \t\r\n' |
| 43 | self.whitespace_split = False |
| 44 | self.quotes = '\'"' |
| 45 | self.escape = '\\' |
| 46 | self.escapedquotes = '"' |
| 47 | self.state = ' ' |
| 48 | self.pushback = deque() |
| 49 | self.lineno = 1 |
| 50 | self.debug = 0 |
| 51 | self.token = '' |
| 52 | self.filestack = deque() |
| 53 | self.source = None |
| 54 | if not punctuation_chars: |
| 55 | punctuation_chars = '' |
| 56 | elif punctuation_chars is True: |
| 57 | punctuation_chars = '();<>|&' |
| 58 | self._punctuation_chars = punctuation_chars |
| 59 | if punctuation_chars: |
| 60 | # _pushback_chars is a push back queue used by lookahead logic |
| 61 | self._pushback_chars = deque() |
| 62 | # these chars added because allowed in file names, args, wildcards |
| 63 | self.wordchars += '~-./*?=' |
| 64 | #remove any punctuation chars from wordchars |
| 65 | t = self.wordchars.maketrans(dict.fromkeys(punctuation_chars)) |
| 66 | self.wordchars = self.wordchars.translate(t) |
| 67 | |
| 68 | @property |
| 69 | def punctuation_chars(self): |
| 70 | return self._punctuation_chars |
| 71 | |
| 72 | def push_token(self, tok): |
| 73 | "Push a token onto the stack popped by the get_token method" |
| 74 | if self.debug >= 1: |
| 75 | print("shlex: pushing token " + repr(tok)) |
| 76 | self.pushback.appendleft(tok) |