Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
shlex Class Reference

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
 

Data Fields

 instream
 
 infile
 
 commenters
 
 wordchars
 
 whitespace
 
 quotes
 
 state
 
 pushback
 
 lineno
 
 debug
 
 token
 
 filestack
 
 source
 

Detailed Description

Definition at line 12 of file shlex.py.

Constructor & Destructor Documentation

def __init__ (   self,
  instream = None,
  infile = None 
)

Definition at line 14 of file shlex.py.

14 
15  def __init__(self, instream=None, infile=None):
16  if instream:
17  self.instream = instream
18  self.infile = infile
19  else:
20  self.instream = sys.stdin
21  self.infile = None
22  self.commenters = '#'
23  self.wordchars = ('abcdfeghijklmnopqrstuvwxyz'
24  'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
25  self.whitespace = ' \t\r\n'
26  self.quotes = '\'"'
27  self.state = ' '
28  self.pushback = []
29  self.lineno = 1
30  self.debug = 0
31  self.token = ''
32  self.filestack = []
33  self.source = None
34  if self.debug:
35  print 'shlex: reading from %s, line %d' \
36  % (self.instream, self.lineno)

Member Function Documentation

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

Definition at line 189 of file shlex.py.

References shlex.infile, NetrcParseError.lineno, shlex.lineno, Pdb.lineno, Class.lineno, and MissingSectionHeaderError.lineno.

190  def error_leader(self, infile=None, lineno=None):
191  "Emit a C-compiler-like, Emacs-friendly error-message leader."
192  if not infile:
193  infile = self.infile
194  if not lineno:
195  lineno = self.lineno
196  return "\"%s\", line %d: " % (infile, lineno)
197 
def get_token (   self)

Definition at line 65 of file shlex.py.

References shlex.debug, FTP.debug, NNTP.debug, dispatcher.debug, shlex.filestack, shlex.get_token(), shlex.pop_source(), shlex.push_source(), shlex.pushback, shlex.read_token(), shlex.source, and shlex.sourcehook().

65 
66  def get_token(self):
67  "Get a token from the input stream (or from stack if it's nonempty)"
68  if self.pushback:
69  tok = self.pushback[0]
70  self.pushback = self.pushback[1:]
71  if self.debug >= 1:
72  print "shlex: popping token " + `tok`
73  return tok
74  # No pushback. Get a token.
75  raw = self.read_token()
76  # Handle inclusions
77  while raw == self.source:
78  spec = self.sourcehook(self.read_token())
79  if spec:
80  (newfile, newstream) = spec
81  self.push_source(newstream, newfile)
82  raw = self.get_token()
83  # Maybe we got EOF instead?
84  while raw == "":
85  if len(self.filestack) == 0:
86  return ""
87  else:
88  self.pop_source()
89  raw = self.get_token()
90  # Neither inclusion nor EOF
91  if self.debug >= 1:
92  if raw:
93  print "shlex: token=" + `raw`
94  else:
95  print "shlex: token=EOF"
96  return raw
def pop_source (   self)

Definition at line 55 of file shlex.py.

References shlex.debug, FTP.debug, NNTP.debug, dispatcher.debug, shlex.filestack, shlex.infile, shlex.instream, NetrcParseError.lineno, shlex.lineno, Pdb.lineno, Class.lineno, MissingSectionHeaderError.lineno, shlex.state, BinHex.state, HexBin.state, and Scanner.state.

55 
56  def pop_source(self):
57  "Pop the input source stack."
58  self.instream.close()
59  (self.infile, self.instream, self.lineno) = self.filestack[0]
60  self.filestack = self.filestack[1:]
61  if self.debug:
62  print 'shlex: popping to %s, line %d' \
63  % (self.instream, self.lineno)
64  self.state = ' '
def push_source (   self,
  newstream,
  newfile = None 
)

Definition at line 43 of file shlex.py.

References shlex.debug, FTP.debug, NNTP.debug, dispatcher.debug, shlex.infile, shlex.instream, NetrcParseError.lineno, shlex.lineno, Pdb.lineno, Class.lineno, and MissingSectionHeaderError.lineno.

43 
44  def push_source(self, newstream, newfile=None):
45  "Push an input source onto the lexer's input source stack."
46  self.filestack.insert(0, (self.infile, self.instream, self.lineno))
47  self.infile = newfile
48  self.instream = newstream
49  self.lineno = 1
50  if self.debug:
51  if newfile:
52  print 'shlex: pushing to file %s' % (self.infile,)
53  else:
54  print 'shlex: pushing to stream %s' % (self.instream,)
def push_token (   self,
  tok 
)

Definition at line 37 of file shlex.py.

References shlex.debug, FTP.debug, NNTP.debug, dispatcher.debug, and shlex.pushback.

37 
38  def push_token(self, tok):
39  "Push a token onto the stack popped by the get_token method"
40  if self.debug >= 1:
41  print "shlex: pushing token " + `tok`
42  self.pushback = [tok] + self.pushback
def read_token (   self)

Definition at line 97 of file shlex.py.

References shlex.commenters, shlex.debug, FTP.debug, NNTP.debug, dispatcher.debug, NetrcParseError.lineno, shlex.lineno, Pdb.lineno, Class.lineno, MissingSectionHeaderError.lineno, shlex.pushback, shlex.quotes, shlex.state, BinHex.state, HexBin.state, Scanner.state, shlex.token, shlex.whitespace, and shlex.wordchars.

97 
98  def read_token(self):
99  "Read a token from the input stream (no pushback or inclusions)"
100  while 1:
101  nextchar = self.instream.read(1)
102  if nextchar == '\n':
103  self.lineno = self.lineno + 1
104  if self.debug >= 3:
105  print "shlex: in state", repr(self.state), \
106  "I see character:", repr(nextchar)
107  if self.state is None:
108  self.token = '' # past end of file
109  break
110  elif self.state == ' ':
111  if not nextchar:
112  self.state = None # end of file
113  break
114  elif nextchar in self.whitespace:
115  if self.debug >= 2:
116  print "shlex: I see whitespace in whitespace state"
117  if self.token:
118  break # emit current token
119  else:
120  continue
121  elif nextchar in self.commenters:
122  self.instream.readline()
123  self.lineno = self.lineno + 1
124  elif nextchar in self.wordchars:
125  self.token = nextchar
126  self.state = 'a'
127  elif nextchar in self.quotes:
128  self.token = nextchar
129  self.state = nextchar
130  else:
131  self.token = nextchar
132  if self.token:
133  break # emit current token
134  else:
135  continue
136  elif self.state in self.quotes:
137  self.token = self.token + nextchar
138  if nextchar == self.state:
139  self.state = ' '
140  break
141  elif not nextchar: # end of file
142  if self.debug >= 2:
143  print "shlex: I see EOF in quotes state"
144  # XXX what error should be raised here?
145  raise ValueError, "No closing quotation"
146  elif self.state == 'a':
147  if not nextchar:
148  self.state = None # end of file
149  break
150  elif nextchar in self.whitespace:
151  if self.debug >= 2:
152  print "shlex: I see whitespace in word state"
153  self.state = ' '
154  if self.token:
155  break # emit current token
156  else:
157  continue
158  elif nextchar in self.commenters:
159  self.instream.readline()
160  self.lineno = self.lineno + 1
161  elif nextchar in self.wordchars or nextchar in self.quotes:
162  self.token = self.token + nextchar
163  else:
164  self.pushback = [nextchar] + self.pushback
165  if self.debug >= 2:
166  print "shlex: I see punctuation in word state"
167  self.state = ' '
168  if self.token:
169  break # emit current token
170  else:
171  continue
172  result = self.token
173  self.token = ''
174  if self.debug > 1:
175  if result:
176  print "shlex: raw token=" + `result`
177  else:
178  print "shlex: raw token=EOF"
179  return result
def sourcehook (   self,
  newfile 
)

Definition at line 180 of file shlex.py.

References shlex.infile, and aifc.open().

181  def sourcehook(self, newfile):
182  "Hook called on a filename to be sourced."
183  if newfile[0] == '"':
184  newfile = newfile[1:-1]
185  # This implements cpp-like semantics for relative-path inclusion.
186  if type(self.infile) == type("") and not os.path.isabs(newfile):
187  newfile = os.path.join(os.path.dirname(self.infile), newfile)
188  return (newfile, open(newfile, "r"))

Field Documentation

commenters

Definition at line 21 of file shlex.py.

debug

Definition at line 29 of file shlex.py.

filestack

Definition at line 31 of file shlex.py.

infile

Definition at line 17 of file shlex.py.

instream

Definition at line 16 of file shlex.py.

lineno

Definition at line 28 of file shlex.py.

pushback

Definition at line 27 of file shlex.py.

quotes

Definition at line 25 of file shlex.py.

source

Definition at line 32 of file shlex.py.

state

Definition at line 26 of file shlex.py.

token

Definition at line 30 of file shlex.py.

whitespace

Definition at line 24 of file shlex.py.

wordchars

Definition at line 22 of file shlex.py.


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