/Mac/PythonLauncher/FileSettings.m

http://unladen-swallow.googlecode.com/ · Objective C · 313 lines · 261 code · 34 blank · 18 comment · 48 complexity · 8d218759c93dcdbf27e04178b0807d32 MD5 · raw file

  1. //
  2. // FileSettings.m
  3. // PythonLauncher
  4. //
  5. // Created by Jack Jansen on Sun Jul 21 2002.
  6. // Copyright (c) 2002 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "FileSettings.h"
  9. @implementation FileSettings
  10. + (id)getFactorySettingsForFileType: (NSString *)filetype
  11. {
  12. static FileSettings *fsdefault_py, *fsdefault_pyw, *fsdefault_pyc;
  13. FileSettings **curdefault;
  14. if ([filetype isEqualToString: @"Python Script"]) {
  15. curdefault = &fsdefault_py;
  16. } else if ([filetype isEqualToString: @"Python GUI Script"]) {
  17. curdefault = &fsdefault_pyw;
  18. } else if ([filetype isEqualToString: @"Python Bytecode Document"]) {
  19. curdefault = &fsdefault_pyc;
  20. } else {
  21. NSLog(@"Funny File Type: %@\n", filetype);
  22. curdefault = &fsdefault_py;
  23. filetype = @"Python Script";
  24. }
  25. if (!*curdefault) {
  26. *curdefault = [[FileSettings new] initForFSDefaultFileType: filetype];
  27. }
  28. return *curdefault;
  29. }
  30. + (id)getDefaultsForFileType: (NSString *)filetype
  31. {
  32. static FileSettings *default_py, *default_pyw, *default_pyc;
  33. FileSettings **curdefault;
  34. if ([filetype isEqualToString: @"Python Script"]) {
  35. curdefault = &default_py;
  36. } else if ([filetype isEqualToString: @"Python GUI Script"]) {
  37. curdefault = &default_pyw;
  38. } else if ([filetype isEqualToString: @"Python Bytecode Document"]) {
  39. curdefault = &default_pyc;
  40. } else {
  41. NSLog(@"Funny File Type: %@\n", filetype);
  42. curdefault = &default_py;
  43. filetype = @"Python Script";
  44. }
  45. if (!*curdefault) {
  46. *curdefault = [[FileSettings new] initForDefaultFileType: filetype];
  47. }
  48. return *curdefault;
  49. }
  50. + (id)newSettingsForFileType: (NSString *)filetype
  51. {
  52. FileSettings *cur;
  53. cur = [FileSettings new];
  54. [cur initForFileType: filetype];
  55. return [cur retain];
  56. }
  57. - (id)initWithFileSettings: (FileSettings *)source
  58. {
  59. self = [super init];
  60. if (!self) return self;
  61. interpreter = [source->interpreter retain];
  62. honourhashbang = source->honourhashbang;
  63. debug = source->debug;
  64. verbose = source->verbose;
  65. inspect = source->inspect;
  66. optimize = source->optimize;
  67. nosite = source->nosite;
  68. tabs = source->tabs;
  69. others = [source->others retain];
  70. scriptargs = [source->scriptargs retain];
  71. with_terminal = source->with_terminal;
  72. prefskey = source->prefskey;
  73. if (prefskey) [prefskey retain];
  74. return self;
  75. }
  76. - (id)initForFileType: (NSString *)filetype
  77. {
  78. FileSettings *defaults;
  79. defaults = [FileSettings getDefaultsForFileType: filetype];
  80. self = [self initWithFileSettings: defaults];
  81. origsource = [defaults retain];
  82. return self;
  83. }
  84. //- (id)init
  85. //{
  86. // self = [self initForFileType: @"Python Script"];
  87. // return self;
  88. //}
  89. - (id)initForFSDefaultFileType: (NSString *)filetype
  90. {
  91. int i;
  92. NSString *filename;
  93. NSDictionary *dict;
  94. static NSDictionary *factorySettings;
  95. self = [super init];
  96. if (!self) return self;
  97. if (factorySettings == NULL) {
  98. NSBundle *bdl = [NSBundle mainBundle];
  99. NSString *path = [ bdl pathForResource: @"factorySettings"
  100. ofType: @"plist"];
  101. factorySettings = [[NSDictionary dictionaryWithContentsOfFile:
  102. path] retain];
  103. if (factorySettings == NULL) {
  104. NSLog(@"Missing %@", path);
  105. return NULL;
  106. }
  107. }
  108. dict = [factorySettings objectForKey: filetype];
  109. if (dict == NULL) {
  110. NSLog(@"factorySettings.plist misses file type \"%@\"", filetype);
  111. interpreter = [@"no default found" retain];
  112. return NULL;
  113. }
  114. [self applyValuesFromDict: dict];
  115. interpreters = [dict objectForKey: @"interpreter_list"];
  116. interpreter = NULL;
  117. for (i=0; i < [interpreters count]; i++) {
  118. filename = [interpreters objectAtIndex: i];
  119. filename = [filename stringByExpandingTildeInPath];
  120. if ([[NSFileManager defaultManager] fileExistsAtPath: filename]) {
  121. interpreter = [filename retain];
  122. break;
  123. }
  124. }
  125. if (interpreter == NULL)
  126. interpreter = [@"no default found" retain];
  127. origsource = NULL;
  128. return self;
  129. }
  130. - (void)applyUserDefaults: (NSString *)filetype
  131. {
  132. NSUserDefaults *defaults;
  133. NSDictionary *dict;
  134. defaults = [NSUserDefaults standardUserDefaults];
  135. dict = [defaults dictionaryForKey: filetype];
  136. if (!dict)
  137. return;
  138. [self applyValuesFromDict: dict];
  139. }
  140. - (id)initForDefaultFileType: (NSString *)filetype
  141. {
  142. FileSettings *fsdefaults;
  143. fsdefaults = [FileSettings getFactorySettingsForFileType: filetype];
  144. self = [self initWithFileSettings: fsdefaults];
  145. if (!self) return self;
  146. interpreters = [fsdefaults->interpreters retain];
  147. scriptargs = [@"" retain];
  148. [self applyUserDefaults: filetype];
  149. prefskey = [filetype retain];
  150. return self;
  151. }
  152. - (void)reset
  153. {
  154. if (origsource) {
  155. [self updateFromSource: origsource];
  156. } else {
  157. FileSettings *fsdefaults;
  158. fsdefaults = [FileSettings getFactorySettingsForFileType: prefskey];
  159. [self updateFromSource: fsdefaults];
  160. }
  161. }
  162. - (void)updateFromSource: (id <FileSettingsSource>)source
  163. {
  164. interpreter = [[source interpreter] retain];
  165. honourhashbang = [source honourhashbang];
  166. debug = [source debug];
  167. verbose = [source verbose];
  168. inspect = [source inspect];
  169. optimize = [source optimize];
  170. nosite = [source nosite];
  171. tabs = [source tabs];
  172. others = [[source others] retain];
  173. scriptargs = [[source scriptargs] retain];
  174. with_terminal = [source with_terminal];
  175. // And if this is a user defaults object we also save the
  176. // values
  177. if (!origsource) {
  178. NSUserDefaults *defaults;
  179. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
  180. interpreter, @"interpreter",
  181. [NSNumber numberWithBool: honourhashbang], @"honourhashbang",
  182. [NSNumber numberWithBool: debug], @"debug",
  183. [NSNumber numberWithBool: verbose], @"verbose",
  184. [NSNumber numberWithBool: inspect], @"inspect",
  185. [NSNumber numberWithBool: optimize], @"optimize",
  186. [NSNumber numberWithBool: nosite], @"nosite",
  187. [NSNumber numberWithBool: tabs], @"tabs",
  188. others, @"others",
  189. scriptargs, @"scriptargs",
  190. [NSNumber numberWithBool: with_terminal], @"with_terminal",
  191. nil];
  192. defaults = [NSUserDefaults standardUserDefaults];
  193. [defaults setObject: dict forKey: prefskey];
  194. }
  195. }
  196. - (void)applyValuesFromDict: (NSDictionary *)dict
  197. {
  198. id value;
  199. value = [dict objectForKey: @"interpreter"];
  200. if (value) interpreter = [value retain];
  201. value = [dict objectForKey: @"honourhashbang"];
  202. if (value) honourhashbang = [value boolValue];
  203. value = [dict objectForKey: @"debug"];
  204. if (value) debug = [value boolValue];
  205. value = [dict objectForKey: @"verbose"];
  206. if (value) verbose = [value boolValue];
  207. value = [dict objectForKey: @"inspect"];
  208. if (value) inspect = [value boolValue];
  209. value = [dict objectForKey: @"optimize"];
  210. if (value) optimize = [value boolValue];
  211. value = [dict objectForKey: @"nosite"];
  212. if (value) nosite = [value boolValue];
  213. value = [dict objectForKey: @"tabs"];
  214. if (value) tabs = [value boolValue];
  215. value = [dict objectForKey: @"others"];
  216. if (value) others = [value retain];
  217. value = [dict objectForKey: @"scriptargs"];
  218. if (value) scriptargs = [value retain];
  219. value = [dict objectForKey: @"with_terminal"];
  220. if (value) with_terminal = [value boolValue];
  221. }
  222. - (NSString*)_replaceSingleQuotes: (NSString*)string
  223. {
  224. /* Replace all single-quotes by '"'"', that way shellquoting will
  225. * be correct when the result value is delimited using single quotes.
  226. */
  227. NSArray* components = [string componentsSeparatedByString:@"'"];
  228. return [components componentsJoinedByString:@"'\"'\"'"];
  229. }
  230. - (NSString *)commandLineForScript: (NSString *)script
  231. {
  232. NSString *cur_interp = NULL;
  233. NSString* script_dir = NULL;
  234. char hashbangbuf[1024];
  235. FILE *fp;
  236. char *p;
  237. script_dir = [script substringToIndex:
  238. [script length]-[[script lastPathComponent] length]];
  239. if (honourhashbang &&
  240. (fp=fopen([script cString], "r")) &&
  241. fgets(hashbangbuf, sizeof(hashbangbuf), fp) &&
  242. strncmp(hashbangbuf, "#!", 2) == 0 &&
  243. (p=strchr(hashbangbuf, '\n'))) {
  244. *p = '\0';
  245. p = hashbangbuf + 2;
  246. while (*p == ' ') p++;
  247. cur_interp = [NSString stringWithCString: p];
  248. }
  249. if (!cur_interp)
  250. cur_interp = interpreter;
  251. return [NSString stringWithFormat:
  252. @"cd '%@' && '%@'%s%s%s%s%s%s %@ '%@' %@ %s",
  253. [self _replaceSingleQuotes:script_dir],
  254. [self _replaceSingleQuotes:cur_interp],
  255. debug?" -d":"",
  256. verbose?" -v":"",
  257. inspect?" -i":"",
  258. optimize?" -O":"",
  259. nosite?" -S":"",
  260. tabs?" -t":"",
  261. others,
  262. [self _replaceSingleQuotes:script],
  263. scriptargs ? scriptargs : @"",
  264. with_terminal? "&& echo Exit status: $? && exit 1" : " &"];
  265. }
  266. - (NSArray *) interpreters { return interpreters;};
  267. // FileSettingsSource protocol
  268. - (NSString *) interpreter { return interpreter;};
  269. - (BOOL) honourhashbang { return honourhashbang; };
  270. - (BOOL) debug { return debug;};
  271. - (BOOL) verbose { return verbose;};
  272. - (BOOL) inspect { return inspect;};
  273. - (BOOL) optimize { return optimize;};
  274. - (BOOL) nosite { return nosite;};
  275. - (BOOL) tabs { return tabs;};
  276. - (NSString *) others { return others;};
  277. - (NSString *) scriptargs { return scriptargs;};
  278. - (BOOL) with_terminal { return with_terminal;};
  279. @end