<s> """<STR_LIT>""" <EOL> import importlib <EOL> import json <EOL> import os <EOL> from container import Container <EOL> from kraken . core . kraken_system import KrakenSystem <EOL> from kraken . core . profiler import Profiler <EOL> from kraken . core . objects . layer import Layer <EOL> from kraken . helpers . utility_methods import prepareToSave , prepareToLoad <EOL> class Rig ( Container ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name = '<STR_LIT>' ) : <EOL> super ( Rig , self ) . __init__ ( name ) <EOL> self . _metaData = { } <EOL> def writeRigDefinitionFile ( self , filepath ) : <EOL> """<STR_LIT>""" <EOL> Profiler . getInstance ( ) . push ( "<STR_LIT>" + filepath ) <EOL> jsonData = self . getData ( ) <EOL> pureJSON = prepareToSave ( jsonData ) <EOL> with open ( filepath , '<STR_LIT:w>' ) as rigFile : <EOL> rigFile . write ( json . dumps ( pureJSON , indent = <NUM_LIT:2> ) ) <EOL> Profiler . getInstance ( ) . pop ( ) <EOL> def loadRigDefinitionFile ( self , filepath ) : <EOL> """<STR_LIT>""" <EOL> Profiler . getInstance ( ) . push ( "<STR_LIT>" + filepath ) <EOL> if not os . path . exists ( filepath ) : <EOL> raise Exception ( "<STR_LIT>" + filepath ) <EOL> with open ( filepath ) as rigFile : <EOL> jsonData = json . load ( rigFile ) <EOL> jsonData = prepareToLoad ( jsonData ) <EOL> self . loadRigDefinition ( jsonData ) <EOL> Profiler . getInstance ( ) . pop ( ) <EOL> def _loadComponents ( self , componentsJson ) : <EOL> """<STR_LIT>""" <EOL> Profiler . getInstance ( ) . push ( "<STR_LIT>" ) <EOL> krakenSystem = KrakenSystem . getInstance ( ) <EOL> for componentData in componentsJson : <EOL> modulePath = '<STR_LIT:.>' . join ( componentData [ '<STR_LIT:class>' ] . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] ) <EOL> if modulePath is not "<STR_LIT>" : <EOL> importlib . import_module ( modulePath ) <EOL> componentClass = krakenSystem . getComponentClass ( componentData [ '<STR_LIT:class>' ] ) <EOL> if '<STR_LIT:name>' in componentData : <EOL> component = componentClass ( name = componentData [ '<STR_LIT:name>' ] , parent = self ) <EOL> else : <EOL> component = componentClass ( parent = self ) <EOL> component . loadData ( componentData ) <EOL> Profiler . getInstance ( ) . pop ( ) <EOL> def _makeConnections ( self , connectionsJson ) : <EOL> """<STR_LIT>""" <EOL> Profiler . getInstance ( ) . push ( "<STR_LIT>" ) <EOL> for connectionData in connectionsJson : <EOL> sourceComponentDecoratedName , outputName = connectionData [ '<STR_LIT:source>' ] . split ( '<STR_LIT:.>' ) <EOL> targetComponentDecoratedName , inputName = connectionData [ '<STR_LIT:target>' ] . split ( '<STR_LIT:.>' ) <EOL> connectionFailure = False <EOL> sourceComponent = self . getChildByDecoratedName ( sourceComponentDecoratedName ) <EOL> if sourceComponent is None : <EOL> print ( "<STR_LIT>" + connectionData [ '<STR_LIT:source>' ] + "<STR_LIT>" + connectionData [ '<STR_LIT:target>' ] + "<STR_LIT>" + sourceComponentDecoratedName ) <EOL> connectionFailure = True <EOL> targetComponent = self . getChildByDecoratedName ( targetComponentDecoratedName ) <EOL> if targetComponent is None : <EOL> print ( "<STR_LIT>" + connectionData [ '<STR_LIT:source>' ] + "<STR_LIT>" + connectionData [ '<STR_LIT:target>' ] + "<STR_LIT>" + targetComponentDecoratedName ) <EOL> connectionFailure = True <EOL> outputPort = sourceComponent . getOutputByName ( outputName ) <EOL> if outputPort is None : <EOL> print ( "<STR_LIT>" + connectionData [ '<STR_LIT:source>' ] + "<STR_LIT>" + connectionData [ '<STR_LIT:target>' ] + "<STR_LIT>" + outputName + "<STR_LIT>" + sourceComponent . getPath ( ) ) <EOL> connectionFailure = True <EOL> inputPort = targetComponent . getInputByName ( inputName ) <EOL> if inputPort is None : <EOL> print ( "<STR_LIT>" + connectionData [ '<STR_LIT:source>' ] + "<STR_LIT>" + connectionData [ '<STR_LIT:target>' ] + "<STR_LIT>" + inputName + "<STR_LIT>" + targetComponent . getPath ( ) ) <EOL> connectionFailure = True <EOL> if connectionFailure is False : <EOL> inputPort . setConnection ( outputPort ) <EOL> inputPort . setIndex ( connectionData . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> Profiler . getInstance ( ) . pop ( ) <EOL> def loadRigDefinition ( self , jsonData ) : <EOL> """<STR_LIT>""" <EOL> Profiler . getInstance ( ) . push ( "<STR_LIT>" + self . getName ( ) ) <EOL> krakenSystem = KrakenSystem . getInstance ( ) <EOL> if '<STR_LIT:name>' in jsonData : <EOL> self . setName ( jsonData [ '<STR_LIT:name>' ] ) <EOL> if '<STR_LIT>' in jsonData : <EOL> self . _loadComponents ( jsonData [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in jsonData : <EOL> self . _makeConnections ( jsonData [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in jsonData : <EOL> for k , v in jsonData [ '<STR_LIT>' ] . iteritems ( ) : <EOL> self . setMetaData ( k , v ) <EOL> Profiler . getInstance ( ) . pop ( ) <EOL> def writeGuideDefinitionFile ( self , filepath ) : <EOL> """<STR_LIT>""" <EOL> Profiler . getInstance ( ) . push ( "<STR_LIT>" + filepath ) <EOL> guideData = self . getRigBuildData ( ) <EOL> pureJSON = prepareToSave ( guideData ) <EOL> with open ( filepath , '<STR_LIT:w>' ) as rigDef : <EOL> rigDef . write ( json . dumps ( pureJSON , indent = <NUM_LIT:2> ) ) <EOL> Profiler . getInstance ( ) . pop ( ) <EOL> def getData ( self ) : <EOL> """<STR_LIT>""" <EOL> guideData = { <EOL> '<STR_LIT:name>' : self . getName ( ) <EOL> } <EOL> componentsJson = [ ] <EOL> guideComponents = self . getChildrenByType ( '<STR_LIT>' ) <EOL> for component in guideComponents : <EOL> componentsJson . append ( component . saveData ( ) ) <EOL> guideData [ '<STR_LIT>' ] = componentsJson <EOL> connectionsJson = [ ] <EOL> for component in guideComponents : <EOL> for i in range ( component . getNumInputs ( ) ) : <EOL> componentInput = component . getInputByIndex ( i ) <EOL> if componentInput . isConnected ( ) : <EOL> componentOutput = componentInput . getConnection ( ) <EOL> connectionJson = { <EOL> '<STR_LIT:source>' : componentOutput . getParent ( ) . getDecoratedName ( ) + '<STR_LIT:.>' + componentOutput . getName ( ) , <EOL> '<STR_LIT:target>' : component . getDecoratedName ( ) + '<STR_LIT:.>' + componentInput . getName ( ) , <EOL> '<STR_LIT>' : componentInput . getIndex ( ) <EOL> } <EOL> connectionsJson . append ( connectionJson ) <EOL> guideData [ '<STR_LIT>' ] = connectionsJson <EOL> guideData [ '<STR_LIT>' ] = self . _metaData <EOL> return guideData <EOL> def getRigBuildData ( self ) : <EOL> """<STR_LIT>""" <EOL> guideData = { <EOL> '<STR_LIT:name>' : self . getName ( ) <EOL> } <EOL> componentsJson = [ ] <EOL> guideComponents = self . getChildrenByType ( '<STR_LIT>' ) <EOL> for component in guideComponents : <EOL> componentsJson . append ( component . getRigBuildData ( ) ) <EOL> guideData [ '<STR_LIT>' ] = componentsJson <EOL> connectionsJson = [ ] <EOL> for component in guideComponents : <EOL> for i in xrange ( component . getNumInputs ( ) ) : <EOL> componentInput = component . getInputByIndex ( i ) <EOL> if componentInput . isConnected ( ) : <EOL> componentOutput = componentInput . getConnection ( ) <EOL> connectionJson = { <EOL> '<STR_LIT:source>' : componentOutput . getParent ( ) . getDecoratedName ( ) + '<STR_LIT:.>' + componentOutput . getName ( ) , <EOL> '<STR_LIT:target>' : component . getDecoratedName ( ) + '<STR_LIT:.>' + componentInput . getName ( ) , <EOL> '<STR_LIT>' : componentInput . getIndex ( ) <EOL> } <EOL> connectionsJson . append ( connectionJson ) <EOL> guideData [ '<STR_LIT>' ] = connectionsJson <EOL> return guideData <EOL> def getMetaData ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _metaData <EOL> def setMetaData ( self , name , data ) : <EOL> """<STR_LIT>""" <EOL> self . _metaData [ name ] = data <EOL> return True </s>
