import os.path

from tron import g, hub
from tron.Hub.Command.Encoders.ASCIICmdEncoder import ASCIICmdEncoder
from tron.Hub.Nub.TCCShellNub import TCCShellNub
from tron.Hub.Reply.Decoders.ASCIIReplyDecoder import ASCIIReplyDecoder


name = 'tcc'


def start(poller):

    stop()

    initCmds = ('show version', 'show users', 'show time', 'show status', 'show inst/full',
                'show object/full', 'show axisconfig', 'show focus', 'axis status', 'show scale',
                'mir status')

    safeCmds = r'(^show )|(status$)'

    d = ASCIIReplyDecoder(EOL='\r', stripChars='\n', CIDfirst=False, debug=1)
    e = ASCIICmdEncoder(EOL='\r', debug=1, CIDfirst=False)
    tcc = TCCShellNub(poller, [
        '/usr/bin/ssh', '-1', '-e', 'none', '-a', '-x', '-i',
        os.path.expanduser('~/.ssh/tron'), '-T', 'tccuser@tcc25m'
    ],
        initCmds=initCmds,
        safeCmds=safeCmds,
        needsAuth=True,
        name=name,
        encoder=e,
        decoder=d,
        logDir=os.path.join(g.logDir, name),
        debug=1)

    hub.addActor(tcc)


def stop():
    n = hub.findActor(name)
    if n:
        hub.dropActor(n)
        del n

[((547, 616), 'tron.Hub.Reply.Decoders.ASCIIReplyDecoder.ASCIIReplyDecoder', 'ASCIIReplyDecoder', ([], {'EOL': "'\\r'", 'stripChars': '"""\n"""', 'CIDfirst': '(False)', 'debug': '(1)'}), "(EOL='\\r', stripChars='\\n', CIDfirst=False, debug=1)\n", (564, 616), False, 'from tron.Hub.Reply.Decoders.ASCIIReplyDecoder import ASCIIReplyDecoder\n'), ((625, 675), 'tron.Hub.Command.Encoders.ASCIICmdEncoder.ASCIICmdEncoder', 'ASCIICmdEncoder', ([], {'EOL': "'\\r'", 'debug': '(1)', 'CIDfirst': '(False)'}), "(EOL='\\r', debug=1, CIDfirst=False)\n", (640, 675), False, 'from tron.Hub.Command.Encoders.ASCIICmdEncoder import ASCIICmdEncoder\n'), ((1045, 1062), 'tron.hub.addActor', 'hub.addActor', (['tcc'], {}), '(tcc)\n', (1057, 1062), False, 'from tron import g, hub\n'), ((1085, 1104), 'tron.hub.findActor', 'hub.findActor', (['name'], {}), '(name)\n', (1098, 1104), False, 'from tron import g, hub\n'), ((1123, 1139), 'tron.hub.dropActor', 'hub.dropActor', (['n'], {}), '(n)\n', (1136, 1139), False, 'from tron import g, hub\n')]