__init__.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. r"""
  2. The paraview package provides modules used to script ParaView. Generally, users
  3. should import the modules of interest directly e.g.::
  4. from paraview.simple import *
  5. However, one may want to import paraview package before importing any of the
  6. ParaView modules to force backwards compatibility to an older version::
  7. # To run scripts written for ParaView 4.0 in newer versions, you can use the
  8. # following.
  9. import paraview
  10. paraview.compatibility.major = 4
  11. paraview.compatibility.minor = 0
  12. # Now, import the modules of interest.
  13. from paraview.simple import *
  14. """
  15. from __future__ import absolute_import
  16. __version__ = '5.11.1'
  17. __version_full__ = '5.11.1'
  18. #==============================================================================
  19. #
  20. # Program: ParaView
  21. # Module: __init__.py
  22. #
  23. # Copyright (c) Kitware, Inc.
  24. # All rights reserved.
  25. # See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
  26. #
  27. # This software is distributed WITHOUT ANY WARRANTY; without even
  28. # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  29. # PURPOSE. See the above copyright notice for more information.
  30. #
  31. #==============================================================================
  32. class _version(object):
  33. def __init__(self, major, minor):
  34. if major is not None and minor is not None:
  35. self.__version = (int(major), int(minor))
  36. elif major is not None:
  37. self.__version = (int(major),)
  38. else:
  39. self.__version = tuple()
  40. @property
  41. def major(self):
  42. "major version number"
  43. try:
  44. return self.__version[0]
  45. except IndexError:
  46. return None
  47. @major.setter
  48. def major(self, value):
  49. if value is None:
  50. self.__version = tuple()
  51. elif len(self.__version) <= 1:
  52. self.__version = (int(value),)
  53. else:
  54. self.__version = (int(value), self.__version[1])
  55. @property
  56. def minor(self):
  57. "minor version number"
  58. try:
  59. return self.__version[1]
  60. except IndexError:
  61. return None
  62. @minor.setter
  63. def minor(self, value):
  64. if value is None:
  65. if len(self.__version) >= 1:
  66. self.__version = (self._version[0], )
  67. else:
  68. self.__version = tuple()
  69. elif len(self._version) >= 2:
  70. self.__version = (self.__version[0], int(value))
  71. else:
  72. self.__version = (0, int(value))
  73. @property
  74. def version(self):
  75. "version as a tuple (major, minor)"
  76. return self.__version
  77. def __bool__(self):
  78. return bool(self.__version)
  79. def GetVersion(self):
  80. """::deprecated:: 5.10
  81. Return version as a float. Will return None is no version is
  82. specified.
  83. This method is deprecated in 5.10. It's return value is not appropriate
  84. for version number comparisons and hence should no longer be used.
  85. """
  86. if self:
  87. version = float(self.minor)
  88. while version >= 1.0:
  89. version = version / 10.0
  90. version += float(self.major)
  91. return version
  92. return None
  93. def _convert(self, value):
  94. # convert value to _version
  95. if type(value) == type(self):
  96. return value
  97. elif type(value) == tuple or type(value) == list:
  98. if len(value) == 0:
  99. value = _version(None, None)
  100. elif len(value) == 1:
  101. value = _version(value[0], 0)
  102. elif len(value) >= 2:
  103. value = _version(value[0], value[1])
  104. elif type(value) == int:
  105. value = _version(value, 0)
  106. elif type(value) == float:
  107. major = int(value)
  108. minor = int(10 * value) - (10 * major)
  109. value = _version(major, minor)
  110. return value
  111. def __lt__(self, other):
  112. """This will always return False if compatibility is not being forced
  113. to a particular version."""
  114. if not self:
  115. return False
  116. # convert other to _version, if possible
  117. other = self._convert(other)
  118. if type(other) == type(self):
  119. return self.__version < other.__version
  120. else:
  121. raise TypeError("unsupport type %s", type(other))
  122. def __le__(self, other):
  123. """This will always return False if compatibility is not forced to a
  124. particular version."""
  125. if not self:
  126. return False
  127. # convert other to _version, if possible
  128. other = self._convert(other)
  129. if type(other) == type(self):
  130. return self.__version <= other.__version
  131. else:
  132. raise TypeError("unsupport type %s", type(other))
  133. def __eq__(self, other):
  134. raise RuntimeError("Equal operation not supported.")
  135. def __ne__(self, other):
  136. raise RuntimeError("NotEqual operation not supported.")
  137. def __gt__(self, other):
  138. """This will always return True if compatibility is not being forced to
  139. a particular version"""
  140. if not self:
  141. return True
  142. # convert other to _version, if possible
  143. other = self._convert(other)
  144. if type(other) == type(self):
  145. return self.__version > other.__version
  146. else:
  147. raise TypeError("unsupport type %s", type(other))
  148. def __ge__(self, other):
  149. """This will always return True if compatibility is not being forced to
  150. a particular version"""
  151. myversion = self.GetVersion()
  152. if not myversion:
  153. return True
  154. # convert other to _version, if possible
  155. other = self._convert(other)
  156. if type(other) == type(self):
  157. return self.__version >= other.__version
  158. else:
  159. raise TypeError("unsupport type %s", type(other))
  160. def __repr__(self):
  161. return str(self.__version)
  162. class compatibility:
  163. """Class used to check version number and compatibility. Users should only
  164. set the compatibility explicitly to force backwards compatibility to and
  165. older versions.
  166. """
  167. minor = None
  168. major = None
  169. @classmethod
  170. def GetVersion(cls):
  171. return _version(cls.major, cls.minor)
  172. # This is reimplemented in vtkSMCoreUtilities::SanitizeName(). Keep both
  173. # implementations consistent.
  174. def make_name_valid(name):
  175. """Make a string into a valid Python variable name."""
  176. if not name:
  177. return None
  178. import string
  179. valid_chars = "_%s%s" % (string.ascii_letters, string.digits)
  180. name = str().join([c for c in name if c in valid_chars])
  181. if name and not name[0].isalpha():
  182. name = 'a' + name
  183. return name
  184. class options:
  185. """Values set here have any effect, only when importing the paraview module
  186. in python interpretor directly i.e. not through pvpython or pvbatch. In
  187. that case, one should use command line arguments for the two
  188. executables"""
  189. """When True, act as pvbatch. Default behaviour is to act like pvpython"""
  190. batch = False
  191. """When True, acts like pvbatch --symmetric. Requires that batch is set to
  192. True to have any effect."""
  193. symmetric = False
  194. """When True, `paraview.print_debug_info()` will result in printing the
  195. debug messages to stdout. Default is False, hence all debug messages will be
  196. suppressed."""
  197. print_debug_messages = False
  198. """When True, This mean the current process is a satellite and should not try to
  199. connect or do anything else."""
  200. satelite = False
  201. class NotSupportedException(Exception):
  202. """Exception that is fired when obsolete API is used in a script."""
  203. def __init__(self, msg):
  204. self.msg = msg
  205. print_debug_info("\nDEBUG: %s\n" % msg)
  206. def __str__(self):
  207. return "%s" % (self.msg)
  208. """This variable is set whenever Python is initialized within a ParaView
  209. Qt-based application. Modules within the 'paraview' package often use this to
  210. tailor their behaviour based on whether the Python environment is embedded
  211. within an application or not."""
  212. fromGUI = False
  213. #------------------------------------------------------------------------------
  214. # this little trick is for static builds of ParaView. In such builds, if
  215. # the user imports this Python package in a non-statically linked Python
  216. # interpreter i.e. not of the of the ParaView-python executables, then we import the
  217. # static components importer module.
  218. try:
  219. from .modules import vtkRemotingCore
  220. except ImportError:
  221. import _paraview_modules_static
  222. #------------------------------------------------------------------------------
  223. def _create_logger(name="paraview"):
  224. import logging
  225. logger = logging.getLogger(name)
  226. if not hasattr(logger, "_paraview_initialized"):
  227. logger._paraview_initialized = True
  228. from paraview.detail import loghandler
  229. logger.addHandler(loghandler.VTKHandler())
  230. logger.setLevel(loghandler.get_level())
  231. return logger
  232. logger = _create_logger()
  233. print_error = logger.error
  234. print_warning = logger.warning
  235. print_debug_info = logger.debug
  236. print_debug = logger.debug
  237. print_info = logger.info
  238. log = logger.log