0001 import os 0002 from Tkinter import * 0003 import tkMessageBox 0004 0005 0006 class FileList: 0007 0008 from EditorWindow import EditorWindow # class variable, may be overridden 0009 # e.g. by PyShellFileList 0010 0011 def __init__(self, root): 0012 self.root = root 0013 self.dict = {} 0014 self.inversedict = {} 0015 self.vars = {} # For EditorWindow.getrawvar (shared Tcl variables) 0016 0017 def open(self, filename, action=None): 0018 assert filename 0019 filename = self.canonize(filename) 0020 if os.path.isdir(filename): 0021 # This can happen when bad filename is passed on command line: 0022 tkMessageBox.showerror( 0023 "File Error", 0024 "%r is a directory." % (filename,), 0025 master=self.root) 0026 return None 0027 key = os.path.normcase(filename) 0028 if self.dict.has_key(key): 0029 edit = self.dict[key] 0030 edit.top.wakeup() 0031 return edit 0032 if action: 0033 # Don't create window, perform 'action', e.g. open in same window 0034 return action(filename) 0035 else: 0036 return self.EditorWindow(self, filename, key) 0037 0038 def gotofileline(self, filename, lineno=None): 0039 edit = self.open(filename) 0040 if edit is not None and lineno is not None: 0041 edit.gotoline(lineno) 0042 0043 def new(self, filename=None): 0044 return self.EditorWindow(self, filename) 0045 0046 def close_all_callback(self, event): 0047 for edit in self.inversedict.keys(): 0048 reply = edit.close() 0049 if reply == "cancel": 0050 break 0051 return "break" 0052 0053 def close_edit(self, edit): 0054 try: 0055 key = self.inversedict[edit] 0056 except KeyError: 0057 print "Don't know this EditorWindow object. (close)" 0058 return 0059 if key: 0060 del self.dict[key] 0061 del self.inversedict[edit] 0062 if not self.inversedict: 0063 self.root.quit() 0064 0065 def filename_changed_edit(self, edit): 0066 edit.saved_change_hook() 0067 try: 0068 key = self.inversedict[edit] 0069 except KeyError: 0070 print "Don't know this EditorWindow object. (rename)" 0071 return 0072 filename = edit.io.filename 0073 if not filename: 0074 if key: 0075 del self.dict[key] 0076 self.inversedict[edit] = None 0077 return 0078 filename = self.canonize(filename) 0079 newkey = os.path.normcase(filename) 0080 if newkey == key: 0081 return 0082 if self.dict.has_key(newkey): 0083 conflict = self.dict[newkey] 0084 self.inversedict[conflict] = None 0085 tkMessageBox.showerror( 0086 "Name Conflict", 0087 "You now have multiple edit windows open for %r" % (filename,), 0088 master=self.root) 0089 self.dict[newkey] = edit 0090 self.inversedict[edit] = newkey 0091 if key: 0092 try: 0093 del self.dict[key] 0094 except KeyError: 0095 pass 0096 0097 def canonize(self, filename): 0098 if not os.path.isabs(filename): 0099 try: 0100 pwd = os.getcwd() 0101 except os.error: 0102 pass 0103 else: 0104 filename = os.path.join(pwd, filename) 0105 return os.path.normpath(filename) 0106 0107 0108 def _test(): 0109 from EditorWindow import fixwordbreaks 0110 import sys 0111 root = Tk() 0112 fixwordbreaks(root) 0113 root.withdraw() 0114 flist = FileList(root) 0115 if sys.argv[1:]: 0116 for filename in sys.argv[1:]: 0117 flist.open(filename) 0118 else: 0119 flist.new() 0120 if flist.inversedict: 0121 root.mainloop() 0122 0123 if __name__ == '__main__': 0124 _test() 0125
Generated by PyXR 0.9.4