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

[((22, 8, 22, 77), 'tron.Hub.Reply.Decoders.ASCIIReplyDecoder.ASCIIReplyDecoder', 'ASCIIReplyDecoder', (), '', False, 'from tron.Hub.Reply.Decoders.ASCIIReplyDecoder import ASCIIReplyDecoder\n'), ((23, 8, 23, 58), 'tron.Hub.Command.Encoders.ASCIICmdEncoder.ASCIICmdEncoder', 'ASCIICmdEncoder', (), '', False, 'from tron.Hub.Command.Encoders.ASCIICmdEncoder import ASCIICmdEncoder\n'), ((37, 4, 37, 21), 'tron.hub.addActor', 'hub.addActor', ({(37, 17, 37, 20): 'tcc'}, {}), '(tcc)', False, 'from tron import g, hub\n'), ((41, 8, 41, 27), 'tron.hub.findActor', 'hub.findActor', ({(41, 22, 41, 26): 'name'}, {}), '(name)', False, 'from tron import g, hub\n'), ((43, 8, 43, 24), 'tron.hub.dropActor', 'hub.dropActor', ({(43, 22, 43, 23): 'n'}, {}), '(n)', False, 'from tron import g, hub\n')]