[Pythonmac-SIG] Python based App free of Python Preferences

Magladry, Stephen stephenm@humongous.com
Thu, 14 Nov 2002 09:29:34 -0800


Recently, I have been trying to put together an app, really a game, that
uses the Python Core,
but has no reliance on the Python Prefs. The reason being two fold, we may
have more that one
game and want to include different libraries. The second being, I do not
want to screw up 
someone's hard created prefs just by running one of our games. This means
that we have a lib 
folder per game, and our 'STR#' 229 to point to them, but since we know what
lib folder we 
ship with, we can better guarantee long term stability.

Thankfully, the Python Core is mostly set up to handle this. I ran into two
problems in the 
process. First, there was the problem I discovered with
PyMac_PreferenceOptions (see 
yesterday's post). The second problem was I could not get relative path
aliases working
correctly. I followed the guildlines from
http://developer.apple.com/technotes/tn/tn1188.html
using the Finder to create a relative alias. I then copied the 'alis'
resource and pasted it
into my app, 'alis',id # 229. The Python Core even found the resource. I
must of built my 
relative alias incorrectly, because ResolveAlias didn't resolve to the
expected location. 
Because of that, I made a minor change to the source code.

I do not have CVS access, though I have tried. The problem is mostly on our
server side, I
think. I have had other problems with are firewall. So if I may I would like
to give you 
the source code changes I needed to make to be added to the release stream.


-- add near the top of macgetpath.c ---------

 /* Use this if you want to use the current directory your app is running
from
** as your $(PYTHON) folder. You will have to make sure that you use the
STR# 229
** resource to properly point to the files that are need in the Python boot
process.
*/
#define USE_APP_DIR_AS_PYTHON_DIR 1

----------


-- Replace PyMac_GetPythonDir with this -------
char *
PyMac_GetPythonDir()
{
#if USE_APP_DIR_AS_PYTHON_DIR
	static int diditbefore = 0;
	static char name[PATHNAMELEN] = {':', '\0'};

	if ( diditbefore )
		return name;

	name[0] = 0;
	/* just use the current apps directory as the Python Directory */
	(void)getcwd(name, sizeof(name));

	diditbefore = 1;
	return name;
#else
	static int diditbefore = 0;
	static char name[PATHNAMELEN] = {':', '\0'};

	AliasHandle handle;
	FSSpec dirspec;
	Boolean modified = 0;
	short oldrh, prefrh = -1, homerh;
	
	if ( diditbefore )
		return name;
		
	oldrh = CurResFile();

	/* First look for an override in the application file */
	UseResFile(PyMac_AppRefNum);
	handle = (AliasHandle)Get1Resource('alis', PYTHONHOMEOVERRIDE_ID);
	UseResFile(oldrh);
	if ( handle != NULL ) {
		homerh = PyMac_AppRefNum;
	} else {   
		/* Try to open preferences file in the preferences folder.
*/
		prefrh = PyMac_OpenPrefFile();
		handle = (AliasHandle)Get1Resource('alis', PYTHONHOME_ID);
		if ( handle == NULL ) {
			/* (void)StopAlert(BADPREFFILE_ID, NULL); */
			diditbefore=1;
			return ":";
		}
		homerh = prefrh;
	}
	/* It exists. Resolve it (possibly updating it) */
	if ( ResolveAlias(NULL, handle, &dirspec, &modified) != noErr ) {
		(void)StopAlert(BADPREFFILE_ID, NULL);
		diditbefore=1;
		return ":";
	}
	if ( modified ) {
   		ChangedResource((Handle)handle);
		UpdateResFile(homerh);
	}
	if ( prefrh != -1 ) CloseResFile(prefrh);
	UseResFile(oldrh);
   	if ( PyMac_GetFullPathname(&dirspec, name, PATHNAMELEN) == 0 ) {
   		strcat(name, ":");
	} else {
 		/* If all fails, we return the current directory */
   		printf("Python home dir exists but I cannot find the
pathname!!\n");
		name[0] = 0;
		(void)getcwd(name, sizeof(name));
	}
	diditbefore = 1;
	return name;
#endif
}
-----------

I didn't change the existing code, at least not meaningfully. I just added
my #if block of code.

Could some review this and summit it properly? I would be much appreciated.

Thanks,

Stephen Magladry