Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added auto save/load of watch expressions
Browse files Browse the repository at this point in the history
lechat committed Dec 11, 2017
1 parent 481ee21 commit 1ea224c
Showing 3 changed files with 59 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pudb/debugger.py
Original file line number Diff line number Diff line change
@@ -898,6 +898,18 @@ def edit_inspector_detail(w, size, key):
var.watch_expr.expression = watch_edit.get_edit_text()

elif result == "del":
from pudb.settings import load_watches, save_watches
stored_expressions = [expr.strip() for expr in load_watches()]

# Remove saved expression
for i, stored_expr in enumerate(stored_expressions):
if stored_expr == var.watch_expr.expression:
del stored_watches[i]

# Save it here because self.update_var_view() is going to
# read saved watches again
save_watches(stored_watches)

for i, watch_expr in enumerate(fvi.watches):
if watch_expr is var.watch_expr:
del fvi.watches[i]
33 changes: 33 additions & 0 deletions pudb/settings.py
Original file line number Diff line number Diff line change
@@ -69,6 +69,7 @@ def get_save_config_path(*resource):

SAVED_BREAKPOINTS_FILE_NAME = "saved-breakpoints-%d.%d" % sys.version_info[:2]
BREAKPOINTS_FILE_NAME = "breakpoints-%d.%d" % sys.version_info[:2]
SAVED_WATCHES_FILE_NAME = "saved-watches-%d.%d" % sys.version_info[:2]


def load_config():
@@ -550,4 +551,36 @@ def save_breakpoints(bp_list):

# }}}


def get_watches_file_name():
from os.path import join
return join(get_save_config_path(), SAVED_WATCHES_FILE_NAME)


def save_watches(w_list):
"""
:arg w_list: a list of strings
"""

try:
with open(get_watches_file_name(), 'w+') as histfile:
for watch in w_list:
histfile.write(watch + '\n')
except:
pass


def load_watches():
if os.path.exists(get_watches_file_name()):
try:
with open(get_watches_file_name(), 'r') as histfile:
watches = histfile.readlines()
for line in watches:
line = line.strip()
return watches
except:
pass

return []

# vim:foldmethod=marker
14 changes: 14 additions & 0 deletions pudb/var_view.py
Original file line number Diff line number Diff line change
@@ -536,6 +536,20 @@ def make_var_view(frame_var_info, locals, globals):
ret_walker = BasicValueWalker(frame_var_info)
watch_widget_list = []

from pudb.settings import load_watches, save_watches
stored_expressions = [expr.strip() for expr in load_watches()]

# As watch expressions are stored in a list, simply appending stored
# expressions to that list will add duplicates. This part is to avoid that.
from pudb.var_view import WatchExpression
existing_expressions = [expr.expression for expr in frame_var_info.watches]
for stored_expr in stored_expressions:
if stored_expr not in existing_expressions:
frame_var_info.watches.append(WatchExpression(stored_expr))

# Save watches because new ones may have added to a list
save_watches([expr.expression for expr in frame_var_info.watches])

for watch_expr in frame_var_info.watches:
try:
value = eval(watch_expr.expression, globals, locals)

0 comments on commit 1ea224c

Please sign in to comment.