def get_dict_shell_input(dat, type_dict=None, tabstop='', prompt=None): if prompt: print('----------\n%s\n----------' % prompt) output = deepcopy(dat) if type_dict is None: type_dict = make_type_dict(dat) for k, v in dat.items(): if isinstance(v, Mapping) and v != {}: print('%s%s\n%s----------' % (tabstop, k, tabstop)) tmp = get_dict_shell_input(v, type_dict[k], tabstop+' ') output[k] = deepcopy(tmp) continue prompt = '%s%s (%s) ' % (tabstop, k, type_dict[k].__name__) if type_dict[k] is list: prompt += '(comma-separated)' prompt += ' : ' if any([isinstance(v, x) for x in [str, list, int, float]]): default_val = str(v).replace('[','').replace(']','') tmp = pt_prompt(prompt, default=default_val) else: tmp = pt_prompt(prompt) if tmp == 'abort': return None output[k] = convert_str_to_type(tmp, type_dict[k]) return output
def __call__(self, message): cmd = pt_prompt( message, get_bottom_toolbar_tokens=self.get_bottom_toolbar_tokens, patch_stdout=True ) return cmd.split() events.fire('cmd:%s' % cmd[0], cmd)
def get_user_input(msg, default=None, shell=False): '''Get single input from user. Parameters ---------- msg : str, prompt default : str (optional) value returned if user enters nothing. default is None shell : bool (optional) True for CLI, False (default) for GUI ''' if 'SSH_CONNECTION' in os.environ: shell = True if shell: original = sys.stdout sys.stdout = sys.__stdout__ try: prompt = msg + ' ' #if default is not None: # prompt += '(default=%s) ' % default if default is None: default = '' prompt += ' : ' out = pt_prompt(prompt, default=default) if out == '': out = default except EOFError: out = None sys.stdout = original return out else: out = eg.enterbox(msg, default=default) return out
def get_files(prompt='', root=None, filetypes=None, multi=False, shell=False): '''Queries user for file or multiple files Parameters ---------- prompt : str root : str, where to start file chooser multi : bool, whether to query for multiple directories filetypes : list of str, list of acceptable file suffixes shell : bool True for command-line interface, False (default) for GUI forced True if ssh Returns ------- str or list of str ''' if 'SSH_CONNECTION' in os.environ: shell=True if filetypes is not None: file_filter = lambda x: (any([x.endswith(y) for y in filetypes]) or os.path.isdir(x)) else: file_filter = None path_completer = PathCompleter(file_filter=file_filter) history = InMemoryHistory() go = True out = [] if shell: print(prompt) if multi: print('Leave blank or ctrl-c to terminate collection') print('----------') while go: if shell: if root is None: root = '' try: tmp = pt_prompt(' >> ', completer=path_completer, history=history, auto_suggest=AutoSuggestFromHistory(), default=root) except KeyboardInterrupt: break else: tmp = eg.fileopenbox(prompt + ' (cancel to stop collection)', default=root, multiple=multi, filetypes=filetypes) if tmp == '' or tmp is None: go = False elif isinstance(tmp, list) and all([os.path.isfile(x) for x in tmp]): root = os.path.dirname(tmp[0]) out.extend(tmp) elif os.path.isfile(tmp): root = os.path.dirname(tmp) out.append(tmp) else: print('Must enter a valid path to file') if not multi: out = out[0] break if out == [] or out == '': out = None return out
def get_filedirs(prompt='', root=None, multi=False, shell=False): '''Queries user for directory or multiple directories Parameters ---------- prompt : str root : str, where to start file chooser multi : bool, whether to query for multiple directories shell : bool True for command-line interface, False (default) for GUI forced True if ssh Returns ------- str or list of str ''' if 'SSH_CONNECTION' in os.environ: shell=True path_completer = PathCompleter(only_directories=True) history = InMemoryHistory() go = True out = [] if shell: print(prompt) if multi: print('Leave blank or ctrl-c to terminate collection') print('----------') while go: if shell: if root is None: root = '' try: tmp = pt_prompt(' >> ', completer=path_completer, history=history, auto_suggest=AutoSuggestFromHistory(), default=root) except KeyboardInterrupt: break else: tmp = eg.diropenbox(prompt + ' (cancel to stop collection)', default=root) if tmp == '' or tmp is None: go = False elif os.path.isdir(tmp): root = os.path.dirname(tmp) out.append(tmp) else: print('Must enter a valid path to a directory') if not multi: out = out[0] break if out == [] or out == '': out = None return out