Source code for tcssubsystem
#!/bin/env python
import os
import sys
import json
import re
from types import MethodType
from pytcs import *
def def_handler(handler):
return """\
def {h}_method( self, **kwargs ):
return json.loads( tcs_send( self.name, "{h}", str(kwargs).replace("'",'"') ) )
self.{h} = MethodType( {h}_method, self, TCSSubSystem )
""".format( h=handler )
def def_handler_async(handler):
return """\
def {h}_async_method( self, **kwargs ):
return tcs_send_async( self.name, "{h}", str(kwargs).replace("'",'"') )
self.{h}_async = MethodType( {h}_async_method, self, TCSSubSystem )
""".format( h=handler )
[docs]class TCSSubSystem:
def __init__(self, name, system):
# Create the sender object with the name given. Save the name given in
# the object for use in the handler definition.
self.name = name + str( os.getpid() )
if not tcs_create_sender( self.name, system ):
raise Exception("tcs_create_sender(%s,%s) returned False. Perhaps not a unique name?"%(name, system))
# Get the list of methods supported by the remote end.
self.api = json.loads( tcs_send( self.name, 'help', '' ) )
if len( self.api ) == 0:
raise
# keep a list of outstanding async commands.
self.outstanding_ = {}
# Loop over the handler names handed back by the help() handler.
for handler in self.api.iterkeys():
#ignore the built-in help handler, use local method
if( handler == 'help') : continue
# Execute the method definition for the handler. This binds the
# remotely named handler to this Python TCSSubSystem class.
exec( def_handler(handler) )
exec( def_handler_async(handler) )
def help( self, command='' ):
#First, see if there is an exact match
helpList = self.api.items()
fullval = self.api.get(command)
if( fullval != None ):
print str(fullval)
return
# do a pattern search, if not found
matchproc = re.compile(command)
numItems = 0
for i in helpList:
(key,val) = i
if( key.find('__' ) == 0 ): continue
output = str(val)
if( matchproc.search( key ) and output ):
numItems = numItems + 1
print output
if( numItems == 0 ) : print "No help information found for '" + command + "'"
def wait(self, msgid):
if not self: raise Exception( self, msgid )
return json.loads( tcs_wait( self.name, msgid ) )
def __exit__(self):
delete_sender( self.name )