Quellcode durchsuchen

Add plugin base class

Alois Mahdal vor 10 Jahren
Ursprung
Commit
f8d05a0b85
4 geänderte Dateien mit 30 neuen und 13 gelöschten Zeilen
  1. 20
    0
      include/gmon.py
  2. 3
    4
      plugins/dump.py
  3. 4
    5
      plugins/echo.py
  4. 3
    4
      plugins/sh.py

+ 20
- 0
include/gmon.py Datei anzeigen

@@ -0,0 +1,20 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+
4
+class GmonPluginError(Exception):
5
+    pass
6
+
7
+
8
+class BasePlugin(object):
9
+
10
+    def __init__(self, data):
11
+        self.data = data
12
+
13
+    def render(self):
14
+        method_n = "render_" + self.data.fmt
15
+
16
+        def ex(__):
17
+            raise GmonPluginError("method not defined: " + method_n)
18
+
19
+        render_fn = getattr(self, method_n, ex)
20
+        return render_fn()

+ 3
- 4
plugins/dump.py Datei anzeigen

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

+ 4
- 5
plugins/echo.py Datei anzeigen

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

+ 3
- 4
plugins/sh.py Datei anzeigen

@@ -2,11 +2,10 @@
2 2
 
3 3
 import subprocess
4 4
 
5
+import gmon
5 6
 
6
-class Plugin():
7 7
 
8
-    def __init__(self, subcommand):
9
-        self.args = subcommand.args
8
+class Plugin(gmon.BasePlugin):
10 9
 
11
-    def render(self):
10
+    def render_plain(self):
12 11
         return subprocess.check_output(self.args)