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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  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.         if args[0].startswith('--'):
  39.             (opts, args) = do_longs(opts, args[0][2:], longopts, args[1:])
  40.             continue
  41.         (opts, args) = do_shorts(opts, args[0][1:], shortopts, args[1:])
  42.     return (opts, args)
  43.  
  44.  
  45. def gnu_getopt(args, shortopts, longopts = []):
  46.     opts = []
  47.     prog_args = []
  48.     if isinstance(longopts, str):
  49.         longopts = [
  50.             longopts]
  51.     else:
  52.         longopts = list(longopts)
  53.     if shortopts.startswith('+'):
  54.         shortopts = shortopts[1:]
  55.         all_options_first = True
  56.     elif os.environ.get('POSIXLY_CORRECT'):
  57.         all_options_first = True
  58.     else:
  59.         all_options_first = False
  60.     while args:
  61.         if args[0] == '--':
  62.             prog_args += args[1:]
  63.             break
  64.         if args[0][:2] == '--':
  65.             (opts, args) = do_longs(opts, args[0][2:], longopts, args[1:])
  66.             continue
  67.         if args[0][:1] == '-' and args[0] != '-':
  68.             (opts, args) = do_shorts(opts, args[0][1:], shortopts, args[1:])
  69.             continue
  70.         if all_options_first:
  71.             prog_args += args
  72.             break
  73.             continue
  74.         prog_args.append(args[0])
  75.         args = args[1:]
  76.     return (opts, prog_args)
  77.  
  78.  
  79. def do_longs(opts, opt, longopts, args):
  80.     
  81.     try:
  82.         i = opt.index('=')
  83.     except ValueError:
  84.         optarg = None
  85.  
  86.     opt = opt[:i]
  87.     optarg = opt[i + 1:]
  88.     (has_arg, opt) = long_has_args(opt, longopts)
  89.     if has_arg or optarg is None:
  90.         if not args:
  91.             raise GetoptError('option --%s requires argument' % opt, opt)
  92.         optarg = args[0]
  93.         args = args[1:]
  94.     
  95.     if optarg is not None:
  96.         raise GetoptError('option --%s must not have an argument' % opt, opt)
  97.     if not optarg:
  98.         pass
  99.     opts.append(('--' + opt, ''))
  100.     return (opts, args)
  101.  
  102.  
  103. def long_has_args(opt, longopts):
  104.     possibilities = [ o for o in longopts if o.startswith(opt) ]
  105.     if not possibilities:
  106.         raise GetoptError('option --%s not recognized' % opt, opt)
  107.     if opt in possibilities:
  108.         return (False, opt)
  109.     if None + '=' in possibilities:
  110.         return (True, opt)
  111.     if None(possibilities) > 1:
  112.         raise GetoptError('option --%s not a unique prefix' % opt, opt)
  113.     unique_match = possibilities[0]
  114.     has_arg = unique_match.endswith('=')
  115.     if has_arg:
  116.         unique_match = unique_match[:-1]
  117.     return (has_arg, unique_match)
  118.  
  119.  
  120. def do_shorts(opts, optstring, shortopts, args):
  121.     while optstring != '':
  122.         opt = optstring[0]
  123.         optstring = optstring[1:]
  124.         if short_has_arg(opt, shortopts):
  125.             if optstring == '':
  126.                 if not args:
  127.                     raise GetoptError('option -%s requires argument' % opt, opt)
  128.                 optstring = args[0]
  129.                 args = args[1:]
  130.             optarg = optstring
  131.             optstring = ''
  132.         else:
  133.             optarg = ''
  134.         opts.append(('-' + opt, optarg))
  135.     return (opts, args)
  136.  
  137.  
  138. def short_has_arg(opt, shortopts):
  139.     for i in range(len(shortopts)):
  140.         if shortopts[i] == shortopts[i]:
  141.             pass
  142.         elif shortopts[i] != ':':
  143.             return shortopts.startswith(':', i + 1)
  144.     raise GetoptError('option -%s not recognized' % opt, opt)
  145.  
  146. if __name__ == '__main__':
  147.     import sys
  148.     print getopt(sys.argv[1:], 'a:b', [
  149.         'alpha=',
  150.         'beta'])
  151.