Mercurial > cpython-lru_cache-weakref
annotate Lib/plat-mac/macresource.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | e7a6a4515ab0 |
| children | 899bbcbaf9a6 34496bed9365 |
| rev | line source |
|---|---|
|
26884
f0f9acca4164
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
1 """macresource - Locate and open the resources needed for a script.""" |
|
f0f9acca4164
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
2 |
|
46967
6df0394abbf1
Add warnings to and deprecated all those Mac modules
Benjamin Peterson <benjamin@python.org>
parents:
32736
diff
changeset
|
3 from warnings import warnpy3k |
|
47958
e7a6a4515ab0
fix test_py3kwarns
Benjamin Peterson <benjamin@python.org>
parents:
46967
diff
changeset
|
4 warnpy3k("In 3.x, the macresource module is removed.", stacklevel=2) |
|
46967
6df0394abbf1
Add warnings to and deprecated all those Mac modules
Benjamin Peterson <benjamin@python.org>
parents:
32736
diff
changeset
|
5 |
|
26884
f0f9acca4164
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
6 from Carbon import Res |
|
f0f9acca4164
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
7 import os |
|
f0f9acca4164
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
8 import sys |
|
27914
c7d7f350e96c
When installing resource files whose name ends in .rsrc use the
Jack Jansen <jack.jansen@cwi.nl>
parents:
27102
diff
changeset
|
9 import MacOS |
|
c7d7f350e96c
When installing resource files whose name ends in .rsrc use the
Jack Jansen <jack.jansen@cwi.nl>
parents:
27102
diff
changeset
|
10 import macostools |
|
26884
f0f9acca4164
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
11 |
|
f0f9acca4164
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
12 class ArgumentError(TypeError): pass |
|
f0f9acca4164
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
13 class ResourceFileNotFoundError(ImportError): pass |
|
f0f9acca4164
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
14 |
|
f0f9acca4164
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
15 def need(restype, resid, filename=None, modname=None): |
| 28545 | 16 """Open a resource file, if needed. restype and resid |
| 17 are required parameters, and identify the resource for which to test. If it | |
| 18 is available we are done. If it is not available we look for a file filename | |
| 19 (default: modname with .rsrc appended) either in the same folder as | |
| 20 where modname was loaded from, or otherwise across sys.path. | |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
28545
diff
changeset
|
21 |
| 28545 | 22 Returns the refno of the resource file opened (or None)""" |
|
26884
f0f9acca4164
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
23 |
| 28545 | 24 if modname is None and filename is None: |
| 25 raise ArgumentError, "Either filename or modname argument (or both) must be given" | |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
28545
diff
changeset
|
26 |
| 28545 | 27 if type(resid) is type(1): |
| 28 try: | |
| 29 h = Res.GetResource(restype, resid) | |
| 30 except Res.Error: | |
| 31 pass | |
| 32 else: | |
| 33 return None | |
| 34 else: | |
| 35 try: | |
| 36 h = Res.GetNamedResource(restype, resid) | |
| 37 except Res.Error: | |
| 38 pass | |
| 39 else: | |
| 40 return None | |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
28545
diff
changeset
|
41 |
| 28545 | 42 # Construct a filename if we don't have one |
| 43 if not filename: | |
| 44 if '.' in modname: | |
| 45 filename = modname.split('.')[-1] + '.rsrc' | |
| 46 else: | |
| 47 filename = modname + '.rsrc' | |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
28545
diff
changeset
|
48 |
| 28545 | 49 # Now create a list of folders to search |
| 50 searchdirs = [] | |
| 51 if modname == '__main__': | |
| 52 # If we're main we look in the current directory | |
| 53 searchdirs = [os.curdir] | |
| 54 if sys.modules.has_key(modname): | |
| 55 mod = sys.modules[modname] | |
| 56 if hasattr(mod, '__file__'): | |
| 57 searchdirs = [os.path.dirname(mod.__file__)] | |
| 58 searchdirs.extend(sys.path) | |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
28545
diff
changeset
|
59 |
| 28545 | 60 # And look for the file |
| 61 for dir in searchdirs: | |
| 62 pathname = os.path.join(dir, filename) | |
| 63 if os.path.exists(pathname): | |
| 64 break | |
| 65 else: | |
| 66 raise ResourceFileNotFoundError, filename | |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
28545
diff
changeset
|
67 |
| 28545 | 68 refno = open_pathname(pathname) |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
28545
diff
changeset
|
69 |
| 28545 | 70 # And check that the resource exists now |
| 71 if type(resid) is type(1): | |
| 72 h = Res.GetResource(restype, resid) | |
| 73 else: | |
| 74 h = Res.GetNamedResource(restype, resid) | |
| 75 return refno | |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
28545
diff
changeset
|
76 |
|
26884
f0f9acca4164
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
77 def open_pathname(pathname, verbose=0): |
| 28545 | 78 """Open a resource file given by pathname, possibly decoding an |
| 79 AppleSingle file""" | |
| 80 try: | |
| 81 refno = Res.FSpOpenResFile(pathname, 1) | |
| 82 except Res.Error, arg: | |
| 83 if arg[0] in (-37, -39): | |
| 84 # No resource fork. We may be on OSX, and this may be either | |
| 85 # a data-fork based resource file or a AppleSingle file | |
| 86 # from the CVS repository. | |
| 87 try: | |
| 88 refno = Res.FSOpenResourceFile(pathname, u'', 1) | |
| 89 except Res.Error, arg: | |
| 90 if arg[0] != -199: | |
| 91 # -199 is "bad resource map" | |
| 92 raise | |
| 93 else: | |
| 94 return refno | |
| 95 # Finally try decoding an AppleSingle file | |
| 96 pathname = _decode(pathname, verbose=verbose) | |
| 97 refno = Res.FSOpenResourceFile(pathname, u'', 1) | |
| 98 else: | |
| 99 raise | |
| 100 return refno | |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
28545
diff
changeset
|
101 |
|
28071
5b3d59b978a2
If a resource file cannot be decoded because the directory is readonly
Jack Jansen <jack.jansen@cwi.nl>
parents:
27914
diff
changeset
|
102 def resource_pathname(pathname, verbose=0): |
| 28545 | 103 """Return the pathname for a resource file (either DF or RF based). |
| 104 If the pathname given already refers to such a file simply return it, | |
| 105 otherwise first decode it.""" | |
| 106 try: | |
| 107 refno = Res.FSpOpenResFile(pathname, 1) | |
| 108 Res.CloseResFile(refno) | |
| 109 except Res.Error, arg: | |
| 110 if arg[0] in (-37, -39): | |
| 111 # No resource fork. We may be on OSX, and this may be either | |
| 112 # a data-fork based resource file or a AppleSingle file | |
| 113 # from the CVS repository. | |
| 114 try: | |
| 115 refno = Res.FSOpenResourceFile(pathname, u'', 1) | |
| 116 except Res.Error, arg: | |
| 117 if arg[0] != -199: | |
| 118 # -199 is "bad resource map" | |
| 119 raise | |
| 120 else: | |
| 121 return refno | |
| 122 # Finally try decoding an AppleSingle file | |
| 123 pathname = _decode(pathname, verbose=verbose) | |
| 124 else: | |
| 125 raise | |
| 126 return pathname | |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
28545
diff
changeset
|
127 |
|
26884
f0f9acca4164
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
128 def open_error_resource(): |
| 28545 | 129 """Open the resource file containing the error code to error message |
| 130 mapping.""" | |
| 131 need('Estr', 1, filename="errors.rsrc", modname=__name__) | |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
28545
diff
changeset
|
132 |
|
28071
5b3d59b978a2
If a resource file cannot be decoded because the directory is readonly
Jack Jansen <jack.jansen@cwi.nl>
parents:
27914
diff
changeset
|
133 def _decode(pathname, verbose=0): |
| 28545 | 134 # Decode an AppleSingle resource file, return the new pathname. |
| 135 newpathname = pathname + '.df.rsrc' | |
| 136 if os.path.exists(newpathname) and \ | |
| 137 os.stat(newpathname).st_mtime >= os.stat(pathname).st_mtime: | |
| 138 return newpathname | |
| 139 if hasattr(os, 'access') and not \ | |
| 140 os.access(os.path.dirname(pathname), os.W_OK|os.X_OK): | |
| 141 # The destination directory isn't writeable. Create the file in | |
| 142 # a temporary directory | |
| 143 import tempfile | |
| 144 fd, newpathname = tempfile.mkstemp(".rsrc") | |
| 145 if verbose: | |
| 146 print 'Decoding', pathname, 'to', newpathname | |
| 147 import applesingle | |
| 148 applesingle.decode(pathname, newpathname, resonly=1) | |
| 149 return newpathname |
