From 821a9b001846d3a4f459b45a34208505f78d1667 Mon Sep 17 00:00:00 2001 From: Aleksey Maksimov Date: Wed, 24 Jun 2015 23:47:14 +0800 Subject: [PATCH] Added auto save/load of watch expressions --- pudb/debugger.py | 12 ++++++++++++ pudb/settings.py | 33 +++++++++++++++++++++++++++++++++ pudb/var_view.py | 14 ++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/pudb/debugger.py b/pudb/debugger.py index a05d6e46..f5a5129d 100644 --- a/pudb/debugger.py +++ b/pudb/debugger.py @@ -881,6 +881,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] diff --git a/pudb/settings.py b/pudb/settings.py index 58020156..a7c476b4 100644 --- a/pudb/settings.py +++ b/pudb/settings.py @@ -42,6 +42,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(): @@ -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 diff --git a/pudb/var_view.py b/pudb/var_view.py index 7edb466d..36bbb31e 100644 --- a/pudb/var_view.py +++ b/pudb/var_view.py @@ -494,6 +494,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)