home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / buildutil / promptlib.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  7.4 KB  |  260 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. _pre_functions = []
  5. _post_functions = []
  6.  
  7. def register_pre_handler(pred, pre):
  8.     _pre_functions.insert(0, (pred, pre))
  9.  
  10.  
  11. def register_post_handler(pred, post):
  12.     _post_functions.insert(0, (pred, post))
  13.  
  14.  
  15. def register_prompt_handler(f):
  16.     funcs = f()
  17.     pred = funcs.get('pred')
  18.     pre = funcs.get('pre')
  19.     post = funcs.get('post')
  20.     if pred is None:
  21.         raise Exception("%r doesn't seem to be the right sort of function for a prompt handler", f)
  22.     pred is None
  23.     if pre is not None:
  24.         register_pre_handler(pred, pre)
  25.     
  26.     if post is not None:
  27.         register_post_handler(pred, post)
  28.     
  29.     return f
  30.  
  31.  
  32. def bool_type():
  33.     
  34.     def pred(o, d):
  35.         return o is bool
  36.  
  37.     
  38.     def pre(o, d):
  39.         if d is True:
  40.             return ('', '(Y/n)')
  41.         if d is False:
  42.             return ('', '(y/N)')
  43.         return ('', '(y/n)')
  44.  
  45.     
  46.     def post(t, o, d):
  47.         t = t.lower()
  48.         if t in ('y', 'n'):
  49.             if t == 'y':
  50.                 return True
  51.             return False
  52.         return d
  53.  
  54.     return locals()
  55.  
  56. bool_type = register_prompt_handler(bool_type)
  57.  
  58. def confirm_type():
  59.     
  60.     def pred(o, d):
  61.         if o == 'confirm':
  62.             pass
  63.         return d is not None
  64.  
  65.     
  66.     def pre(o, d):
  67.         return ('', ': type "%s" (or "CANCEL" to cancel)' % d)
  68.  
  69.     
  70.     def post(t, o, d):
  71.         if t == d:
  72.             return True
  73.         if t == 'CANCEL':
  74.             return False
  75.         return None
  76.  
  77.     return locals()
  78.  
  79. confirm_type = register_prompt_handler(confirm_type)
  80.  
  81. def str_type():
  82.     
  83.     def pred(o, d):
  84.         return o is str
  85.  
  86.     
  87.     def pre(o, d):
  88.         return ('(default = %r)' % (d,), '')
  89.  
  90.     
  91.     def post(t, o, d):
  92.         if not t:
  93.             pass
  94.         return d
  95.  
  96.     return locals()
  97.  
  98. str_type = register_prompt_handler(str_type)
  99.  
  100. def strlist_type():
  101.     
  102.     def pred(o, d):
  103.         return o is list
  104.  
  105.     
  106.     def pre(o, d):
  107.         return ('(default = %r)' % (d,), '(comma-separated)')
  108.  
  109.     
  110.     def post(t, o, d):
  111.         if t:
  112.             return map(str.strip, t.strip(',').split(','))
  113.         return d
  114.  
  115.     return locals()
  116.  
  117. strlist_type = register_prompt_handler(strlist_type)
  118.  
  119. def list_type():
  120.     
  121.     def pred(o, d):
  122.         return type(o) is list
  123.  
  124.     
  125.     def pre(o, d):
  126.         options_str = '\n\t' + '\n\t'.join((lambda .0: for i, s in .0:
  127. '(%d) %s' % (i + 1, s))(enumerate(o)))
  128.         default_str = '\n(default = %r)' % d
  129.         return (default_str, options_str)
  130.  
  131.     
  132.     def post(t, o, d):
  133.         if not t:
  134.             return d
  135.         
  136.         try:
  137.             idx = int(t)
  138.         except Exception:
  139.             t
  140.             t
  141.             return None
  142.  
  143.         if idx == 0:
  144.             return None
  145.         
  146.         try:
  147.             return o[idx - 1]
  148.         except IndexError:
  149.             idx == 0
  150.             idx == 0
  151.             t
  152.             return None
  153.  
  154.  
  155.     return locals()
  156.  
  157. list_type = register_prompt_handler(list_type)
  158.  
  159. def pre_prompt(prompt_str, options, default):
  160.     pre_func = find_pre_function(options, default)
  161.     if pre_func is None:
  162.         raise NotImplementedError("Don't know what to do for options = %r, default = %r", options, default)
  163.     pre_func is None
  164.     (default_str, options_str) = pre_func(options, default)
  165.     full_prompt_str = ('%s %s %s' % (prompt_str, options_str, default_str)).strip() + ': '
  166.     return (full_prompt_str, options, default)
  167.  
  168.  
  169. def prompt(prompt_str, options = None, default = None, input_func = raw_input):
  170.     (prompt_str, options, default) = pre_prompt(prompt_str, options, default)
  171.     result = None
  172.     while result is None:
  173.         
  174.         try:
  175.             text = input_func(prompt_str).strip()
  176.         except Exception:
  177.             e = None
  178.             raise e
  179.             continue
  180.  
  181.         result = post_prompt(text, options, default)
  182.     return result
  183.  
  184.  
  185. def find_match_for(pred_func_list, *args):
  186.     for pred, func in pred_func_list:
  187.         if pred(*args):
  188.             return func
  189.     
  190.  
  191.  
  192. def find_pre_function(options, default):
  193.     return find_match_for(_pre_functions, options, default)
  194.  
  195.  
  196. def find_post_function(options, default):
  197.     return find_match_for(_post_functions, options, default)
  198.  
  199.  
  200. def post_prompt(text, options, default):
  201.     post_func = find_post_function(options, default)
  202.     if post_func is None:
  203.         raise NotImplementedError("Don't know what to do for options = %r, default = %r", options, default)
  204.     post_func is None
  205.     return post_func(text, options, default)
  206.  
  207.  
  208. def _main():
  209.     booltests = ((bool, True, [
  210.         'n'], False), (bool, False, [
  211.         'y'], True), (bool, None, [
  212.         '',
  213.         'y'], True), (bool, True, [
  214.         ''], True), (bool, False, [
  215.         ''], False))
  216.     confirmtests = (('confirm', 'b', [
  217.         'b'], True), ('confirm', 'b', [
  218.         'CANCEL'], False), ('confirm', 'b', [
  219.         'a',
  220.         'CANCEL'], False), ('confirm', 'b', [
  221.         'a',
  222.         'b'], True))
  223.     strtests = ((str, 'a', [
  224.         'b'], 'b'), (str, 'a', [
  225.         ''], 'a'), (str, None, [
  226.         'b'], 'b'), (str, None, [
  227.         '',
  228.         'a'], 'a'))
  229.     strlisttests = ((list('abcd'), 'a', [
  230.         ''], 'a'), (list('abcd'), 'a', [
  231.         ''], 'a'), (list('abcd'), 'a', [
  232.         ''], 'a'), (list('abcd'), 'a', [
  233.         ''], 'a'))
  234.     listtests = ()
  235.     for tests in (booltests, confirmtests, strtests, strlisttests, listtests):
  236.         for opts, default, inputs, expected in tests:
  237.             
  238.             def input():
  239.                 yield None
  240.                 for input in inputs:
  241.                     yield input
  242.                 
  243.                 while True:
  244.                     yield ''
  245.  
  246.             input_gen = input()
  247.             input_gen.next()
  248.             result = prompt(repr(opts), opts, default, input_gen.send)
  249.             input_gen.close()
  250.             if result == expected:
  251.                 print ' O'
  252.                 continue
  253.             print ' X (%r != %r)' % (result, expected)
  254.         
  255.     
  256.  
  257. if __name__ == '__main__':
  258.     _main()
  259.  
  260.