v2_internals.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. r"""
  2. INTERNAL MODULE, NOT FOR PUBLIC CONSUMPTION.
  3. Used by vtkCPPythonScriptV2Pipeline and may change without notice.
  4. """
  5. from . import log_level
  6. from .. import log, print_warning
  7. from ..modules.vtkPVPythonCatalyst import vtkCPPythonScriptV2Helper
  8. def register_module(path):
  9. """register a file/directory as an importable module.
  10. Returns the name to use to import the module, on success, otherwise `None`.
  11. """
  12. from .detail import RegisterPackageFromZip
  13. from .detail import RegisterPackageFromDir
  14. from .detail import RegisterModuleFromFile
  15. from paraview.vtk.vtkParallelCore import vtkPSystemTools
  16. from . import importers
  17. # plug into Python import machinery to help import pipeline modules
  18. # seamlessly.
  19. importers.install_pathfinder()
  20. if vtkPSystemTools.FileIsDirectory(path):
  21. return RegisterPackageFromDir(path)
  22. elif path.lower().endswith(".zip"):
  23. return RegisterPackageFromZip(path)
  24. else:
  25. return RegisterModuleFromFile(path)
  26. class CatalystV1Information:
  27. """
  28. Provides information to the current `catalyst_execute` call.
  29. """
  30. def __init__(self, dataDescription):
  31. self._dataDescription = dataDescription
  32. @property
  33. def time(self):
  34. """returns the current simulation time"""
  35. return self._dataDescription.GetTime()
  36. @property
  37. def timestep(self):
  38. """returns the current simulation cycle or timestep index"""
  39. return self._dataDescription.GetTimeStep()
  40. @property
  41. def cycle(self):
  42. """returns the current simulation cycle or timestep index"""
  43. return self._dataDescription.GetTimeStep()
  44. @property
  45. def dataDescription(self):
  46. """avoid using this unless absolutely sure what you're doing"""
  47. return self._dataDescription
  48. class CatalystV2Information:
  49. def __init__(self):
  50. from paraview.modules.vtkPVInSitu import vtkInSituInitializationHelper
  51. self.helper = vtkInSituInitializationHelper
  52. @property
  53. def time(self):
  54. """returns the current simulation time"""
  55. return self.helper.GetTime()
  56. @property
  57. def timestep(self):
  58. """returns the current simulation cycle or timestep index"""
  59. return self.helper.GetTimeStep()
  60. @property
  61. def cycle(self):
  62. """returns the current simulation cycle or timestep index"""
  63. return self.helper.GetTimeStep()
  64. def import_and_validate(modulename):
  65. import importlib
  66. m = importlib.import_module(modulename)
  67. _validate_and_initialize(m)
  68. return m
  69. def do_request_data_description(module, dataDescription):
  70. log(log_level(), "called do_request_data_description %r", module)
  71. # call 'RequestDataDescription' on module, if exists
  72. if hasattr(module, "RequestDataDescription"):
  73. log(log_level(), "calling '%s.%s'", module.__name__, "RequestDataDescription")
  74. module.RequestDataDescription(dataDescription)
  75. return True # indicates RequestDataDescription was overridden in the script.
  76. return False
  77. def do_catalyst_initialize(module):
  78. log(log_level(), "called do_catalyst_initialize %r", module)
  79. # call 'catalyst_initialize' on module if exits
  80. if hasattr(module, "catalyst_initialize"):
  81. log(log_level(), "calling '%s.%s'", module.__name__, "catalyst_initialize")
  82. module.catalyst_initialize()
  83. def do_catalyst_finalize(module):
  84. log(log_level(), "called do_catalyst_finalize %r", module)
  85. # call 'catalyst_finalize' on module if exits
  86. if hasattr(module, "catalyst_finalize"):
  87. log(log_level(), "calling '%s.%s'", module.__name__, "catalyst_finalize")
  88. module.catalyst_finalize()
  89. def do_catalyst_execute(module):
  90. log(log_level(), "called do_catalyst_execute %r", module)
  91. # call 'catalyst_execute' on module, if exists
  92. if hasattr(module, "catalyst_execute"):
  93. log(log_level(), "calling '%s.%s'", module.__name__, "catalyst_execute")
  94. dataDescription = _get_active_data_description()
  95. if dataDescription:
  96. info = CatalystV1Information(dataDescription)
  97. else:
  98. info = CatalystV2Information()
  99. module.catalyst_execute(info)
  100. return True
  101. return False
  102. def _get_active_data_description():
  103. helper = vtkCPPythonScriptV2Helper.GetActiveInstance()
  104. return helper.GetDataDescription()
  105. def _get_active_arguments():
  106. args = []
  107. helper = vtkCPPythonScriptV2Helper.GetActiveInstance()
  108. if not helper:
  109. # happens when script is executed in pvbatch, and not catalyst
  110. return args
  111. slist = helper.GetArgumentsAsStringList()
  112. for cc in range(slist.GetLength()):
  113. args.append(slist.GetString(cc))
  114. return args
  115. def _get_execute_parameters():
  116. params = []
  117. helper = vtkCPPythonScriptV2Helper.GetActiveInstance()
  118. if not helper:
  119. # happens when script is executed in pvbatch, and not catalyst
  120. return params
  121. slist = helper.GetParametersAsStringList()
  122. for cc in range(slist.GetLength()):
  123. params.append(slist.GetString(cc))
  124. return params
  125. def _get_script_filename():
  126. helper = vtkCPPythonScriptV2Helper.GetActiveInstance()
  127. if not helper:
  128. return None
  129. return helper.GetScriptFileName()
  130. def has_customized_execution(module):
  131. return hasattr(module, "catalyst_execute") or \
  132. hasattr(module, "RequestDataDescription") or \
  133. hasattr(module, "catalyst_initialize")
  134. def _validate_and_initialize(module):
  135. """Validates a module to confirm that it is a `module` that we can treat as
  136. a Catalyst script. Additionally, adds some private meta-data that this
  137. module will use during exection."""
  138. if not hasattr(module, "options"):
  139. print_warning("Module '%s' missing Catalyst 'options', will use a default options object", module.__name__)
  140. from . import Options
  141. module.options = Options()
  142. # provide the options to vtkCPPythonScriptV2Helper.
  143. helper = vtkCPPythonScriptV2Helper.GetActiveInstance()
  144. helper.SetOptions(module.options.SMProxy)