[spp] [PATCH 04/13] controller: add load command
ogawa.yasufumi at lab.ntt.co.jp
ogawa.yasufumi at lab.ntt.co.jp
Tue Mar 6 11:50:46 CET 2018
From: Yasufumi Ogawa <ogawa.yasufumi at lab.ntt.co.jp>
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 <ogawa.yasufumi at lab.ntt.co.jp>
---
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
More information about the spp
mailing list