Path: blob/master/elisp/emacs-for-python/rope-dist/ropemode/dialog.py
990 views
class Data(object):12def __init__(self, prompt=None, default=None, values=None,3kind=None, decode=None):4self.prompt = prompt5self.default = default6self.values = values7self.kind = kind8self._decode = decode910def decode(self, value):11if self._decode:12return self._decode(value)13return value141516class Boolean(Data):1718def __init__(self, prompt=None, default=False):19Data.__init__(self, prompt, self._encode(default),20[self._encode(True), self._encode(False)])2122def _encode(self, value):23if value:24return 'yes'25return 'no'2627def decode(self, value):28if value.lower() in ('yes', '1', 'true'):29return True30return False313233def show_dialog(askdata, actions, confs={}, optionals={}, initial_asking=True):34result = {}35if initial_asking:36for name, conf in confs.items():37result[name] = askdata(conf)38actions.append('batchset')39names = list(actions)40names.extend(optionals.keys())41names.extend(confs.keys())42base_question = Data('Choose what to do: ',43default=actions[0], values=names)44batchset_question = Data('Batch sets: ')45while True:46response = askdata(base_question)47if response == '':48response = base_question.default49elif response == 'batchset':50sets = askdata(batchset_question)51for key, value in _parse_batchset(sets).items():52if key.endswith(':'):53key = key[:-1]54if key in names:55conf = confs.get(key, optionals.get(key))56result[key] = value57elif response in actions:58break59else:60if response in confs:61conf = confs[response]62else:63conf = optionals[response]64oldvalue = result.get(response, None)65result[response] = askdata(conf, starting=oldvalue)66decoded = {}67all_confs = dict(confs)68all_confs.update(optionals)69for key in all_confs:70conf = all_confs.get(key)71if key in result:72decoded[key] = conf.decode(result[key])73else:74decoded[key] = conf.decode(conf.default)75return response, decoded767778def _parse_batchset(sets):79result = []80multiline = False81for line in sets.splitlines(True):82if line[0].isspace():83if multiline:84result[-1][1] += line[1:]85else:86if not line.strip():87continue88multiline= False89tokens = line.split(None, 1)90value = ''91if len(tokens) > 1:92result.append([tokens[0], tokens[1].rstrip('\r\n')])93else:94multiline = True95result.append([tokens[0], ''])96return dict(result)979899