toxygen_wrapper/wrapper/libtox.py

88 lines
2.8 KiB
Python
Raw Normal View History

2022-09-24 06:00:32 +02:00
# -*- mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*-
import os
import sys
from ctypes import CDLL
2022-10-07 06:47:34 +02:00
# You need a libs directory beside this directory
2022-09-24 06:00:32 +02:00
# and you need to link your libtoxcore.so and libtoxav.so
# and libtoxencryptsave.so into ../libs/
# Link all 3 to libtoxcore.so if you have only libtoxcore.so
try:
import utils.util as util
sLIBS_DIR = util.get_libs_directory()
except ImportError:
sLIBS_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)),
'libs')
2022-10-07 06:47:34 +02:00
2022-10-28 04:33:36 +02:00
# environment variable TOXCORE_LIBS overrides
d = os.environ.get('TOXCORE_LIBS', '')
if d and os.path.exists(d):
sLIBS_DIR = d
2022-11-23 21:02:29 +01:00
if os.environ.get('DEBUG', ''):
2022-10-28 04:33:36 +02:00
print ('DBUG: Setting TOXCORE_LIBS to ' +d)
del d
2022-09-24 06:00:32 +02:00
class LibToxCore:
def __init__(self):
platform = sys.platform
if platform == 'win32':
libtoxcore = 'libtox.dll'
elif platform == 'darwin':
libtoxcore = 'libtoxcore.dylib'
else:
libtoxcore = 'libtoxcore.so'
# libtoxcore and libsodium may be installed in your os
# give libs/ precedence
libFile = os.path.join(sLIBS_DIR, libtoxcore)
if os.path.isfile(libFile):
self._libtoxcore = CDLL(libFile)
else:
self._libtoxcore = CDLL(libtoxcore)
def __getattr__(self, item):
return self._libtoxcore.__getattr__(item)
class LibToxAV:
def __init__(self):
2022-10-07 06:47:34 +02:00
platform = sys.platform
if platform == 'win32':
2022-09-24 06:00:32 +02:00
# on Windows av api is in libtox.dll
self._libtoxav = CDLL(os.path.join(sLIBS_DIR, 'libtox.dll'))
2022-10-07 06:47:34 +02:00
elif platform == 'darwin':
2022-09-24 06:00:32 +02:00
self._libtoxav = CDLL('libtoxcore.dylib')
else:
libFile = os.path.join(sLIBS_DIR, 'libtoxav.so')
if os.path.isfile(libFile):
self._libtoxav = CDLL(libFile)
else:
self._libtoxav = CDLL('libtoxav.so')
def __getattr__(self, item):
return self._libtoxav.__getattr__(item)
# figure out how to see if we have a combined library
class LibToxEncryptSave:
def __init__(self):
platform = sys.platform
if platform == 'win32':
# on Windows profile encryption api is in libtox.dll
self._lib_tox_encrypt_save = CDLL(os.path.join(sLIBS_DIR, 'libtox.dll'))
elif platform == 'darwin':
self._lib_tox_encrypt_save = CDLL('libtoxcore.dylib')
else:
libFile = os.path.join(sLIBS_DIR, 'libtoxencryptsave.so')
if os.path.isfile(libFile):
self._lib_tox_encrypt_save = CDLL(libFile)
else:
self._lib_tox_encrypt_save = CDLL('libtoxencryptsave.so')
def __getattr__(self, item):
return self._lib_tox_encrypt_save.__getattr__(item)
# figure out how to see if we have a combined library