Skip to content

Commit

Permalink
Restructured according to good python package conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Cairns committed Jun 8, 2012
1 parent b1cd467 commit 074d523
Show file tree
Hide file tree
Showing 17 changed files with 914 additions and 106 deletions.
102 changes: 101 additions & 1 deletion plugin/dbgp.py
Expand Up @@ -132,6 +132,25 @@ def __init__(self,response,cmd,cmd_args):
self.cmd = cmd
self.cmd_args = cmd_args
self.xml = None
if "<error" in self.response:
self.__parse_error()

def __parse_error(self):
"""Parse an error message which has been returned
in the response, then raise it as a DBGPError."""
xml = self.as_xml()
err_el = xml.find('error')
code = err_el.get("code")
if code is None:
raise ResponseError(
"Missing error code in response",
self.response)
msg_el = err_el.find('message')
if msg_el is None:
raise ResponseError(
"Missing error message in response",
self.response)
raise DBGPError(msg_el.text,code)

def get_cmd(self):
"""Get the command that created this response."""
Expand Down Expand Up @@ -162,6 +181,12 @@ def as_xml(self):
def __str__(self):
return self.as_string()

class StatusResponse(Response):
"""Response object returned by the status command."""

def __str__(self):
return self.as_xml().get('status')

class FeatureGetResponse(Response):
"""Response object specifically for the feature_get command."""

Expand Down Expand Up @@ -248,7 +273,7 @@ def status(self):
Returns a Response object.
"""
return self.send_cmd('status')
return self.send_cmd('status','',StatusResponse)

def feature_get(self,name):
"""Get the value of a feature from the debugger.
Expand Down Expand Up @@ -278,6 +303,49 @@ def feature_set(self,name,value):
'feature_set',
'-n ' + str(name) + ' -v ' + str(value))

def run(self):
"""Tell the debugger to start or resume
execution."""
return self.send_cmd('run','',StatusResponse)

def step_into(self):
"""Tell the debugger to step to the next
statement.
If there's a function call, the debugger engine
will break on the first statement in the function.
"""
return self.send_cmd('step_into','',StatusResponse)

def step_over(self):
"""Tell the debugger to step to the next
statement.
If there's a function call, the debugger engine
will stop at the next statement after the function call.
"""
return self.send_cmd('step_over','',StatusResponse)

def step_out(self):
"""Tell the debugger to step out of the statement.
The debugger will step out of the current scope.
"""
return self.send_cmd('step_out','',StatusResponse)

def stop(self):
"""Tell the debugger to stop execution.
The script is terminated immediately."""
return self.send_cmd('stop','',StatusResponse)

def detach(self):
"""Tell the debugger to detach itself from this
client.
The script is not terminated, but runs as normal
from this point."""
return self.send_cmd('detach','',StatusResponse)

""" Errors/Exceptions """

