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

Public Member Functions

def __init__
 
def __repr__
 
def __getattr__
 
def __getitem__
 
def getvalue
 
def getfirst
 
def getlist
 
def keys
 
def has_key
 
def __len__
 
def read_urlencoded
 
def read_multi
 
def read_single
 
def read_binary
 
def read_lines
 
def read_lines_to_eof
 
def read_lines_to_outerboundary
 
def skip_lines
 
def make_file
 

Data Fields

 keep_blank_values
 
 strict_parsing
 
 fp
 
 headers
 
 outerboundary
 
 disposition
 
 disposition_options
 
 name
 
 filename
 
 type
 
 type_options
 
 innerboundary
 
 length
 
 list
 
 file
 
 done
 

Static Public Attributes

 FieldStorageClass = None
 
int bufsize = 8
 

Detailed Description

Store a sequence of fields, reading multipart/form-data.

This class provides naming, typing, files stored on disk, and
more.  At the top level, it is accessible like a dictionary, whose
keys are the field names.  (Note: None can occur as a field name.)
The items are either a Python list (if there's multiple values) or
another FieldStorage or MiniFieldStorage object.  If it's a single
object, it has the following attributes:

name: the field name, if specified; otherwise None

filename: the filename, if specified; otherwise None; this is the
    client side filename, *not* the file name on which it is
    stored (that's a temporary file you don't deal with)

value: the value as a *string*; for file uploads, this
    transparently reads the file every time you request the value

file: the file(-like) object from which you can read the data;
    None if the data is stored a simple string

type: the content-type, or None if not specified

type_options: dictionary of options specified on the content-type
    line

disposition: content-disposition, or None if not specified

disposition_options: dictionary of corresponding options

headers: a dictionary(-like) object (sometimes rfc822.Message or a
    subclass thereof) containing *all* headers

The class is subclassable, mostly for the purpose of overriding
the make_file() method, which is called internally to come up with
a file open for reading and writing.  This makes it possible to
override the default choice of storing all files in a temporary
directory and unlinking them as soon as they have been opened.

Definition at line 368 of file cgi.py.

Constructor & Destructor Documentation

def __init__ (   self,
  fp = None,
  headers = None,
  outerboundary = "",
  environ = os.environ,
  keep_blank_values = 0,
  strict_parsing = 0 
)
Constructor.  Read multipart/* until last part.

Arguments, all optional:

fp              : file pointer; default: sys.stdin
    (not used when the request method is GET)

headers         : header dictionary-like object; default:
    taken from environ as per CGI spec

outerboundary   : terminating multipart boundary
    (for internal use only)

environ         : environment dictionary; default: os.environ

keep_blank_values: flag indicating whether blank values in
    URL encoded forms should be treated as blank strings.
    A true value indicates that blanks should be retained as
    blank strings.  The default false value indicates that
    blank values are to be ignored and treated as if they were
    not included.

strict_parsing: flag indicating what to do with parsing errors.
    If false (the default), errors are silently ignored.
    If true, errors raise a ValueError exception.

Definition at line 412 of file cgi.py.

413  environ=os.environ, keep_blank_values=0, strict_parsing=0):
414  """Constructor. Read multipart/* until last part.
415 
416  Arguments, all optional:
417 
418  fp : file pointer; default: sys.stdin
419  (not used when the request method is GET)
420 
421  headers : header dictionary-like object; default:
422  taken from environ as per CGI spec
423 
424  outerboundary : terminating multipart boundary
425  (for internal use only)
426 
427  environ : environment dictionary; default: os.environ
428 
429  keep_blank_values: flag indicating whether blank values in
430  URL encoded forms should be treated as blank strings.
431  A true value indicates that blanks should be retained as
432  blank strings. The default false value indicates that
433  blank values are to be ignored and treated as if they were
434  not included.
435 
436  strict_parsing: flag indicating what to do with parsing errors.
437  If false (the default), errors are silently ignored.
438  If true, errors raise a ValueError exception.
439 
440  """
441  method = 'GET'
442  self.keep_blank_values = keep_blank_values
443  self.strict_parsing = strict_parsing
444  if environ.has_key('REQUEST_METHOD'):
445  method = environ['REQUEST_METHOD'].upper()
446  if method == 'GET' or method == 'HEAD':
447  if environ.has_key('QUERY_STRING'):
448  qs = environ['QUERY_STRING']
449  elif sys.argv[1:]:
450  qs = sys.argv[1]
451  else:
452  qs = ""
453  fp = StringIO(qs)
454  if headers is None:
455  headers = {'content-type':
456  "application/x-www-form-urlencoded"}
457  if headers is None:
458  headers = {}
459  if method == 'POST':
460  # Set default content-type for POST to what's traditional
461  headers['content-type'] = "application/x-www-form-urlencoded"
462  if environ.has_key('CONTENT_TYPE'):
463  headers['content-type'] = environ['CONTENT_TYPE']
464  if environ.has_key('CONTENT_LENGTH'):
465  headers['content-length'] = environ['CONTENT_LENGTH']
466  self.fp = fp or sys.stdin
467  self.headers = headers
468  self.outerboundary = outerboundary
469 
470  # Process content-disposition header
471  cdisp, pdict = "", {}
472  if self.headers.has_key('content-disposition'):
473  cdisp, pdict = parse_header(self.headers['content-disposition'])
474  self.disposition = cdisp
475  self.disposition_options = pdict
476  self.name = None
477  if pdict.has_key('name'):
478  self.name = pdict['name']
479  self.filename = None
480  if pdict.has_key('filename'):
481  self.filename = pdict['filename']
482 
483  # Process content-type header
484  #
485  # Honor any existing content-type header. But if there is no
486  # content-type header, use some sensible defaults. Assume
487  # outerboundary is "" at the outer level, but something non-false
488  # inside a multi-part. The default for an inner part is text/plain,
489  # but for an outer part it should be urlencoded. This should catch
490  # bogus clients which erroneously forget to include a content-type
491  # header.
492  #
493  # See below for what we do if there does exist a content-type header,
494  # but it happens to be something we don't understand.
495  if self.headers.has_key('content-type'):
496  ctype, pdict = parse_header(self.headers['content-type'])
497  elif self.outerboundary or method != 'POST':
498  ctype, pdict = "text/plain", {}
499  else:
500  ctype, pdict = 'application/x-www-form-urlencoded', {}
501  self.type = ctype
502  self.type_options = pdict
503  self.innerboundary = ""
504  if pdict.has_key('boundary'):
505  self.innerboundary = pdict['boundary']
506  clen = -1
507  if self.headers.has_key('content-length'):
508  try:
509  clen = int(self.headers['content-length'])
510  except:
511  pass
512  if maxlen and clen > maxlen:
513  raise ValueError, 'Maximum content length exceeded'
514  self.length = clen
516  self.list = self.file = None
517  self.done = 0
518  if ctype == 'application/x-www-form-urlencoded':
519  self.read_urlencoded()
520  elif ctype[:10] == 'multipart/':
521  self.read_multi(environ, keep_blank_values, strict_parsing)
522  else:
523  self.read_single()

Member Function Documentation

def __getattr__ (   self,
  name 
)

Definition at line 529 of file cgi.py.

References MiniFieldStorage.file, Breakpoint.file, FieldStorage.file, fifo.list, MiniFieldStorage.list, and FieldStorage.list.

530  def __getattr__(self, name):
531  if name != 'value':
532  raise AttributeError, name
533  if self.file:
534  self.file.seek(0)
535  value = self.file.read()
536  self.file.seek(0)
537  elif self.list is not None:
538  value = self.list
539  else:
540  value = None
541  return value
def __getitem__ (   self,
  key 
)
Dictionary style indexing.

Definition at line 542 of file cgi.py.

References fifo.list, MiniFieldStorage.list, and FieldStorage.list.

543  def __getitem__(self, key):
544  """Dictionary style indexing."""
545  if self.list is None:
546  raise TypeError, "not indexable"
547  found = []
548  for item in self.list:
549  if item.name == key: found.append(item)
550  if not found:
551  raise KeyError, key
552  if len(found) == 1:
553  return found[0]
554  else:
555  return found
def __len__ (   self)
Dictionary style len(x) support.

Definition at line 606 of file cgi.py.

References FieldStorage.keys().

607  def __len__(self):
608  """Dictionary style len(x) support."""
609  return len(self.keys())
def __repr__ (   self)
Return a printable representation.

Definition at line 524 of file cgi.py.

References MiniFieldStorage.filename, FieldStorage.filename, MiniFieldStorage.name, FieldStorage.name, SaveVariableCondition.value, _Stop.value, Argument.value, PositiveFraction.value, ErrorDuringImport.value, Boolean.value, SaveVar.value, DateTime.value, FactionList.value, ShipTypeList.value, MiniFieldStorage.value, SetSaveVariable.value, Morsel.value, FormContent.value(), and GUIRadioButton.value.

525  def __repr__(self):
526  """Return a printable representation."""
527  return "FieldStorage(%s, %s, %s)" % (
528  `self.name`, `self.filename`, `self.value`)
def getfirst (   self,
  key,
  default = None 
)
Return the first value received.

Definition at line 567 of file cgi.py.

References FieldStorage.has_key(), and FieldStorage.type.

568  def getfirst(self, key, default=None):
569  """ Return the first value received."""
570  if self.has_key(key):
571  value = self[key]
572  if type(value) is type([]):
573  return value[0].value
574  else:
575  return value.value
576  else:
577  return default
def getlist (   self,
  key 
)
Return list of received values.

Definition at line 578 of file cgi.py.

References FieldStorage.has_key(), and FieldStorage.type.

579  def getlist(self, key):
580  """ Return list of received values."""
581  if self.has_key(key):
582  value = self[key]
583  if type(value) is type([]):
584  return map(lambda v: v.value, value)
585  else:
586  return [value.value]
587  else:
588  return []
def getvalue (   self,
  key,
  default = None 
)
Dictionary style get() method, including 'value' lookup.

Definition at line 556 of file cgi.py.

References FieldStorage.has_key(), and FieldStorage.type.

557  def getvalue(self, key, default=None):
558  """Dictionary style get() method, including 'value' lookup."""
559  if self.has_key(key):
560  value = self[key]
561  if type(value) is type([]):
562  return map(lambda v: v.value, value)
563  else:
564  return value.value
565  else:
566  return default
def has_key (   self,
  key 
)
Dictionary style has_key() method.

Definition at line 598 of file cgi.py.

References fifo.list, MiniFieldStorage.list, and FieldStorage.list.

599  def has_key(self, key):
600  """Dictionary style has_key() method."""
601  if self.list is None:
602  raise TypeError, "not indexable"
603  for item in self.list:
604  if item.name == key: return 1
605  return 0
def keys (   self)
Dictionary style keys() method.

Definition at line 589 of file cgi.py.

References fifo.list, MiniFieldStorage.list, and FieldStorage.list.

590  def keys(self):
591  """Dictionary style keys() method."""
592  if self.list is None:
593  raise TypeError, "not indexable"
594  keys = []
595  for item in self.list:
596  if item.name not in keys: keys.append(item.name)
597  return keys
def make_file (   self,
  binary = None 
)
Overridable: return a readable & writable file.

The file will be used as follows:
- data is written to it
- seek(0)
- data is read from it

The 'binary' argument is unused -- the file is always opened
in binary mode.

This version opens a temporary file for reading and writing,
and immediately deletes (unlinks) it.  The trick (on Unix!) is
that the file can still be used, but it can't be opened by
another process, and it will automatically be deleted when it
is closed or when the current process terminates.

If you want a more permanent file, you derive a class which
overrides this method.  If you want a visible temporary file
that is nevertheless automatically deleted when the script
terminates, try defining a __del__ method in a derived class
which unlinks the temporary files you have created.

Definition at line 735 of file cgi.py.

References tempfile.TemporaryFile().

736  def make_file(self, binary=None):
737  """Overridable: return a readable & writable file.
738 
739  The file will be used as follows:
740  - data is written to it
741  - seek(0)
742  - data is read from it
743 
744  The 'binary' argument is unused -- the file is always opened
745  in binary mode.
746 
747  This version opens a temporary file for reading and writing,
748  and immediately deletes (unlinks) it. The trick (on Unix!) is
749  that the file can still be used, but it can't be opened by
750  another process, and it will automatically be deleted when it
751  is closed or when the current process terminates.
752 
753  If you want a more permanent file, you derive a class which
754  overrides this method. If you want a visible temporary file
755  that is nevertheless automatically deleted when the script
756  terminates, try defining a __del__ method in a derived class
757  which unlinks the temporary files you have created.
758 
759  """
760  import tempfile
761  return tempfile.TemporaryFile("w+b")
762 
763 
764 
765 # Backwards Compatibility Classes
766 # ===============================
def read_binary (   self)
Internal: read binary data.

Definition at line 650 of file cgi.py.

References FieldStorage.bufsize, FieldStorage.done, MiniFieldStorage.file, Breakpoint.file, FieldStorage.file, FieldStorage.length, FieldStorage.make_file(), and sre_parse.min.

651  def read_binary(self):
652  """Internal: read binary data."""
653  self.file = self.make_file('b')
654  todo = self.length
655  if todo >= 0:
656  while todo > 0:
657  data = self.fp.read(min(todo, self.bufsize))
658  if not data:
659  self.done = -1
660  break
661  self.file.write(data)
662  todo = todo - len(data)
def read_lines (   self)
Internal: read lines until EOF or outerboundary.

Definition at line 663 of file cgi.py.

References FieldStorage.__file, MiniFieldStorage.file, Breakpoint.file, FieldStorage.file, FieldStorage.make_file(), FieldStorage.outerboundary, FieldStorage.read_lines_to_eof(), and FieldStorage.read_lines_to_outerboundary().

664  def read_lines(self):
665  """Internal: read lines until EOF or outerboundary."""
666  self.file = self.__file = StringIO()
667  if self.outerboundary:
669  else:
670  self.read_lines_to_eof()
def read_lines_to_eof (   self)
Internal: read lines until EOF.

Definition at line 679 of file cgi.py.

References FieldStorage.__write(), and FieldStorage.done.

680  def read_lines_to_eof(self):
681  """Internal: read lines until EOF."""
682  while 1:
683  line = self.fp.readline()
684  if not line:
685  self.done = -1
686  break
687  self.__write(line)
def read_lines_to_outerboundary (   self)
Internal: read lines until outerboundary.

Definition at line 688 of file cgi.py.

References FieldStorage.__write(), FieldStorage.done, and FieldStorage.outerboundary.

689  def read_lines_to_outerboundary(self):
690  """Internal: read lines until outerboundary."""
691  next = "--" + self.outerboundary
692  last = next + "--"
693  delim = ""
694  while 1:
695  line = self.fp.readline()
696  if not line:
697  self.done = -1
698  break
699  if line[:2] == "--":
700  strippedline = line.strip()
701  if strippedline == next:
702  break
703  if strippedline == last:
704  self.done = 1
705  break
706  odelim = delim
707  if line[-2:] == "\r\n":
708  delim = "\r\n"
709  line = line[:-2]
710  elif line[-1] == "\n":
711  delim = "\n"
712  line = line[:-1]
713  else:
714  delim = ""
715  self.__write(odelim + line)
def read_multi (   self,
  environ,
  keep_blank_values,
  strict_parsing 
)
Internal: read a part that is itself multipart.

Definition at line 621 of file cgi.py.

References SymbolTable.__class__, FieldStorage.FieldStorageClass, FieldStorage.fp, FieldStorage.innerboundary, fifo.list, MiniFieldStorage.list, FieldStorage.list, FieldStorage.skip_lines(), and cgi.valid_boundary().

622  def read_multi(self, environ, keep_blank_values, strict_parsing):
623  """Internal: read a part that is itself multipart."""
624  ib = self.innerboundary
625  if not valid_boundary(ib):
626  raise ValueError, ('Invalid boundary in multipart form: %s'
627  % `ib`)
628  self.list = []
629  klass = self.FieldStorageClass or self.__class__
630  part = klass(self.fp, {}, ib,
631  environ, keep_blank_values, strict_parsing)
632  # Throw first part away
633  while not part.done:
634  headers = rfc822.Message(self.fp)
635  part = klass(self.fp, headers, ib,
636  environ, keep_blank_values, strict_parsing)
637  self.list.append(part)
638  self.skip_lines()
def read_single (   self)
Internal: read an atomic part.

Definition at line 639 of file cgi.py.

References FieldStorage.length, FieldStorage.read_binary(), FieldStorage.read_lines(), and FieldStorage.skip_lines().

640  def read_single(self):
641  """Internal: read an atomic part."""
642  if self.length >= 0:
643  self.read_binary()
644  self.skip_lines()
645  else:
646  self.read_lines()
647  self.file.seek(0)
def read_urlencoded (   self)
Internal: read data in query string format.

Definition at line 610 of file cgi.py.

References FieldStorage.keep_blank_values, FieldStorage.length, fifo.list, MiniFieldStorage.list, FieldStorage.list, cgi.parse_qsl(), FieldStorage.skip_lines(), and FieldStorage.strict_parsing.

611  def read_urlencoded(self):
612  """Internal: read data in query string format."""
613  qs = self.fp.read(self.length)
614  self.list = list = []
615  for key, value in parse_qsl(qs, self.keep_blank_values,
616  self.strict_parsing):
617  list.append(MiniFieldStorage(key, value))
618  self.skip_lines()
def skip_lines (   self)
Internal: skip lines until outer boundary if defined.

Definition at line 716 of file cgi.py.

References FieldStorage.done, and FieldStorage.outerboundary.

717  def skip_lines(self):
718  """Internal: skip lines until outer boundary if defined."""
719  if not self.outerboundary or self.done:
720  return
721  next = "--" + self.outerboundary
722  last = next + "--"
723  while 1:
724  line = self.fp.readline()
725  if not line:
726  self.done = -1
727  break
728  if line[:2] == "--":
729  strippedline = line.strip()
730  if strippedline == next:
731  break
732  if strippedline == last:
733  self.done = 1
734  break

Field Documentation

int bufsize = 8
static

Definition at line 648 of file cgi.py.

disposition

Definition at line 473 of file cgi.py.

disposition_options

Definition at line 474 of file cgi.py.

done

Definition at line 516 of file cgi.py.

FieldStorageClass = None
static

Definition at line 619 of file cgi.py.

file

Definition at line 515 of file cgi.py.

filename

Definition at line 478 of file cgi.py.

fp

Definition at line 465 of file cgi.py.

headers

Definition at line 466 of file cgi.py.

innerboundary

Definition at line 502 of file cgi.py.

keep_blank_values

Definition at line 441 of file cgi.py.

length

Definition at line 513 of file cgi.py.

list

Definition at line 515 of file cgi.py.

name

Definition at line 475 of file cgi.py.

outerboundary

Definition at line 467 of file cgi.py.

strict_parsing

Definition at line 442 of file cgi.py.

type

Definition at line 500 of file cgi.py.

type_options

Definition at line 501 of file cgi.py.


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