home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / BitTorrent / parseargs.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  3.8 KB  |  189 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. from types import *
  5. from cStringIO import StringIO
  6.  
  7. def formatDefinitions(options, COLS):
  8.     s = StringIO()
  9.     indent = ' ' * 10
  10.     width = COLS - 11
  11.     if width < 15:
  12.         width = COLS - 2
  13.         indent = ' '
  14.     
  15.     for longname, default, doc in options:
  16.         s.write('--' + longname + ' <arg>\n')
  17.         if default is not None:
  18.             doc += ' (defaults to ' + repr(default) + ')'
  19.         
  20.         i = 0
  21.         for word in doc.split():
  22.             if i == 0:
  23.                 s.write(indent + word)
  24.                 i = len(word)
  25.                 continue
  26.             if i + len(word) >= width:
  27.                 s.write('\n' + indent + word)
  28.                 i = len(word)
  29.                 continue
  30.             s.write(' ' + word)
  31.             i += len(word) + 1
  32.         
  33.         s.write('\n\n')
  34.     
  35.     return s.getvalue()
  36.  
  37.  
  38. def usage(str):
  39.     raise ValueError(str)
  40.  
  41.  
  42. def parseargs(argv, options, minargs = None, maxargs = None):
  43.     config = { }
  44.     longkeyed = { }
  45.     for option in options:
  46.         (longname, default, doc) = option
  47.         longkeyed[longname] = option
  48.         config[longname] = default
  49.     
  50.     options = []
  51.     args = []
  52.     pos = 0
  53.     while pos < len(argv):
  54.         if argv[pos][:2] != '--':
  55.             args.append(argv[pos])
  56.             pos += 1
  57.             continue
  58.         if pos == len(argv) - 1:
  59.             usage('parameter passed in at end with no value')
  60.         
  61.         key = argv[pos][2:]
  62.         value = argv[pos + 1]
  63.         pos += 2
  64.         if not longkeyed.has_key(key):
  65.             usage('unknown key --' + key)
  66.         
  67.         (longname, default, doc) = longkeyed[key]
  68.         
  69.         try:
  70.             t = type(config[longname])
  71.             if t is NoneType or t is StringType:
  72.                 config[longname] = value
  73.             elif t in (IntType, LongType):
  74.                 config[longname] = long(value)
  75.             elif t is FloatType:
  76.                 config[longname] = float(value)
  77.             elif not 0:
  78.                 raise AssertionError
  79.         continue
  80.         except ValueError:
  81.             e = None
  82.             usage('wrong format of --%s - %s' % (key, str(e)))
  83.             continue
  84.         
  85.  
  86.         None<EXCEPTION MATCH>ValueError
  87.     for key, value in config.items():
  88.         if value is None:
  89.             usage('Option --%s is required.' % key)
  90.             continue
  91.     
  92.     if minargs is not None and len(args) < minargs:
  93.         usage('Must supply at least %d args.' % minargs)
  94.     
  95.     if maxargs is not None and len(args) > maxargs:
  96.         usage('Too many args - %d max.' % maxargs)
  97.     
  98.     return (config, args)
  99.  
  100.  
  101. def test_parseargs():
  102.     if not parseargs(('d', '--a', 'pq', 'e', '--b', '3', '--c', '4.5', 'f'), (('a', 'x', ''), ('b', 1, ''), ('c', 2.2999999999999998, ''))) == ({
  103.         'a': 'pq',
  104.         'b': 3,
  105.         'c': 4.5 }, [
  106.         'd',
  107.         'e',
  108.         'f']):
  109.         raise AssertionError
  110.     if not parseargs([], [
  111.         ('a', 'x', '')]) == ({
  112.         'a': 'x' }, []):
  113.         raise AssertionError
  114.     if not parseargs([
  115.         '--a',
  116.         'x',
  117.         '--a',
  118.         'y'], [
  119.         ('a', '', '')]) == ({
  120.         'a': 'y' }, []):
  121.         raise AssertionError
  122.     
  123.     try:
  124.         parseargs([], [
  125.             ('a', 'x', '')])
  126.     except ValueError:
  127.         pass
  128.  
  129.     
  130.     try:
  131.         parseargs([
  132.             '--a',
  133.             'x'], [])
  134.     except ValueError:
  135.         pass
  136.  
  137.     
  138.     try:
  139.         parseargs([
  140.             '--a'], [
  141.             ('a', 'x', '')])
  142.     except ValueError:
  143.         pass
  144.  
  145.     
  146.     try:
  147.         parseargs([], [], 1, 2)
  148.     except ValueError:
  149.         pass
  150.  
  151.     if not parseargs([
  152.         'x'], [], 1, 2) == ({ }, [
  153.         'x']):
  154.         raise AssertionError
  155.     if not parseargs([
  156.         'x',
  157.         'y'], [], 1, 2) == ({ }, [
  158.         'x',
  159.         'y']):
  160.         raise AssertionError
  161.     
  162.     try:
  163.         parseargs([
  164.             'x',
  165.             'y',
  166.             'z'], [], 1, 2)
  167.     except ValueError:
  168.         pass
  169.  
  170.     
  171.     try:
  172.         parseargs([
  173.             '--a',
  174.             '2.0'], [
  175.             ('a', 3, '')])
  176.     except ValueError:
  177.         pass
  178.  
  179.     
  180.     try:
  181.         parseargs([
  182.             '--a',
  183.             'z'], [
  184.             ('a', 2.1000000000000001, '')])
  185.     except ValueError:
  186.         pass
  187.  
  188.  
  189.