bucho / bucho / command.py

import os
import sys
import cmd
import optparse
import bucho
import pkg_resources
from bucho.compat import print_

description = '''
Actions are commands like "show", "latest_status" and "all_status".
If -i is specified or no action is specified on the command line, a
"shell" interpreting actions typed interactively is started. Use the
action "help" to find out about available actions.

bucho_encoding = sys.stdout.encoding
if not bucho_encoding:
    bucho_encoding = 'utf-8'

class BuchoOptions(object):

    def __init__(self):
        programname = os.path.basename(sys.argv[0])
        base, ext = os.path.splitext(programname)
        if ext == ".py":
            if base.endswith('-script'):
                base = base[:-7]
            programname = base
        sys.argv[0] = programname

        usage = 'usage: %prog [options] command'
        parser = optparse.OptionParser(usage=usage, description=description)
        parser.add_option('-i', '--interactive',
                help='start an interactive shell after executing commands',
                action='store_true', default=False)

        parser.add_option('-p', '--prompt',
                help='prompt string',
                action='store', default=(programname+">"))
                help='command plugin',
        self.options, self.args = parser.parse_args()

        if not self.args and not self.options.interactive:
            self.options.interactive = 1

    def __getattr__(self, name):
        if hasattr(self.options, name):
            return getattr(self.options, name)
        raise AttributeError(name)

    def __add__(self, obj):
        return obj

class BuchoCmd(cmd.Cmd):

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


    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')
        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')
        return 0
    return f

for name in ['show', 'latest_status', 'all_status', 'torumemo', 'show_gui']:
    setattr(BuchoCmd, "do_" + name, makecmd(name))

def main(options=None, cmdclass=BuchoCmd):
        if options is None:
            options = BuchoOptions()
        c = cmdclass(options)
    except Exception as e:

    if options.args:
        c.onecmd(" ".join(options.args))

    if options.interactive:
            import readline
        except ImportError:

def console():

if __name__ == '__main__':
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.