Skip to content

Commit

Permalink
Use importlib instead of pkg_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
cmbruns committed Sep 2, 2021
1 parent 418f1d0 commit ac4847a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@
Operating System :: Microsoft :: Windows :: Windows 10
Operating System :: POSIX :: Linux
Operating System :: MacOS :: MacOS X
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: Implementation :: CPython
Topic :: Multimedia :: Graphics :: 3D Rendering
Topic :: Scientific/Engineering :: Visualization
Development Status :: 4 - Beta
Development Status :: 5 - Production/Stable
""".splitlines(),
install_requires=[],
extras_require={
Expand Down
18 changes: 11 additions & 7 deletions src/openvr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
# from https://github.com/cmbruns/pyopenvr
# based on OpenVR C++ API at https://github.com/ValveSoftware/openvr

import os
import pkg_resources
import platform
import atexit
from contextlib import ExitStack
import ctypes
from ctypes import *
import importlib.resources
import platform

from .version import __version__
import openvr.error_code
Expand All @@ -32,7 +33,7 @@ class Pack4Structure(Structure):
# Detect platform
if sizeof(c_void_p) == 4:
if platform.system() == 'Windows':
_openvr_lib_name = "libopenvr_api_32"
_openvr_lib_name = "libopenvr_api_32.dll"
elif platform.system() == 'Linux':
_openvr_lib_name = "libopenvr_api_32.so"
elif platform.system() == 'Darwin':
Expand All @@ -41,7 +42,7 @@ class Pack4Structure(Structure):
raise ValueError("Libraries not available for this platform: " + platform.system())
else:
if platform.system() == 'Windows':
_openvr_lib_name = "libopenvr_api_64"
_openvr_lib_name = "libopenvr_api_64.dll"
elif platform.system() == 'Linux':
_openvr_lib_name = "libopenvr_api_64.so"
elif platform.system() == 'Darwin':
Expand All @@ -50,8 +51,11 @@ class Pack4Structure(Structure):
raise ValueError("Libraries not available for this platform: " + platform.system())

# Load library
_openvr_lib_path = pkg_resources.resource_filename("openvr", _openvr_lib_name)
_openvr = ctypes.cdll.LoadLibrary(_openvr_lib_path)
_lib_manager = ExitStack()
atexit.register(_lib_manager.close)
_lib_ref = importlib.resources.files(openvr) / _openvr_lib_name
_openvr_lib_path = _lib_manager.enter_context(importlib.resources.as_file(_lib_ref))
_openvr = ctypes.cdll.LoadLibrary(str(_openvr_lib_path))

# Function pointer table calling convention
if platform.system() == 'Windows':
Expand Down

0 comments on commit ac4847a

Please sign in to comment.