<s> import re <EOL> import Gaffer <EOL> import GafferUI <EOL> class Layouts ( object ) : <EOL> def __init__ ( self ) : <EOL> self . __namedLayouts = { } <EOL> self . __registeredEditors = [ ] <EOL> @ classmethod <EOL> def acquire ( cls , applicationOrApplicationRoot ) : <EOL> if isinstance ( applicationOrApplicationRoot , Gaffer . Application ) : <EOL> applicationRoot = applicationOrApplicationRoot . root ( ) <EOL> else : <EOL> assert ( isinstance ( applicationOrApplicationRoot , Gaffer . ApplicationRoot ) ) <EOL> applicationRoot = applicationOrApplicationRoot <EOL> try : <EOL> return applicationRoot . __layouts <EOL> except AttributeError : <EOL> pass <EOL> applicationRoot . __layouts = Layouts ( ) <EOL> return applicationRoot . __layouts <EOL> def add ( self , name , editor ) : <EOL> if not isinstance ( editor , basestring ) : <EOL> editor = repr ( editor ) <EOL> self . __namedLayouts [ name ] = editor <EOL> def remove ( self , name ) : <EOL> del self . __namedLayouts [ name ] <EOL> def names ( self ) : <EOL> return self . __namedLayouts . keys ( ) <EOL> def create ( self , name , scriptNode ) : <EOL> layout = self . __namedLayouts [ name ] <EOL> contextDict = { "<STR_LIT>" : scriptNode } <EOL> imported = set ( ) <EOL> classNameRegex = re . compile ( "<STR_LIT>" ) <EOL> for className in classNameRegex . findall ( layout ) : <EOL> moduleName = className . partition ( "<STR_LIT:.>" ) [ <NUM_LIT:0> ] <EOL> if moduleName not in imported : <EOL> exec ( "<STR_LIT>" % moduleName , contextDict , contextDict ) <EOL> imported . add ( moduleName ) <EOL> return eval ( layout , contextDict , contextDict ) <EOL> def save ( self , fileObject , nameRegex = None ) : <EOL> namesToWrite = [ ] <EOL> for name in self . names ( ) : <EOL> if nameRegex . match ( name ) or nameRegex is None : <EOL> namesToWrite . append ( name ) <EOL> fileObject . write ( "<STR_LIT>" ) <EOL> fileObject . write ( "<STR_LIT>" ) <EOL> for name in namesToWrite : <EOL> fileObject . write ( "<STR_LIT>" % ( name , self . __namedLayouts [ name ] ) ) <EOL> fileObject . write ( "<STR_LIT>" ) <EOL> def registerEditor ( self , editorName ) : <EOL> if editorName not in self . __registeredEditors : <EOL> self . __registeredEditors . append ( editorName ) <EOL> def deregisterEditor ( self , editorName ) : <EOL> self . __registeredEditors . remove ( editorName ) <EOL> def registeredEditors ( self ) : <EOL> return self . __registeredEditors </s>
