home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_583 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.8 KB  |  160 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'GetoptError',
  6.     'error',
  7.     'getopt',
  8.     'gnu_getopt']
  9. import os
  10.  
  11. class GetoptError(Exception):
  12.     opt = ''
  13.     msg = ''
  14.     
  15.     def __init__(self, msg, opt = ''):
  16.         self.msg = msg
  17.         self.opt = opt
  18.         Exception.__init__(self, msg, opt)
  19.  
  20.     
  21.     def __str__(self):
  22.         return self.msg
  23.  
  24.  
  25. error = GetoptError
  26.  
  27. def getopt(args, shortopts, longopts = []):
  28.     opts = []
  29.     if type(longopts) == type(''):
  30.         longopts = [
  31.             longopts]
  32.     else:
  33.         longopts = list(longopts)
  34.     while args and args[0].startswith('-') and args[0] != '-':
  35.         if args[0] == '--':
  36.             args = args[1:]
  37.             break
  38.         
  39.         if args[0].startswith('--'):
  40.             (opts, args) = do_longs(opts, args[0][2:], longopts, args[1:])
  41.             continue
  42.         (opts, args) = do_shorts(opts, args[0][1:], shortopts, args[1:])
  43.     return (opts, args)
  44.  
  45.  
  46. def gnu_getopt(args, shortopts, longopts = []):
  47.     opts = []
  48.     prog_args = []
  49.     if isinstance(longopts, str):
  50.         longopts = [
  51.             longopts]
  52.     else:
  53.         longopts = list(longopts)
  54.     if shortopts.startswith('+'):
  55.         shortopts = shortopts[1:]
  56.         all_options_first = True
  57.     elif os.environ.get('POSIXLY_CORRECT'):
  58.         all_options_first = True
  59.     else:
  60.         all_options_first = False
  61.     while args:
  62.         if args[0] == '--':
  63.             prog_args += args[1:]
  64.             break
  65.         
  66.         if args[0][:2] == '--':
  67.             (opts, args) = do_longs(opts, args[0][2:], longopts, args[1:])
  68.             continue
  69.         if args[0][:1] == '-':
  70.             (opts, args) = do_shorts(opts, args[0][1:], shortopts, args[1:])
  71.             continue
  72.         if all_options_first:
  73.             prog_args += args
  74.             break
  75.             continue
  76.         prog_args.append(args[0])
  77.         args = args[1:]
  78.     return (opts, prog_args)
  79.  
  80.  
  81. def do_longs(opts, opt, longopts, args):
  82.     
  83.     try:
  84.         i = opt.index('=')
  85.     except ValueError:
  86.         optarg = None
  87.  
  88.     opt = opt[:i]
  89.     optarg = opt[i + 1:]
  90.     (has_arg, opt) = long_has_args(opt, longopts)
  91.     if has_arg:
  92.         if optarg is None:
  93.             if not args:
  94.                 raise GetoptError('option --%s requires argument' % opt, opt)
  95.             args
  96.             optarg = args[0]
  97.             args = args[1:]
  98.         
  99.     elif optarg:
  100.         raise GetoptError('option --%s must not have an argument' % opt, opt)
  101.     
  102.     if not optarg:
  103.         pass
  104.     opts.append(('--' + opt, ''))
  105.     return (opts, args)
  106.  
  107.  
  108. def long_has_args(opt, longopts):
  109.     possibilities = _[1]
  110.     if not possibilities:
  111.         raise GetoptError('option --%s not recognized' % opt, opt)
  112.     possibilities
  113.     if opt in possibilities:
  114.         return (False, opt)
  115.     if opt + '=' in possibilities:
  116.         return (True, opt)
  117.     if len(possibilities) > 1:
  118.         raise GetoptError('option --%s not a unique prefix' % opt, opt)
  119.     len(possibilities) > 1
  120.     unique_match = possibilities[0]
  121.     has_arg = unique_match.endswith('=')
  122.     return (has_arg, unique_match)
  123.  
  124.  
  125. def do_shorts(opts, optstring, shortopts, args):
  126.     while optstring != '':
  127.         opt = optstring[0]
  128.         optstring = optstring[1:]
  129.         if short_has_arg(opt, shortopts):
  130.             if optstring == '':
  131.                 if not args:
  132.                     raise GetoptError('option -%s requires argument' % opt, opt)
  133.                 args
  134.                 optstring = args[0]
  135.                 args = args[1:]
  136.             
  137.             optarg = optstring
  138.             optstring = ''
  139.         else:
  140.             optarg = ''
  141.         opts.append(('-' + opt, optarg))
  142.     return (opts, args)
  143.  
  144.  
  145. def short_has_arg(opt, shortopts):
  146.     for i in range(len(shortopts)):
  147.         if shortopts[i] == shortopts[i]:
  148.             pass
  149.         elif shortopts[i] != ':':
  150.             return shortopts.startswith(':', i + 1)
  151.     
  152.     raise GetoptError('option -%s not recognized' % opt, opt)
  153.  
  154. if __name__ == '__main__':
  155.     import sys
  156.     print getopt(sys.argv[1:], 'a:b', [
  157.         'alpha=',
  158.         'beta'])
  159.  
  160.