1from Foundation import * 2from AppKit import * 3from PyObjCTools import NibClassBuilder 4import os 5from FileSettings import * 6from doscript import doscript 7 8class MyDocument(NSDocument): 9 commandline = objc.IBOutlet() 10 debug = objc.IBOutlet() 11 honourhashbang = objc.IBOutlet() 12 inspect = objc.IBOutlet() 13 interpreter = objc.IBOutlet() 14 nosite = objc.IBOutlet() 15 optimize = objc.IBOutlet() 16 others = objc.IBOutlet() 17 scriptargs = objc.IBOutlet() 18 tabs = objc.IBOutlet() 19 verbose = objc.IBOutlet() 20 with_terminal = objc.IBOutlet() 21 22 def init(self): 23 self = super(MyDocument, self).init() 24 if self is not None: 25 self.script = u'<no script>.py' 26 self.filetype = u'Python Script' 27 self.settings = None 28 return self 29 30 def windowNibName(self): 31 return u'MyDocument' 32 33 def close(self): 34 super(MyDocument, self).close() 35 if NSApp().delegate().shouldTerminate(): 36 NSApp().terminate_(self) 37 38 def load_defaults(self): 39 self.settings = FileSettings.newSettingsForFileType_(self.filetype) 40 41 def updateDisplay(self): 42 dct = self.settings.fileSettingsAsDict() 43 self.interpreter.setStringValue_(dct['interpreter']) 44 self.honourhashbang.setState_(dct['honourhashbang']) 45 self.debug.setState_(dct['verbose']) 46 self.inspect.setState_(dct['inspect']) 47 self.optimize.setState_(dct['optimize']) 48 self.nosite.setState_(dct['nosite']) 49 self.tabs.setState_(dct['tabs']) 50 self.others.setStringValue_(dct['others']) 51 self.scriptargs.setStringValue_(dct['scriptargs']) 52 self.with_terminal.setState_(dct['with_terminal']) 53 self.commandline.setStringValue_(self.settings.commandLineForScript_(self.script)) 54 55 def update_settings(self): 56 self.settings.updateFromSource_(self) 57 58 def run(self): 59 cmdline = self.settings.commandLineForScript_(self.script) 60 dct = self.settings.fileSettingsAsDict() 61 if dct['with_terminal']: 62 res = doscript(cmdline) 63 else: 64 res = os.system(cmdline) 65 if res: 66 NSLog(u'Exit status: %d', res) 67 return False 68 return True 69 70 def windowControllerDidLoadNib_(self, aController): 71 super(MyDocument, self).windowControllerDidLoadNib_(aController) 72 self.load_defaults() 73 self.updateDisplay() 74 75 def dataRepresentationOfType_(self, aType): 76 return None 77 78 def readFromFile_ofType_(self, filename, typ): 79 show_ui = NSApp().delegate().shouldShowUI() 80 self.script = filename 81 self.filetype = typ 82 self.settings = FileSettings.newSettingsForFileType_(typ) 83 if show_ui: 84 self.updateDisplay() 85 return True 86 else: 87 self.run() 88 self.close() 89 return False 90 91 @objc.IBAction 92 def doRun_(self, sender): 93 self.update_settings() 94 self.updateDisplay() 95 if self.run(): 96 self.close() 97 98 @objc.IBAction 99 def doCancel_(self, sender): 100 self.close() 101 102 @objc.IBAction 103 def doReset_(self, sender): 104 self.settings.reset() 105 self.updateDisplay() 106 107 @objc.IBAction 108 def doApply_(self, sender): 109 self.update_settings() 110 self.updateDisplay() 111 112 def controlTextDidChange_(self, aNotification): 113 self.update_settings() 114 self.updateDisplay() 115 116 def fileSettingsAsDict(self): 117 return dict( 118 interpreter=self.interpreter.stringValue(), 119 honourhashbang=self.honourhashbang.state(), 120 debug=self.debug.state(), 121 verbose=self.verbose.state(), 122 inspect=self.inspect.state(), 123 optimize=self.optimize.state(), 124 nosite=self.nosite.state(), 125 tabs=self.tabs.state(), 126 others=self.others.stringValue(), 127 with_terminal=self.with_terminal.state(), 128 scriptargs=self.scriptargs.stringValue(), 129 ) 130