Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import re
from sjrpc.client import SimpleRpcClient
from cccli import printer
from cccli.clierror import *
class Command(object):
def __init__(self, argv, rpc):
if len(argv) < 1:
raise Exception("Empty command")
self._argv = argv
self._rpc = rpc
def call(self):
'''Run command'''
if re.match("^[a-zA-Z0-9]+$", self._argv[0]):
name = "cmd_%s"%self._argv[0]
if hasattr(self, name):
cmd = getattr(self, name)
return cmd(self._argv)
raise BadCommand(self._argv[0])
def usage(self, cmdname, printf=None):
'''Return usage of a command'''
fname = "cmd_%s"%cmdname
if hasattr(self, fname):
if hasattr(getattr(self, fname), "usage"):
s = getattr(getattr(self, fname), "usage")
if printf != None:
printf(s)
return s
else:
return "No usage"
else:
raise myError("No such command: %s"%cmdname)
def cmd_exit(self, argv):
'''Quit application with respect'''
raise SystemExit()
def cmd_list(self, argv):
'''List something'''
if len(argv) == 1:
argv.append("hv")
items = self._rpc.list(str.join("", argv[1:]))
for item in items:
for key, val in item.items():
printer.out("%s: %s "%(key, val))
printer.out("*"*80)
def cmd_export(self, argv):
'''List server exported methods'''
for cmds in self._rpc.list_commands():
printer.out("%s"%cmds["name"])