| 1 |
ninoborges |
8 |
#! /usr/local/bin/python
|
| 2 |
|
|
|
| 3 |
|
|
from Tkinter import *
|
| 4 |
|
|
import os
|
| 5 |
|
|
import string
|
| 6 |
|
|
import sys
|
| 7 |
|
|
|
| 8 |
|
|
def normalize_pathname(path):
|
| 9 |
|
|
"""Given PATHNAME, return an absolute pathname relative to cwd, reducing
|
| 10 |
|
|
out all current-dir (unix: '.') and parent-dir (unix: '..') references."""
|
| 11 |
|
|
|
| 12 |
|
|
outwards, inwards = 0, []
|
| 13 |
|
|
for nm in string.splitfields(path, os.sep):
|
| 14 |
|
|
if nm != os.curdir:
|
| 15 |
|
|
if nm == os.pardir:
|
| 16 |
|
|
# Translate parent-dir entries to outward notches:
|
| 17 |
|
|
if inwards:
|
| 18 |
|
|
# Pop a containing inwards:
|
| 19 |
|
|
del inwards[-1]
|
| 20 |
|
|
else:
|
| 21 |
|
|
# Register leading outward notches:
|
| 22 |
|
|
outwards = outwards + 1
|
| 23 |
|
|
else:
|
| 24 |
|
|
inwards.append(nm)
|
| 25 |
|
|
inwards = string.joinfields(inwards, os.sep)
|
| 26 |
|
|
|
| 27 |
|
|
if (not inwards) or (inwards[0] != os.sep):
|
| 28 |
|
|
# Relative path - join with current working directory, (ascending
|
| 29 |
|
|
# outwards to account for leading parent-dir components):
|
| 30 |
|
|
cwd = os.getcwd()
|
| 31 |
|
|
if outwards:
|
| 32 |
|
|
cwd = string.splitfields(cwd, os.sep)
|
| 33 |
|
|
cwd = string.joinfields(cwd[:len(cwd) - outwards], os.sep)
|
| 34 |
|
|
if inwards:
|
| 35 |
|
|
return os.path.join(cwd, inwards) # ==>
|
| 36 |
|
|
else:
|
| 37 |
|
|
return cwd # ==>
|
| 38 |
|
|
else:
|
| 39 |
|
|
return inwards # ==>
|
| 40 |
|
|
|
| 41 |
|
|
def newdialog(master, text):
|
| 42 |
|
|
"""Given a text, presents a dialog w/ a title containing that text, an entry,
|
| 43 |
|
|
an OK and cancel button. The OK button is the default.
|
| 44 |
|
|
|
| 45 |
|
|
Returns the text in the entry widget, or None if canceled."""
|
| 46 |
|
|
|
| 47 |
|
|
OKBUTTON = 0
|
| 48 |
|
|
CANCELBUTTON = 1
|
| 49 |
|
|
w = Toplevel( master)# class = text)
|
| 50 |
|
|
w.title( text )
|
| 51 |
|
|
w.iconname( text )
|
| 52 |
|
|
top = Frame( w, relief = GROOVE, bd = 1)
|
| 53 |
|
|
bot = Frame( w, relief = GROOVE, bd = 1)
|
| 54 |
|
|
msg = Message( top, width = '3i', text = text, font = '-Adobe-Times-Medium-R-Normal-*-180-*')
|
| 55 |
|
|
msg.pack( fill = X, padx = '2m', pady = '2m')
|
| 56 |
|
|
top.pack( expand = 1, side = TOP, fill = BOTH)
|
| 57 |
|
|
entry = Entry( top, relief = GROOVE)
|
| 58 |
|
|
entry.pack( pady = '2m', anchor = CENTER)
|
| 59 |
|
|
|
| 60 |
|
|
ok = Button( bot, relief = RIDGE, borderwidth = 3, text = 'OK', command = ('set', 'button', OKBUTTON))
|
| 61 |
|
|
ok.pack ( side = LEFT, expand = 1, padx = 0, pady = 0, ipady = 0, fill = X)
|
| 62 |
|
|
cancel = Button(bot, text = 'Cancel', relief = GROOVE, command = ('set', 'button', CANCELBUTTON))
|
| 63 |
|
|
cancel.pack (side = LEFT, expand = 1, padx = 0, pady = 0, ipady = 0, fill = X)
|
| 64 |
|
|
bot.pack(side = BOTTOM, fill = BOTH)
|
| 65 |
|
|
|
| 66 |
|
|
default =0
|
| 67 |
|
|
w.bind('<Return>',
|
| 68 |
|
|
lambda e, b=ok, i=OKBUTTON:
|
| 69 |
|
|
(b.flash(),
|
| 70 |
|
|
b.setvar('button', i)))
|
| 71 |
|
|
oldFocus = w.tk.call('focus') # XXX
|
| 72 |
|
|
w.grab_set()
|
| 73 |
|
|
w.focus()
|
| 74 |
|
|
entry.focus()
|
| 75 |
|
|
dirname = None
|
| 76 |
|
|
w.waitvar('button')
|
| 77 |
|
|
retu = w.getint(w.getvar('button'))
|
| 78 |
|
|
if retu == OKBUTTON:
|
| 79 |
|
|
dirname = entry.get()
|
| 80 |
|
|
try:
|
| 81 |
|
|
os.mkdir(dirname, 755 )
|
| 82 |
|
|
except os.error:
|
| 83 |
|
|
pass
|
| 84 |
|
|
w.destroy()
|
| 85 |
|
|
w.tk.call('focus', oldFocus) # XXX
|
| 86 |
|
|
return dirname
|
| 87 |
|
|
|
| 88 |
|
|
def fileselector(new, master, title, text = None, origdir = None):
|
| 89 |
|
|
"""Given a title, and a text, and optionally a starting directory (defaults to
|
| 90 |
|
|
current directory), presents a MODAL file selection dialog box which allows navigation
|
| 91 |
|
|
up and down the file structure. Allows the creation of new subdirectories and the
|
| 92 |
|
|
naming of a file which does not currently exist. Does not create such files.
|
| 93 |
|
|
|
| 94 |
|
|
Returns the full, normalized pathname to the selected file, or None if the dialog
|
| 95 |
|
|
is canceled"""
|
| 96 |
|
|
|
| 97 |
|
|
NEWBUTTON = 0
|
| 98 |
|
|
OPENBUTTON = 1
|
| 99 |
|
|
CANCELBUTTON = 2
|
| 100 |
|
|
UPBUTTON = 3
|
| 101 |
|
|
ORIGBUTTON = 4
|
| 102 |
|
|
NEWDIRBUTTON = 5
|
| 103 |
|
|
|
| 104 |
|
|
if origdir == None:
|
| 105 |
|
|
origdir = os.curdir
|
| 106 |
|
|
origdir = normalize_pathname(origdir)
|
| 107 |
|
|
|
| 108 |
|
|
# 1. Create the top-level window and divide it into top
|
| 109 |
|
|
# and bottom parts.
|
| 110 |
|
|
|
| 111 |
|
|
w = Toplevel(master) #, class = 'Dialog')
|
| 112 |
|
|
w.title(title)
|
| 113 |
|
|
w.iconname('Dialog')
|
| 114 |
|
|
|
| 115 |
|
|
if text:
|
| 116 |
|
|
top = Frame( w, relief = GROOVE, bd = 1)
|
| 117 |
|
|
msg = Message( top, width = '2.5i', text = text, font = '-Adobe-Times-Medium-R-Normal-*-180-*')
|
| 118 |
|
|
msg.pack( expand = 1, fill = X, padx = '2m', pady = '2m')
|
| 119 |
|
|
top.pack( side = TOP, fill = BOTH)
|
| 120 |
|
|
|
| 121 |
|
|
middle = Frame( w, relief = GROOVE, bd = 1)
|
| 122 |
|
|
middle.pack( fill = BOTH, expand = 1)
|
| 123 |
|
|
left = Frame( middle, bd = 1)
|
| 124 |
|
|
left.pack( side = LEFT, fill = BOTH, expand = 1)
|
| 125 |
|
|
right = Frame( middle, bd = 1)
|
| 126 |
|
|
right.pack( side = RIGHT, fill = BOTH, expand = 1)
|
| 127 |
|
|
label = Label( left, text = normalize_pathname(os.curdir))
|
| 128 |
|
|
label.pack( fill = X, anchor = W)
|
| 129 |
|
|
|
| 130 |
|
|
f = Frame( left, relief = SUNKEN, borderwidth = 1)
|
| 131 |
|
|
filelist = Listbox( f, highlightcolor = 'grey', selectmode = BROWSE, selectborderwidth = 0,
|
| 132 |
|
|
width = 25, relief = FLAT, height = 10, borderwidth = 0)
|
| 133 |
|
|
|
| 134 |
|
|
objectscroll = Scrollbar(left, highlightcolor = 'grey', orient = VERTICAL, width = 12, borderwidth = 1)
|
| 135 |
|
|
filelist['yscrollcommand'] = objectscroll.set
|
| 136 |
|
|
objectscroll['command'] = filelist.yview
|
| 137 |
|
|
filelist.pack( expand = 1, fill = BOTH)
|
| 138 |
|
|
f.pack({'side':'left','expand':1,'fill':'both', 'pady':2})
|
| 139 |
|
|
objectscroll.pack({'side':'right','fill':'both'})
|
| 140 |
|
|
|
| 141 |
|
|
up = Button(right, text = 'Up', relief = GROOVE, command = ('set','button',UPBUTTON))
|
| 142 |
|
|
up.pack(expand = 1, padx = 0, pady = 0, ipady = 0, fill = X)
|
| 143 |
|
|
orig = Button(right, text = 'Original Directory', relief = GROOVE, command = ('set','button',ORIGBUTTON))
|
| 144 |
|
|
orig.pack(expand = 1, padx = 0, pady = 0, ipady = 0, fill = X)
|
| 145 |
|
|
if new:
|
| 146 |
|
|
newdir = Button(right, text = 'New Directory...', relief = GROOVE,
|
| 147 |
|
|
command = ('set','button',NEWDIRBUTTON))
|
| 148 |
|
|
newdir.pack(expand = 1, padx = 0, pady = 0, ipady = 0, fill = X)
|
| 149 |
|
|
new = Button(right, text = 'New File...', relief = GROOVE,
|
| 150 |
|
|
command = ('set', 'button', NEWBUTTON))
|
| 151 |
|
|
new.pack (expand = 1, padx = 0, pady = 0, ipady = 0, fill = X)
|
| 152 |
|
|
open = Button(right, text = 'Open', borderwidth = 3, relief = RIDGE,
|
| 153 |
|
|
command = ('set', 'button', OPENBUTTON))
|
| 154 |
|
|
open.pack (expand = 1, padx = 0, pady = 0, ipady = 0, fill = X)
|
| 155 |
|
|
cancel = Button(right, text = 'Cancel', relief = GROOVE, command = ('set', 'button', CANCELBUTTON))
|
| 156 |
|
|
cancel.pack ( expand = 1, padx = 0, pady = 0, ipady = 0, fill = X)
|
| 157 |
|
|
# 4. Set up a binding for <Return>, if there's a default,
|
| 158 |
|
|
# set a grab, and claim the focus too.
|
| 159 |
|
|
|
| 160 |
|
|
default =1
|
| 161 |
|
|
w.bind('<Return>',
|
| 162 |
|
|
lambda e, b=open, i=OPENBUTTON:
|
| 163 |
|
|
(b.flash(),
|
| 164 |
|
|
b.setvar('button', i)))
|
| 165 |
|
|
filelist.bind('<Double-1>',
|
| 166 |
|
|
lambda e, b=open, i=OPENBUTTON:
|
| 167 |
|
|
(b.flash(),
|
| 168 |
|
|
b.setvar('button', i)))
|
| 169 |
|
|
filelist.bind('<Escape>',
|
| 170 |
|
|
lambda e, b=cancel, i = CANCELBUTTON:
|
| 171 |
|
|
(b.flash(),
|
| 172 |
|
|
b.setvar('button', i)))
|
| 173 |
|
|
filelist.bind('<Left>',
|
| 174 |
|
|
lambda e, b=up, i = UPBUTTON:
|
| 175 |
|
|
(b.flash(),
|
| 176 |
|
|
b.setvar('button', i)))
|
| 177 |
|
|
filelist.bind('<Right>',
|
| 178 |
|
|
lambda e, b=open, i = OPENBUTTON:
|
| 179 |
|
|
(b.flash(),
|
| 180 |
|
|
b.setvar('button', i)))
|
| 181 |
|
|
oldFocus = w.tk.call('focus') # XXX
|
| 182 |
|
|
w.grab_set()
|
| 183 |
|
|
filelist.focus()
|
| 184 |
|
|
|
| 185 |
|
|
# 5. Wait for the user to respond, then restore the focus
|
| 186 |
|
|
# and return the index of the selected button.
|
| 187 |
|
|
|
| 188 |
|
|
filename = None
|
| 189 |
|
|
retu = None
|
| 190 |
|
|
while (filename == None and retu != CANCELBUTTON):
|
| 191 |
|
|
filelist.delete(0,'end')
|
| 192 |
|
|
try:
|
| 193 |
|
|
l = os.listdir(os.curdir)
|
| 194 |
|
|
l.sort()
|
| 195 |
|
|
for file in l:
|
| 196 |
|
|
if file != os.curdir and file != os.pardir:
|
| 197 |
|
|
if (os.path.isdir(file)):
|
| 198 |
|
|
filelist.insert('end',file+os.sep)
|
| 199 |
|
|
else:
|
| 200 |
|
|
filelist.insert('end',file)
|
| 201 |
|
|
except os.error:
|
| 202 |
|
|
pass
|
| 203 |
|
|
dname = normalize_pathname(os.curdir)
|
| 204 |
|
|
if len(dname) > 25:
|
| 205 |
|
|
dname = dname[:10]+'...'+dname[-15:]
|
| 206 |
|
|
label['text'] = dname
|
| 207 |
|
|
|
| 208 |
|
|
filelist.select_set(0,0)
|
| 209 |
|
|
w.waitvar('button')
|
| 210 |
|
|
retu = w.getint(w.getvar('button'))
|
| 211 |
|
|
if retu == NEWBUTTON: # New File
|
| 212 |
|
|
filename = newdialog(master, 'Create file named:')
|
| 213 |
|
|
elif retu == UPBUTTON:
|
| 214 |
|
|
try:
|
| 215 |
|
|
os.chdir(os.pardir)
|
| 216 |
|
|
except:
|
| 217 |
|
|
pass
|
| 218 |
|
|
elif retu == ORIGBUTTON:
|
| 219 |
|
|
os.chdir(origdir)
|
| 220 |
|
|
elif retu == NEWDIRBUTTON:
|
| 221 |
|
|
dir = newdialog(master, 'Create directory named:')
|
| 222 |
|
|
if dir:
|
| 223 |
|
|
try:
|
| 224 |
|
|
os.chdir(dir)
|
| 225 |
|
|
except os.error:
|
| 226 |
|
|
pass
|
| 227 |
|
|
elif retu == OPENBUTTON:
|
| 228 |
|
|
selection = filelist.curselection()
|
| 229 |
|
|
if selection and len(selection) == 1:
|
| 230 |
|
|
f = filelist.get(selection[0])
|
| 231 |
|
|
try:
|
| 232 |
|
|
if (os.path.isdir(f)):
|
| 233 |
|
|
os.chdir(filelist.get(selection[0]))
|
| 234 |
|
|
elif new == 0:
|
| 235 |
|
|
filename = f
|
| 236 |
|
|
except os.error:
|
| 237 |
|
|
pass
|
| 238 |
|
|
|
| 239 |
|
|
if retu == CANCELBUTTON:
|
| 240 |
|
|
os.chdir(origdir)
|
| 241 |
|
|
|
| 242 |
|
|
w.destroy()
|
| 243 |
|
|
w.tk.call('focus', oldFocus) # XXX
|
| 244 |
|
|
if filename:
|
| 245 |
|
|
return normalize_pathname(filename)
|
| 246 |
|
|
else:
|
| 247 |
|
|
return None
|
| 248 |
|
|
|
| 249 |
|
|
def newfileselector(master, title, text = None, origdir = None):
|
| 250 |
|
|
return fileselector(1, master, title, text, origdir)
|
| 251 |
|
|
|
| 252 |
|
|
def oldfileselector(master, title, text = None, origdir = None):
|
| 253 |
|
|
return fileselector(0, master, title, text, origdir)
|
| 254 |
|
|
|
| 255 |
|
|
|
| 256 |
|
|
def go():
|
| 257 |
|
|
i = newfileselector(mainWidget, 'Subject Set Selector')
|
| 258 |
|
|
if i:
|
| 259 |
|
|
print i
|
| 260 |
|
|
|
| 261 |
|
|
def test():
|
| 262 |
|
|
import sys
|
| 263 |
|
|
global mainWidget
|
| 264 |
|
|
mainWidget = Frame()
|
| 265 |
|
|
Pack.config( mainWidget )
|
| 266 |
|
|
start = Button( mainWidget, text= 'Press Here To Start', command = go )
|
| 267 |
|
|
start.pack( fill = BOTH )
|
| 268 |
|
|
endit = Button( mainWidget, text = 'Exit', command = sys.exit )
|
| 269 |
|
|
endit.pack( fill = BOTH )
|
| 270 |
|
|
mainWidget.mainloop()
|
| 271 |
|
|
|
| 272 |
|
|
if __name__ == '__main__':
|
| 273 |
|
|
test()
|