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 openMedia(path, prefDict=None): """open a media or text file, file type is determined by extension will assume that path is to a media file, rather than an exe for executable scripts, use launch if prefDict is provided, will get app path from there keys in this disc must start w/ media type; image, audio, midi, text, ps TODO: this should be updated to use application preferences stored in prefTools.py, not here """ path = drawer.pathScrub(path) dir, name = os.path.split(path) name, ext = extSplit(name) # this should be as method of a the pref tools # pref object/dict, or a function in this module or drawer if ext in imageEXT: fileType = 'image' elif ext in audioEXT: fileType = 'audio' elif ext in videoEXT: fileType = 'video' elif ext in midiEXT: fileType = 'midi' elif ext in textEXT or ext in codeEXT: fileType = 'text' elif ext in psEXT: fileType = 'ps' else: fileType = None app = None # default nothing if prefDict == None and fileType != None: # get default apps # if os.name == 'mac': pass # rely on system if os.name == 'posix': if drawer.isDarwin(): # assume sys default for most if fileType in ['audio', 'midi', 'video']: app = '/Applications/QuickTime Player.app' else: # other unix if fileType == 'text': app = 'more' elif fileType == 'audio': app = 'xmms' #'play' should work too elif fileType == 'midi': app = 'playmidi' # elif fileType == 'image': app = 'imagemagick' elif fileType == 'ps': app = 'gs' else: # win or other pass # rely on system elif fileType != None: # get from prefDict for key in prefDict.keys(): # match by filetype string leading key and path string if key.startswith(fileType) and 'path' in key.lower(): app = prefDict[key] # may be a complete file path, or a name if app == '': app = None # if given as empty, convert to none elif not drawer.isApp(app): app = None # if not an app any more, drop # if os.name == 'mac': # os9 # cmdExe = '%s' % path # on mac, just launch txt file, rely on pref if os.name == 'posix': if drawer.isDarwin(): if app == None: # no app use system default cmdExe = 'open "%s"' % path elif app.lower().endswith('.app'): # if a .app type app cmdExe = 'open -a "%s" "%s"' % (app, path) else: # its a unix style command cmdExe = '%s %s' % (app, path) else: # other unix if app == None: cmdExe = '%s' % (path) else: cmdExe = '%s %s' % (app, path) else: # win or other, rely on system settings cmdExe = '%s' % path # on win, just launch path return launch(cmdExe) # returns None on success
def openMedia(path, prefDict=None): """open a media or text file, file type is determined by extension will assume that path is to a media file, rather than an exe for executable scripts, use launch if prefDict is provided, will get app path from there keys in this disc must start w/ media type; image, audio, midi, text, ps TODO: this should be updated to use application preferences stored in prefTools.py, not here """ path = drawer.pathScrub(path) dir, name = os.path.split(path) name, ext = extSplit(name) # this should be as method of a the pref tools # pref object/dict, or a function in this module or drawer if ext in imageEXT: fileType = 'image' elif ext in audioEXT: fileType = 'audio' elif ext in videoEXT: fileType = 'video' elif ext in midiEXT: fileType = 'midi' elif ext in textEXT or ext in codeEXT: fileType = 'text' elif ext in psEXT: fileType = 'ps' else: fileType = None app = None # default nothing if prefDict == None and fileType != None: # get default apps # if os.name == 'mac': pass # rely on system if os.name == 'posix': if drawer.isDarwin(): # assume sys default for most if fileType in ['audio', 'midi', 'video']: app = '/Applications/QuickTime Player.app' else: # other unix if fileType == 'text' : app = 'more' elif fileType == 'audio': app = 'xmms' #'play' should work too elif fileType == 'midi' : app = 'playmidi' # elif fileType == 'image': app = 'imagemagick' elif fileType == 'ps': app = 'gs' else: # win or other pass # rely on system elif fileType != None: # get from prefDict for key in prefDict.keys(): # match by filetype string leading key and path string if key.startswith(fileType) and 'path' in key.lower(): app = prefDict[key] # may be a complete file path, or a name if app == '': app = None # if given as empty, convert to none elif not drawer.isApp(app): app = None # if not an app any more, drop # if os.name == 'mac': # os9 # cmdExe = '%s' % path # on mac, just launch txt file, rely on pref if os.name == 'posix': if drawer.isDarwin(): if app == None: # no app use system default cmdExe = 'open "%s"' % path elif app.lower().endswith('.app'): # if a .app type app cmdExe = 'open -a "%s" "%s"' % (app, path) else: # its a unix style command cmdExe = '%s %s' % (app, path) else: # other unix if app == None: cmdExe = '%s' % (path) else: cmdExe = '%s %s' % (app, path) else: # win or other, rely on system settings cmdExe = '%s' % path # on win, just launch path return launch(cmdExe) # returns None on success