diff options
author | Jack Jansen <jack.jansen@cwi.nl> | 2001-08-27 21:21:07 (GMT) |
---|---|---|
committer | Jack Jansen <jack.jansen@cwi.nl> | 2001-08-27 21:21:07 (GMT) |
commit | de3226f7f9cda4e6bb4c871c39b9bb2a3c00534a (patch) | |
tree | bf9b06891f454bb5901717e7b748c6dd521e0d24 /Mac | |
parent | 8f2b13efce66d1ce66060d55f26753d0e593d19f (diff) | |
download | cpython-de3226f7f9cda4e6bb4c871c39b9bb2a3c00534a.zip cpython-de3226f7f9cda4e6bb4c871c39b9bb2a3c00534a.tar.gz cpython-de3226f7f9cda4e6bb4c871c39b9bb2a3c00534a.tar.bz2 |
Module to help other modules locate their resources. It currently knows about
resources in applets and separate OS9 style resource files, but it will
eventually also be thought the hoops to jump through on OSX/MachO.
Diffstat (limited to 'Mac')
-rw-r--r-- | Mac/Lib/macresource.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/Mac/Lib/macresource.py b/Mac/Lib/macresource.py new file mode 100644 index 0000000..1aabe12 --- /dev/null +++ b/Mac/Lib/macresource.py @@ -0,0 +1,68 @@ +"""macresource - Locate and open the resources needed for a script.""" + +from Carbon import Res +import os +import sys + +class ArgumentError(TypeError): pass +class ResourceFileNotFoundError(ImportError): pass + +def need(restype, resid, filename=None, modname=None): + """Open a resource file, if needed. restype and resid + are required parameters, and identify the resource for which to test. If it + is available we are done. If it is not available we look for a file filename + (default: modname with .rsrc appended) either in the same folder as + where modname was loaded from, or otherwise across sys.path.""" + + if modname is None and filename is None: + raise ArgumentError, "Either filename or modname argument (or both) must be given" + + if type(resid) is type(1): + try: + h = Res.GetResource(restype, resid) + except Res.Error: + pass + else: + return + else: + try: + h = Res.GetNamedResource(restype, resid) + except Res.Error: + pass + else: + return + + # Construct a filename if we don't have one + if not filename: + if '.' in modname: + filename = modname.split('.')[-1] + '.rsrc' + else: + filename = modname + '.rsrc' + + # Now create a list of folders to search + searchdirs = [] + if modname == '__main__': + # If we're main we look in the current directory + searchdirs = [os.curdir] + if sys.modules.has_key(modname): + mod = sys.modules[modname] + if hasattr(mod, '__file__'): + searchdirs = [mod.__file__] + if not searchdirs: + searchdirs = sys.path + + # And look for the file + for dir in searchdirs: + pathname = os.path.join(dir, filename) + if os.path.exists(pathname): + break + else: + raise ResourceFileNotFoundError, filename + + Res.FSpOpenResFile(pathname, 1) + + # And check that the resource exists now + if type(resid) is type(1): + h = Res.GetResource(restype, resid) + else: + h = Res.GetNamedResource(restype, resid) |