1. Atsushi Odagiri
  2. bucho

Commits

Atsushi Odagiri  committed 0bf15e0

plugin system

  • Participants
  • Parent commits 0a52550
  • Branches aodag

Comments (0)

Files changed (3)

File bucho/command.py

View file
 import cmd
 import optparse
 import bucho
+import pkg_resources
 
 description = '''
 Actions are commands like "show", "latest_status" and "all_status".
                 help='prompt string',
                 dest='prompt',
                 action='store', default=(programname+">"))
+        parser.add_option('--plugin',
+                help='command plugin',
+                dest='plugin',
+                action='store')
 
         self.options, self.args = parser.parse_args()
 
     def __init__(self, options):
         self.options = options
         self.prompt = self.options.prompt + ' '
+        if getattr(self.options, 'plugin'):
+            cls = self.__class__
+            dist = self.options.plugin
+            for name, entry_point in pkg_resources.get_entry_map(dist, 'bucho.commands').iteritems():
+                setattr(cls, "do_" + name, makeplugincmd(name, entry_point.load()))
+                
+
         cmd.Cmd.__init__(self)
 
     def do_quit(self, arg):
         return 1
 
+def makeplugincmd(n, func):
+    def f(self, arg):
+        t = func()
+        t = t.encode(bucho_encoding, 'replace')
+        t = t.decode(bucho_encoding, 'replace')
+        print(t)
+        return 0
+    return f
+def makecmd(n):
+    def f(self, arg):
+        t = getattr(bucho, n)()
+        t = t.encode(bucho_encoding, 'replace')
+        t = t.decode(bucho_encoding, 'replace')
+        print(t)
+        return 0
+    return f
+
 for name in ['show', 'latest_status', 'all_status', 'torumemo']:
-    def makecmd(n):
-        def f(self, arg):
-            t = getattr(bucho, n)()
-            t = t.encode(bucho_encoding, 'replace')
-            t = t.decode(bucho_encoding, 'replace')
-            print(t)
-            return 0
-        return f
     setattr(BuchoCmd, "do_" + name, makecmd(name))
 
 

File bucho/plugins.py

View file
+def sample():
+    """ sample plugin command
+    """
+    return "sample"

File setup.py

View file
         'paste.app_factory': [
             'main=bucho.wsgi:app_factory',
         ],
+        'bucho.commands': [
+            'sample=bucho.plugins:sample',
+        ],
     },
     test_suite="bucho",
     **extra