Pārlūkot izejas kodu

Add basic structure and example plugins

Alois Mahdal 10 gadus atpakaļ
vecāks
revīzija
d660592f06
4 mainītis faili ar 125 papildinājumiem un 0 dzēšanām
  1. 91
    0
      gmon
  2. 11
    0
      plugins/dump.py
  3. 11
    0
      plugins/echo.py
  4. 12
    0
      plugins/sh.py

+ 91
- 0
gmon Parādīt failu

@@ -0,0 +1,91 @@
1
+#!/usr/bin/python
2
+
3
+"""Generic mon echoer
4
+
5
+usage: gmon [--format=<format>] [-p|--plain] [-x|--xgp] <command> [<args>...]
6
+
7
+Options:
8
+    -c, --chars     specify limit in characters
9
+    --format <format>   specify format: plain is a plain text, xgp
10
+                        is specific for xfce4-genmon-plugin
11
+    -p, --plain     same as --format=plain
12
+    -x, --xgp       same as --format=xgp
13
+    -h, --help      show this screen
14
+    --version       show version
15
+"""
16
+
17
+import os
18
+import sys
19
+import imp
20
+
21
+import docopt
22
+
23
+
24
+class PluginData(object):
25
+
26
+    def __init__(self, cmdargs):
27
+        self.args = []
28
+        self.fmt = "plain"
29
+        self.maxlen = 40
30
+        self.name = ""
31
+        self._load(cmdargs)
32
+
33
+    def _load(self, cmdargs):
34
+        if cmdargs['--plain']:
35
+            self.fmt = "plain"
36
+        elif cmdargs['--xgp']:
37
+            self.fmt = "xgp"
38
+        self.name = cmdargs['<command>']
39
+        self.args = cmdargs['<args>']
40
+
41
+
42
+class Subcommand(object):
43
+
44
+    def __init__(self, args, pluginpath):
45
+        self.plugindata = PluginData(args)
46
+        self.name = args['<command>']
47
+        self._load_plugin(pluginpath)
48
+
49
+    def _load_plugin(self, pluginpath):
50
+        modpath = "%s/%s.py" % (pluginpath, self.name)
51
+        with open(modpath) as fh:
52
+            module = imp.load_module(self.name, fh, modpath,
53
+                                     (".py", "r", imp.PY_SOURCE))
54
+        self.plugin = module.Plugin(self.plugindata)
55
+
56
+    def _format_output(self, raw):
57
+        suffix = "..."
58
+        cooked = raw
59
+        if self.plugindata.fmt == "plain":
60
+            if len(raw) > self.plugindata.maxlen:
61
+                newlen = self.plugindata.maxlen - len(suffix)
62
+                cooked = raw[:newlen] + suffix
63
+        return cooked
64
+
65
+    def get_output(self):
66
+        return self._format_output(self.plugin.render())
67
+
68
+
69
+class App:
70
+
71
+    def __init__(self, args):
72
+        self._set_pluginpath()
73
+        self.subcommand = Subcommand(args, self.pluginpath)
74
+
75
+    def _set_pluginpath(self):
76
+        mypath = os.path.dirname(os.path.realpath(sys.argv[0]))
77
+        path = mypath + "/plugins"
78
+        path = os.environ.get("GMON_PLUGINS", path)
79
+        self.pluginpath = path
80
+
81
+    def run(self):
82
+        print self.subcommand.get_output()
83
+
84
+
85
+if __name__ == '__main__':
86
+
87
+    args = docopt.docopt(__doc__,
88
+                         version="gmon 0.1",
89
+                         options_first=True)
90
+    app = App(args)
91
+    app.run()

+ 11
- 0
plugins/dump.py Parādīt failu

@@ -0,0 +1,11 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+
4
+class Plugin():
5
+
6
+    def __init__(self, subcommand):
7
+        self.args = subcommand.args
8
+
9
+    def render(self):
10
+        out = "args = %s" % repr(self.args)
11
+        return out

+ 11
- 0
plugins/echo.py Parādīt failu

@@ -0,0 +1,11 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+
4
+class Plugin():
5
+
6
+    def __init__(self, subcommand):
7
+        self.args = subcommand.args
8
+
9
+    def render(self):
10
+        out = "".join([str(s) for s in self.args])
11
+        return out

+ 12
- 0
plugins/sh.py Parādīt failu

@@ -0,0 +1,12 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+import subprocess
4
+
5
+
6
+class Plugin():
7
+
8
+    def __init__(self, subcommand):
9
+        self.args = subcommand.args
10
+
11
+    def render(self):
12
+        return subprocess.check_output(self.args)