From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mogw0935.ocn.ad.jp (mogw0935.ocn.ad.jp [153.149.227.41]) by dpdk.org (Postfix) with ESMTP id C64FA4F9A for ; Tue, 6 Mar 2018 11:51:15 +0100 (CET) Received: from mf-smf-ucb032c3 (mf-smf-ucb032c3.ocn.ad.jp [153.153.66.205]) by mogw0935.ocn.ad.jp (Postfix) with ESMTP id 3C9E5D8024B; Tue, 6 Mar 2018 19:51:14 +0900 (JST) Received: from ntt.pod01.mv-mta-ucb021 ([153.149.142.84]) by mf-smf-ucb032c3 with ESMTP id tABQew1nkxVzhtABee5Oxi; Tue, 06 Mar 2018 19:51:14 +0900 Received: from smtp.ocn.ne.jp ([153.149.227.165]) by ntt.pod01.mv-mta-ucb021 with id JarD1x00N3akymp01arDS8; Tue, 06 Mar 2018 10:51:14 +0000 Received: from localhost.localdomain (sp1-66-103-93.msc.spmode.ne.jp [1.66.103.93]) by smtp.ocn.ne.jp (Postfix) with ESMTPA; Tue, 6 Mar 2018 19:51:13 +0900 (JST) From: ogawa.yasufumi@lab.ntt.co.jp To: ferruh.yigit@intel.com, spp@dpdk.org Cc: Yasufumi Ogawa Date: Tue, 6 Mar 2018 19:50:47 +0900 Message-Id: <20180306105055.65210-6-ogawa.yasufumi@lab.ntt.co.jp> X-Mailer: git-send-email 2.13.1 In-Reply-To: <20180306105055.65210-1-ogawa.yasufumi@lab.ntt.co.jp> References: <20180306105055.65210-1-ogawa.yasufumi@lab.ntt.co.jp> Subject: [spp] [PATCH 05/13] controller: move common methods to shell_lib 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: Tue, 06 Mar 2018 10:51:16 -0000 From: Yasufumi Ogawa For refactoring, move common methods in 'shell.py' to shell_lib/. Signed-off-by: Yasufumi Ogawa --- src/controller/shell.py | 106 +++-------------------------------- src/controller/shell_lib/__init__.py | 0 src/controller/shell_lib/common.py | 94 +++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 98 deletions(-) create mode 100644 src/controller/shell_lib/__init__.py create mode 100644 src/controller/shell_lib/common.py diff --git a/src/controller/shell.py b/src/controller/shell.py index 7c7d94a..3cb5f96 100644 --- a/src/controller/shell.py +++ b/src/controller/shell.py @@ -1,9 +1,9 @@ import cmd -# import importlib import json import os from Queue import Empty import re +from shell_lib import common import spp_common from spp_common import logger import subprocess @@ -28,96 +28,6 @@ class Shell(cmd.Cmd, object): SEC_SUBCMDS = ['vhost', 'ring', 'pcap', 'nullpmd'] BYE_CMDS = ['sec', 'all'] - def decorate_dir(self, curdir, filelist): - """Add '/' the end of dirname for path completion - - 'filelist' is a list of files contained in a directory. - """ - - res = [] - for f in filelist: - if os.path.isdir('%s/%s' % (curdir, f)): - res.append('%s/' % f) - else: - res.append(f) - return res - - def compl_common(self, text, line, ftype=None): - """File path completion for 'complete_*' method - - This method is called from 'complete_*' to complete 'do_*'. - 'text' and 'line' are arguments of 'complete_*'. - - `complete_*` is a member method of builtin Cmd class and - called if tab key is pressed in a command defiend by 'do_*'. - 'text' and 'line' are contents of command line. - For example, if you type tab at 'command arg1 ar', - last token 'ar' is assigned to 'text' and whole line - 'command arg1 ar' is assigned to 'line'. - - NOTE: - If tab is typed after '/', empty text '' is assigned to - 'text'. For example 'aaa b/', text is not 'b/' but ''. - """ - - if text == '': # tab is typed after command name or '/' - tokens = line.split(' ') - target_dir = tokens[-1] # get dirname for competion - if target_dir == '': # no dirname means current dir - res = self.decorate_dir( - '.', os.listdir(os.getcwd())) - else: # after '/' - res = self.decorate_dir( - target_dir, os.listdir(target_dir)) - else: # tab is typed in the middle of a word - tokens = line.split(' ') - target = tokens[-1] # target dir for completion - - if '/' in target: # word is a path such as 'path/to/file' - seg = target.split('/')[-1] # word to be completed - target_dir = '/'.join(target.split('/')[0:-1]) - else: - seg = text - target_dir = os.getcwd() - - matched = [] - for t in os.listdir(target_dir): - if t.find(seg) == 0: # get words matched with 'seg' - matched.append(t) - res = self.decorate_dir(target_dir, matched) - - if ftype is not None: # filtering by ftype - completions = [] - if ftype == 'directory': - for fn in res: - if fn[-1] == '/': - completions.append(fn) - elif ftype == 'file': - for fn in res: - if fn[-1] != '/': - completions.append(fn) - else: - completions = res - else: - completions = res - return completions - - def is_comment_line(self, line): - """Find commend line to not to interpret as a command - - Return True if given line is a comment, or False. - Supported comment styles are - * python ('#') - * C ('//') - """ - - input_line = line.strip() - if len(input_line) > 0: - if (input_line[0] == '#') or (input_line[0:2] == '//'): - return True - else: - return False - def default(self, line): """Define defualt behaviour @@ -125,7 +35,7 @@ class Shell(cmd.Cmd, object): as a comment. """ - if self.is_comment_line(line): + if common.is_comment_line(line): print("%s" % line.strip()) else: super(Shell, self).default(line) @@ -401,7 +311,7 @@ class Shell(cmd.Cmd, object): self.response(self.CMD_ERROR, "invalid format") def complete_record(self, text, line, begidx, endidx): - return self.compl_common(text, line) + return common.compl_common(text, line) def do_record(self, fname): """Save commands to a log file @@ -421,7 +331,7 @@ class Shell(cmd.Cmd, object): self.response(self.CMD_OK, "record") def complete_playback(self, text, line, begidx, endidx): - return self.compl_common(text, line) + return common.compl_common(text, line) def do_playback(self, fname): """Load a config file to reproduce network configuration @@ -440,7 +350,7 @@ class Shell(cmd.Cmd, object): with open(fname) as recorded_file: lines = [] for line in recorded_file: - if not self.is_comment_line(line): + if not common.is_comment_line(line): lines.append("# %s" % line) lines.append(line) self.cmdqueue.extend(lines) @@ -483,7 +393,7 @@ class Shell(cmd.Cmd, object): print(os.getcwd()) def complete_ls(self, text, line, begidx, endidx): - return self.compl_common(text, line) + return common.compl_common(text, line) def do_ls(self, args): """Show a list of specified directory @@ -500,7 +410,7 @@ class Shell(cmd.Cmd, object): print("No such a directory.") def complete_cd(self, text, line, begidx, endidx): - return self.compl_common(text, line, 'directory') + return common.compl_common(text, line, 'directory') def do_cd(self, args): """Change current directory @@ -515,7 +425,7 @@ class Shell(cmd.Cmd, object): print("No such a directory.") def complete_mkdir(self, text, line, begidx, endidx): - return self.compl_common(text, line) + return common.compl_common(text, line) def do_mkdir(self, args): """Create a new directory diff --git a/src/controller/shell_lib/__init__.py b/src/controller/shell_lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/controller/shell_lib/common.py b/src/controller/shell_lib/common.py new file mode 100644 index 0000000..d011684 --- /dev/null +++ b/src/controller/shell_lib/common.py @@ -0,0 +1,94 @@ +import os + + +def decorate_dir(curdir, filelist): + """Add '/' the end of dirname for path completion + + 'filelist' is a list of files contained in a directory. + """ + + res = [] + for f in filelist: + if os.path.isdir('%s/%s' % (curdir, f)): + res.append('%s/' % f) + else: + res.append(f) + return res + + +def compl_common(text, line, ftype=None): + """File path completion for 'complete_*' method + + This method is called from 'complete_*' to complete 'do_*'. + 'text' and 'line' are arguments of 'complete_*'. + + `complete_*` is a member method of builtin Cmd class and + called if tab key is pressed in a command defiend by 'do_*'. + 'text' and 'line' are contents of command line. + For example, if you type tab at 'command arg1 ar', + last token 'ar' is assigned to 'text' and whole line + 'command arg1 ar' is assigned to 'line'. + + NOTE: + If tab is typed after '/', empty text '' is assigned to + 'text'. For example 'aaa b/', text is not 'b/' but ''. + """ + + if text == '': # tab is typed after command name or '/' + tokens = line.split(' ') + target_dir = tokens[-1] # get dirname for competion + if target_dir == '': # no dirname means current dir + res = decorate_dir( + '.', os.listdir(os.getcwd())) + else: # after '/' + res = decorate_dir( + target_dir, os.listdir(target_dir)) + else: # tab is typed in the middle of a word + tokens = line.split(' ') + target = tokens[-1] # target dir for completion + + if '/' in target: # word is a path such as 'path/to/file' + seg = target.split('/')[-1] # word to be completed + target_dir = '/'.join(target.split('/')[0:-1]) + else: + seg = text + target_dir = os.getcwd() + + matched = [] + for t in os.listdir(target_dir): + if t.find(seg) == 0: # get words matched with 'seg' + matched.append(t) + res = decorate_dir(target_dir, matched) + + if ftype is not None: # filtering by ftype + completions = [] + if ftype == 'directory': + for fn in res: + if fn[-1] == '/': + completions.append(fn) + elif ftype == 'file': + for fn in res: + if fn[-1] != '/': + completions.append(fn) + else: + completions = res + else: + completions = res + return completions + + +def is_comment_line(line): + """Find commend line to not to interpret as a command + + Return True if given line is a comment, or False. + Supported comment styles are + * python ('#') + * C ('//') + """ + + input_line = line.strip() + if len(input_line) > 0: + if (input_line[0] == '#') or (input_line[0:2] == '//'): + return True + else: + return False -- 2.13.1