home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / cgi.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  21.1 KB  |  820 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. __version__ = '2.6'
  5. from operator import attrgetter
  6. import sys
  7. import os
  8. import urllib
  9. import UserDict
  10. import urlparse
  11. from warnings import filterwarnings, catch_warnings, warn
  12. with catch_warnings():
  13.     if sys.py3kwarning:
  14.         filterwarnings('ignore', '.*mimetools has been removed', DeprecationWarning)
  15.         filterwarnings('ignore', '.*rfc822 has been removed', DeprecationWarning)
  16.     import mimetools
  17.     import rfc822
  18.  
  19. try:
  20.     from cStringIO import StringIO
  21. except ImportError:
  22.     from StringIO import StringIO
  23.  
  24. __all__ = [
  25.     'MiniFieldStorage',
  26.     'FieldStorage',
  27.     'FormContentDict',
  28.     'SvFormContentDict',
  29.     'InterpFormContentDict',
  30.     'FormContent',
  31.     'parse',
  32.     'parse_qs',
  33.     'parse_qsl',
  34.     'parse_multipart',
  35.     'parse_header',
  36.     'print_exception',
  37.     'print_environ',
  38.     'print_form',
  39.     'print_directory',
  40.     'print_arguments',
  41.     'print_environ_usage',
  42.     'escape']
  43. logfile = ''
  44. logfp = None
  45.  
  46. def initlog(*allargs):
  47.     global logfp, log, log
  48.     if logfile and not logfp:
  49.         
  50.         try:
  51.             logfp = open(logfile, 'a')
  52.         except IOError:
  53.             pass
  54.         
  55.  
  56.     if not logfp:
  57.         log = nolog
  58.     else:
  59.         log = dolog
  60.     log(*allargs)
  61.  
  62.  
  63. def dolog(fmt, *args):
  64.     logfp.write(fmt % args + '\n')
  65.  
  66.  
  67. def nolog(*allargs):
  68.     pass
  69.  
  70. log = initlog
  71. maxlen = 0
  72.  
  73. def parse(fp = None, environ = os.environ, keep_blank_values = 0, strict_parsing = 0):
  74.     if fp is None:
  75.         fp = sys.stdin
  76.     if 'REQUEST_METHOD' not in environ:
  77.         environ['REQUEST_METHOD'] = 'GET'
  78.     if environ['REQUEST_METHOD'] == 'POST':
  79.         (ctype, pdict) = parse_header(environ['CONTENT_TYPE'])
  80.         if ctype == 'multipart/form-data':
  81.             return parse_multipart(fp, pdict)
  82.         if None == 'application/x-www-form-urlencoded':
  83.             clength = int(environ['CONTENT_LENGTH'])
  84.             if maxlen and clength > maxlen:
  85.                 raise ValueError, 'Maximum content length exceeded'
  86.             qs = fp.read(clength)
  87.         else:
  88.             qs = ''
  89.         if 'QUERY_STRING' in environ:
  90.             if qs:
  91.                 qs = qs + '&'
  92.             qs = qs + environ['QUERY_STRING']
  93.         elif sys.argv[1:]:
  94.             if qs:
  95.                 qs = qs + '&'
  96.             qs = qs + sys.argv[1]
  97.         environ['QUERY_STRING'] = qs
  98.     elif 'QUERY_STRING' in environ:
  99.         qs = environ['QUERY_STRING']
  100.     elif sys.argv[1:]:
  101.         qs = sys.argv[1]
  102.     else:
  103.         qs = ''
  104.     environ['QUERY_STRING'] = qs
  105.     return urlparse.parse_qs(qs, keep_blank_values, strict_parsing)
  106.  
  107.  
  108. def parse_qs(qs, keep_blank_values = 0, strict_parsing = 0):
  109.     warn('cgi.parse_qs is deprecated, use urlparse.parse_qs instead', PendingDeprecationWarning, 2)
  110.     return urlparse.parse_qs(qs, keep_blank_values, strict_parsing)
  111.  
  112.  
  113. def parse_qsl(qs, keep_blank_values = 0, strict_parsing = 0):
  114.     warn('cgi.parse_qsl is deprecated, use urlparse.parse_qsl instead', PendingDeprecationWarning, 2)
  115.     return urlparse.parse_qsl(qs, keep_blank_values, strict_parsing)
  116.  
  117.  
  118. def parse_multipart(fp, pdict):
  119.     boundary = ''
  120.     if 'boundary' in pdict:
  121.         boundary = pdict['boundary']
  122.     if not valid_boundary(boundary):
  123.         raise ValueError, 'Invalid boundary in multipart form: %r' % (boundary,)
  124.     nextpart = '--' + boundary
  125.     lastpart = '--' + boundary + '--'
  126.     partdict = { }
  127.     terminator = ''
  128.     while terminator != lastpart:
  129.         bytes = -1
  130.         data = None
  131.         if terminator:
  132.             headers = mimetools.Message(fp)
  133.             clength = headers.getheader('content-length')
  134.             if clength:
  135.                 
  136.                 try:
  137.                     bytes = int(clength)
  138.                 except ValueError:
  139.                     pass
  140.                 
  141.  
  142.             if bytes > 0:
  143.                 if maxlen and bytes > maxlen:
  144.                     raise ValueError, 'Maximum content length exceeded'
  145.                 data = fp.read(bytes)
  146.             else:
  147.                 data = ''
  148.         lines = []
  149.         while None:
  150.             line = fp.readline()
  151.             if not line:
  152.                 terminator = lastpart
  153.                 break
  154.             if line[:2] == '--':
  155.                 terminator = line.strip()
  156.                 if terminator in (nextpart, lastpart):
  157.                     break
  158.                 
  159.             continue
  160.             if data is None:
  161.                 continue
  162.             if bytes < 0 and lines:
  163.                 line = lines[-1]
  164.                 if line[-2:] == '\r\n':
  165.                     line = line[:-2]
  166.                 elif line[-1:] == '\n':
  167.                     line = line[:-1]
  168.                 lines[-1] = line
  169.                 data = ''.join(lines)
  170.             
  171.         line = headers['content-disposition']
  172.         if not line:
  173.             continue
  174.         (key, params) = parse_header(line)
  175.         if key != 'form-data':
  176.             continue
  177.         if 'name' in params:
  178.             name = params['name']
  179.         
  180.         if name in partdict:
  181.             partdict[name].append(data)
  182.             continue
  183.         partdict[name] = [
  184.             data]
  185.     return partdict
  186.  
  187.  
  188. def _parseparam(s):
  189.     while s[:1] == ';':
  190.         s = s[1:]
  191.         end = s.find(';')
  192.         while end > 0 and s.count('"', 0, end) % 2:
  193.             end = s.find(';', end + 1)
  194.         if end < 0:
  195.             end = len(s)
  196.         f = s[:end]
  197.         yield f.strip()
  198.         s = s[end:]
  199.  
  200.  
  201. def parse_header(line):
  202.     parts = _parseparam(';' + line)
  203.     key = parts.next()
  204.     pdict = { }
  205.     for p in parts:
  206.         i = p.find('=')
  207.         if i >= 0:
  208.             name = p[:i].strip().lower()
  209.             value = p[i + 1:].strip()
  210.             if len(value) >= 2:
  211.                 if value[-1] == value[-1]:
  212.                     pass
  213.                 elif value[-1] == '"':
  214.                     value = value[1:-1]
  215.                     value = value.replace('\\\\', '\\').replace('\\"', '"')
  216.         pdict[name] = value
  217.     
  218.     return (key, pdict)
  219.  
  220.  
  221. class MiniFieldStorage:
  222.     filename = None
  223.     list = None
  224.     type = None
  225.     file = None
  226.     type_options = { }
  227.     disposition = None
  228.     disposition_options = { }
  229.     headers = { }
  230.     
  231.     def __init__(self, name, value):
  232.         self.name = name
  233.         self.value = value
  234.  
  235.     
  236.     def __repr__(self):
  237.         return 'MiniFieldStorage(%r, %r)' % (self.name, self.value)
  238.  
  239.  
  240.  
  241. class FieldStorage:
  242.     
  243.     def __init__(self, fp = None, headers = None, outerboundary = '', environ = os.environ, keep_blank_values = 0, strict_parsing = 0):
  244.         method = 'GET'
  245.         self.keep_blank_values = keep_blank_values
  246.         self.strict_parsing = strict_parsing
  247.         if 'REQUEST_METHOD' in environ:
  248.             method = environ['REQUEST_METHOD'].upper()
  249.         self.qs_on_post = None
  250.         if method == 'GET' or method == 'HEAD':
  251.             if 'QUERY_STRING' in environ:
  252.                 qs = environ['QUERY_STRING']
  253.             elif sys.argv[1:]:
  254.                 qs = sys.argv[1]
  255.             else:
  256.                 qs = ''
  257.             fp = StringIO(qs)
  258.             if headers is None:
  259.                 headers = {
  260.                     'content-type': 'application/x-www-form-urlencoded' }
  261.             
  262.         if headers is None:
  263.             headers = { }
  264.             if method == 'POST':
  265.                 headers['content-type'] = 'application/x-www-form-urlencoded'
  266.             if 'CONTENT_TYPE' in environ:
  267.                 headers['content-type'] = environ['CONTENT_TYPE']
  268.             if 'QUERY_STRING' in environ:
  269.                 self.qs_on_post = environ['QUERY_STRING']
  270.             if 'CONTENT_LENGTH' in environ:
  271.                 headers['content-length'] = environ['CONTENT_LENGTH']
  272.             
  273.         if not fp:
  274.             pass
  275.         self.fp = sys.stdin
  276.         self.headers = headers
  277.         self.outerboundary = outerboundary
  278.         cdisp = ''
  279.         pdict = { }
  280.         if 'content-disposition' in self.headers:
  281.             (cdisp, pdict) = parse_header(self.headers['content-disposition'])
  282.         self.disposition = cdisp
  283.         self.disposition_options = pdict
  284.         self.name = None
  285.         if 'name' in pdict:
  286.             self.name = pdict['name']
  287.         self.filename = None
  288.         if 'filename' in pdict:
  289.             self.filename = pdict['filename']
  290.         if 'content-type' in self.headers:
  291.             (ctype, pdict) = parse_header(self.headers['content-type'])
  292.         elif self.outerboundary or method != 'POST':
  293.             ctype = 'text/plain'
  294.             pdict = { }
  295.         else:
  296.             ctype = 'application/x-www-form-urlencoded'
  297.             pdict = { }
  298.         self.type = ctype
  299.         self.type_options = pdict
  300.         self.innerboundary = ''
  301.         if 'boundary' in pdict:
  302.             self.innerboundary = pdict['boundary']
  303.         clen = -1
  304.         if 'content-length' in self.headers:
  305.             
  306.             try:
  307.                 clen = int(self.headers['content-length'])
  308.             except ValueError:
  309.                 pass
  310.  
  311.             if maxlen and clen > maxlen:
  312.                 raise ValueError, 'Maximum content length exceeded'
  313.         self.length = clen
  314.         self.list = None
  315.         self.file = None
  316.         self.done = 0
  317.         if ctype == 'application/x-www-form-urlencoded':
  318.             self.read_urlencoded()
  319.         elif ctype[:10] == 'multipart/':
  320.             self.read_multi(environ, keep_blank_values, strict_parsing)
  321.         else:
  322.             self.read_single()
  323.  
  324.     
  325.     def __repr__(self):
  326.         return 'FieldStorage(%r, %r, %r)' % (self.name, self.filename, self.value)
  327.  
  328.     
  329.     def __iter__(self):
  330.         return iter(self.keys())
  331.  
  332.     
  333.     def __getattr__(self, name):
  334.         if name != 'value':
  335.             raise AttributeError, name
  336.         if self.file:
  337.             self.file.seek(0)
  338.             value = self.file.read()
  339.             self.file.seek(0)
  340.         elif self.list is not None:
  341.             value = self.list
  342.         else:
  343.             value = None
  344.         return value
  345.  
  346.     
  347.     def __getitem__(self, key):
  348.         if self.list is None:
  349.             raise TypeError, 'not indexable'
  350.         found = []
  351.         for item in self.list:
  352.             if item.name == key:
  353.                 found.append(item)
  354.                 continue
  355.         if not found:
  356.             raise KeyError, key
  357.         if len(found) == 1:
  358.             return found[0]
  359.         return None
  360.  
  361.     
  362.     def getvalue(self, key, default = None):
  363.         if key in self:
  364.             value = self[key]
  365.             if type(value) is type([]):
  366.                 return map(attrgetter('value'), value)
  367.             return None.value
  368.         return default
  369.  
  370.     
  371.     def getfirst(self, key, default = None):
  372.         if key in self:
  373.             value = self[key]
  374.             if type(value) is type([]):
  375.                 return value[0].value
  376.             return None.value
  377.         return default
  378.  
  379.     
  380.     def getlist(self, key):
  381.         if key in self:
  382.             value = self[key]
  383.             if type(value) is type([]):
  384.                 return map(attrgetter('value'), value)
  385.             return [
  386.                 None.value]
  387.         return []
  388.  
  389.     
  390.     def keys(self):
  391.         if self.list is None:
  392.             raise TypeError, 'not indexable'
  393.         return list(set((lambda .0: pass)(self.list)))
  394.  
  395.     
  396.     def has_key(self, key):
  397.         if self.list is None:
  398.             raise TypeError, 'not indexable'
  399.         return (any,)((lambda .0: pass)(self.list))
  400.  
  401.     
  402.     def __contains__(self, key):
  403.         if self.list is None:
  404.             raise TypeError, 'not indexable'
  405.         return (any,)((lambda .0: pass)(self.list))
  406.  
  407.     
  408.     def __len__(self):
  409.         return len(self.keys())
  410.  
  411.     
  412.     def __nonzero__(self):
  413.         return bool(self.list)
  414.  
  415.     
  416.     def read_urlencoded(self):
  417.         qs = self.fp.read(self.length)
  418.         if self.qs_on_post:
  419.             qs += '&' + self.qs_on_post
  420.         self.list = list = []
  421.         for key, value in urlparse.parse_qsl(qs, self.keep_blank_values, self.strict_parsing):
  422.             list.append(MiniFieldStorage(key, value))
  423.         
  424.         self.skip_lines()
  425.  
  426.     FieldStorageClass = None
  427.     
  428.     def read_multi(self, environ, keep_blank_values, strict_parsing):
  429.         ib = self.innerboundary
  430.         if not valid_boundary(ib):
  431.             raise ValueError, 'Invalid boundary in multipart form: %r' % (ib,)
  432.         self.list = []
  433.         if self.qs_on_post:
  434.             for key, value in urlparse.parse_qsl(self.qs_on_post, self.keep_blank_values, self.strict_parsing):
  435.                 self.list.append(MiniFieldStorage(key, value))
  436.             
  437.             FieldStorageClass = None
  438.         if not self.FieldStorageClass:
  439.             pass
  440.         klass = self.__class__
  441.         part = klass(self.fp, { }, ib, environ, keep_blank_values, strict_parsing)
  442.         while not part.done:
  443.             headers = rfc822.Message(self.fp)
  444.             part = klass(self.fp, headers, ib, environ, keep_blank_values, strict_parsing)
  445.             self.list.append(part)
  446.         self.skip_lines()
  447.  
  448.     
  449.     def read_single(self):
  450.         if self.length >= 0:
  451.             self.read_binary()
  452.             self.skip_lines()
  453.         else:
  454.             self.read_lines()
  455.         self.file.seek(0)
  456.  
  457.     bufsize = 8192
  458.     
  459.     def read_binary(self):
  460.         self.file = self.make_file('b')
  461.         todo = self.length
  462.         if todo >= 0:
  463.             while todo > 0:
  464.                 data = self.fp.read(min(todo, self.bufsize))
  465.                 if not data:
  466.                     self.done = -1
  467.                     break
  468.                 self.file.write(data)
  469.                 todo = todo - len(data)
  470.  
  471.     
  472.     def read_lines(self):
  473.         self.file = self._FieldStorage__file = StringIO()
  474.         if self.outerboundary:
  475.             self.read_lines_to_outerboundary()
  476.         else:
  477.             self.read_lines_to_eof()
  478.  
  479.     
  480.     def __write(self, line):
  481.         if self._FieldStorage__file is not None and self._FieldStorage__file.tell() + len(line) > 1000:
  482.             self.file = self.make_file('')
  483.             self.file.write(self._FieldStorage__file.getvalue())
  484.             self._FieldStorage__file = None
  485.         
  486.         self.file.write(line)
  487.  
  488.     
  489.     def read_lines_to_eof(self):
  490.         while None:
  491.             line = self.fp.readline(65536)
  492.             if not line:
  493.                 self.done = -1
  494.                 break
  495.             continue
  496.             return None
  497.  
  498.     
  499.     def read_lines_to_outerboundary(self):
  500.         next = '--' + self.outerboundary
  501.         last = next + '--'
  502.         delim = ''
  503.         last_line_lfend = True
  504.         while None:
  505.             line = self.fp.readline(65536)
  506.             if not line:
  507.                 self.done = -1
  508.                 break
  509.             if line[:2] == '--' and last_line_lfend:
  510.                 strippedline = line.strip()
  511.                 if strippedline == next:
  512.                     break
  513.                 if strippedline == last:
  514.                     self.done = 1
  515.                     break
  516.                 
  517.             odelim = delim
  518.             if line[-2:] == '\r\n':
  519.                 delim = '\r\n'
  520.                 line = line[:-2]
  521.                 last_line_lfend = True
  522.             elif line[-1] == '\n':
  523.                 delim = '\n'
  524.                 line = line[:-1]
  525.                 last_line_lfend = True
  526.             else:
  527.                 delim = ''
  528.                 last_line_lfend = False
  529.             continue
  530.             return None
  531.  
  532.     
  533.     def skip_lines(self):
  534.         if not (self.outerboundary) or self.done:
  535.             return None
  536.         next = None + self.outerboundary
  537.         last = next + '--'
  538.         last_line_lfend = True
  539.         while None:
  540.             line = self.fp.readline(65536)
  541.             if not line:
  542.                 self.done = -1
  543.                 break
  544.             if line[:2] == '--' and last_line_lfend:
  545.                 strippedline = line.strip()
  546.                 if strippedline == next:
  547.                     break
  548.                 if strippedline == last:
  549.                     self.done = 1
  550.                     break
  551.                 
  552.             last_line_lfend = line.endswith('\n')
  553.             continue
  554.             return None
  555.  
  556.     
  557.     def make_file(self, binary = None):
  558.         import tempfile
  559.         return tempfile.TemporaryFile('w+b')
  560.  
  561.  
  562.  
  563. class FormContentDict(UserDict.UserDict):
  564.     
  565.     def __init__(self, environ = os.environ, keep_blank_values = 0, strict_parsing = 0):
  566.         self.dict = self.data = parse(environ = environ, keep_blank_values = keep_blank_values, strict_parsing = strict_parsing)
  567.         self.query_string = environ['QUERY_STRING']
  568.  
  569.  
  570.  
  571. class SvFormContentDict(FormContentDict):
  572.     
  573.     def __getitem__(self, key):
  574.         if len(self.dict[key]) > 1:
  575.             raise IndexError, 'expecting a single value'
  576.         return self.dict[key][0]
  577.  
  578.     
  579.     def getlist(self, key):
  580.         return self.dict[key]
  581.  
  582.     
  583.     def values(self):
  584.         result = []
  585.         for value in self.dict.values():
  586.             if len(value) == 1:
  587.                 result.append(value[0])
  588.                 continue
  589.             result.append(value)
  590.         
  591.         return result
  592.  
  593.     
  594.     def items(self):
  595.         result = []
  596.         for key, value in self.dict.items():
  597.             if len(value) == 1:
  598.                 result.append((key, value[0]))
  599.                 continue
  600.             result.append((key, value))
  601.         
  602.         return result
  603.  
  604.  
  605.  
  606. class InterpFormContentDict(SvFormContentDict):
  607.     
  608.     def __getitem__(self, key):
  609.         v = SvFormContentDict.__getitem__(self, key)
  610.         if v[0] in '0123456789+-.':
  611.             
  612.             try:
  613.                 return int(v)
  614.             except ValueError:
  615.                 
  616.                 try:
  617.                     return float(v)
  618.                 except ValueError:
  619.                     pass
  620.                 
  621.  
  622.             
  623.  
  624.         return v.strip()
  625.  
  626.     
  627.     def values(self):
  628.         result = []
  629.         for key in self.keys():
  630.             
  631.             try:
  632.                 result.append(self[key])
  633.             continue
  634.             except IndexError:
  635.                 result.append(self.dict[key])
  636.                 continue
  637.             
  638.  
  639.         
  640.         return result
  641.  
  642.     
  643.     def items(self):
  644.         result = []
  645.         for key in self.keys():
  646.             
  647.             try:
  648.                 result.append((key, self[key]))
  649.             continue
  650.             except IndexError:
  651.                 result.append((key, self.dict[key]))
  652.                 continue
  653.             
  654.  
  655.         
  656.         return result
  657.  
  658.  
  659.  
  660. class FormContent(FormContentDict):
  661.     
  662.     def values(self, key):
  663.         if key in self.dict:
  664.             return self.dict[key]
  665.         return None
  666.  
  667.     
  668.     def indexed_value(self, key, location):
  669.         if key in self.dict:
  670.             if len(self.dict[key]) > location:
  671.                 return self.dict[key][location]
  672.             return None
  673.         return None
  674.  
  675.     
  676.     def value(self, key):
  677.         if key in self.dict:
  678.             return self.dict[key][0]
  679.         return None
  680.  
  681.     
  682.     def length(self, key):
  683.         return len(self.dict[key])
  684.  
  685.     
  686.     def stripped(self, key):
  687.         if key in self.dict:
  688.             return self.dict[key][0].strip()
  689.         return None
  690.  
  691.     
  692.     def pars(self):
  693.         return self.dict
  694.  
  695.  
  696.  
  697. def test(environ = os.environ):
  698.     global maxlen
  699.     print 'Content-type: text/html'
  700.     print 
  701.     sys.stderr = sys.stdout
  702.     
  703.     try:
  704.         form = FieldStorage()
  705.         print_directory()
  706.         print_arguments()
  707.         print_form(form)
  708.         print_environ(environ)
  709.         print_environ_usage()
  710.         
  711.         def f():
  712.             exec 'testing print_exception() -- <I>italics?</I>'
  713.  
  714.         
  715.         def g(f = f):
  716.             f()
  717.  
  718.         print '<H3>What follows is a test, not an actual exception:</H3>'
  719.         g()
  720.     except:
  721.         print_exception()
  722.  
  723.     print '<H1>Second try with a small maxlen...</H1>'
  724.     maxlen = 50
  725.     
  726.     try:
  727.         form = FieldStorage()
  728.         print_directory()
  729.         print_arguments()
  730.         print_form(form)
  731.         print_environ(environ)
  732.     except:
  733.         print_exception()
  734.  
  735.  
  736.  
  737. def print_exception(type = None, value = None, tb = None, limit = None):
  738.     if type is None:
  739.         (type, value, tb) = sys.exc_info()
  740.     import traceback
  741.     print 
  742.     print '<H3>Traceback (most recent call last):</H3>'
  743.     list = traceback.format_tb(tb, limit) + traceback.format_exception_only(type, value)
  744.     print '<PRE>%s<B>%s</B></PRE>' % (escape(''.join(list[:-1])), escape(list[-1]))
  745.     del tb
  746.  
  747.  
  748. def print_environ(environ = os.environ):
  749.     keys = environ.keys()
  750.     keys.sort()
  751.     print 
  752.     print '<H3>Shell Environment:</H3>'
  753.     print '<DL>'
  754.     for key in keys:
  755.         print '<DT>', escape(key), '<DD>', escape(environ[key])
  756.     
  757.     print '</DL>'
  758.     print 
  759.  
  760.  
  761. def print_form(form):
  762.     keys = form.keys()
  763.     keys.sort()
  764.     print 
  765.     print '<H3>Form Contents:</H3>'
  766.     if not keys:
  767.         print '<P>No form fields.'
  768.     print '<DL>'
  769.     for key in keys:
  770.         print '<DT>' + escape(key) + ':',
  771.         value = form[key]
  772.         print '<i>' + escape(repr(type(value))) + '</i>'
  773.         print '<DD>' + escape(repr(value))
  774.     
  775.     print '</DL>'
  776.     print 
  777.  
  778.  
  779. def print_directory():
  780.     print 
  781.     print '<H3>Current Working Directory:</H3>'
  782.     
  783.     try:
  784.         pwd = os.getcwd()
  785.     except os.error:
  786.         msg = None
  787.         print 'os.error:', escape(str(msg))
  788.  
  789.     print escape(pwd)
  790.     print 
  791.  
  792.  
  793. def print_arguments():
  794.     print 
  795.     print '<H3>Command Line Arguments:</H3>'
  796.     print 
  797.     print sys.argv
  798.     print 
  799.  
  800.  
  801. def print_environ_usage():
  802.     print '\n<H3>These environment variables could have been set:</H3>\n<UL>\n<LI>AUTH_TYPE\n<LI>CONTENT_LENGTH\n<LI>CONTENT_TYPE\n<LI>DATE_GMT\n<LI>DATE_LOCAL\n<LI>DOCUMENT_NAME\n<LI>DOCUMENT_ROOT\n<LI>DOCUMENT_URI\n<LI>GATEWAY_INTERFACE\n<LI>LAST_MODIFIED\n<LI>PATH\n<LI>PATH_INFO\n<LI>PATH_TRANSLATED\n<LI>QUERY_STRING\n<LI>REMOTE_ADDR\n<LI>REMOTE_HOST\n<LI>REMOTE_IDENT\n<LI>REMOTE_USER\n<LI>REQUEST_METHOD\n<LI>SCRIPT_NAME\n<LI>SERVER_NAME\n<LI>SERVER_PORT\n<LI>SERVER_PROTOCOL\n<LI>SERVER_ROOT\n<LI>SERVER_SOFTWARE\n</UL>\nIn addition, HTTP headers sent by the server may be passed in the\nenvironment as well.  Here are some common variable names:\n<UL>\n<LI>HTTP_ACCEPT\n<LI>HTTP_CONNECTION\n<LI>HTTP_HOST\n<LI>HTTP_PRAGMA\n<LI>HTTP_REFERER\n<LI>HTTP_USER_AGENT\n</UL>\n'
  803.  
  804.  
  805. def escape(s, quote = None):
  806.     s = s.replace('&', '&')
  807.     s = s.replace('<', '<')
  808.     s = s.replace('>', '>')
  809.     if quote:
  810.         s = s.replace('"', '"')
  811.     return s
  812.  
  813.  
  814. def valid_boundary(s, _vb_pattern = '^[ -~]{0,200}[!-~]$'):
  815.     import re
  816.     return re.match(_vb_pattern, s)
  817.  
  818. if __name__ == '__main__':
  819.     test()
  820.