def ask(self, question, default, choices=[], path_info=[]): """ ask a question """ assert type(path_info) == list if not self.force: # add choice info to the question if choices + path_info: question += ' [' for data in choices[:9] + path_info: if default == data: question += "\033[%dm%s\033[0m" % (4, data) else: question += "%s" % data question += ', ' if len(choices) > 9: question += '... , ' question = question[:-2]+']' if path_info: fct = lambda q: cmd.raw_path_input(q, allow_arg=choices, default=default) else: fct = lambda q: cmd.smart_input(q, allow_arg=choices, default=default) try: out = misc.timed_input(question, default, timeout=self.timeout, noerror=False, fct=fct) except misc.TimeOutError: # avoid to always wait a given time for the next answer self.force = True else: self.timeout=None # answer at least one question so wait... return out else: return str(default)
def treat_input_file(self, filename, default=None, msg=''): """ask to edit a file""" if msg == '' and filename == 'param_card.dat': msg = \ """WARNING: If you edit this file don\'t forget to modify consistently the different parameters, especially the width of all particles.""" fct = lambda q: cmd.raw_path_input(q, allow_arg=['y','n']) if not self.force: if msg: print msg question = 'Do you want to edit file: %(card)s?' % {'card':filename} choices = ['y', 'n'] path_info = ['path of the new %(card)s' % {'card':os.path.basename(filename)}] ans = self.ask(question, default, choices, path_info) else: ans = default if ans == 'y': path = os.path.join(self.card_dir, filename) self.edit_file(path) elif ans == 'n': return else: path = os.path.join(self.card_dir, filename) files.cp(ans, path)