comparison mxtool/mx.py @ 11295:bb70a309a7cf

enhanced select_items to support multiple selections
author Doug Simon <doug.simon@oracle.com>
date Tue, 13 Aug 2013 11:07:08 +0200
parents d89a5dbaaaf6
children 8bcae501c51b
comparison
equal deleted inserted replaced
11292:5040ec3ff3aa 11295:bb70a309a7cf
136 136
137 Property values can use environment variables with Bash syntax (e.g. ${HOME}). 137 Property values can use environment variables with Bash syntax (e.g. ${HOME}).
138 """ 138 """
139 139
140 import sys, os, errno, time, subprocess, shlex, types, urllib2, contextlib, StringIO, zipfile, signal, xml.sax.saxutils, tempfile 140 import sys, os, errno, time, subprocess, shlex, types, urllib2, contextlib, StringIO, zipfile, signal, xml.sax.saxutils, tempfile
141 import textwrap
141 import xml.parsers.expat 142 import xml.parsers.expat
142 import shutil, re, xml.dom.minidom 143 import shutil, re, xml.dom.minidom
143 from collections import Callable 144 from collections import Callable
144 from threading import Thread 145 from threading import Thread
145 from argparse import ArgumentParser, REMAINDER 146 from argparse import ArgumentParser, REMAINDER
3277 matches.append(classname) 3278 matches.append(classname)
3278 if logToConsole: 3279 if logToConsole:
3279 log(classname) 3280 log(classname)
3280 return matches 3281 return matches
3281 3282
3282 def select_items(candidates): 3283 def select_items(items, descriptions=None, allowMultiple=True):
3283 """ 3284 """
3284 Presents a command line interface for selecting one or more items from a sequence. 3285 Presents a command line interface for selecting one or more (if allowMultiple is true) items.
3285 """ 3286
3286 if len(candidates) == 0: 3287 """
3288 if len(items) == 0:
3287 return [] 3289 return []
3288 elif len(candidates) > 1: 3290 elif len(items) > 1:
3289 log('[0] <all>') 3291 if allowMultiple:
3290 for i in range(0, len(candidates)): 3292 log('[0] <all>')
3291 log('[{0}] {1}'.format(i + 1, candidates[i])) 3293 for i in range(0, len(items)):
3292 s = raw_input('Enter number of selection: ') 3294 if descriptions is None:
3293 try: 3295 log('[{0}] {1}'.format(i + 1, items[i]))
3294 si = int(s) 3296 else:
3295 except: 3297 assert len(items) == len(descriptions)
3296 si = 0 3298 wrapper = textwrap.TextWrapper(subsequent_indent=' ')
3297 if si == 0 or si not in range(1, len(candidates) + 1): 3299 log('\n'.join(wrapper.wrap('[{0}] {1} - {2}'.format(i + 1, items[i], descriptions[i]))))
3298 return candidates 3300 while True:
3299 else: 3301 if allowMultiple:
3300 return [candidates[si - 1]] 3302 s = raw_input('Enter number(s) of selection (separate multiple choices with spaces): ').split()
3303 else:
3304 s = [raw_input('Enter number of selection: ')]
3305 try:
3306 s = [int(x) for x in s]
3307 except:
3308 log('Selection contains non-numeric characters: "' + ' '.join(s) + '"')
3309 continue
3310
3311 if allowMultiple and 0 in s:
3312 return items
3313
3314 indexes = []
3315 for n in s:
3316 if n not in range(1, len(items) + 1):
3317 log('Invalid selection: ' + str(n))
3318 continue
3319 else:
3320 indexes.append(n - 1)
3321 if allowMultiple:
3322 return [items[i] for i in indexes]
3323 if len(indexes) == 1:
3324 return items[indexes[0]]
3325 return None
3301 3326
3302 def javap(args): 3327 def javap(args):
3303 """disassemble classes matching given pattern with javap""" 3328 """disassemble classes matching given pattern with javap"""
3304 3329
3305 javap = java().javap 3330 javap = java().javap