PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/boto-2.5.2/boto/mashups/iobject.py

#
Python | 115 lines | 91 code | 4 blank | 20 comment | 1 complexity | d2274b1b483febdd32d0680e93a99c30 MD5 | raw file
  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. import os
  22. def int_val_fn(v):
  23. try:
  24. int(v)
  25. return True
  26. except:
  27. return False
  28. class IObject(object):
  29. def choose_from_list(self, item_list, search_str='',
  30. prompt='Enter Selection'):
  31. if not item_list:
  32. print 'No Choices Available'
  33. return
  34. choice = None
  35. while not choice:
  36. n = 1
  37. choices = []
  38. for item in item_list:
  39. if isinstance(item, basestring):
  40. print '[%d] %s' % (n, item)
  41. choices.append(item)
  42. n += 1
  43. else:
  44. obj, id, desc = item
  45. if desc:
  46. if desc.find(search_str) >= 0:
  47. print '[%d] %s - %s' % (n, id, desc)
  48. choices.append(obj)
  49. n += 1
  50. else:
  51. if id.find(search_str) >= 0:
  52. print '[%d] %s' % (n, id)
  53. choices.append(obj)
  54. n += 1
  55. if choices:
  56. val = raw_input('%s[1-%d]: ' % (prompt, len(choices)))
  57. if val.startswith('/'):
  58. search_str = val[1:]
  59. else:
  60. try:
  61. int_val = int(val)
  62. if int_val == 0:
  63. return None
  64. choice = choices[int_val-1]
  65. except ValueError:
  66. print '%s is not a valid choice' % val
  67. except IndexError:
  68. print '%s is not within the range[1-%d]' % (val,
  69. len(choices))
  70. else:
  71. print "No objects matched your pattern"
  72. search_str = ''
  73. return choice
  74. def get_string(self, prompt, validation_fn=None):
  75. okay = False
  76. while not okay:
  77. val = raw_input('%s: ' % prompt)
  78. if validation_fn:
  79. okay = validation_fn(val)
  80. if not okay:
  81. print 'Invalid value: %s' % val
  82. else:
  83. okay = True
  84. return val
  85. def get_filename(self, prompt):
  86. okay = False
  87. val = ''
  88. while not okay:
  89. val = raw_input('%s: %s' % (prompt, val))
  90. val = os.path.expanduser(val)
  91. if os.path.isfile(val):
  92. okay = True
  93. elif os.path.isdir(val):
  94. path = val
  95. val = self.choose_from_list(os.listdir(path))
  96. if val:
  97. val = os.path.join(path, val)
  98. okay = True
  99. else:
  100. val = ''
  101. else:
  102. print 'Invalid value: %s' % val
  103. val = ''
  104. return val
  105. def get_int(self, prompt):
  106. s = self.get_string(prompt, int_val_fn)
  107. return int(s)