A modest string writer

gmon.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8 -*-
  2. import imp
  3. import os
  4. import sys
  5. class GmonPluginError(Exception):
  6. pass
  7. class XGPRenderer(object):
  8. def __init__(self, txt=None):
  9. self.txt = txt
  10. self.img = None
  11. self.tool = txt
  12. self.bar = None
  13. self.click = None
  14. self._fields = ['txt', 'img', 'tool', 'bar', 'click']
  15. def __str__(self):
  16. out = ""
  17. for field in self._fields:
  18. value = getattr(self, field, None)
  19. if value is not None:
  20. out += "<%(f)s>%(v)s</%(f)s>" % {'f': field, 'v': value}
  21. return out
  22. class BasePlugin(object):
  23. def __init__(self, data):
  24. self.data = data
  25. def render(self):
  26. method_n = "render_" + self.data.fmt
  27. def ex(__):
  28. raise GmonPluginError("method not defined: " + method_n)
  29. render_fn = getattr(self, method_n, ex)
  30. return render_fn()
  31. def render_xgp(self):
  32. return XGPRenderer(self.render_plain())
  33. class PluginData(object):
  34. def __init__(self, cmdargs):
  35. self.args = []
  36. self.fmt = "plain"
  37. self.maxlen = 40
  38. self.name = ""
  39. self._load(cmdargs)
  40. def _load(self, cmdargs):
  41. if cmdargs['--plain']:
  42. self.fmt = "plain"
  43. elif cmdargs['--xgp']:
  44. self.fmt = "xgp"
  45. self.name = cmdargs['<command>']
  46. self.args = cmdargs['<args>']
  47. class Subcommand(object):
  48. def __init__(self, cmdargs, pluginpath):
  49. self.plugindata = PluginData(cmdargs)
  50. self.name = cmdargs['<command>']
  51. self._load_plugin(pluginpath)
  52. def _load_plugin(self, pluginpath):
  53. modpath = "%s/%s.py" % (pluginpath, self.name)
  54. try:
  55. with open(modpath) as fh:
  56. module = imp.load_module(self.name, fh, modpath,
  57. (".py", "r", imp.PY_SOURCE))
  58. except IOError:
  59. raise GmonPluginError(self.name)
  60. self.plugin = module.Plugin(self.plugindata)
  61. def _format_output(self, raw):
  62. suffix = "..."
  63. cooked = raw
  64. if self.plugindata.fmt == "plain":
  65. if len(raw) > self.plugindata.maxlen:
  66. newlen = self.plugindata.maxlen - len(suffix)
  67. cooked = raw[:newlen] + suffix
  68. return cooked
  69. def get_output(self):
  70. return self._format_output(self.plugin.render())
  71. class App:
  72. def __init__(self, cmdargs):
  73. self._set_pluginpath()
  74. try:
  75. self.subcommand = Subcommand(cmdargs, self.pluginpath)
  76. except GmonPluginError as e:
  77. self.die("no such plugin: %s" % e)
  78. def _set_pluginpath(self):
  79. mypath = os.path.dirname(os.path.realpath(sys.argv[0]))
  80. path = mypath + "/plugins"
  81. path = os.environ.get("GMON_PLUGINS", path)
  82. self.pluginpath = path
  83. def die(self, msg, rv=9):
  84. sys.stderr.write(msg + "\n")
  85. self.exit(rv)
  86. def exit(self, rv=0):
  87. sys.exit(rv)
  88. def run(self):
  89. print self.subcommand.get_output()