1 """macresource - Locate and open the resources needed for a script."""
3 from warnings
import warnpy3k
4 warnpy3k("In 3.x, the macresource module is removed.", stacklevel
=2)
12 class ArgumentError(TypeError): pass
13 class ResourceFileNotFoundError(ImportError): pass
15 def need(restype
, resid
, filename
=None, modname
=None):
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.
22 Returns the refno of the resource file opened (or None)"""
24 if modname
is None and filename
is None:
25 raise ArgumentError
, "Either filename or modname argument (or both) must be given"
27 if type(resid
) is type(1):
29 h
= Res
.GetResource(restype
, resid
)
36 h
= Res
.GetNamedResource(restype
, resid
)
42 # Construct a filename if we don't have one
45 filename
= modname
.split('.')[-1] + '.rsrc'
47 filename
= modname
+ '.rsrc'
49 # Now create a list of folders to search
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
)
60 # And look for the file
61 for dir in searchdirs
:
62 pathname
= os
.path
.join(dir, filename
)
63 if os
.path
.exists(pathname
):
66 raise ResourceFileNotFoundError
, filename
68 refno
= open_pathname(pathname
)
70 # And check that the resource exists now
71 if type(resid
) is type(1):
72 h
= Res
.GetResource(restype
, resid
)
74 h
= Res
.GetNamedResource(restype
, resid
)
77 def open_pathname(pathname
, verbose
=0):
78 """Open a resource file given by pathname, possibly decoding an
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.
84 refno
= Res
.FSOpenResourceFile(pathname
, u
'', 1)
85 except Res
.Error
, arg
:
87 # -199 is "bad resource map"
91 # Finally try decoding an AppleSingle file
92 pathname
= _decode(pathname
, verbose
=verbose
)
93 refno
= Res
.FSOpenResourceFile(pathname
, u
'', 1)
95 def resource_pathname(pathname
, verbose
=0):
96 """Return the pathname for a resource file (either DF or RF based).
97 If the pathname given already refers to such a file simply return it,
98 otherwise first decode it."""
99 # No resource fork. We may be on OSX, and this may be either
100 # a data-fork based resource file or a AppleSingle file
101 # from the CVS repository.
103 refno
= Res
.FSOpenResourceFile(pathname
, u
'', 1)
104 except Res
.Error
, arg
:
106 # -199 is "bad resource map"
110 # Finally try decoding an AppleSingle file
111 pathname
= _decode(pathname
, verbose
=verbose
)
114 def open_error_resource():
115 """Open the resource file containing the error code to error message
117 need('Estr', 1, filename
="errors.rsrc", modname
=__name__
)
119 def _decode(pathname
, verbose
=0):
120 # Decode an AppleSingle resource file, return the new pathname.
121 newpathname
= pathname
+ '.df.rsrc'
122 if os
.path
.exists(newpathname
) and \
123 os
.stat(newpathname
).st_mtime
>= os
.stat(pathname
).st_mtime
:
125 if hasattr(os
, 'access') and not \
126 os
.access(os
.path
.dirname(pathname
), os
.W_OK|os
.X_OK
):
127 # The destination directory isn't writeable. Create the file in
128 # a temporary directory
130 fd
, newpathname
= tempfile
.mkstemp(".rsrc")
132 print 'Decoding', pathname
, 'to', newpathname
134 applesingle
.decode(pathname
, newpathname
, resonly
=1)