_core.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #*****************************************************************************
  2. # Copyright 2004-2008 Steve Menard
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. #*****************************************************************************
  17. import sys
  18. import _jpype
  19. from . import _jclass
  20. from . import _jarray
  21. from . import _jwrapper
  22. from . import _jproxy
  23. from . import _jexception
  24. from . import _jcollection
  25. from . import _jobject
  26. from . import _properties
  27. from . import nio
  28. from . import reflect
  29. from . import _refdaemon
  30. _usePythonThreadForDaemon = False
  31. def setUsePythonThreadForDeamon(v):
  32. global _usePythonThreadForDaemon
  33. _usePythonThreadForDaemon = v
  34. def isJVMStarted() :
  35. return _jpype.isStarted()
  36. def startJVM(jvm, *args):
  37. """
  38. Starts a Java Virtual Machine
  39. :param jvm: Path to the jvm library file (libjvm.so, jvm.dll, ...)
  40. :param args: Arguments to give to the JVM
  41. """
  42. _jpype.startup(jvm, tuple(args), True)
  43. _jclass._initialize()
  44. _jarray._initialize()
  45. _jwrapper._initialize()
  46. _jproxy._initialize()
  47. _jexception._initialize()
  48. _jcollection._initialize()
  49. _jobject._initialize()
  50. _properties._initialize()
  51. nio._initialize()
  52. reflect._initialize()
  53. # start the reference daemon thread
  54. if _usePythonThreadForDaemon:
  55. _refdaemon.startPython()
  56. else:
  57. _refdaemon.startJava()
  58. def attachToJVM(jvm):
  59. _jpype.attach(jvm)
  60. _jclass._initialize()
  61. _jarray._initialize()
  62. _jwrapper._initialize()
  63. _jproxy._initialize()
  64. _jexception._initialize()
  65. _jcollection._initialize()
  66. _jobject._initialize()
  67. _properties._initialize()
  68. def shutdownJVM():
  69. _refdaemon.stop()
  70. _jpype.shutdown()
  71. def isThreadAttachedToJVM():
  72. return _jpype.isThreadAttachedToJVM()
  73. def attachThreadToJVM():
  74. _jpype.attachThreadToJVM()
  75. def detachThreadFromJVM():
  76. _jpype.detachThreadFromJVM()
  77. def get_default_jvm_path():
  78. """
  79. Retrieves the path to the default or first found JVM library
  80. :return: The path to the JVM shared library file
  81. :raise ValueError: No JVM library found
  82. """
  83. if sys.platform in ("win32", "cygwin"):
  84. # Windows or Cygwin
  85. from ._windows import WindowsJVMFinder
  86. finder = WindowsJVMFinder()
  87. elif sys.platform == "darwin":
  88. # Mac OS X
  89. from ._darwin import DarwinJVMFinder
  90. finder = DarwinJVMFinder()
  91. else:
  92. # Use the Linux way for other systems
  93. from ._linux import LinuxJVMFinder
  94. finder = LinuxJVMFinder()
  95. return finder.get_jvm_path()
  96. # Naming compatibility
  97. getDefaultJVMPath = get_default_jvm_path
  98. class ConversionConfigClass(object):
  99. def __init__(self):
  100. self._convertString = 1
  101. def _getConvertString(self):
  102. return self._convertString
  103. def _setConvertString(self, value):
  104. if value:
  105. self._convertString = 1
  106. else:
  107. self._convertString = 0
  108. _jpype.setConvertStringObjects(self._convertString)
  109. string = property(_getConvertString, _setConvertString, None)
  110. ConversionConfig = ConversionConfigClass()