#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Wed Feb 11 13:37:43 2009

import wx

import sys, os

app = None


# begin wxGlade: extracode
# end wxGlade

class TextOutput(file):
    def __init__(self, text_ctrl):
        self.text_ctrl = text_ctrl
    def write(self, text):
        self.text_ctrl.AppendText(text+"\n")
        self.text_ctrl.Refresh()
            
    def writelines(self, list):
        for text in list:
            self.text_ctrl.AppendText(text+"\n")
        self.text_ctrl.Refresh()
        
    def fileno(self):
        return 1
        
    def clear(self):
        self.text_ctrl.Clear()

class MainFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MainFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.action_choice = wx.RadioBox(self, -1, "Action", choices=["Convert", "Dryrun", "Diff", "Clean", "Revert"], majorDimension=2, style=wx.RA_SPECIFY_ROWS)
        self.label_1 = wx.StaticText(self, -1, "Verbosity Level")
        self.verbosity = wx.Choice(self, -1, choices=["0", "1", "2", "3"])
        self.unix_endings = wx.CheckBox(self, -1, "Convert to UNIX line-endings")
        self.spell_check = wx.CheckBox(self, -1, "Enable spell-checking")
        self.path_ctrl = wx.TextCtrl(self, -1, "")
        self.choose_file = wx.Button(self, -1, "Browse")
        self.Convert = wx.Button(self, -1, "Go")
        self.Output = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.TE_READONLY)

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.button_browse, self.choose_file)
        self.Bind(wx.EVT_BUTTON, self.button_convert, self.Convert)
        # end wxGlade
        
        self.text_output = TextOutput(self.Output)
        
        self.process = None
        self.Bind(wx.EVT_IDLE, self.on_idle)
        self.Bind(wx.EVT_END_PROCESS, self.subprocess_finished)

    def __set_properties(self):
        # begin wxGlade: MainFrame.__set_properties
        self.SetTitle("wmllint")
        self.SetSize((340, 481))
        self.action_choice.SetSelection(0)
        self.label_1.SetMinSize((110, 17))
        self.verbosity.SetSelection(0)
        self.path_ctrl.SetMinSize((250, 34))
        self.Output.SetMinSize((340, 270))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MainFrame.__do_layout
        sizer_3 = wx.BoxSizer(wx.VERTICAL)
        sizer_4 = wx.BoxSizer(wx.VERTICAL)
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_5 = wx.BoxSizer(wx.VERTICAL)
        sizer_7 = wx.BoxSizer(wx.VERTICAL)
        sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_5.Add(self.action_choice, 0, wx.EXPAND, 3)
        sizer_6.Add(self.label_1, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        sizer_6.Add(self.verbosity, 0, wx.EXPAND, 0)
        sizer_5.Add(sizer_6, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        sizer_7.Add(self.unix_endings, 0, wx.ADJUST_MINSIZE, 0)
        sizer_7.Add(self.spell_check, 0, wx.ADJUST_MINSIZE, 0)
        sizer_5.Add(sizer_7, 1, wx.ALIGN_CENTER_HORIZONTAL, 0)
        sizer_4.Add(sizer_5, 1, wx.EXPAND, 0)
        sizer_2.Add(self.path_ctrl, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
        sizer_2.Add(self.choose_file, 0, wx.ADJUST_MINSIZE, 0)
        sizer_1.Add(sizer_2, 0, wx.EXPAND, 0)
        sizer_1.Add(self.Convert, 0, wx.EXPAND, 0)
        sizer_4.Add(sizer_1, 0, wx.EXPAND, 0)
        sizer_3.Add(sizer_4, 0, wx.EXPAND, 0)
        sizer_3.Add(self.Output, 2, wx.EXPAND, 0)
        self.SetSizer(sizer_3)
        sizer_3.SetSizeHints(self)
        self.Layout()
        # end wxGlade
        
    def start_wmllint(self,output,\
        arguments,\
        clean = False,\
        diffs = False,\
        dryrun = False,\
        future = False,\
        revert = False,\
        stringfreeze = False,\
        stripcr = False,\
        spell_check = False,\
        verbose = 0):
        
        cmd = "python -u wmllint"
        if clean:
            cmd+=" -c"
        if diffs:
            cmd+=" -D"
        if dryrun:
            cmd+=" -d"
        if future:
            cmd+=" --future"
        if revert:
            cmd+=" -r"
        if stripcr:
            cmd+=" -s"
        for i in range(verbose):
            cmd+=" -v"
        for path in arguments:
            cmd+=" "+path
            
        self.text_output.write("Executing \""+cmd+"\".")

        self.process = wx.Process(self)
        self.process.Redirect()
        self.wmllint_pid = wx.Execute(cmd, wx.EXEC_ASYNC, self.process)

    def button_convert(self, event): # wxGlade: MainFrame.<event_handler>
    	if self.process:
    		self.process.Kill(self.wmllint_pid)
    	else:
			self.text_output.clear()
			
			path = self.path_ctrl.GetValue()
			
			clean = False
			diffs = False
			dryrun = False
			future = False
			revert = False
			stringfreeze = False
			stripcr = False
			spell_check = False
			verbose = 0
			
			mode = self.action_choice.GetStringSelection()
			if(mode == "Clean"):
				clean = True
			if(mode == "Diff"):
				diffs = True
			if(mode == "Dryrun"):
				dryrun = True
			if(mode == "Revert"):
				revert = True
			
			spell_check = self.spell_check.GetValue()
			stripcr = self.unix_endings.GetValue()
			
			verbose = self.verbosity.GetSelection()
			
			if not os.path.exists(path):
				self.text_output.write("Path could not be found.")
				return
			
			self.start_wmllint(self.text_output, [path], clean, diffs, dryrun, future, revert,\
				stringfreeze, stripcr, spell_check, verbose)
				
			self.set_running(True)
        
    def set_running(self, val):
        if val == True:
            self.Convert.SetLabel("Stop")
            self.action_choice.Enable(False)
            self.unix_endings.Enable(False)
            self.spell_check.Enable(False)
            self.verbosity.Enable(False)
            self.path_ctrl.Enable(False)
            self.choose_file.Enable(False)
        else:
            self.Convert.SetLabel("Go")
            self.action_choice.Enable(True)
            self.unix_endings.Enable(True)
            self.spell_check.Enable(True)
            self.verbosity.Enable(True)
            self.path_ctrl.Enable(True)
            self.choose_file.Enable(True)
            
    def subprocess_finished(self, event):
        self.set_running(False)
        self.process = None
        

    def button_browse(self, event): # wxGlade: MainFrame.<event_handler>
        dir_dialog = wx.DirDialog(self)
        dir_dialog.ShowModal()
        path = dir_dialog.GetPath()
        self.path_ctrl.SetValue(path)
        dir_dialog.Show(False)
        del dir_dialog
        
    def on_idle(self, evt):
        if self.process is not None:
            stream = self.process.GetInputStream()
            if stream.CanRead():
                text = stream.read()
                self.text_output.write(text)

# end of class MainFrame


if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    main_frame = MainFrame(None, -1, "")
    app.SetTopWindow(main_frame)
    main_frame.Show()
    app.MainLoop()
