summaryrefslogtreecommitdiffstats
path: root/Lib/plat-mac/macresource.py
diff options
context:
space:
mode:
authorJack Jansen <jack.jansen@cwi.nl>2002-12-30 22:04:23 (GMT)
committerJack Jansen <jack.jansen@cwi.nl>2002-12-30 22:04:23 (GMT)
commit60087fb45092d9c199cea162e58d9193c7c1558c (patch)
tree05f3343e7707c4a4179e409506b39601279f04c1 /Lib/plat-mac/macresource.py
parentc262a1f51ce89dbea4aeb072cf631686c47ed97f (diff)
downloadcpython-60087fb45092d9c199cea162e58d9193c7c1558c.zip
cpython-60087fb45092d9c199cea162e58d9193c7c1558c.tar.gz
cpython-60087fb45092d9c199cea162e58d9193c7c1558c.tar.bz2
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
in MacPython-OS9 and MacPython-OSX (or the equivalent unix Python on Mac OS X). The only items remaining in Mac/Lib are modules that are meaningful only for MacPython-OS9 (CFM stuff, MacPython preferences in resources, etc).
Diffstat (limited to 'Lib/plat-mac/macresource.py')
-rw-r--r--Lib/plat-mac/macresource.py115
1 files changed, 115 insertions, 0 deletions
diff --git a/Lib/plat-mac/macresource.py b/Lib/plat-mac/macresource.py
new file mode 100644
index 0000000..26f90940
--- /dev/null
+++ b/Lib/plat-mac/macresource.py
@@ -0,0 +1,115 @@
+"""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.
+
+ Returns the refno of the resource file opened (or None)"""
+
+ 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 None
+ else:
+ try:
+ h = Res.GetNamedResource(restype, resid)
+ except Res.Error:
+ pass
+ else:
+ return None
+
+ # 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 = [os.path.split(mod.__file__)[0]]
+ 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
+
+ refno = open_pathname(pathname)
+
+ # And check that the resource exists now
+ if type(resid) is type(1):
+ h = Res.GetResource(restype, resid)
+ else:
+ h = Res.GetNamedResource(restype, resid)
+ return refno
+
+def open_pathname(pathname, verbose=0):
+ """Open a resource file given by pathname, possibly decoding an
+ AppleSingle file"""
+ try:
+ refno = Res.FSpOpenResFile(pathname, 1)
+ except Res.Error, arg:
+ if arg[0] in (-37, -39):
+ # No resource fork. We may be on OSX, and this may be either
+ # a data-fork based resource file or a AppleSingle file
+ # from the CVS repository.
+ try:
+ refno = Res.FSOpenResourceFile(pathname, u'', 1)
+ except Res.Error, arg:
+ if arg[0] != -199:
+ # -199 is "bad resource map"
+ raise
+ else:
+ return refno
+ # Finally try decoding an AppleSingle file
+ pathname = _decode(pathname, verbose=verbose)
+ refno = Res.FSOpenResourceFile(pathname, u'', 1)
+ else:
+ raise
+ return refno
+
+def open_error_resource():
+ """Open the resource file containing the error code to error message
+ mapping."""
+ need('Estr', 1, filename="errors.rsrc", modname=__name__)
+
+def _decode(pathname, verbose=0):
+ # Decode an AppleSingle resource file, return the new pathname.
+ newpathname = pathname + '.df.rsrc'
+ if os.path.exists(newpathname) and \
+ os.stat(newpathname).st_mtime >= os.stat(pathname).st_mtime:
+ return newpathname
+ if verbose:
+ print 'Decoding', pathname
+ import applesingle
+ applesingle.decode(pathname, newpathname, resonly=1)
+ return newpathname
+
+ \ No newline at end of file