12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #!/usr/bin/python
-
- """Generic mon echoer
-
- usage: gmon [--format=<format>] [-p|--plain] [-x|--xgp] <command> [<args>...]
-
- Options:
- -c, --chars specify limit in characters
- --format <format> specify format: plain is a plain text, xgp
- is specific for xfce4-genmon-plugin
- -p, --plain same as --format=plain
- -x, --xgp same as --format=xgp
- -h, --help show this screen
- --version show version
- """
-
- import os
- import sys
- import imp
-
- import docopt
-
-
- class PluginData(object):
-
- def __init__(self, cmdargs):
- self.args = []
- self.fmt = "plain"
- self.maxlen = 40
- self.name = ""
- self._load(cmdargs)
-
- def _load(self, cmdargs):
- if cmdargs['--plain']:
- self.fmt = "plain"
- elif cmdargs['--xgp']:
- self.fmt = "xgp"
- self.name = cmdargs['<command>']
- self.args = cmdargs['<args>']
-
-
- class Subcommand(object):
-
- def __init__(self, args, pluginpath):
- self.plugindata = PluginData(args)
- self.name = args['<command>']
- self._load_plugin(pluginpath)
-
- def _load_plugin(self, pluginpath):
- modpath = "%s/%s.py" % (pluginpath, self.name)
- with open(modpath) as fh:
- module = imp.load_module(self.name, fh, modpath,
- (".py", "r", imp.PY_SOURCE))
- self.plugin = module.Plugin(self.plugindata)
-
- def _format_output(self, raw):
- suffix = "..."
- cooked = raw
- if self.plugindata.fmt == "plain":
- if len(raw) > self.plugindata.maxlen:
- newlen = self.plugindata.maxlen - len(suffix)
- cooked = raw[:newlen] + suffix
- return cooked
-
- def get_output(self):
- return self._format_output(self.plugin.render())
-
-
- class App:
-
- def __init__(self, args):
- self._set_pluginpath()
- self.subcommand = Subcommand(args, self.pluginpath)
-
- def _set_pluginpath(self):
- mypath = os.path.dirname(os.path.realpath(sys.argv[0]))
- path = mypath + "/plugins"
- path = os.environ.get("GMON_PLUGINS", path)
- self.pluginpath = path
-
- def run(self):
- print self.subcommand.get_output()
-
-
- if __name__ == '__main__':
-
- args = docopt.docopt(__doc__,
- version="gmon 0.1",
- options_first=True)
- app = App(args)
- app.run()
|