Jack Jansen | de3226f | 2001-08-27 21:21:07 +0000 | [diff] [blame] | 1 | """macresource - Locate and open the resources needed for a script.""" |
| 2 | |
| 3 | from Carbon import Res |
| 4 | import os |
| 5 | import sys |
| 6 | |
| 7 | class ArgumentError(TypeError): pass |
| 8 | class ResourceFileNotFoundError(ImportError): pass |
| 9 | |
| 10 | def need(restype, resid, filename=None, modname=None): |
| 11 | """Open a resource file, if needed. restype and resid |
| 12 | are required parameters, and identify the resource for which to test. If it |
| 13 | is available we are done. If it is not available we look for a file filename |
| 14 | (default: modname with .rsrc appended) either in the same folder as |
Jack Jansen | a5d7da5 | 2001-08-27 21:37:45 +0000 | [diff] [blame] | 15 | where modname was loaded from, or otherwise across sys.path. |
| 16 | |
| 17 | Returns the refno of the resource file opened (or None)""" |
Jack Jansen | de3226f | 2001-08-27 21:21:07 +0000 | [diff] [blame] | 18 | |
| 19 | if modname is None and filename is None: |
| 20 | raise ArgumentError, "Either filename or modname argument (or both) must be given" |
| 21 | |
| 22 | if type(resid) is type(1): |
| 23 | try: |
| 24 | h = Res.GetResource(restype, resid) |
| 25 | except Res.Error: |
| 26 | pass |
| 27 | else: |
Jack Jansen | a5d7da5 | 2001-08-27 21:37:45 +0000 | [diff] [blame] | 28 | return None |
Jack Jansen | de3226f | 2001-08-27 21:21:07 +0000 | [diff] [blame] | 29 | else: |
| 30 | try: |
| 31 | h = Res.GetNamedResource(restype, resid) |
| 32 | except Res.Error: |
| 33 | pass |
| 34 | else: |
Jack Jansen | a5d7da5 | 2001-08-27 21:37:45 +0000 | [diff] [blame] | 35 | return None |
Jack Jansen | de3226f | 2001-08-27 21:21:07 +0000 | [diff] [blame] | 36 | |
| 37 | # Construct a filename if we don't have one |
| 38 | if not filename: |
| 39 | if '.' in modname: |
| 40 | filename = modname.split('.')[-1] + '.rsrc' |
| 41 | else: |
| 42 | filename = modname + '.rsrc' |
| 43 | |
| 44 | # Now create a list of folders to search |
| 45 | searchdirs = [] |
| 46 | if modname == '__main__': |
| 47 | # If we're main we look in the current directory |
| 48 | searchdirs = [os.curdir] |
| 49 | if sys.modules.has_key(modname): |
| 50 | mod = sys.modules[modname] |
| 51 | if hasattr(mod, '__file__'): |
Jack Jansen | b214c36 | 2001-08-30 21:19:42 +0000 | [diff] [blame] | 52 | searchdirs = [os.path.split(mod.__file__)[0]] |
Jack Jansen | de3226f | 2001-08-27 21:21:07 +0000 | [diff] [blame] | 53 | if not searchdirs: |
| 54 | searchdirs = sys.path |
| 55 | |
| 56 | # And look for the file |
| 57 | for dir in searchdirs: |
| 58 | pathname = os.path.join(dir, filename) |
| 59 | if os.path.exists(pathname): |
| 60 | break |
| 61 | else: |
| 62 | raise ResourceFileNotFoundError, filename |
| 63 | |
Jack Jansen | 562baab | 2002-03-21 22:38:32 +0000 | [diff] [blame] | 64 | refno = open_pathname(pathname) |
| 65 | |
| 66 | # And check that the resource exists now |
| 67 | if type(resid) is type(1): |
| 68 | h = Res.GetResource(restype, resid) |
| 69 | else: |
| 70 | h = Res.GetNamedResource(restype, resid) |
| 71 | return refno |
| 72 | |
| 73 | def open_pathname(pathname): |
| 74 | """Open a resource file given by pathname, possibly decoding an |
| 75 | AppleSingle file""" |
Jack Jansen | 32d1a3b | 2002-01-13 23:18:00 +0000 | [diff] [blame] | 76 | try: |
| 77 | refno = Res.FSpOpenResFile(pathname, 1) |
| 78 | except Res.Error, arg: |
| 79 | if arg[0] in (-37, -39): |
Jack Jansen | 5053b70 | 2002-03-29 14:29:35 +0000 | [diff] [blame] | 80 | # No resource fork. We may be on OSX, and this may be either |
| 81 | # a data-fork based resource file or a AppleSingle file |
| 82 | # from the CVS repository. |
| 83 | try: |
Jack Jansen | 32d1a3b | 2002-01-13 23:18:00 +0000 | [diff] [blame] | 84 | refno = Res.FSOpenResourceFile(pathname, u'', 1) |
Jack Jansen | 5053b70 | 2002-03-29 14:29:35 +0000 | [diff] [blame] | 85 | except Res.Error, arg: |
| 86 | if arg[0] != -199: |
| 87 | # -199 is "bad resource map" |
| 88 | raise |
Jack Jansen | 32d1a3b | 2002-01-13 23:18:00 +0000 | [diff] [blame] | 89 | else: |
Jack Jansen | 5053b70 | 2002-03-29 14:29:35 +0000 | [diff] [blame] | 90 | return refno |
| 91 | # Finally try decoding an AppleSingle file |
| 92 | pathname = _decode(pathname) |
| 93 | refno = Res.FSOpenResourceFile(pathname, u'', 1) |
Jack Jansen | 32d1a3b | 2002-01-13 23:18:00 +0000 | [diff] [blame] | 94 | return refno |
| 95 | |
| 96 | def _decode(pathname): |
| 97 | # Decode an AppleSingle resource file, return the new pathname. |
| 98 | newpathname = pathname + '.df.rsrc' |
| 99 | if os.path.exists(newpathname): |
| 100 | return newpathname |
| 101 | import applesingle |
| 102 | applesingle.decode(pathname, newpathname, resonly=1) |
| 103 | return newpathname |
| 104 | |
| 105 | |