def _textPutFile(prompt='name this file:', defaultName='name', defaultDir='', extension='*', dlgVisualMethod='text', termObj=None): """text based ui for getting a file to save""" finalPath = '' status = -2 # init value # check if dir has been cleared, recheck if defaultDir != '': # used as default dir selection directory = defaultDir elif drawer.getcwd() != None: directory = drawer.getcwd() elif drawer.getud() != None: directory = drawer.getud() else: directory = '' directory = drawer.pathScrub(directory) while 1: usrStr = askStr(prompt, termObj) # get file name if usrStr == None: return None, -1 # error # check for bad chars in fileName if usrStr[0] not in lang.IDENTCHARS: msgOut(lang.msgDlgBadFileNameStart % usrStr[0], termObj) return None, -1 # error for char in usrStr: if char not in lang.FILECHARS: msgOut(lang.msgDlgBadFileNameChar % char, termObj) return None, -1 # get directory status = askYesNoCancel((lang.msgDlgSaveInThisDir % directory), termObj) if status == -1: #cancel break if status == 0: #no, get new directory path, ok = promptGetDir('', directory, dlgVisualMethod, termObj) if ok != 1: status = -1 break else: directory = path status = 1 if status == 1: # test for existing file dirContent = os.listdir(directory) if usrStr in dirContent: ok = askYesNoCancel(lang.msgDlgFileExists, termObj) if ok != 1: continue finalPath = os.path.join(directory, usrStr) finalPath = drawer.pathScrub(finalPath) ok = askYesNoCancel((lang.msgDlgSaveThisFile % finalPath), termObj) if ok == -1: status = -1 break elif ok == 0: continue elif ok == 1: status = 1 break return finalPath, status
def promptGetFile(prompt='select a file', defaultDir='', mode='file', dlgVisualMethod=None, termObj=None): """multi platform/gui methods for selecting file to open text version allowes user to browse file system mode can be either filr or app, to allow application selection this is only explicitly necessary in text-based file selection """ if dlgVisualMethod == None: # test for vis method if not provided dlgVisualMethod = autoDlgMethod() if defaultDir != '': pass # used as default dir selection elif drawer.getcwd() != None: defaultDir = drawer.getcwd() elif drawer.getud() != None: defaultDir = drawer.getud() else: defaultDir = '' defaultDir = drawer.pathScrub(defaultDir) path = None # empty until defined # platform specific file dialogs. if dlgVisualMethod == 'mac': try: path, stat = _macGetFile(prompt, defaultDir) return path, stat except: print(lang.msgDlgMacError) # platform specific file dialogs. if dlgVisualMethod[:2] == 'tk': try: import tkinter import tkinter.filedialog TK = 1 except ImportError: TK = 0 print(lang.msgDlgTkError) if dlgVisualMethod[:2] == 'tk' and TK: tkTemp = tkinter.Tk() tkTemp.withdraw() options = { 'filetypes': [("all files", "*")], 'title': prompt, 'parent': tkTemp } # need to check if defaultDir still exists if os.path.isdir(defaultDir): options['initialdir'] = defaultDir guiTemp = tkinter.filedialog.Open() guiTemp.options = options # filename = apply(tkFileDialog.Open, (), options).show( try: path = guiTemp.show() # clean up gui mess del guiTemp tkTemp.destroy() except: pass # tk broke somehow if path not in ['', None] and drawer.isStr(path): path = drawer.pathScrub(path) return path, 1 # may be problem here with a keyboard interupt exception #elif path == None: # if not set yet, something went wrong # pass # fall below to text based else: return '', 0 # failure # for all other platforms, dlgVisualMethod == text try: msg, ok = _textGetFile(prompt, defaultDir, mode, dlgVisualMethod, termObj) except (OSError, IOError): # catch file errors, or dirs called tt dont exist msg = '' # this is usually the path itself ok = 0 return msg, ok
def _textGetFile(prompt='select a file', defaultDir='', mode='file', dlgVisualMethod='text', termObj=None): """text based ui for user selection of files; can optionally be used to get applications; this is tricky as sometimes apps are folders, not files mode can either be file or app """ finalPath = '' if defaultDir != '': # used as default dir selection directory = defaultDir elif drawer.getcwd() != None: directory = drawer.getcwd() elif drawer.getud() != None: directory = drawer.getud() else: directory = '' directory = drawer.pathScrub(directory) while 1: header = '\n%s' % directory msgOut('%s\n' % prompt, termObj) usrStr = askStr(lang.msgMenuFile, termObj) if usrStr == None: return None, -1 # cancel # check first for a complete path elif os.path.isfile(drawer.pathScrub(usrStr)): status = 1 finalPath = drawer.pathScrub(usrStr) elif usrStr.lower() == 'cd': # change directory path, ok = promptGetDir('', directory, dlgVisualMethod, termObj) if not ok: # error status = -1 break # cancel entire session else: directory = path continue elif usrStr.lower() == 'c': # cancel selected return None, -1 # cancel else: if usrStr.lower() != 'f': msgOut(lang.msgConfusedInput, termObj) continue status = 0 # reinit status flag while 1: # file selection loop name = askStr(lang.msgDlgNameFile, termObj) if name == None: return None, -1 # will cancel # if an application, filter path before testing if mode == 'app': name = drawer.appPathFilter(name) # check first if this is an absolute file path, accept if so if ((os.path.isabs(name) and mode == 'file' and not os.path.isdir(name) and os.path.exists(name)) or (os.path.isabs(name) and mode == 'app' and drawer.isApp(name) and os.path.exists(name))): finalPath = name break # else see if name is in dir; cant adjust name for apps here # must assume that they are displayed to e user in w/ extension elif name in os.listdir(directory): fp = os.path.join(directory, name) # check: if a dir or not app, fail if ((mode == 'file' and os.path.isdir(fp)) or (mode == 'app' and not drawer.isApp(fp))): if mode == 'file': errorMsg = lang.msgDlgDirNotFile elif mode == 'app': errorMsg = lang.msgDlgNotApp ok = askYesNo(errorMsg, 1, termObj) if ok: continue elif not ok: status = -2 break finalPath = fp break else: # not in this directory ok = askYesNoCancel(lang.msgDlgBadFileName, termObj) if ok: continue elif not ok: status = -2 # wil continue break else: break if status == -2: continue # final check if finalPath == '': status = -1 break else: # final check query = lang.msgDlgSelectThisFile % finalPath ok = askYesNoCancel(query, termObj) if ok == -1: # cancel selection status = -1 break elif ok == 0: continue # no, retry elif ok: status = 1 break finalPath = drawer.pathScrub(finalPath) return finalPath, status
def promptGetDir(prompt='select a directory:', sampleDir='', dlgVisualMethod=None, termObj=None): """multi platform/gui methods for optaining a directory text based version displays dir listings """ if dlgVisualMethod == None: # test for vis method if not provided dlgVisualMethod = autoDlgMethod() # check for bad directories if sampleDir != '': pass # used as default dir selection elif drawer.getcwd() != None: sampleDir = drawer.getcwd() elif drawer.getud() != None: sampleDir = drawer.getud() else: sampleDir = '' sampleDir = drawer.pathScrub(sampleDir) pathToFile = None # empty until defined # platform specific file dialogs. if dlgVisualMethod == 'mac': try: path, stat = _macGetDirectory(prompt) return path, stat except: print(lang.msgDlgMacError) # platform specific file dialogs. if dlgVisualMethod[:2] == 'tk': try: import tkinter import tkinter.filedialog TK = 1 except ImportError: TK = 0 print(lang.msgDlgTkError) if dlgVisualMethod[:2] == 'tk' and TK == 1: tkTemp = tkinter.Tk() tkTemp.withdraw() ## "dir" only shows directories, but are unable to select options = { 'filetypes': [("directory", "*")], 'title': prompt, 'parent': tkTemp } # need to check if defaultDir still exists if os.path.isdir(sampleDir): options['initialdir'] = sampleDir guiTemp = tkinter.filedialog.Open() guiTemp.options = options # filename = apply(tkFileDialog.Open, (), options).show( try: pathToFile = guiTemp.show() del guiTemp # clean up gui mess tkTemp.destroy() except: # tk broke somehow pass # return path if pathToFile not in ['', None] and drawer.isStr(pathToFile): pathToFile = os.path.dirname(pathToFile) # remove file name pathToFile = drawer.pathScrub(pathToFile) return pathToFile, 1 elif pathToFile == None: # if not set yet, something went wrong pass # fall below to text based else: return '', 0 # for all other platforms, dlgVisualMethod == text try: msg, ok = _textGetDirectory(prompt, sampleDir, termObj) except (OSError, IOError): # catch file errors, or dirs called tt dont exist msg = '' # this is usually the path itself ok = 0 return msg, ok
def _textGetDirectory(prompt='select a directory: ', sampleDir='', termObj=None): """this method allows the user to select a directory using a text based interface. """ if sampleDir != '': currentDir = sampleDir elif drawer.getcwd() != None: currentDir = drawer.getcwd() elif drawer.getud() != None: currentDir = drawer.getud() else: currentDir = '' currentDir = drawer.pathScrub(currentDir) while 1: cwdContents = os.listdir(currentDir) maxLength = 0 for fName in cwdContents: if len(fName) >= maxLength: maxLength = len(fName) maxLength = maxLength + 2 # add two spaces of buffer header = '%s' % currentDir #delete extra space here if termObj == None: # update termwidth termWidth = 70 # default else: h, termWidth = termObj.size() msg = typeset.formatEqCol(header, cwdContents, maxLength, termWidth) msgOut((msg + prompt), termObj) # dont want extra return usrStr = askStr(lang.msgMenuChangeDir, termObj) finalPath = '' # keep empty until cmds parsed # scan for each of the options if usrStr == None: continue # bad input if usrStr.lower() == 's': # if entry is s, return cwd finalPath = currentDir # selected, asuign to final if usrStr == '..': # up one dir, display pathValid = os.path.exists(currentDir) currentDir = os.path.dirname(currentDir) continue if finalPath == '': # still not asigned, check if its a path downDir = os.path.join(currentDir, usrStr) downDir = drawer.pathScrub(downDir) # assume this is a dir cwdContents = os.listdir(currentDir) if usrStr in cwdContents: if os.path.isdir(downDir) != 1: # if not a dir continue else: # is a dir currentDir = downDir # make downDir current, back to top continue if finalPath == '': #user has entered a abs path absPath = drawer.pathScrub(usrStr) ### resolve aliases here! if os.path.exists(absPath) and os.path.isdir(absPath): currentDir = absPath # this is a dir, same as selecting a dir continue if finalPath == '': # not assigned yet if usrStr.lower() == 'c': return '', 0 # canceled else: # bad input, continue msgOut((lang.msgDlgBadInput % usrStr), termObj) continue if not os.path.exists(finalPath): # if doesnt exist status = askYesNo(lang.msgDlgBadPath, 1, termObj) if status != 1: return '', 0 # cancle msg else: continue else: # its good finalPath = drawer.pathScrub(finalPath) return finalPath, 1
def promptGetFile(prompt='select a file', defaultDir='', mode='file', dlgVisualMethod=None, termObj=None): """multi platform/gui methods for selecting file to open text version allowes user to browse file system mode can be either filr or app, to allow application selection this is only explicitly necessary in text-based file selection """ if dlgVisualMethod == None: # test for vis method if not provided dlgVisualMethod = autoDlgMethod() if defaultDir != '': pass # used as default dir selection elif drawer.getcwd() != None: defaultDir = drawer.getcwd() elif drawer.getud() != None: defaultDir = drawer.getud() else: defaultDir = '' defaultDir = drawer.pathScrub(defaultDir) path = None # empty until defined # platform specific file dialogs. if dlgVisualMethod == 'mac': try: path, stat = _macGetFile(prompt, defaultDir) return path, stat except: print lang.msgDlgMacError # platform specific file dialogs. if dlgVisualMethod[:2] == 'tk': try: import Tkinter import tkFileDialog TK = 1 except ImportError: TK = 0 print lang.msgDlgTkError if dlgVisualMethod[:2] == 'tk' and TK: tkTemp = Tkinter.Tk() tkTemp.withdraw() options = {'filetypes':[("all files", "*")], 'title' : prompt, 'parent' : tkTemp} # need to check if defaultDir still exists if os.path.isdir(defaultDir): options['initialdir'] = defaultDir guiTemp = tkFileDialog.Open() guiTemp.options = options # filename = apply(tkFileDialog.Open, (), options).show( try: path = guiTemp.show() # clean up gui mess del guiTemp tkTemp.destroy() except: pass # tk broke somehow if path not in ['', None] and drawer.isStr(path): path = drawer.pathScrub(path) return path, 1 # may be problem here with a keyboard interupt exception #elif path == None: # if not set yet, something went wrong # pass # fall below to text based else: return '', 0 # failure # for all other platforms, dlgVisualMethod == text try: msg, ok = _textGetFile(prompt, defaultDir, mode, dlgVisualMethod, termObj) except (OSError, IOError): # catch file errors, or dirs called tt dont exist msg = '' # this is usually the path itself ok = 0 return msg, ok
def promptGetDir(prompt='select a directory:', sampleDir='', dlgVisualMethod=None, termObj=None): """multi platform/gui methods for optaining a directory text based version displays dir listings """ if dlgVisualMethod == None: # test for vis method if not provided dlgVisualMethod = autoDlgMethod() # check for bad directories if sampleDir != '': pass # used as default dir selection elif drawer.getcwd() != None: sampleDir = drawer.getcwd() elif drawer.getud() != None: sampleDir = drawer.getud() else: sampleDir = '' sampleDir = drawer.pathScrub(sampleDir) pathToFile = None # empty until defined # platform specific file dialogs. if dlgVisualMethod == 'mac': try: path, stat = _macGetDirectory(prompt) return path, stat except: print lang.msgDlgMacError # platform specific file dialogs. if dlgVisualMethod[:2] == 'tk': try: import Tkinter import tkFileDialog TK = 1 except ImportError: TK = 0 print lang.msgDlgTkError if dlgVisualMethod[:2] == 'tk' and TK == 1: tkTemp = Tkinter.Tk() tkTemp.withdraw() ## "dir" only shows directories, but are unable to select options = {'filetypes':[("directory", "*")], 'title' : prompt, 'parent' : tkTemp} # need to check if defaultDir still exists if os.path.isdir(sampleDir): options['initialdir'] = sampleDir guiTemp = tkFileDialog.Open() guiTemp.options = options # filename = apply(tkFileDialog.Open, (), options).show( try: pathToFile = guiTemp.show() del guiTemp # clean up gui mess tkTemp.destroy() except: # tk broke somehow pass # return path if pathToFile not in ['', None] and drawer.isStr(pathToFile): pathToFile = os.path.dirname(pathToFile) # remove file name pathToFile = drawer.pathScrub(pathToFile) return pathToFile, 1 elif pathToFile == None: # if not set yet, something went wrong pass # fall below to text based else: return '', 0 # for all other platforms, dlgVisualMethod == text try: msg, ok = _textGetDirectory(prompt, sampleDir, termObj) except (OSError, IOError): # catch file errors, or dirs called tt dont exist msg = '' # this is usually the path itself ok = 0 return msg, ok