"""Subclass of ChooseFile, which is generated by wxFormBuilder."""


import os
import wx
import wxx
import Tool
import ast
import analytic.astd as astd


# Implementing ChooseFile
class ToolChooseFile(Tool.ChooseFile):
    """
    Use this dialog for finding the python file
    you want to explore in ast explorer.
    The ast explorer will show the python abstract
    syntax tree.
    Please pay attention to the fact that the ast
    structure may change, so you must take this
    in consideration, if you want to use ast for
    your own purposes.
    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent):
        """init dialog"""
        Tool.ChooseFile.__init__(self, parent)

    def Validate(self):
        """Test if the dialog is ok"""
        #get the selected file
        self._path = self.m_filePicker1.GetPath().strip()
        if len(self._path) == 0:
            wx.MessageBox("Please choose any file", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        #check for readability
        if not os.access(self._path, os.F_OK | os.R_OK):
            wx.MessageBox("The file is not readable", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        #check the file contents
        try:
            with open(self._path, "r") as python_file:
                content = python_file.read()
                self._tree = astd.parse(content)
        except:
            wx.MessageBox("The file contains errors", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        return True

    def OnOK(self, event):
        """Process Ok button"""
        if self.Validate():
            self.EndModal(wx.ID_OK)

