From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from tama50.ecl.ntt.co.jp (tama50.ecl.ntt.co.jp [129.60.39.147]) by dpdk.org (Postfix) with ESMTP id 62E5A1B12D for ; Mon, 4 Feb 2019 04:13:30 +0100 (CET) Received: from vc2.ecl.ntt.co.jp (vc2.ecl.ntt.co.jp [129.60.86.154]) by tama50.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id x143DSPa030580; Mon, 4 Feb 2019 12:13:28 +0900 Received: from vc2.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id DB7D463887E; Mon, 4 Feb 2019 12:13:28 +0900 (JST) Received: from localhost.localdomain (lobster.nslab.ecl.ntt.co.jp [129.60.13.95]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id CB7A8638B14; Mon, 4 Feb 2019 12:13:28 +0900 (JST) From: ogawa.yasufumi@lab.ntt.co.jp To: spp@dpdk.org, ferruh.yigit@intel.com, ogawa.yasufumi@lab.ntt.co.jp Date: Mon, 4 Feb 2019 12:11:17 +0900 Message-Id: <1549249879-31580-4-git-send-email-ogawa.yasufumi@lab.ntt.co.jp> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1549249879-31580-1-git-send-email-ogawa.yasufumi@lab.ntt.co.jp> References: <1549249879-31580-1-git-send-email-ogawa.yasufumi@lab.ntt.co.jp> X-TM-AS-MML: disable Subject: [spp] [PATCH 3/5] controller: fix bug of history command X-BeenThere: spp@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Soft Patch Panel List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Feb 2019 03:13:31 -0000 From: Yasufumi Ogawa In SPP CLI, command is not added to history without running `history` because setting a hook for flushing from `readline` is inappropriate. It should be set to when after command is done. This update is to fix the issue by using `postcmd()` of Cmd class. Signed-off-by: Yasufumi Ogawa --- src/controller/shell.py | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/src/controller/shell.py b/src/controller/shell.py index 40bafc2..c2a31c6 100644 --- a/src/controller/shell.py +++ b/src/controller/shell.py @@ -25,9 +25,11 @@ class Shell(cmd.Cmd, object): recorded_file = None hist_file = os.path.expanduser('~/.spp_history') + + # Commands not included in history HIST_EXCEPT = ['bye', 'exit', 'history', 'redo'] - intro = 'Welcome to the spp. Type help or ? to list commands.\n' + intro = 'Welcome to the SPP CLI. Type `help` or `?` to list commands.\n' prompt = 'spp > ' PLUGIN_DIR = 'plugins' @@ -76,10 +78,19 @@ class Shell(cmd.Cmd, object): self.secondaries['mirror'][sec_id] = mirror.SppMirror( self.spp_ctl_cli, sec_id) + # Called everytime after running command. `stop` is returned from `do_*` + # method and SPP CLI is terminated if it is True. It means that only + # `do_bye` and `do_exit` return True. + def postcmd(self, stop, line): + # TODO(yasufum) do not add to history if command is failed. + if line.strip().split(' ')[0] not in self.HIST_EXCEPT: + readline.write_history_file(self.hist_file) + return stop + def default(self, line): """Define defualt behaviour. - If user input is commend styled, controller simply echo + If user input is comment styled, controller simply echo as a comment. """ @@ -112,24 +123,6 @@ class Shell(cmd.Cmd, object): ids.append(ent['client-id']) return ids - def clean_hist_file(self): - """Remove useless entries in history file.""" - - entries = [] - - try: - for line in open(self.hist_file): - line_s = line.strip() - if not (line_s.split(' ')[0] in self.HIST_EXCEPT): - entries.append(line_s) - f = open(self.hist_file, "w+") - contents = '\n'.join(entries) - contents += '\n' - f.write(contents) - f.close() - except IOError: - print('Error: Cannot open history file "%s"' % self.hist_file) - def print_status(self): """Display information about connected clients.""" @@ -761,12 +754,6 @@ class Shell(cmd.Cmd, object): 'bye', 'exit', 'history', 'redo' """ - # flush all of history to the hist_file. - readline.write_history_file(self.hist_file) - - # remove commands defined in `self.HIST_EXCEPT` - self.clean_hist_file() - try: f = open(self.hist_file) -- 2.7.4