PageRenderTime 26ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/idlelib/FileList.py

http://unladen-swallow.googlecode.com/
Python | 124 lines | 107 code | 14 blank | 3 comment | 26 complexity | 20bdb24b30f5e8013de1e0af5529a19e MD5 | raw file
  1import os
  2from Tkinter import *
  3import tkMessageBox
  4
  5
  6class FileList:
  7
  8    from EditorWindow import EditorWindow  # class variable, may be overridden
  9                                           # e.g. by PyShellFileList
 10
 11    def __init__(self, root):
 12        self.root = root
 13        self.dict = {}
 14        self.inversedict = {}
 15        self.vars = {} # For EditorWindow.getrawvar (shared Tcl variables)
 16
 17    def open(self, filename, action=None):
 18        assert filename
 19        filename = self.canonize(filename)
 20        if os.path.isdir(filename):
 21            # This can happen when bad filename is passed on command line:
 22            tkMessageBox.showerror(
 23                "File Error",
 24                "%r is a directory." % (filename,),
 25                master=self.root)
 26            return None
 27        key = os.path.normcase(filename)
 28        if self.dict.has_key(key):
 29            edit = self.dict[key]
 30            edit.top.wakeup()
 31            return edit
 32        if action:
 33            # Don't create window, perform 'action', e.g. open in same window
 34            return action(filename)
 35        else:
 36            return self.EditorWindow(self, filename, key)
 37
 38    def gotofileline(self, filename, lineno=None):
 39        edit = self.open(filename)
 40        if edit is not None and lineno is not None:
 41            edit.gotoline(lineno)
 42
 43    def new(self, filename=None):
 44        return self.EditorWindow(self, filename)
 45
 46    def close_all_callback(self, event):
 47        for edit in self.inversedict.keys():
 48            reply = edit.close()
 49            if reply == "cancel":
 50                break
 51        return "break"
 52
 53    def unregister_maybe_terminate(self, edit):
 54        try:
 55            key = self.inversedict[edit]
 56        except KeyError:
 57            print "Don't know this EditorWindow object.  (close)"
 58            return
 59        if key:
 60            del self.dict[key]
 61        del self.inversedict[edit]
 62        if not self.inversedict:
 63            self.root.quit()
 64
 65    def filename_changed_edit(self, edit):
 66        edit.saved_change_hook()
 67        try:
 68            key = self.inversedict[edit]
 69        except KeyError:
 70            print "Don't know this EditorWindow object.  (rename)"
 71            return
 72        filename = edit.io.filename
 73        if not filename:
 74            if key:
 75                del self.dict[key]
 76            self.inversedict[edit] = None
 77            return
 78        filename = self.canonize(filename)
 79        newkey = os.path.normcase(filename)
 80        if newkey == key:
 81            return
 82        if self.dict.has_key(newkey):
 83            conflict = self.dict[newkey]
 84            self.inversedict[conflict] = None
 85            tkMessageBox.showerror(
 86                "Name Conflict",
 87                "You now have multiple edit windows open for %r" % (filename,),
 88                master=self.root)
 89        self.dict[newkey] = edit
 90        self.inversedict[edit] = newkey
 91        if key:
 92            try:
 93                del self.dict[key]
 94            except KeyError:
 95                pass
 96
 97    def canonize(self, filename):
 98        if not os.path.isabs(filename):
 99            try:
100                pwd = os.getcwd()
101            except os.error:
102                pass
103            else:
104                filename = os.path.join(pwd, filename)
105        return os.path.normpath(filename)
106
107
108def _test():
109    from EditorWindow import fixwordbreaks
110    import sys
111    root = Tk()
112    fixwordbreaks(root)
113    root.withdraw()
114    flist = FileList(root)
115    if sys.argv[1:]:
116        for filename in sys.argv[1:]:
117            flist.open(filename)
118    else:
119        flist.new()
120    if flist.inversedict:
121        root.mainloop()
122
123if __name__ == '__main__':
124    _test()