#   Programmer: Daniel Pozmanter
#   E-mail:     drpython@bluebottle.com
#   Note:       You must reply to the verification e-mail to get through.
#
#   Copyright 2003-2004 Daniel Pozmanter
#
#   Distributed under the terms of the GPL (GNU Public License)
#
#    DrPython is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#Plugin
#Traceback Menu

import wx
import wx.lib.dialogs

def OnAbout(DrFrame):
    Version = "Traceback Menu: Version: 1.0.2\n"
    AboutString = Version + "By Daniel Pozmanter\n\nReleased under the GPL."
    d = wx.lib.dialogs.ScrolledMessageDialog(DrFrame, AboutString, "About")
    d.ShowModal()
    d.Destroy()

def InsertTraceback(DrFrame, Position, LineNumber):
    eol = DrFrame.txtDocument.GetEndOfLineCharacter()
    addchar = DrFrame.txtDocument.GetIndentationString()
    aistring = ""
    numtabs = DrFrame.txtDocument.GetLineIndentation(LineNumber) / DrFrame.prefs.tabwidth
    x = 0
    while x < numtabs:
        aistring = aistring + addchar
        x = x + 1

    aistring = aistring + addchar

    insertText = eol + aistring \
+ "import sys, traceback" + eol + aistring \
+ "slist = traceback.format_tb(sys.exc_info()[2])" + eol + aistring \
+ "l = len(slist)" + eol + aistring \
+ "x = 0" + eol + aistring \
+ "rstring = \"\"" + eol + aistring \
+ "while x < l:" + eol + aistring \
+ addchar  + "rstring = rstring + slist[x]" + eol + aistring \
+ addchar + "x = x + 1" + eol + aistring \
+ "print \"Traceback (most recent call last):\\n\" + rstring \\" + eol + aistring \
+ "+ str(sys.exc_info()[0]).lstrip(\"exceptions.\") + \": \" + str(sys.exc_info()[1])" + eol

    #Add Tag:
    insertText = eol + "#***DRPYTHON TRACEBACK***" + eol + insertText + eol + "#***END DRPYTHON TRACEBACK***"

    DrFrame.txtDocument.InsertText(Position, insertText)

def Plugin(DrFrame):
    def OnAddTracebacks(event):
        pos = 0
        while pos < DrFrame.txtDocument.GetLength():
            exceptpos = DrFrame.txtDocument.GetText()[pos:].find("except:")

            if exceptpos > -1:
                exceptpos = pos + exceptpos + 7
                DrFrame.txtDocument.SetCurrentPos(exceptpos)
                InsertTraceback(DrFrame, exceptpos, DrFrame.txtDocument.GetCurrentLine())
                pos = exceptpos
            else:
                pos = DrFrame.txtDocument.GetLength() + 1


    def OnInsertTraceback(event):
        InsertTraceback(DrFrame, DrFrame.txtDocument.GetCurrentPos(), DrFrame.txtDocument.GetCurrentLine())

    def OnRemoveTracebacks(event):
        text = DrFrame.txtDocument.GetText()

        l = len("#***END DRPYTHON TRACEBACK***")

        start = text.find("#***DRPYTHON TRACEBACK***")
        end = text.find("#***END DRPYTHON TRACEBACK***") + l

        while start > -1:
            text = text[:start] + text[end:]
            start = text.find("#***DRPYTHON TRACEBACK***")
            end = text.find("#***END DRPYTHON TRACEBACK***") + l

        DrFrame.txtDocument.SetText(text)

    ID_INSERT_TRACEBACK = DrFrame.GetNewId()
    ID_ADD_TRACEBACKS = DrFrame.GetNewId()
    ID_REMOVE_TRACEBACKS = DrFrame.GetNewId()

    DrFrame.LoadPluginShortcuts('TracebackMenu')

    tracebackmenu = wx.Menu()
    tracebackmenu.Append(ID_INSERT_TRACEBACK, "Insert Traceback")
    tracebackmenu.Append(ID_ADD_TRACEBACKS, "Add Tracebacks")
    tracebackmenu.Append(ID_REMOVE_TRACEBACKS, "Remove All Tracebacks")

    DrFrame.editmenu.AppendSeparator()
    DrFrame.editmenu.AppendMenu(DrFrame.GetNewId(), "Tracebacks", tracebackmenu)

    DrFrame.Bind(wx.EVT_MENU, OnInsertTraceback, id=ID_INSERT_TRACEBACK)
    DrFrame.Bind(wx.EVT_MENU, OnAddTracebacks, id=ID_ADD_TRACEBACKS)
    DrFrame.Bind(wx.EVT_MENU, OnRemoveTracebacks, id=ID_REMOVE_TRACEBACKS)

    DrFrame.AddPluginShortcutFunction("TracebackMenu", "OnInsertTraceback", OnInsertTraceback)
    DrFrame.AddPluginShortcutFunction("TracebackMenu", "OnAddTracebacks", OnAddTracebacks)
    DrFrame.AddPluginShortcutFunction("TracebackMenu", "OnRemoveTracebacks", OnRemoveTracebacks)

    DrFrame.AddPluginPopUpMenuFunction("TracebackMenu", "Insert Traceback", OnInsertTraceback)
    DrFrame.AddPluginPopUpMenuFunction("TracebackMenu", "Add Tracebacks", OnAddTracebacks)
    DrFrame.AddPluginPopUpMenuFunction("TracebackMenu", "Remove Tracebacks", OnRemoveTracebacks)
