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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5.  
  6. class ParseOptions:
  7.     
  8.     def __init__(self, system_string, options_dict):
  9.         self._ParseOptions__system_string = system_string[1:]
  10.         long_list = self._ParseOptions__make_long_list_func(options_dict)
  11.         short_list = self._ParseOptions__make_short_list_func(options_dict)
  12.         self._ParseOptions__legal_options = long_list + short_list
  13.         self._ParseOptions__short_long_dict = self._ParseOptions__make_short_long_dict_func(options_dict)
  14.         self._ParseOptions__opt_with_args = self._ParseOptions__make_options_with_arg_list(options_dict)
  15.         self._ParseOptions__options_okay = 1
  16.  
  17.     
  18.     def __make_long_list_func(self, options_dict):
  19.         legal_list = []
  20.         keys = options_dict.keys()
  21.         for key in keys:
  22.             key = '--' + key
  23.             legal_list.append(key)
  24.         
  25.         return legal_list
  26.  
  27.     
  28.     def __make_short_list_func(self, options_dict):
  29.         legal_list = []
  30.         keys = options_dict.keys()
  31.         for key in keys:
  32.             values = options_dict[key]
  33.             
  34.             try:
  35.                 legal_list.append('-' + values[1])
  36.             continue
  37.             except IndexError:
  38.                 continue
  39.             
  40.  
  41.         
  42.         return legal_list
  43.  
  44.     
  45.     def __make_short_long_dict_func(self, options_dict):
  46.         short_long_dict = { }
  47.         keys = options_dict.keys()
  48.         for key in keys:
  49.             values = options_dict[key]
  50.             
  51.             try:
  52.                 short = '-' + values[1]
  53.                 long = '--' + key
  54.                 short_long_dict[short] = long
  55.             continue
  56.             except IndexError:
  57.                 continue
  58.             
  59.  
  60.         
  61.         return short_long_dict
  62.  
  63.     
  64.     def __make_options_with_arg_list(self, options_dict):
  65.         opt_with_arg = []
  66.         keys = options_dict.keys()
  67.         for key in keys:
  68.             values = options_dict[key]
  69.             
  70.             try:
  71.                 if values[0]:
  72.                     opt_with_arg.append('--' + key)
  73.             continue
  74.             except IndexError:
  75.                 continue
  76.             
  77.  
  78.         
  79.         return opt_with_arg
  80.  
  81.     
  82.     def __sub_short_with_long(self):
  83.         new_string = []
  84.         sub_list = self._ParseOptions__short_long_dict.keys()
  85.         for item in self._ParseOptions__system_string:
  86.             if item in sub_list:
  87.                 item = self._ParseOptions__short_long_dict[item]
  88.             
  89.             new_string.append(item)
  90.         
  91.         return new_string
  92.  
  93.     
  94.     def __pair_arg_with_option(self):
  95.         opt_len = len(self._ParseOptions__system_string)
  96.         new_system_string = []
  97.         counter = 0
  98.         slurp_value = 0
  99.         for arg in self._ParseOptions__system_string:
  100.             counter += 1
  101.             if slurp_value:
  102.                 slurp_value = 0
  103.                 continue
  104.             
  105.             if arg[0] != '-':
  106.                 new_system_string.append(arg)
  107.                 continue
  108.             if '=' in arg:
  109.                 new_system_string.append(arg)
  110.                 continue
  111.             if arg in self._ParseOptions__opt_with_args:
  112.                 if counter + 1 > opt_len:
  113.                     sys.stderr.write('option "%s" must take an argument\n' % arg)
  114.                     new_system_string.append(arg)
  115.                     self._ParseOptions__options_okay = 0
  116.                 elif self._ParseOptions__system_string[counter][0] == '-':
  117.                     sys.stderr.write('option "%s" must take an argument\n' % arg)
  118.                     new_system_string.append(arg)
  119.                     self._ParseOptions__options_okay = 0
  120.                 else:
  121.                     new_system_string.append(arg + '=' + self._ParseOptions__system_string[counter])
  122.                     slurp_value = 1
  123.             counter + 1 > opt_len
  124.             new_system_string.append(arg)
  125.         
  126.         return new_system_string
  127.  
  128.     
  129.     def __get_just_options(self):
  130.         highest = 0
  131.         counter = 0
  132.         found_options = 0
  133.         for item in self._ParseOptions__system_string:
  134.             if item[0] == '-':
  135.                 highest = counter
  136.                 found_options = 1
  137.             
  138.             counter += 1
  139.         
  140.         if found_options:
  141.             just_options = self._ParseOptions__system_string[:highest + 1]
  142.             arguments = self._ParseOptions__system_string[highest + 1:]
  143.         else:
  144.             just_options = []
  145.             arguments = self._ParseOptions__system_string
  146.         if found_options:
  147.             for item in just_options:
  148.                 if item[0] != '-':
  149.                     sys.stderr.write('%s is an argument in an option list\n' % item)
  150.                     self._ParseOptions__options_okay = 0
  151.                     continue
  152.             
  153.         
  154.         return (just_options, arguments)
  155.  
  156.     
  157.     def __is_legal_option_func(self):
  158.         illegal_options = []
  159.         for arg in self._ParseOptions__system_string:
  160.             if '=' in arg:
  161.                 temp_list = arg.split('=')
  162.                 arg = temp_list[0]
  163.             
  164.             if arg not in self._ParseOptions__legal_options and arg[0] == '-':
  165.                 illegal_options.append(arg)
  166.                 continue
  167.         
  168.         if illegal_options:
  169.             self._ParseOptions__options_okay = 0
  170.             sys.stderr.write('The following options are not permitted:\n')
  171.             for not_legal in illegal_options:
  172.                 sys.stderr.write('%s\n' % not_legal)
  173.             
  174.         
  175.  
  176.     
  177.     def __make_options_dict(self, options):
  178.         options_dict = { }
  179.         for item in options:
  180.             if '=' in item:
  181.                 (option, arg) = item.split('=')
  182.             else:
  183.                 option = item
  184.                 arg = None
  185.             if option[0] == '-':
  186.                 option = option[1:]
  187.             
  188.             if option[0] == '-':
  189.                 option = option[1:]
  190.             
  191.             options_dict[option] = arg
  192.         
  193.         return options_dict
  194.  
  195.     
  196.     def parse_options(self):
  197.         self._ParseOptions__system_string = self._ParseOptions__sub_short_with_long()
  198.         self._ParseOptions__system_string = self._ParseOptions__pair_arg_with_option()
  199.         (options, arguments) = self._ParseOptions__get_just_options()
  200.         self._ParseOptions__is_legal_option_func()
  201.         if self._ParseOptions__options_okay:
  202.             options_dict = self._ParseOptions__make_options_dict(options)
  203.             return (options_dict, arguments)
  204.         return (0, 0)
  205.  
  206.  
  207. if __name__ == '__main__':
  208.     this_dict = {
  209.         'indents': [
  210.             0,
  211.             'i'],
  212.         'output': [
  213.             1,
  214.             'o'],
  215.         'test3': [
  216.             1,
  217.             't'] }
  218.     test_obj = ParseOptions(system_string = sys.argv, options_dict = this_dict)
  219.     (options, the_args) = test_obj.parse_options()
  220.     print options, the_args
  221.  
  222.