0
0
mirror of https://github.com/python/cpython.git synced 2024-11-29 17:07:13 +01:00
cpython/Mac/OSX/PythonLauncher/FileSettings.m
Jack Jansen 3bbb617ca4 First stab at the launcher application. This will be run when the user
doubleclicks a .py, .pyw or .pyc file. It runs the file by invoking the
relevant interpreter (either the command line Python in a terminal window
or a Python.app for GUI-based scripts). Interpreter to use and the options
to pass are settable through preferences.

If PythonLauncher wasn't running it does its thing for one script and exits.
If it was manually started before a dialog is presented where the user
can set the options to use, etc.

To be done:
- option-drag/doubleclick should always open the interactive dialog
- Terminal-window isn't done yet
- Should be reimplemented in Python, but pyobjc isn't part of the core.
- Various menu entries should be disabled.
2002-07-29 21:36:35 +00:00

183 lines
5.4 KiB
Objective-C
Executable File

//
// FileSettings.m
// PythonLauncher
//
// Created by Jack Jansen on Sun Jul 21 2002.
// Copyright (c) 2002 __MyCompanyName__. All rights reserved.
//
#import "FileSettings.h"
@implementation FileSettings
+ (id)getDefaultsForFileType: (NSString *)filetype
{
static FileSettings *default_py, *default_pyw, *default_pyc;
FileSettings **curdefault;
if ([filetype isEqualToString: @"Python Script"]) {
curdefault = &default_py;
} else if ([filetype isEqualToString: @"Python GUI Script"]) {
curdefault = &default_pyw;
} else if ([filetype isEqualToString: @"Python Bytecode Document"]) {
curdefault = &default_pyc;
} else {
NSLog(@"Funny File Type: %@\n", filetype);
curdefault = &default_py;
filetype = @"Python Script";
}
if (!*curdefault) {
*curdefault = [[FileSettings new] init];
[*curdefault factorySettingsForFileType: filetype];
[*curdefault updateFromUserDefaults: filetype];
}
return *curdefault;
}
+ (id)newSettingsForFileType: (NSString *)filetype
{
FileSettings *cur;
cur = [[FileSettings new] init];
[cur initWithFileSettings: [FileSettings getDefaultsForFileType: filetype]];
return cur;
}
- (id)init
{
self = [super init];
return [self factorySettingsForFileType: @"Python Script"];
}
- (id)factorySettingsForFileType: (NSString *)filetype
{
debug = NO;
verbose = NO;
inspect = NO;
optimize = NO;
nosite = NO;
tabs = NO;
others = @"";
if ([filetype isEqualToString: @"Python Script"] ||
[filetype isEqualToString: @"Python Bytecode Document"]) {
interpreter = @"/usr/local/bin/python";
with_terminal = YES;
} else if ([filetype isEqualToString: @"Python GUI Script"]) {
interpreter = @"/Applications/Python.app/Contents/MacOS/python";
with_terminal = NO;
} else {
NSLog(@"Funny File Type: %@\n", filetype);
}
origsource = NULL;
return self;
}
- (id)initWithFileSettings: (FileSettings *)source
{
interpreter = [source->interpreter retain];
debug = source->debug;
verbose = source->verbose;
inspect = source->inspect;
optimize = source->optimize;
nosite = source->nosite;
tabs = source->tabs;
others = [source->others retain];
with_terminal = source->with_terminal;
origsource = [source retain];
return self;
}
- (void)saveDefaults
{
[origsource updateFromSource: self];
}
- (void)updateFromSource: (id <FileSettingsSource>)source
{
interpreter = [[source interpreter] retain];
debug = [source debug];
verbose = [source verbose];
inspect = [source inspect];
optimize = [source optimize];
nosite = [source nosite];
tabs = [source tabs];
others = [[source others] retain];
with_terminal = [source with_terminal];
if (!origsource) {
NSUserDefaults *defaults;
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
interpreter, @"interpreter",
[NSNumber numberWithBool: debug], @"debug",
[NSNumber numberWithBool: verbose], @"verbose",
[NSNumber numberWithBool: inspect], @"inspect",
[NSNumber numberWithBool: optimize], @"optimize",
[NSNumber numberWithBool: nosite], @"nosite",
[NSNumber numberWithBool: nosite], @"nosite",
others, @"others",
[NSNumber numberWithBool: with_terminal], @"with_terminal",
nil];
defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: dict forKey: prefskey];
}
}
- (void)updateFromUserDefaults: (NSString *)filetype
{
NSUserDefaults *defaults;
NSDictionary *dict;
id value;
prefskey = [filetype retain];
defaults = [NSUserDefaults standardUserDefaults];
dict = [defaults dictionaryForKey: filetype];
if (!dict)
return;
value = [dict objectForKey: @"interpreter"];
if (value) interpreter = [value retain];
value = [dict objectForKey: @"debug"];
if (value) debug = [value boolValue];
value = [dict objectForKey: @"verbose"];
if (value) verbose = [value boolValue];
value = [dict objectForKey: @"inspect"];
if (value) inspect = [value boolValue];
value = [dict objectForKey: @"optimize"];
if (value) optimize = [value boolValue];
value = [dict objectForKey: @"nosite"];
if (value) nosite = [value boolValue];
value = [dict objectForKey: @"nosite"];
if (value) tabs = [value boolValue];
value = [dict objectForKey: @"others"];
if (value) others = [value retain];
value = [dict objectForKey: @"with_terminal"];
if (value) with_terminal = [value boolValue];
}
- (NSString *)commandLineForScript: (NSString *)script
{
return [NSString stringWithFormat:
@"\"%@\"%s%s%s%s%s%s %@ \"%@\" %s",
interpreter,
debug?" -d":"",
verbose?" -v":"",
inspect?" -i":"",
optimize?" -O":"",
nosite?" -S":"",
tabs?" -t":"",
others,
script,
with_terminal? "" : " &"];
}
// FileSettingsSource protocol
- (NSString *) interpreter { return interpreter;};
- (BOOL) debug { return debug;};
- (BOOL) verbose { return verbose;};
- (BOOL) inspect { return inspect;};
- (BOOL) optimize { return optimize;};
- (BOOL) nosite { return nosite;};
- (BOOL) tabs { return tabs;};
- (NSString *) others { return others;};
- (BOOL) with_terminal { return with_terminal;};
@end