Source code for fmeTestSupport.MlabContext

# Copyright (c) Fraunhofer MEVIS, Germany. All rights reserved.
# **InsertLicense** code

from typing import Any, ContextManager, Dict, Optional
from .Context import Context
from contextlib import contextmanager
from mevis import MLAB
from TestSupport import Base

ModuleContext = "ModuleContext" # just for type hints. NewType() does not work well with our sphinx version.

[docs]@contextmanager def ScriptVariable( context:ModuleContext, variableName:str, value:Any, unsetOnError=True ) -> ContextManager[None]: """Context manager to temporarily set a script variable (via MLAB.setScriptVariable()). :param context: The module context. :param variableName: Name of the variable :param unsetOnError: If set, the variable is NOT unset if an exception occurred within the context manager body. :yields: None """ def ___do(): context.setScriptVariable( variableName, value ) def ___undo( _ ): context.deleteScriptVariable( variableName ) # with Context( ___do, ___undo, undoOnError=unsetOnError ): yield
[docs]@contextmanager def MlabVariable( variableName:str, value:Any, unsetOnError=True ) -> ContextManager[None]: """Context manager to temporarily set an MLAB (MDL) variable (via MLAB.setVariable()). :param context: The module context. :param variableName: Name of the variable :param unsetOnError: If set, the variable is NOT unset if an exception occurred within the context manager body. :yields: None """ def ___do(): MLAB.setVariable( variableName, value ) def ___undo( _ ): MLAB.unsetVariable( variableName ) # with Context( ___do, ___undo, undoOnError=unsetOnError ): yield
[docs]@contextmanager def MlabVariables( varDict:Dict[str, Any], unsetOnError=True ) -> ContextManager[None]: """Context manager to temporarily set multiple MLAB (MDL) variables (via MLAB.setVariable()). :param context: The module context. :param variableName: Name of the variable :param unsetOnError: If set, the variable is NOT unset if an exception occurred within the context manager body. :yields: None """ def ___do(): for variableName, value in varDict.items(): MLAB.setVariable( variableName, value ) def ___undo( _ ): for variableName in varDict: MLAB.unsetVariable( variableName ) # with Context( ___do, ___undo, undoOnError=unsetOnError ): yield
[docs]@contextmanager def FieldValues( moduleContext, fieldDict:Dict[str, Any], unsetOnError=True ) -> ContextManager[None]: """ Context manager to temporarily set multiple MeVisLab fields to different values. Note that 'updateValue' is used to set/unset the value, so notifications will only arise if a value changes. :param moduleContext: The module context. :param fieldDict: Dictionary with field names and values. The field names must be relative to the passed module context. :param unsetOnError: If set, the variable is NOT unset if an exception occurred within the context manager body. :yields: None """ def ___do( oldValues ): for variableName, value in fieldDict.items(): oldValues[ variableName ] = moduleContext.field( variableName ).value moduleContext.field( variableName ).updateValue( value ) def ___undo( _, oldValues ): for variableName in fieldDict: moduleContext.field( variableName ).updateValue( oldValues[variableName] ) # with Context( ___do, ___undo, undoOnError=unsetOnError, memory={} ): yield
[docs]@contextmanager def NewModule( moduleType:str, instanceName:Optional[str]=None, context:Optional[ModuleContext]=None, addAsRemoteModule=False, alsoRemoveOnError=True ) -> ContextManager[ModuleContext]: """Context manager to temporarily add a module to the network. :param moduleType: The module type :param instanceName: Optional instance name for the created module :param context: Optional context for the created module. If omitted/None, Base.getTestCaseContext() is assumed. :param addAsRemoteModule: Id true, the module is added as a remote module (instead of in the same process) :param alsoRemoveOnError: If set, the module is NOT removed if an exception occurred in the context manager body. :yields: The created module """ def ___addModule(): assert not context.hasModule( instanceName ) if addAsRemoteModule: m = context.addRemoteModule( moduleType ) else: m = context.addModule( moduleType ) m.field( "instanceName" ).value = instanceName return m # def ___removeModule( m ): m.remove() # if instanceName is None: instanceName = moduleType if context is None: context = Base.getTestCaseContext( ) with Context( ___addModule, ___removeModule, undoOnError=alsoRemoveOnError ) as m: yield m