Skip to content

Commit

Permalink
Added auto save/load of watch expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
lechat committed Apr 3, 2016
1 parent f2cc5db commit 755119c
Show file tree
Hide file tree
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
Expand Up @@ -878,6 +878,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]
Expand Down
33 changes: 33 additions & 0 deletions pudb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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():
Expand Down Expand Up @@ -502,4 +503,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
Expand Up @@ -491,6 +491,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)
Expand Down

0 comments on commit 755119c

Please sign in to comment.