1 """macresource - Locate and open the resources needed for a script."""
9 class ArgumentError(TypeError): pass
10 class ResourceFileNotFoundError(ImportError): pass
12 def need(restype
, resid
, filename
=None, modname
=None):
13 """Open a resource file, if needed. restype and resid
14 are required parameters, and identify the resource for which to test. If it
15 is available we are done. If it is not available we look for a file filename
16 (default: modname with .rsrc appended) either in the same folder as
17 where modname was loaded from, or otherwise across sys.path.
19 Returns the refno of the resource file opened (or None)"""
21 if modname
is None and filename
is None:
22 raise ArgumentError
, "Either filename or modname argument (or both) must be given"
24 if type(resid
) is type(1):
26 h
= Res
.GetResource(restype
, resid
)
33 h
= Res
.GetNamedResource(restype
, resid
)
39 # Construct a filename if we don't have one
42 filename
= modname
.split('.')[-1] + '.rsrc'
44 filename
= modname
+ '.rsrc'
46 # Now create a list of folders to search
48 if modname
== '__main__':
49 # If we're main we look in the current directory
50 searchdirs
= [os
.curdir
]
51 if sys
.modules
.has_key(modname
):
52 mod
= sys
.modules
[modname
]
53 if hasattr(mod
, '__file__'):
54 searchdirs
= [os
.path
.dirname(mod
.__file
__)]
55 searchdirs
.extend(sys
.path
)
57 # And look for the file
58 for dir in searchdirs
:
59 pathname
= os
.path
.join(dir, filename
)
60 if os
.path
.exists(pathname
):
63 raise ResourceFileNotFoundError
, filename
65 refno
= open_pathname(pathname
)
67 # And check that the resource exists now
68 if type(resid
) is type(1):
69 h
= Res
.GetResource(restype
, resid
)
71 h
= Res
.GetNamedResource(restype
, resid
)
74 def open_pathname(pathname
, verbose
=0):
75 """Open a resource file given by pathname, possibly decoding an
78 refno
= Res
.FSpOpenResFile(pathname
, 1)
79 except Res
.Error
, arg
:
80 if arg
[0] in (-37, -39):
81 # No resource fork. We may be on OSX, and this may be either
82 # a data-fork based resource file or a AppleSingle file
83 # from the CVS repository.
85 refno
= Res
.FSOpenResourceFile(pathname
, u
'', 1)
86 except Res
.Error
, arg
:
88 # -199 is "bad resource map"
92 # Finally try decoding an AppleSingle file
93 pathname
= _decode(pathname
, verbose
=verbose
)
94 refno
= Res
.FSOpenResourceFile(pathname
, u
'', 1)
99 def resource_pathname(pathname
, verbose
=0):
100 """Return the pathname for a resource file (either DF or RF based).
101 If the pathname given already refers to such a file simply return it,
102 otherwise first decode it."""
104 refno
= Res
.FSpOpenResFile(pathname
, 1)
105 Res
.CloseResFile(refno
)
106 except Res
.Error
, arg
:
107 if arg
[0] in (-37, -39):
108 # No resource fork. We may be on OSX, and this may be either
109 # a data-fork based resource file or a AppleSingle file
110 # from the CVS repository.
112 refno
= Res
.FSOpenResourceFile(pathname
, u
'', 1)
113 except Res
.Error
, arg
:
115 # -199 is "bad resource map"
119 # Finally try decoding an AppleSingle file
120 pathname
= _decode(pathname
, verbose
=verbose
)
125 def open_error_resource():
126 """Open the resource file containing the error code to error message
128 need('Estr', 1, filename
="errors.rsrc", modname
=__name__
)
130 def _decode(pathname
, verbose
=0):
131 # Decode an AppleSingle resource file, return the new pathname.
132 newpathname
= pathname
+ '.df.rsrc'
133 if os
.path
.exists(newpathname
) and \
134 os
.stat(newpathname
).st_mtime
>= os
.stat(pathname
).st_mtime
:
136 if hasattr(os
, 'access') and not \
137 os
.access(os
.path
.dirname(pathname
), os
.W_OK|os
.X_OK
):
138 # The destination directory isn't writeable. Create the file in
139 # a temporary directory
141 fd
, newpathname
= tempfile
.mkstemp(".rsrc")
143 print 'Decoding', pathname
, 'to', newpathname
145 applesingle
.decode(pathname
, newpathname
, resonly
=1)