def getResource(identifier, pkgname=__name__): pkgpath = os.path.dirname(sys.modules[pkgname].__file__) path = os.path.join(pkgpath, identifier) return file(os.path.normpath(path), mode='rb') When a __loader__ is present on the module given by __name__, it will defer getResource to its get_data implementation and return it as a file-like object (such as StringIO). """ __all__ = ['getResource'] import sys import os from pygame.compat import get_BytesIO BytesIO = get_BytesIO() try: from pkg_resources import resource_stream, resource_exists except ImportError: def resource_exists(package_or_requirement, resource_name): return False def resource_stream(package_of_requirement, resource_name): raise NotImplementedError def getResource(identifier, pkgname=__name__): """ Acquire a readable object for a given package name and identifier. An IOError will be raised if the resource can not be found. For example:
pkgpath = os.path.dirname(sys.modules[pkgname].__file__) path = os.path.join(pkgpath, identifier) return file(os.path.normpath(path), mode='rb') When a __loader__ is present on the module given by __name__, it will defer getResource to its get_data implementation and return it as a file-like object (such as StringIO). """ __all__ = ['getResource'] import os import sys from pygame.compat import get_BytesIO BytesIO = get_BytesIO() try: from pkg_resources import resource_stream, resource_exists except ImportError: def resource_exists(package_or_requirement, resource_name): return False def resource_stream(package_of_requirement, resource_name): raise NotImplementedError def getResource(identifier, pkgname=__name__): """ Acquire a readable object for a given package name and identifier.
def test_get_BytesIO(self): BytesIO = compat.get_BytesIO() b1 = compat.as_bytes("\x00\xffabc") b2 = BytesIO(b1).read() self.failUnless(isinstance(b2, compat.bytes_)) self.failUnlessEqual(b2, b1)
def test_get_BytesIO(self): BytesIO = compat.get_BytesIO() b1 = compat.as_bytes("\x00\xffabc") b2 = BytesIO(b1).read() self.assertIsInstance(b2, compat.bytes_) self.assertEqual(b2, b1)