From f975aab29b58cc1b337b014dea0735f582f2b7ca Mon Sep 17 00:00:00 2001 From: Aleksey Maksimov Date: Wed, 24 Jun 2015 23:47:14 +0800 Subject: [PATCH 1/2] 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 da668840..3f913821 100644 --- a/pudb/debugger.py +++ b/pudb/debugger.py @@ -901,6 +901,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 2f2f1f6a..ab3c3829 100644 --- a/pudb/settings.py +++ b/pudb/settings.py @@ -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(): @@ -586,4 +587,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 bd80f3ad..490f1d6d 100644 --- a/pudb/var_view.py +++ b/pudb/var_view.py @@ -544,6 +544,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) From 2da202054b9d33ec808a20885a97fff13e228248 Mon Sep 17 00:00:00 2001 From: Aleksey Maksimov Date: Mon, 11 Dec 2017 21:27:49 +0800 Subject: [PATCH 2/2] add option in Preferences to keep watches (default is Off) --- pudb/debugger.py | 24 +++++++++++++----------- pudb/settings.py | 26 ++++++++++++++++++++++++++ pudb/var_view.py | 29 ++++++++++++++++------------- 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/pudb/debugger.py b/pudb/debugger.py index 3f913821..7054eb68 100644 --- a/pudb/debugger.py +++ b/pudb/debugger.py @@ -901,17 +901,19 @@ 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) + if CONFIG["persist_watches"]: + 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_expressions[i] + + # Save it here because self.update_var_view() is going to + # read saved watches again + save_watches(stored_expressions) for i, watch_expr in enumerate(fvi.watches): if watch_expr is var.watch_expr: diff --git a/pudb/settings.py b/pudb/settings.py index ab3c3829..2dd4bbd1 100644 --- a/pudb/settings.py +++ b/pudb/settings.py @@ -109,6 +109,8 @@ def load_config(): conf_dict.setdefault("wrap_variables", True) conf_dict.setdefault("default_variables_access_level", "public") + conf_dict.setdefault("persist_watches", False) + conf_dict.setdefault("display", "auto") conf_dict.setdefault("prompt_on_quit", True) @@ -124,6 +126,7 @@ def normalize_bool_inplace(name): normalize_bool_inplace("line_numbers") normalize_bool_inplace("wrap_variables") + normalize_bool_inplace("persist_watches") normalize_bool_inplace("prompt_on_quit") return conf_dict @@ -179,6 +182,9 @@ def _update_default_variables_access_level(): def _update_wrap_variables(): ui.update_var_view() + def _update_persist_watches(): + pass + def _update_config(check_box, new_state, option_newvalue): option, newvalue = option_newvalue new_conf_dict = {option: newvalue} @@ -230,6 +236,11 @@ def _update_config(check_box, new_state, option_newvalue): conf_dict.update(new_conf_dict) _update_wrap_variables() + elif option == "persist_watches": + new_conf_dict["persist_watches"] = not check_box.get_state() + conf_dict.update(new_conf_dict) + _update_persist_watches() + heading = urwid.Text("This is the preferences screen for PuDB. " "Hit Ctrl-P at any time to get back to it.\n\n" "Configuration settings are saved in " @@ -384,6 +395,17 @@ def _update_config(check_box, new_state, option_newvalue): # }}} + # {{{ persist watches + + cb_persist_watches = urwid.CheckBox("Persist watches", + bool(conf_dict["persist_watches"]), on_state_change=_update_config, + user_data=("persist_watches", None)) + + persist_watches_info = urwid.Text("\nKeep watched expressions between " + "debugging sessions.") + + # }}} + # {{{ display display_info = urwid.Text("What driver is used to talk to your terminal. " @@ -438,6 +460,10 @@ def _update_config(check_box, new_state, option_newvalue): + [cb_wrap_variables] + [wrap_variables_info] + + [urwid.AttrMap(urwid.Text("\nPersist Watches:\n"), "group head")] + + [cb_persist_watches] + + [persist_watches_info] + + [urwid.AttrMap(urwid.Text("\nDisplay driver:\n"), "group head")] + [display_info] + display_rbs diff --git a/pudb/var_view.py b/pudb/var_view.py index 490f1d6d..7b697011 100644 --- a/pudb/var_view.py +++ b/pudb/var_view.py @@ -544,19 +544,22 @@ 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]) + if CONFIG["persist_watches"]: + 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: