Browse Source

Move most of code go gmon module

Alois Mahdal 10 years ago
parent
commit
af16d2b577
2 changed files with 68 additions and 66 deletions
  1. 2
    66
      gmon
  2. 66
    0
      include/gmon.py

+ 2
- 66
gmon View File

@@ -14,72 +14,8 @@ Options:
14 14
     --version       show version
15 15
 """
16 16
 
17
-import os
18
-import sys
19
-import imp
20
-
21 17
 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()
18
+import gmon
83 19
 
84 20
 
85 21
 if __name__ == '__main__':
@@ -87,5 +23,5 @@ if __name__ == '__main__':
87 23
     args = docopt.docopt(__doc__,
88 24
                          version="gmon 0.1",
89 25
                          options_first=True)
90
-    app = App(args)
26
+    app = gmon.App(args)
91 27
     app.run()

+ 66
- 0
include/gmon.py View File

@@ -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()