1
# Copyright (c) 2006-2008 Alex Holkner
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions are met:
7
# * Redistributions of source code must retain the above copyright
8
# notice, this list of conditions and the following disclaimer.
9
# * Redistributions in binary form must reproduce the above copyright
10
# notice, this list of conditions and the following disclaimer in
11
# the documentation and/or other materials provided with the
13
# * Neither the name of pyglet nor the names of its
14
# contributors may be used to endorse or promote products
15
# derived from this software without specific prior written
18
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
# POSSIBILITY OF SUCH DAMAGE.
42
class LibraryLoader(object):
43
def load_library(self, *names, **kwargs):
44
'''Find and load a library.
46
More than one name can be specified, they will be tried in order.
47
Platform-specific library names (given as kwargs) are tried first.
49
Raises ImportError if library is not found.
51
if 'framework' in kwargs and self.platform == 'darwin':
52
return self.load_framework(kwargs['framework'])
54
platform_names = kwargs.get(self.platform, [])
55
if type(platform_names) in (str, unicode):
56
platform_names = [platform_names]
57
elif type(platform_names) is tuple:
58
platform_names = list(platform_names)
60
if self.platform == 'linux2':
61
platform_names.extend(['lib%s.so' % n for n in names])
63
platform_names.extend(names)
64
for name in platform_names:
66
lib = ctypes.cdll.LoadLibrary(name)
68
print "Loaded library :", name
70
# lib = _TraceLibrary(lib)
73
path = self.find_library(name)
76
lib = ctypes.cdll.LoadLibrary(path)
80
lib = _TraceLibrary(lib)
84
raise ImportError('Library "%s" not found.' % names[0])
86
find_library = lambda self, name: ctypes.util.find_library(name)
88
platform = sys.platform
89
if platform == 'cygwin':
92
def load_framework(self, path):
93
raise RuntimeError("Can't load framework on this platform.")