"""edit.py This module defines the standard 'edit' command interface. edit(foo) launches the most appropriate editor on 'foo', if any are available. Editors are defined in modules named editXXX.py, where 'XXX' can be anything, and located in the same folder as edit.py.""" import string import mac _editmodules = {} def loadeditor(name): "Load the edit module given by name into memory." name = string.split(name,'.')[0] # strip off ".py" if present exec('import %s' % name) mod = eval(name) if hasattr(mod,"canedit") and hasattr(mod,"edit"): _editmodules[name] = eval(name) def loadeditors(): "Search for edit modules and load them into memory." path = __file__[:string.rfind(__file__,':')] editmods = filter(lambda x:x[:4]=="edit" and x[-3:]==".py" and x!="edit.py", mac.listdir(path)) for name in editmods: loadeditor(name) def pickeditor(data): "Pass data to each loaded editor, and return the one which reports highest confidence." if not _editmodules: loadeditors() maxconf = 0 maxmod = None for mod in _editmodules.values(): confidence = mod.canedit(data) if confidence > maxconf: maxconf = confidence maxmod = mod return maxmod def edit(data): "Pick an editor, and launch that editor with the given data; return None." mod = pickeditor(data) if not mod: print "No editor is available for this datatype (%s)." % type(data) return mod.edit(data, returndata=0) def editreturn(data): "Pick an editor, and launch that editor with the given data; return edited data." mod = pickeditor(data) if not mod: print "No editor is available for this datatype (%s)." % type(data) return return mod.edit(data, returndata=1) loadeditors()