[SOLVED] Second Multifile question: Multifiles and code

Related to the previous question…

I’ve heard it mentioned before that ToonTown keeps its source code in a Panda metafile. Does anyone know how that mechanism works? It appears that simply mounting a metafile in the VFS doesn’t expose its contents in a way that allows import to automatically find the modules contained in the .py files in the metafile. Does one have to explicitly use multify at runtime to dump the .py files in to a directory somehow? Or should the VFS be supporting the ability to transparently load .py files as if they were actual files in the filesystem and I just haven’t figured out how yet? :wink:

Thank you for your help!

No, Toontown actually stores its Python files in a pyz file, not in an .mf file. Although I guess it would be possible to store Python files in an .mf file, using a similar mechanism.

The pyz file format comes from the squeezeTool utility. You should be able to find it by googling around for it.


Fredrik Lundh’s squeeze, to be exact.

You’ll have to load the .pyc from the .mf, then import it from memory. Here’s the equivalent of from MODULENAME import * (from disk, not memory, assuming it has a .pyc in the current directory):

import marshal

# read module file
module_file = open('MODULENAME.pyc','rb') # open .pyc file for binary read
module_contents = modulefile.read() # read the entire contents
module_file.close() # close the file

# unmarshal the data
marshal_data = module_contents[8:]
codeobj = marshal.loads(marshal_data)

# execute the code object, causing everything inside the module to be imported into this module's namespace (just like what happens with from -- import *)

All I know is that. Making another namespace and running the exec(codeobj) inside it would effectively work for anything else.