|
@@ -1,9 +1,14 @@
|
1
|
1
|
# -*- coding: utf-8 -*-
|
2
|
2
|
|
|
3
|
+import imp
|
|
4
|
+import os
|
|
5
|
+import sys
|
|
6
|
+
|
3
|
7
|
|
4
|
8
|
class GmonPluginError(Exception):
|
5
|
9
|
pass
|
6
|
10
|
|
|
11
|
+
|
7
|
12
|
class XGPRenderer(object):
|
8
|
13
|
|
9
|
14
|
def __init__(self, txt=None):
|
|
@@ -39,3 +44,64 @@ class BasePlugin(object):
|
39
|
44
|
|
40
|
45
|
def render_xgp(self):
|
41
|
46
|
return XGPRenderer(self.render_plain())
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+class PluginData(object):
|
|
50
|
+
|
|
51
|
+ def __init__(self, cmdargs):
|
|
52
|
+ self.args = []
|
|
53
|
+ self.fmt = "plain"
|
|
54
|
+ self.maxlen = 40
|
|
55
|
+ self.name = ""
|
|
56
|
+ self._load(cmdargs)
|
|
57
|
+
|
|
58
|
+ def _load(self, cmdargs):
|
|
59
|
+ if cmdargs['--plain']:
|
|
60
|
+ self.fmt = "plain"
|
|
61
|
+ elif cmdargs['--xgp']:
|
|
62
|
+ self.fmt = "xgp"
|
|
63
|
+ self.name = cmdargs['<command>']
|
|
64
|
+ self.args = cmdargs['<args>']
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+class Subcommand(object):
|
|
68
|
+
|
|
69
|
+ def __init__(self, args, pluginpath):
|
|
70
|
+ self.plugindata = PluginData(args)
|
|
71
|
+ self.name = args['<command>']
|
|
72
|
+ self._load_plugin(pluginpath)
|
|
73
|
+
|
|
74
|
+ def _load_plugin(self, pluginpath):
|
|
75
|
+ modpath = "%s/%s.py" % (pluginpath, self.name)
|
|
76
|
+ with open(modpath) as fh:
|
|
77
|
+ module = imp.load_module(self.name, fh, modpath,
|
|
78
|
+ (".py", "r", imp.PY_SOURCE))
|
|
79
|
+ self.plugin = module.Plugin(self.plugindata)
|
|
80
|
+
|
|
81
|
+ def _format_output(self, raw):
|
|
82
|
+ suffix = "..."
|
|
83
|
+ cooked = raw
|
|
84
|
+ if self.plugindata.fmt == "plain":
|
|
85
|
+ if len(raw) > self.plugindata.maxlen:
|
|
86
|
+ newlen = self.plugindata.maxlen - len(suffix)
|
|
87
|
+ cooked = raw[:newlen] + suffix
|
|
88
|
+ return cooked
|
|
89
|
+
|
|
90
|
+ def get_output(self):
|
|
91
|
+ return self._format_output(self.plugin.render())
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+class App:
|
|
95
|
+
|
|
96
|
+ def __init__(self, args):
|
|
97
|
+ self._set_pluginpath()
|
|
98
|
+ self.subcommand = Subcommand(args, self.pluginpath)
|
|
99
|
+
|
|
100
|
+ def _set_pluginpath(self):
|
|
101
|
+ mypath = os.path.dirname(os.path.realpath(sys.argv[0]))
|
|
102
|
+ path = mypath + "/plugins"
|
|
103
|
+ path = os.environ.get("GMON_PLUGINS", path)
|
|
104
|
+ self.pluginpath = path
|
|
105
|
+
|
|
106
|
+ def run(self):
|
|
107
|
+ print self.subcommand.get_output()
|