summaryrefslogtreecommitdiffstats
path: root/Mac/Lib/macresource.py
blob: 001e6cc7bb69f69121e5e262f88d9b45b094e880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""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
	where modname was loaded from, or otherwise across sys.path.
	
	Returns the refno of the resource file opened (or None)"""

	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:
			return None
	else:
		try:
			h = Res.GetNamedResource(restype, resid)
		except Res.Error:
			pass
		else:
			return None
			
	# 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__'):
			searchdirs = [os.path.split(mod.__file__)[0]]
	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
	
	refno = Res.FSpOpenResFile(pathname, 1)
	
	# 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