<s> import time <EOL> import pprint <EOL> import uuid <EOL> import json <EOL> from collections import OrderedDict <EOL> from director import lcmUtils <EOL> from director . thirdparty import numpyjsoncoder <EOL> from director import callbacks <EOL> from director . utime import getUtime <EOL> from director . uuidutil import newUUID <EOL> import drc as lcmdrc <EOL> class LCMObjectCollection ( object ) : <EOL> DESCRIPTION_UPDATED_SIGNAL = '<STR_LIT>' <EOL> DESCRIPTION_REMOVED_SIGNAL = '<STR_LIT>' <EOL> def __init__ ( self , channel ) : <EOL> self . collection = OrderedDict ( ) <EOL> self . collectionId = newUUID ( ) <EOL> self . sentCommands = set ( ) <EOL> self . sentRequest = None <EOL> self . channel = channel <EOL> self . callbacks = callbacks . CallbackRegistry ( [ self . DESCRIPTION_UPDATED_SIGNAL , <EOL> self . DESCRIPTION_REMOVED_SIGNAL ] ) <EOL> self . sub = lcmUtils . addSubscriber ( self . channel , messageClass = lcmdrc . affordance_collection_t , callback = self . _onCommandMessage ) <EOL> self . sub . setNotifyAllMessagesEnabled ( True ) <EOL> self . _modified ( ) <EOL> def __del__ ( self ) : <EOL> lcmUtils . removeSubscriber ( self . sub ) <EOL> def connectDescriptionUpdated ( self , func ) : <EOL> return self . callbacks . connect ( self . DESCRIPTION_UPDATED_SIGNAL , func ) <EOL> def disconnectDescriptionUpdated ( self , callbackId ) : <EOL> self . callbacks . disconnect ( callbackId ) <EOL> def connectDescriptionRemoved ( self , func ) : <EOL> return self . callbacks . connect ( self . DESCRIPTION_REMOVED_SIGNAL , func ) <EOL> def disconnectDescriptionRemoved ( self , callbackId ) : <EOL> self . callbacks . disconnect ( callbackId ) <EOL> def getDescriptionId ( self , desc ) : <EOL> return desc [ '<STR_LIT>' ] <EOL> def prettyPrintCollection ( self ) : <EOL> print json . dumps ( json . loads ( numpyjsoncoder . encode ( self . collection ) ) , indent = <NUM_LIT:2> ) <EOL> def getDescription ( self , descriptionId ) : <EOL> return self . collection [ descriptionId ] <EOL> def updateDescription ( self , desc , publish = True , notify = True ) : <EOL> self . collection [ self . getDescriptionId ( desc ) ] = desc <EOL> self . _modified ( ) <EOL> if publish : <EOL> msg = self . _newCommandMessage ( '<STR_LIT>' , description = desc ) <EOL> lcmUtils . publish ( self . channel , msg ) <EOL> if notify : <EOL> self . callbacks . process ( self . DESCRIPTION_UPDATED_SIGNAL , self , self . getDescriptionId ( desc ) ) <EOL> def removeDescription ( self , descriptionId , publish = True , notify = True ) : <EOL> try : <EOL> del self . collection [ descriptionId ] <EOL> self . _modified ( ) <EOL> except KeyError : <EOL> pass <EOL> if publish : <EOL> msg = self . _newCommandMessage ( '<STR_LIT>' , descriptionId = descriptionId , ) <EOL> lcmUtils . publish ( self . channel , msg ) <EOL> if notify : <EOL> self . callbacks . process ( self . DESCRIPTION_REMOVED_SIGNAL , self , descriptionId ) <EOL> def sendEchoRequest ( self ) : <EOL> self . sentRequest = newUUID ( ) <EOL> msg = self . _newCommandMessage ( '<STR_LIT>' , requestId = self . sentRequest ) <EOL> lcmUtils . publish ( self . channel , msg ) <EOL> def sendEchoResponse ( self , requestId = None ) : <EOL> if requestId is None : <EOL> requestId = newUUID ( ) <EOL> msg = self . _newCommandMessage ( '<STR_LIT>' , requestId = requestId , descriptions = self . collection ) <EOL> lcmUtils . publish ( self . channel , msg ) <EOL> def handleEchoResponse ( self , data ) : <EOL> self . sentRequest = None <EOL> for desc in data [ '<STR_LIT>' ] . values ( ) : <EOL> self . updateDescription ( desc , publish = False ) <EOL> def _modified ( self ) : <EOL> self . mtime = getUtime ( ) <EOL> def _newCommandMessage ( self , commandName , ** commandArgs ) : <EOL> commandId = newUUID ( ) <EOL> self . sentCommands . add ( commandId ) <EOL> commandArgs [ '<STR_LIT>' ] = commandId <EOL> commandArgs [ '<STR_LIT>' ] = self . collectionId <EOL> commandArgs [ '<STR_LIT>' ] = commandName <EOL> msg = lcmdrc . affordance_collection_t ( ) <EOL> msg . name = numpyjsoncoder . encode ( commandArgs ) <EOL> msg . utime = getUtime ( ) <EOL> return msg <EOL> def _onCommandMessage ( self , msg ) : <EOL> data = numpyjsoncoder . decode ( msg . name ) <EOL> commandId = data [ '<STR_LIT>' ] <EOL> if commandId in self . sentCommands : <EOL> self . sentCommands . remove ( commandId ) <EOL> return <EOL> command = data [ '<STR_LIT>' ] <EOL> if command == '<STR_LIT>' : <EOL> desc = data [ '<STR_LIT:description>' ] <EOL> self . updateDescription ( desc , publish = False ) <EOL> elif command == '<STR_LIT>' : <EOL> self . removeDescription ( data [ '<STR_LIT>' ] , publish = False ) <EOL> elif command == '<STR_LIT>' : <EOL> self . sendEchoResponse ( data [ '<STR_LIT>' ] ) <EOL> elif command == '<STR_LIT>' : <EOL> self . handleEchoResponse ( data ) </s>