Expand All @@ -295,3 +363,35 @@ class WrongIDEKeyException(Exception):
different to the expected one."""
pass

error_codes = { \
# 000 Command parsing errors
0 : """no error""",\
1 : """parse error in command""",\
2 : """duplicate arguments in command""", \
3 : """invalid options (ie, missing a required option)""",\
4 : """Unimplemented command""",\
5 : """Command not available (Is used for async commands. For instance if the engine is in state "run" than only "break" and "status" are available). """,\
# 100 : File related errors
100 : """can not open file (as a reply to a "source" command if the requested source file can't be opened)""",\
101 : """stream redirect failed """,\
# 200 Breakpoint, or code flow errors
200 : """breakpoint could not be set (for some reason the breakpoint could not be set due to problems registering it)""",\
201 : """breakpoint type not supported (for example I don't support 'watch' yet and thus return this error)""",\
202 : """invalid breakpoint (the IDE tried to set a breakpoint on a line that does not exist in the file (ie "line 0" or lines past the end of the file)""",\
203 : """no code on breakpoint line (the IDE tried to set a breakpoint on a line which does not have any executable code. The debugger engine is NOT required to """ + \
"""return this type if it is impossible to determine if there is code on a given location. (For example, in the PHP debugger backend this will only be """ + \
"""returned in some special cases where the current scope falls into the scope of the breakpoint to be set)).""",\
204 : """Invalid breakpoint state (using an unsupported breakpoint state was attempted)""",\
205 : """No such breakpoint (used in breakpoint_get etc. to show that there is no breakpoint with the given ID)""",\
206 : """Error evaluating code (use from eval() (or perhaps property_get for a full name get))""",\
207 : """Invalid expression (the expression used for a non-eval() was invalid) """,\
# 300 Data errors
300 : """Can not get property (when the requested property to get did not exist, this is NOT used for an existing but uninitialized property, which just gets the """ + \
"""type "uninitialised" (See: PreferredTypeNames)).""",\
301 : """Stack depth invalid (the -d stack depth parameter did not exist (ie, there were less stack elements than the number requested) or the parameter was < 0)""",\
302 : """Context invalid (an non existing context was requested) """,\
# 900 Protocol errors
900 : """Encoding not supported""",\
998 : """An internal exception in the debugger occurred""",\
999 : """Unknown error """\
}
199 changes: 199 additions & 0 deletions plugin/debugger.vim
@@ -0,0 +1,199 @@
" DBGp client: a remote debugger interface to the DBGp protocol
"
" Script Info and Documentation {{{
"=============================================================================
" Copyright: Copyright (C) 2007 Sam Ghods
" License: The MIT License
"
" Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files
" (the "Software"), to deal in the Software without restriction,
" including without limitation the rights to use, copy, modify,
" merge, publish, distribute, sublicense, and/or sell copies of the
" Software, and to permit persons to whom the Software is furnished
" to do so, subject to the following conditions:
"
" The above copyright notice and this permission notice shall be included
" in all copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
" Name Of File: debugger.vim, debugger.py
" Description: remote debugger interface to DBGp protocol
" Maintainer: Sam Ghods <sam <at> box.net>
" Last Change: June 18, 2007
" URL: http://www.vim.org/scripts/script.php?script_id=1929
" Version: 1.1.1
" Originally written by Seung Woo Shin <segv <at> sayclub.com>
" The original script is located at:
" http://www.vim.org/scripts/script.php?script_id=1152
" Usage: N.B.: For a complete tutorial on how to setup this script,
" please visit:
" http://tech.blog.box.net/2007/06/20/how-to-debug-php-with-vim-and-xdebug-on-linux/
" -----
"
" This file should reside in the plugins directory along
" with debugger.py and be automatically sourced.
"
" By default, the script expects the debugging engine to connect
" on port 9000. You can change this with the g:debuggerPort
" variable by putting the following line your vimrc:
"
" let g:debuggerPort = 10001
"
" where 10001 is the new port number you want the server to
" connect to.
"
" There are three maximum limits you can set referring to the
" properties (variables) returned by the debugging engine.
"
" g:debuggerMaxChildren (default 32): The max number of array or
" object children to initially retrieve per variable.
" For example:
"
" let g:debuggerMaxChildren = 64
"
" g:debuggerMaxData (default 1024 bytes): The max amount of
" variable data to retrieve.
" For example:
"
" let g:debuggerMaxData = 2048
"
" g:debuggerMaxDepth (default 1): The maximum depth that the
" debugger engine may return when sending arrays, hashs or
" object structures to the IDE.
" For example:
"
" let g:debuggerMaxDepth = 10
"
" Finally, if you use the Mini Buffer Explorer vim plugin,
" minibufexpl.vim, running the debugger may mess up your window
" setup. As a result the script has support to close and open
" the explorer when you enter and quit debugging sessions. To
" enable this support, add the following line to your vimrc:
"
" let g:debuggerMiniBufExpl = 1
"
" History: 1.1.1 o Added a check so the script doesn't load if python is
" not compiled in. (Contributed by Lars Becker.)
" 1.1 o Added vim variable to change port.
" o You can now put debugger.py in either runtime directory
" or the home directory.
" o Added to ability to change max children, data and depth
" settings.
" o Made it so stack_get wouldn't be called if the debugger
" has already stopped.
" o Added support for minibufexpl.vim.
" o License added.
" 1.0 o Initial release on December 7, 2004
"
" Known Issues: The code is designed for the DBGp protocol, but it has only been
" tested with XDebug 2.0RC4. If anyone would like to contribute patches
" to get it working with other DBGp software, I would be happy
" to implement them.
"
" Sometimes things go a little crazy... breakpoints don't show
" up, too many windows are created / not enough are closed, and
" so on... if you can actually find a set of solidly
" reproducible steps that lead to a bug, please do e-mail <sam
" <at> box.net> and I will take a look.
"
" Todo: Compatibility for other DBGp engines.
"
" Add a status line/window which constantly shows what the current
" status of the debugger is. (starting, break, stopped, etc.)
"
"=============================================================================
" }}}

