
global lastreaddir,lastsavedir
import os
from Tkinter import *
import tkSimpleDialog
import tkFileDialogDir
import Pmw
from string import *

#check for directory prefs file in working path
def loaddefdirprefs():
    global lastreaddir,lastsavedir
    try:
        #open file
        fid=open(filepath+'defdir.spk',"r")
    except:
        return
    #file structure: single path    
    path=fid.readline()
    fid.close()
    lastreaddir=path
    lastsavedir=path
    
class changedefdirs:
    global lastreaddir,lastsavedir

    def __init__(self,master):
        global lastreaddir,lastsavedir
        self.newdir=''
        self.dialog=Pmw.PromptDialog(master,title='Default Directory',label_text='New Default Directory? ',
                                     entryfield_labelpos='n',buttons=('OK','Browse','Cancel'),command=self.click,
                                     entry_width=50)
        self.dialog.clear()
        self.dialog.insertentry(0,lastreaddir)
        self.dialog.withdraw()
        self.dialog.activate()

    def click(self,result):
        global lastreaddir,lastsavedir
        if result is None or result=='Cancel':
            self.dialog.deactivate()
        if result=='OK':
            #reset dirfile
            self.newdir=self.dialog.get()
            self.dialog.deactivate()        
            fid=open(filepath+'defdir.spk',"w")
            fid.write(self.newdir)
            fid.close()            
        if result=='Browse':
            ndir=tkFileDialogDir.choose_directory(initialdir=self.dialog.get())
            if ndir!='':
                self.dialog.clear()
                self.dialog.insertentry(0,ndir)             


#directory info startup
if os.name=='nt':os.sep='/'
filepath=os.getcwd()+os.sep

lastreaddir=filepath
lastsavedir=filepath
loaddefdirprefs()


    