#!/usr/bin/python
#   ReadDB.py

"""
Program to read GanttPV db and list it. 
"""
# 080701  DJ Webre, Jr.  First Version

import wx, os
import cPickle, pprint

frameTitle = 'Read GanttPV db'

class MessageDialog(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
        str =  'Read GanttPV db and list'
        self.str = str
        self.control.WriteText(str)			    # Writes text to frame
        self.Show(True)
        self.dirname = os.getcwd()
        self.OnFileOpen(self)
  
    def OnFileOpen(self, evt):                  # Open
        dlg = wx.FileDialog(self, "Select File", self.dirname, "", "*.*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename=dlg.GetFilename()
            self.dirname=dlg.GetDirectory()
        if self.filename == '': return
        f = open(self.dirname+'\\'+self.filename,'rb')
        self.control.WriteText(str(self.filename) + '\n' + '\n')

        done = 0                                # Read records one at a time
        while not done:
            line = f.readline()           
            if line != "":
                data = cPickle.load(f)          # unPickle record
                data1 = pprint.pformat(data)
                pprint.pprint(data),
                self.control.WriteText(data1)   # Print (write) text to frame
            else:
                done = 1          

        f.close()  
        dlg.Destroy()
        self.SetTitle(frameTitle+': '+self.filename)

    
myApp = wx.App()
MessageDialog(None, -1, 'Message Dialog')
myApp.MainLoop()