" Do not source this script when python is not compiled in.
if !has("python")
finish
endif

" Load debugger.py either from the runtime directory (usually
" /usr/local/share/vim/vim71/plugin/ if you're running Vim 7.1) or from the
" home vim directory (usually ~/.vim/plugin/).
if filereadable($VIMRUNTIME."/plugin/python/debugger.py")
pyfile $VIMRUNTIME/plugin/debugger.py
elseif filereadable($HOME."/.vim/plugin/python/debugger.py")
pyfile $HOME/.vim/plugin/python/debugger.py
else
" when we use pathogen for instance
let $CUR_DIRECTORY=expand("<sfile>:p:h")

if filereadable($CUR_DIRECTORY."/python/debugger.py")
pyfile $CUR_DIRECTORY/python/debugger.py
else
call confirm('debugger.vim: Unable to find debugger.py. Place it in either your home vim directory or in the Vim runtime directory.', 'OK')
endif
endif

map <F1> :python debugger_mark()<cr>
map <F2> :python debugger_command('step_into')<cr>
map <F3> :python debugger_command('step_over')<cr>
map <F4> :python debugger_command('step_out')<cr>
map <Leader>dr :python debugger_resize()<cr>
map <Leader>di :python debugger_command('step_into')<cr>
map <Leader>do :python debugger_command('step_over')<cr>
map <Leader>dt :python debugger_command('step_out')<cr>
nnoremap <Leader>e :python debugger_watch_input("eval")<cr>A
vnoremap <Leader>e :python debugger_visual_eval()<cr>A
map <F5> :python debugger.dbg.listen()<cr>
map <F6> :python debugger_quit()<cr>
map <F10> :python debugger_globals()<cr>
map <F11> :python debugger_context()<cr>
map <F12> :python debugger_property()<cr>
hi DbgCurrent term=reverse ctermfg=White ctermbg=Red gui=reverse
hi DbgBreakPt term=reverse ctermfg=White ctermbg=Green gui=reverse

command! -nargs=? Bp python debugger_mark('<args>')
command! -nargs=? BpRm python debugger_remove_breakpoint('<args>')
command! -nargs=? BpLs python debugger_list_breakpoints()
command! -nargs=1 DebugDepth python debugger_set_depth('<args>')
command! -nargs=0 Up python debugger_up()
command! -nargs=0 Dn python debugger_down()
sign define current text=-> texthl=DbgCurrent linehl=DbgCurrent
sign define breakpt text=B> texthl=DbgBreakPt linehl=DbgBreakPt

if !exists('g:debuggerPort')
let g:debuggerPort = 9000
endif
if !exists('g:debuggerMaxChildren')
let g:debuggerMaxChildren = 64
endif
if !exists('g:debuggerMaxData')
let g:debuggerMaxData = 2048
endif
if !exists('g:debuggerMaxDepth')
let g:debuggerMaxDepth = 1
endif
if !exists('g:debuggerMiniBufExpl')
let g:debuggerMiniBufExpl = 0
endif
if !exists('g:debuggerAutoContext')
let g:debuggerAutoContext = 1
endif
if !exists('g:debuggerDebugMode')
let g:debuggerDebugMode = 0
endif
python debugger_init()

function! xdebug:get_visual_selection()
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
let lines = getline(lnum1, lnum2)
let lines[-1] = lines[-1][: col2 - 1]
let lines[0] = lines[0][col1 - 1:]
return join(lines, "\n")
endfunction
Empty file added plugin/python/__init__.py
Empty file.
1 change: 1 addition & 0 deletions plugin/python/dbgp/__init__.py
@@ -0,0 +1 @@
__all__ = ["response","connection","interface"]
Binary file added plugin/python/dbgp/__init__.pyc
Binary file not shown.

0 comments on commit 074d523

Please sign in to comment.