Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8cd2b72

Browse files
committed
Added PyMac_GetPythonDir routine which obtains python home directory
from an alias resource (or from the user). Note: this uses a dialog resource so there's an accompanying change in the resource file.
1 parent b364257 commit 8cd2b72

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

Mac/Include/macglue.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ void PyMac_HandleEvent Py_PROTO((EventRecord *)); /* Handle one event, if possib
4242

4343
int PyMac_Idle(void); /* Idle routine */
4444

45+
char *PyMac_GetPythonDir(); /* Return the name of the python dir */
46+
4547
int PyMac_GetOSType(PyObject *, OSType *); /* argument parser for OSType */
4648
PyObject *PyMac_BuildOSType(OSType); /* Convert OSType to PyObject */
4749

Mac/Python/macglue.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2727

2828
#include <OSUtils.h> /* for Set(Current)A5 */
2929
#include <Files.h>
30+
#include <Aliases.h>
31+
#include <StandardFile.h>
3032
#include <Resources.h>
3133
#include <Memory.h>
3234
#include <Events.h>
@@ -40,6 +42,11 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4042

4143
#include <signal.h>
4244

45+
#define NOPYTHON_ALERT 128
46+
#define YES_ITEM 1
47+
#define NO_ITEM 2
48+
#define CURWD_ITEM 3
49+
4350
#ifdef __MWERKS__
4451
/*
4552
** With MW we can pass the event off to the console window, so
@@ -359,6 +366,74 @@ PyMac_Idle()
359366
return intrpeek();
360367
}
361368

369+
/*
370+
** Return the name of the Python directory
371+
*/
372+
char *
373+
PyMac_GetPythonDir()
374+
{
375+
int item;
376+
static char name[256];
377+
AliasHandle handle;
378+
FSSpec dirspec;
379+
int ok = 0, exists = 0;
380+
Boolean modified = 0;
381+
StandardFileReply sfreply;
382+
383+
handle = (AliasHandle)GetResource('alis', 128);
384+
if ( handle ) {
385+
if ( ResolveAlias(NULL, handle, &dirspec, &modified) == noErr )
386+
ok = 1;
387+
exists = 1;
388+
}
389+
if ( !ok ) {
390+
item = Alert(NOPYTHON_ALERT, NULL);
391+
if ( item == YES_ITEM ) {
392+
StandardGetFile(NULL, 0, NULL, &sfreply);
393+
if ( sfreply.sfGood ) {
394+
if ( sfreply.sfIsFolder ) {
395+
dirspec = sfreply.sfFile;
396+
ok = 1;
397+
modified = 1;
398+
} else {
399+
/* User selected a file. Use folder containing it */
400+
if (FSMakeFSSpec(sfreply.sfFile.vRefNum,
401+
sfreply.sfFile.parID, "\p", &dirspec) == 0) {
402+
ok = 1;
403+
modified = 1;
404+
}
405+
}
406+
}
407+
} else if ( item == CURWD_ITEM ) {
408+
if ( getwd(name) ) {
409+
if ( FSMakeFSSpec(0, 0, Pstring(name), &dirspec) == 0 ) {
410+
ok = 1;
411+
modified = 1;
412+
}
413+
}
414+
}
415+
}
416+
if ( ok && modified ) {
417+
if ( !exists ) {
418+
if (NewAlias(NULL, &dirspec, &handle) == 0 )
419+
AddResource((Handle)handle, 'alis', 128, "\p");
420+
} else {
421+
ChangedResource((Handle)handle);
422+
}
423+
UpdateResFile(CurResFile());
424+
}
425+
if ( ok ) {
426+
ok = (nfullpath(&dirspec, name) == 0);
427+
if ( ok ) strcat(name, ":");
428+
}
429+
if ( !ok ) {
430+
/* If all fails, we return the current directory */
431+
name[0] = 0;
432+
(void)getwd(name);
433+
}
434+
return name;
435+
}
436+
362437

363438
/* Convert a 4-char string object argument to an OSType value */
364439
int

0 commit comments

Comments
 (0)