OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/ |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 |
| 22 import os |
| 23 |
| 24 def int_val_fn(v): |
| 25 try: |
| 26 int(v) |
| 27 return True |
| 28 except: |
| 29 return False |
| 30 |
| 31 class IObject(object): |
| 32 |
| 33 def choose_from_list(self, item_list, search_str='', |
| 34 prompt='Enter Selection'): |
| 35 if not item_list: |
| 36 print 'No Choices Available' |
| 37 return |
| 38 choice = None |
| 39 while not choice: |
| 40 n = 1 |
| 41 choices = [] |
| 42 for item in item_list: |
| 43 if isinstance(item, basestring): |
| 44 print '[%d] %s' % (n, item) |
| 45 choices.append(item) |
| 46 n += 1 |
| 47 else: |
| 48 obj, id, desc = item |
| 49 if desc: |
| 50 if desc.find(search_str) >= 0: |
| 51 print '[%d] %s - %s' % (n, id, desc) |
| 52 choices.append(obj) |
| 53 n += 1 |
| 54 else: |
| 55 if id.find(search_str) >= 0: |
| 56 print '[%d] %s' % (n, id) |
| 57 choices.append(obj) |
| 58 n += 1 |
| 59 if choices: |
| 60 val = raw_input('%s[1-%d]: ' % (prompt, len(choices))) |
| 61 if val.startswith('/'): |
| 62 search_str = val[1:] |
| 63 else: |
| 64 try: |
| 65 int_val = int(val) |
| 66 if int_val == 0: |
| 67 return None |
| 68 choice = choices[int_val-1] |
| 69 except ValueError: |
| 70 print '%s is not a valid choice' % val |
| 71 except IndexError: |
| 72 print '%s is not within the range[1-%d]' % (val, |
| 73 len(choices)
) |
| 74 else: |
| 75 print "No objects matched your pattern" |
| 76 search_str = '' |
| 77 return choice |
| 78 |
| 79 def get_string(self, prompt, validation_fn=None): |
| 80 okay = False |
| 81 while not okay: |
| 82 val = raw_input('%s: ' % prompt) |
| 83 if validation_fn: |
| 84 okay = validation_fn(val) |
| 85 if not okay: |
| 86 print 'Invalid value: %s' % val |
| 87 else: |
| 88 okay = True |
| 89 return val |
| 90 |
| 91 def get_filename(self, prompt): |
| 92 okay = False |
| 93 val = '' |
| 94 while not okay: |
| 95 val = raw_input('%s: %s' % (prompt, val)) |
| 96 val = os.path.expanduser(val) |
| 97 if os.path.isfile(val): |
| 98 okay = True |
| 99 elif os.path.isdir(val): |
| 100 path = val |
| 101 val = self.choose_from_list(os.listdir(path)) |
| 102 if val: |
| 103 val = os.path.join(path, val) |
| 104 okay = True |
| 105 else: |
| 106 val = '' |
| 107 else: |
| 108 print 'Invalid value: %s' % val |
| 109 val = '' |
| 110 return val |
| 111 |
| 112 def get_int(self, prompt): |
| 113 s = self.get_string(prompt, int_val_fn) |
| 114 return int(s) |
| 115 |
OLD | NEW |