ยปCore Development>Code coverage>Lib/plat-mac/macresource.py

Python code coverage for Lib/plat-mac/macresource.py

#countcontent
1n/a"""macresource - Locate and open the resources needed for a script."""
2n/a
3n/afrom warnings import warnpy3k
4n/awarnpy3k("In 3.x, the macresource module is removed.", stacklevel=2)
5n/a
6n/afrom Carbon import Res
7n/aimport os
8n/aimport sys
9n/aimport MacOS
10n/aimport macostools
11n/a
12n/aclass ArgumentError(TypeError): pass
13n/aclass ResourceFileNotFoundError(ImportError): pass
14n/a
15n/adef need(restype, resid, filename=None, modname=None):
16n/a """Open a resource file, if needed. restype and resid
17n/a are required parameters, and identify the resource for which to test. If it
18n/a is available we are done. If it is not available we look for a file filename
19n/a (default: modname with .rsrc appended) either in the same folder as
20n/a where modname was loaded from, or otherwise across sys.path.
21n/a
22n/a Returns the refno of the resource file opened (or None)"""
23n/a
24n/a if modname is None and filename is None:
25n/a raise ArgumentError, "Either filename or modname argument (or both) must be given"
26n/a
27n/a if type(resid) is type(1):
28n/a try:
29n/a h = Res.GetResource(restype, resid)
30n/a except Res.Error:
31n/a pass
32n/a else:
33n/a return None
34n/a else:
35n/a try:
36n/a h = Res.GetNamedResource(restype, resid)
37n/a except Res.Error:
38n/a pass
39n/a else:
40n/a return None
41n/a
42n/a # Construct a filename if we don't have one
43n/a if not filename:
44n/a if '.' in modname:
45n/a filename = modname.split('.')[-1] + '.rsrc'
46n/a else:
47n/a filename = modname + '.rsrc'
48n/a
49n/a # Now create a list of folders to search
50n/a searchdirs = []
51n/a if modname == '__main__':
52n/a # If we're main we look in the current directory
53n/a searchdirs = [os.curdir]
54n/a if modname in sys.modules:
55n/a mod = sys.modules[modname]
56n/a if hasattr(mod, '__file__'):
57n/a searchdirs = [os.path.dirname(mod.__file__)]
58n/a searchdirs.extend(sys.path)
59n/a
60n/a # And look for the file
61n/a for dir in searchdirs:
62n/a pathname = os.path.join(dir, filename)
63n/a if os.path.exists(pathname):
64n/a break
65n/a else:
66n/a raise ResourceFileNotFoundError, filename
67n/a
68n/a refno = open_pathname(pathname)
69n/a
70n/a # And check that the resource exists now
71n/a if type(resid) is type(1):
72n/a h = Res.GetResource(restype, resid)
73n/a else:
74n/a h = Res.GetNamedResource(restype, resid)
75n/a return refno
76n/a
77n/adef open_pathname(pathname, verbose=0):
78n/a """Open a resource file given by pathname, possibly decoding an
79n/a AppleSingle file"""
80n/a # No resource fork. We may be on OSX, and this may be either
81n/a # a data-fork based resource file or a AppleSingle file
82n/a # from the CVS repository.
83n/a try:
84n/a refno = Res.FSOpenResourceFile(pathname, u'', 1)
85n/a except Res.Error, arg:
86n/a if arg[0] != -199:
87n/a # -199 is "bad resource map"
88n/a raise
89n/a else:
90n/a return refno
91n/a # Finally try decoding an AppleSingle file
92n/a pathname = _decode(pathname, verbose=verbose)
93n/a refno = Res.FSOpenResourceFile(pathname, u'', 1)
94n/a
95n/adef resource_pathname(pathname, verbose=0):
96n/a """Return the pathname for a resource file (either DF or RF based).
97n/a If the pathname given already refers to such a file simply return it,
98n/a otherwise first decode it."""
99n/a # No resource fork. We may be on OSX, and this may be either
100n/a # a data-fork based resource file or a AppleSingle file
101n/a # from the CVS repository.
102n/a try:
103n/a refno = Res.FSOpenResourceFile(pathname, u'', 1)
104n/a except Res.Error, arg:
105n/a if arg[0] != -199:
106n/a # -199 is "bad resource map"
107n/a raise
108n/a else:
109n/a return refno
110n/a # Finally try decoding an AppleSingle file
111n/a pathname = _decode(pathname, verbose=verbose)
112n/a return pathname
113n/a
114n/adef open_error_resource():
115n/a """Open the resource file containing the error code to error message
116n/a mapping."""
117n/a need('Estr', 1, filename="errors.rsrc", modname=__name__)
118n/a
119n/adef _decode(pathname, verbose=0):
120n/a # Decode an AppleSingle resource file, return the new pathname.
121n/a newpathname = pathname + '.df.rsrc'
122n/a if os.path.exists(newpathname) and \
123n/a os.stat(newpathname).st_mtime >= os.stat(pathname).st_mtime:
124n/a return newpathname
125n/a if hasattr(os, 'access') and not \
126n/a os.access(os.path.dirname(pathname), os.W_OK|os.X_OK):
127n/a # The destination directory isn't writeable. Create the file in
128n/a # a temporary directory
129n/a import tempfile
130n/a fd, newpathname = tempfile.mkstemp(".rsrc")
131n/a if verbose:
132n/a print 'Decoding', pathname, 'to', newpathname
133n/a import applesingle
134n/a applesingle.decode(pathname, newpathname, resonly=1)
135n/a return newpathname