From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mogw0836.ocn.ad.jp (mogw0836.ocn.ad.jp [153.149.234.37]) by dpdk.org (Postfix) with ESMTP id D07E52BF1 for ; Tue, 6 Mar 2018 11:51:13 +0100 (CET) Received: from mf-smf-ucb024c3 (mf-smf-ucb024c3.ocn.ad.jp [153.153.66.162]) by mogw0836.ocn.ad.jp (Postfix) with ESMTP id 8E051600058; Tue, 6 Mar 2018 19:51:12 +0900 (JST) Received: from ntt.pod01.mv-mta-ucb027 ([153.149.142.101]) by mf-smf-ucb024c3 with ESMTP id tABaeeGdx1pt0tABceg6fB; Tue, 06 Mar 2018 19:51:12 +0900 Received: from smtp.ocn.ne.jp ([153.149.227.165]) by ntt.pod01.mv-mta-ucb027 with id JarC1x00A3akymp01arCla; Tue, 06 Mar 2018 10:51:12 +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:12 +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:46 +0900 Message-Id: <20180306105055.65210-5-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 04/13] controller: add load 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: Tue, 06 Mar 2018 10:51:14 -0000 From: Yasufumi Ogawa Load command is for loading a command plugin. It enables users to activate a command after SPP controller is launched. Plugin files are included in 'src/controller/command/'. This update also includes a sample command 'hello'. It says hello message with given name. spp > load hello spp > hello alice Hello, alice! Signed-off-by: Yasufumi Ogawa --- src/controller/command/__init__.py | 0 src/controller/command/hello.py | 28 ++++++++++++++++++++++++++++ src/controller/shell.py | 21 +++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/controller/command/__init__.py create mode 100644 src/controller/command/hello.py diff --git a/src/controller/command/__init__.py b/src/controller/command/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/controller/command/hello.py b/src/controller/command/hello.py new file mode 100644 index 0000000..f898234 --- /dev/null +++ b/src/controller/command/hello.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# coding: utf-8 + + +class Hello(object): + def __init__(self, name): + self.name = name + + def say(self): + print("Hello, %s!" % self.name) + + +def do_hello(name): + """Say hello to given user + + spp > hello alice + Hello, alice! + """ + + if name == '': + print('name is required!') + else: + hl = Hello(name) + hl.say() + +if __name__ == "__main__": + hello = Hello() + print(hello.say()) diff --git a/src/controller/shell.py b/src/controller/shell.py index 06b0012..7c7d94a 100644 --- a/src/controller/shell.py +++ b/src/controller/shell.py @@ -1,4 +1,5 @@ import cmd +# import importlib import json import os from Queue import Empty @@ -563,3 +564,23 @@ class Shell(cmd.Cmd, object): self.close() print('Thank you for using Soft Patch Panel') return True + + def do_load(self, args): + """Load command plugin + + Path of plugin file is 'spp/src/controller/command'. + + spp > load hello + """ + + args = re.sub(',', ' ', args) + args = re.sub(r'\s+', ' ', args) + list_args = args.split(' ') + + libdir = 'command' + loaded = '%s.%s' % (libdir, list_args[0]) + # importlib.import_module(loaded) + exec('import %s' % loaded) + do_cmd = '%s.do_%s' % (loaded, list_args[0]) + setattr(self, 'do_%s' % list_args[0], eval(do_cmd)) + print("Module '%s' loaded." % loaded) -- 2.13.1