[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Pythonmac-SIG] Re: [pygame] python + pygame on OSX



On Feb 23, 2004, at 4:02 AM, Jack Jansen wrote:

On 23-feb-04, at 0:24, Bob Ippolito wrote:
I saw a system for including package resources (name eludes me at the moment) by converting the binaries to python code.. so that the resources ended up being Python strings(!!) on import.. I don't think this is a general solution, and it probably has the capability to increases memory consumption and load time considerably, but it *would* work as a hack ;)
Brrrr :-)

In any case, would anyone like to implement this? I am probably too busy to get around to it for a few weeks (and I honestly don't really want to write it), but I would be probably integrate it into bundlebuilder2 sooner than that if it sprung into existence.
Implementation is going to be peanuts, design is what we want.
We should investigate the paradigms people use nowadays to get at their datafiles, and come up with something that is as close as possible to that.

How does pygame find its datafiles?
pygame actually does it in a C extension when it's looking for its font file.. so it's not really typical usage. I will change pygame accordingly when method exists.

In most cases, I've seen os.path.join(os.path.dirname(__file__), "resourcefile") -- maybe with an app-specific fallback if they have baked in support for py2exe.

The more I think about it, the more I just want to recommend that the implementation use PyProtocols or something so that it's flexible.

<this is untested>
# bundlebuilder bootstrap
from protocols import protocolForURI, Adapter, advise, NO_ADAPTER_NEEDED
from Foundation import NSBundle
#
# use a URI protocol so that
# (a) an implementation doesn't have to exist
# (b) the user doesn't have to import it
#
IReadableFileLike = protocolForURI("org.python.packaging.readablefilelike")
declareAdapter(NO_ADAPTER_NEEDED, provides=[IReadableFileLike], forTypes=[file])

def bundlebuilderFile(obj, protocol):
"""Loads a (module, filename) 2-tuple from Contents/Resources/ModuleData/%(moduleName)s/%(fileName)s"""
module, filename = obj
modulename = getattr(module, '__name__', module)
if not isinstance(modulename, basestring):
return None
filepath = os.path.join(NSBundle.mainBundle().resourcePath(), 'ModuleData', modulename, filename)
if not os.path.exists(filepath):
return None
return file(filepath)

declateAdapterForType(IReadableFileLike, bundlebuilderFile, tuple)

# user code
import sys, os
from protocols import protocolForURI, adapt
IReadableFileLike = protocolForURI("org.python.packaging.readablefilelike")
# sensible default method
def defaultFactory((module, filename)):
basedir = os.path.dirname(sys.modules.get(module, module).__file__)
return file(os.path.join(basedir, filename))

myResourceFileObj = adapt((__module__, "resourcefilename"), IReadableFileLike, factory=defaultFactory)


-bob