| 
									
										
										
										
											2001-08-27 21:21:07 +00:00
										 |  |  | """macresource - Locate and open the resources needed for a script.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from Carbon import Res | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ArgumentError(TypeError): pass | 
					
						
							|  |  |  | class ResourceFileNotFoundError(ImportError): pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def need(restype, resid, filename=None, modname=None): | 
					
						
							|  |  |  | 	"""Open a resource file, if needed. restype and resid
 | 
					
						
							|  |  |  | 	are required parameters, and identify the resource for which to test. If it | 
					
						
							|  |  |  | 	is available we are done. If it is not available we look for a file filename | 
					
						
							|  |  |  | 	(default: modname with .rsrc appended) either in the same folder as | 
					
						
							| 
									
										
										
										
											2001-08-27 21:37:45 +00:00
										 |  |  | 	where modname was loaded from, or otherwise across sys.path. | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	Returns the refno of the resource file opened (or None)"""
 | 
					
						
							| 
									
										
										
										
											2001-08-27 21:21:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if modname is None and filename is None: | 
					
						
							|  |  |  | 		raise ArgumentError, "Either filename or modname argument (or both) must be given" | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if type(resid) is type(1): | 
					
						
							|  |  |  | 		try: | 
					
						
							|  |  |  | 			h = Res.GetResource(restype, resid) | 
					
						
							|  |  |  | 		except Res.Error: | 
					
						
							|  |  |  | 			pass | 
					
						
							|  |  |  | 		else: | 
					
						
							| 
									
										
										
										
											2001-08-27 21:37:45 +00:00
										 |  |  | 			return None | 
					
						
							| 
									
										
										
										
											2001-08-27 21:21:07 +00:00
										 |  |  | 	else: | 
					
						
							|  |  |  | 		try: | 
					
						
							|  |  |  | 			h = Res.GetNamedResource(restype, resid) | 
					
						
							|  |  |  | 		except Res.Error: | 
					
						
							|  |  |  | 			pass | 
					
						
							|  |  |  | 		else: | 
					
						
							| 
									
										
										
										
											2001-08-27 21:37:45 +00:00
										 |  |  | 			return None | 
					
						
							| 
									
										
										
										
											2001-08-27 21:21:07 +00:00
										 |  |  | 			 | 
					
						
							|  |  |  | 	# Construct a filename if we don't have one | 
					
						
							|  |  |  | 	if not filename: | 
					
						
							|  |  |  | 		if '.' in modname: | 
					
						
							|  |  |  | 			filename = modname.split('.')[-1] + '.rsrc' | 
					
						
							|  |  |  | 		else: | 
					
						
							|  |  |  | 			filename = modname + '.rsrc' | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	# Now create a list of folders to search | 
					
						
							|  |  |  | 	searchdirs = [] | 
					
						
							|  |  |  | 	if modname == '__main__': | 
					
						
							|  |  |  | 		# If we're main we look in the current directory | 
					
						
							|  |  |  | 		searchdirs = [os.curdir] | 
					
						
							|  |  |  | 	if sys.modules.has_key(modname): | 
					
						
							|  |  |  | 		mod = sys.modules[modname] | 
					
						
							|  |  |  | 		if hasattr(mod, '__file__'): | 
					
						
							| 
									
										
										
										
											2001-08-30 21:19:42 +00:00
										 |  |  | 			searchdirs = [os.path.split(mod.__file__)[0]] | 
					
						
							| 
									
										
										
										
											2001-08-27 21:21:07 +00:00
										 |  |  | 	if not searchdirs: | 
					
						
							|  |  |  | 		searchdirs = sys.path | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	# And look for the file | 
					
						
							|  |  |  | 	for dir in searchdirs: | 
					
						
							|  |  |  | 		pathname = os.path.join(dir, filename) | 
					
						
							|  |  |  | 		if os.path.exists(pathname): | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 	else: | 
					
						
							|  |  |  | 		raise ResourceFileNotFoundError, filename | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2002-03-21 22:38:32 +00:00
										 |  |  | 	refno = open_pathname(pathname) | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	# And check that the resource exists now | 
					
						
							|  |  |  | 	if type(resid) is type(1): | 
					
						
							|  |  |  | 		h = Res.GetResource(restype, resid) | 
					
						
							|  |  |  | 	else: | 
					
						
							|  |  |  | 		h = Res.GetNamedResource(restype, resid) | 
					
						
							|  |  |  | 	return refno | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | def open_pathname(pathname): | 
					
						
							|  |  |  | 	"""Open a resource file given by pathname, possibly decoding an
 | 
					
						
							|  |  |  | 	AppleSingle file"""
 | 
					
						
							| 
									
										
										
										
											2002-01-13 23:18:00 +00:00
										 |  |  | 	try: | 
					
						
							|  |  |  | 		refno = Res.FSpOpenResFile(pathname, 1) | 
					
						
							|  |  |  | 	except Res.Error, arg: | 
					
						
							|  |  |  | 		if arg[0] in (-37, -39): | 
					
						
							|  |  |  | 			# No resource fork. We may be on OSX, try to decode | 
					
						
							|  |  |  | 			# the applesingle file. | 
					
						
							|  |  |  | 			pathname = _decode(pathname) | 
					
						
							|  |  |  | 			if pathname: | 
					
						
							|  |  |  | 				refno = Res.FSOpenResourceFile(pathname, u'', 1) | 
					
						
							|  |  |  | 			else: | 
					
						
							|  |  |  | 				raise | 
					
						
							|  |  |  | 	return refno | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | def _decode(pathname): | 
					
						
							|  |  |  | 	# Decode an AppleSingle resource file, return the new pathname. | 
					
						
							|  |  |  | 	newpathname = pathname + '.df.rsrc' | 
					
						
							|  |  |  | 	if os.path.exists(newpathname): | 
					
						
							|  |  |  | 		return newpathname | 
					
						
							|  |  |  | 	import applesingle | 
					
						
							|  |  |  | 	applesingle.decode(pathname, newpathname, resonly=1) | 
					
						
							|  |  |  | 	return newpathname | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	 |