Soft Patch Panel
 help / color / Atom feed
* [spp] [PATCH] cli: add config option
@ 2019-08-06  7:29 yasufum.o
  0 siblings, 0 replies; only message in thread
From: yasufum.o @ 2019-08-06  7:29 UTC (permalink / raw)
  To: spp, ferruh.yigit, yasufum.o

From: Yasufumi Ogawa <yasufum.o@gmail.com>

SPP CLI has a default config file as `src/cli/config/defautl.yml`. It
might be customized and rename other than default. This update is to add
`--config` option for loading customized config instead of default one.

  $ python3 src/spp.py --config /path/to/your/config.yml

Signed-off-by: Yasufumi Ogawa <yasufum.o@gmail.com>
---
 src/cli/shell.py | 79 +++++++++++++++++++++++++-----------------------
 src/cli/spp.py   |  5 ++-
 2 files changed, 46 insertions(+), 38 deletions(-)

diff --git a/src/cli/shell.py b/src/cli/shell.py
index 2288e1d..989f071 100644
--- a/src/cli/shell.py
+++ b/src/cli/shell.py
@@ -24,39 +24,44 @@ import yaml
 class Shell(cmd.Cmd, object):
     """SPP command prompt."""
 
-    # Load default config, can be changed via `config` command
-    # TODO(yasufum) enable to give config file from option
-    try:
-        config_file = "{}/config/default.yml".format(
-                os.path.dirname(__file__))
-        cli_config = yaml.load(open(config_file),
-                               Loader=yaml.FullLoader)
-    except IOError as e:
-        print('Error: no config file found!')
-        print(e)
-        exit()
-
-    hist_file = os.path.expanduser('~/.spp_history')
-    PLUGIN_DIR = 'plugins'
-
-    # Commands not included in history
-    HIST_EXCEPT = ['bye', 'exit', 'history', 'redo']
-
-    # Shell settings which are reserved vars of Cmd class.
-    # `intro` is to be shown as a welcome message.
-    intro = 'Welcome to the SPP CLI. Type `help` or `?` to list commands.\n'
-    prompt = cli_config['prompt']['val']  # command prompt
-
-    # Recipe file to be recorded with `record` command
-    recorded_file = None
-
-    # setup history file
-    if os.path.exists(hist_file):
-        readline.read_history_file(hist_file)
-    else:
-        readline.write_history_file(hist_file)
-
-    def __init__(self, spp_cli_objs, use_cache=False):
+    def __init__(self, spp_cli_objs, config, use_cache=False):
+
+        # Load default config, can be changed via `config` command
+        try:
+            if config is not None:
+                config_path = "{}/{}".format(
+                        os.getcwd(), config)
+            else:
+                config_path = "{}/config/default.yml".format(
+                        os.path.dirname(__file__))
+
+            self.cli_config = yaml.load(open(config_path),
+                Loader=yaml.FullLoader)
+        except IOError as e:
+            print('Error: No config file found!')
+            print(e)
+            exit()
+
+        self.hist_file = os.path.expanduser('~/.spp_history')
+        self.plugin_dir = 'plugins'
+
+        # Commands not included in history
+        self.hist_except = ['bye', 'exit', 'history', 'redo']
+
+        # Shell settings which are reserved vars of Cmd class.
+        # `intro` is to be shown as a welcome message.
+        self.intro = 'Welcome to the SPP CLI. Type `help` or `?` to list commands.\n'
+        self.prompt = self.cli_config['prompt']['val']  # command prompt
+
+        # Recipe file to be recorded with `record` command
+        self.recorded_file = None
+
+        # setup history file
+        if os.path.exists(self.hist_file):
+            readline.read_history_file(self.hist_file)
+        else:
+            readline.write_history_file(self.hist_file)
+
         cmd.Cmd.__init__(self)
         self.spp_ctl_server = server.SppCtlServer(spp_cli_objs)
         self.spp_ctl_cli = spp_cli_objs[0]
@@ -107,7 +112,7 @@ class Shell(cmd.Cmd, object):
             self.init_spp_procs()
 
         # TODO(yasufum) do not add to history if command is failed.
-        if line.strip().split(' ')[0] not in self.HIST_EXCEPT:
+        if line.strip().split(' ')[0] not in self.hist_except:
             readline.write_history_file(self.hist_file)
         return stop
 
@@ -917,7 +922,7 @@ class Shell(cmd.Cmd, object):
         args = re.sub(r'\s+', ' ', args)
         list_args = args.split(' ')
 
-        libdir = self.PLUGIN_DIR
+        libdir = self.plugin_dir
         mod_name = list_args[0]
         method_name = 'do_%s' % mod_name
         exec('from .%s import %s' % (libdir, mod_name))
@@ -933,7 +938,7 @@ class Shell(cmd.Cmd, object):
     def complete_load_cmd(self, text, line, begidx, endidx):
         """Complete command plugins
 
-        Search under PLUGIN_DIR with compl_common() method.
+        Search under `plugin_dir` with compl_common() method.
         This method is intended to be used for searching current
         directory, but not in this case. If text is not '',
         compl_common() does not work correctly and do filtering
@@ -942,7 +947,7 @@ class Shell(cmd.Cmd, object):
 
         curdir = os.path.dirname(__file__)
         res = common.compl_common(
-            '', '%s/%s' % (curdir, self.PLUGIN_DIR), 'py')
+            '', '%s/%s' % (curdir, self.plugin_dir), 'py')
 
         completions = []
         for t in res:
diff --git a/src/cli/spp.py b/src/cli/spp.py
index 00ffd6f..8cf31eb 100644
--- a/src/cli/spp.py
+++ b/src/cli/spp.py
@@ -3,6 +3,7 @@
 # Copyright(c) 2015-2016 Intel Corporation
 
 import argparse
+import os
 import re
 from .shell import Shell
 from .shell_lib import common
@@ -20,6 +21,8 @@ def main(argv):
     parser.add_argument('-b', '--bind-addr', action='append',
                         default=['%s:%s' % (api_ipaddr, api_port)],
                         help='bind address, default=127.0.0.1:7777')
+    parser.add_argument('--config', type=str,
+                        help='Config file path')
     args = parser.parse_args()
 
     if len(args.bind_addr) > 1:
@@ -48,7 +51,7 @@ def main(argv):
 
         spp_cli_objs.append(spp_ctl_cli)
 
-    shell = Shell(spp_cli_objs)
+    shell = Shell(spp_cli_objs, args.config)
     shell.cmdloop()
     shell = None
 
-- 
2.17.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-06  7:29 [spp] [PATCH] cli: add config option yasufum.o

Soft Patch Panel

Archives are clonable:
	git clone --mirror http://inbox.dpdk.org/spp/0 spp/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 spp spp/ http://inbox.dpdk.org/spp \
		spp@dpdk.org
	public-inbox-index spp


Newsgroup available over NNTP:
	nntp://inbox.dpdk.org/inbox.dpdk.spp


AGPL code for this site: git clone https://public-inbox.org/ public-inbox