<s> from Foundation import * <EOL> from AppKit import * <EOL> from objc import YES , NO , nil , signature <EOL> from WebKit import * <EOL> from devon . web import serve <EOL> import os . path , sys , struct , thread <EOL> kEventHotKeyPressedSubtype = <NUM_LIT:6> <EOL> kEventHotKeyReleasedSubtype = <NUM_LIT:9> <EOL> defaultWindowFrame = ( ( <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> devonURL = "<STR_LIT>" <EOL> def aeKeyword ( fourCharCode ) : <EOL> return struct . unpack ( '<STR_LIT>' , fourCharCode ) [ <NUM_LIT:0> ] <EOL> class DevonApp ( NSApplication ) : <EOL> hotKeys = { } <EOL> commands = [ ] <EOL> def sendEvent_ ( self , theEvent ) : <EOL> if theEvent . type ( ) == NSSystemDefined and theEvent . subtype ( ) == kEventHotKeyPressedSubtype : <EOL> key , handler = self . hotKeys [ theEvent . data1 ( ) ] <EOL> handler . callWebScriptMethod_withArguments_ ( "<STR_LIT>" , [ nil , [ ] ] ) <EOL> super ( DevonApp , self ) . sendEvent_ ( theEvent ) <EOL> def postKeyEvent ( self , character , modifierFlags = <NUM_LIT:0> ) : <EOL> keyEvent = NSEvent . keyEventWithType_location_modifierFlags_timestamp_windowNumber_context_characters_charactersIgnoringModifiers_isARepeat_keyCode_ ( NSKeyDown , ( <NUM_LIT:0> , <NUM_LIT:0> ) , modifierFlags , theEvent . timestamp ( ) , theEvent . windowNumber ( ) , None , character , None , False , <NUM_LIT:0> ) <EOL> self . postEvent_atStart_ ( keyEvent , True ) <EOL> def dispatchCommand ( self , commandName , param , args ) : <EOL> callArgs = [ ] <EOL> for key in args : <EOL> callArgs += [ key , args [ key ] ] <EOL> for name , handler in self . commands : <EOL> if name == commandName : <EOL> handler . callWebScriptMethod_withArguments_ ( "<STR_LIT>" , [ nil , [ param , callArgs ] ] ) <EOL> def buildAppleEvent_ ( self , event ) : <EOL> self . dispatchCommand ( "<STR_LIT>" , event . directParameter ( ) , event . evaluatedArguments ( ) ) <EOL> def cleanAppleEvent_ ( self , event ) : <EOL> self . dispatchCommand ( "<STR_LIT>" , event . directParameter ( ) , event . evaluatedArguments ( ) ) <EOL> def runAppleEvent_ ( self , event ) : <EOL> self . dispatchCommand ( "<STR_LIT>" , event . directParameter ( ) , event . evaluatedArguments ( ) ) <EOL> def findAppleEvent_ ( self , event ) : <EOL> self . dispatchCommand ( "<STR_LIT>" , event . directParameter ( ) , event . evaluatedArguments ( ) ) <EOL> def findNextAppleEvent_ ( self , event ) : <EOL> self . dispatchCommand ( "<STR_LIT>" , event . directParameter ( ) , event . evaluatedArguments ( ) ) <EOL> class AppDelegate ( NSObject ) : <EOL> initialProjectPath = None <EOL> window = None <EOL> webView = None <EOL> def applicationDidFinishLaunching_ ( self , aNotification ) : <EOL> win = self . window = NSWindow . alloc ( ) <EOL> win . initWithContentRect_styleMask_backing_defer_ ( ( ( <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) , <EOL> NSTitledWindowMask | NSClosableWindowMask <EOL> | NSMiniaturizableWindowMask | NSResizableWindowMask , <EOL> NSBackingStoreBuffered , YES ) <EOL> win . setTitle_ ( "<STR_LIT>" ) <EOL> win . setLevel_ ( NSNormalWindowLevel ) <EOL> webView = self . webView = WebView . alloc ( ) . initWithFrame_ ( ( ( <NUM_LIT:0.0> , <NUM_LIT:0.0> ) , ( <NUM_LIT> , <NUM_LIT> ) ) ) <EOL> win . setContentView_ ( webView ) <EOL> win . setInitialFirstResponder_ ( webView ) <EOL> self . loadDelegate = LoadDelegate . alloc ( ) . init ( ) <EOL> webView . setFrameLoadDelegate_ ( self . loadDelegate ) <EOL> self . uiDelegate = UIDelegate . alloc ( ) . init ( ) <EOL> webView . setUIDelegate_ ( self . uiDelegate ) <EOL> if self . initialProjectPath : <EOL> self . openProject ( self . initialProjectPath ) <EOL> else : <EOL> defaults = NSUserDefaults . standardUserDefaults ( ) <EOL> path = defaults . stringForKey_ ( "<STR_LIT>" ) <EOL> if path : <EOL> self . openProject ( path ) <EOL> self . restorePosition ( ) <EOL> win . makeKeyAndOrderFront_ ( nil ) <EOL> win . makeMainWindow ( ) <EOL> win . setDelegate_ ( self ) <EOL> def application_openFile_ ( self , app , path ) : <EOL> name , ext = os . path . splitext ( path ) <EOL> if ext == "<STR_LIT>" : <EOL> self . openProject ( path ) <EOL> return YES <EOL> else : <EOL> return NO <EOL> def application_openFiles_ ( self , app , paths ) : <EOL> for path in paths : <EOL> self . application_openFile_ ( app , path ) <EOL> def windowDidResize_ ( self , notification ) : <EOL> self . persistPosition ( ) <EOL> def windowDidMove_ ( self , notification ) : <EOL> self . persistPosition ( ) <EOL> def openProject ( self , projectPath ) : <EOL> if self . webView : <EOL> defaults = NSUserDefaults . standardUserDefaults ( ) <EOL> defaults . setObject_forKey_ ( projectPath , "<STR_LIT>" ) <EOL> url = "<STR_LIT>" % ( devonURL , projectPath . replace ( "<STR_LIT:/>" , "<STR_LIT::>" ) ) <EOL> request = NSURLRequest . requestWithURL_ ( NSURL . URLWithString_ ( url ) ) <EOL> self . webView . mainFrame ( ) . loadRequest_ ( request ) <EOL> else : <EOL> self . initialProjectPath = projectPath <EOL> def persistPosition ( self ) : <EOL> defaults = NSUserDefaults . standardUserDefaults ( ) <EOL> frame = self . window . frame ( ) <EOL> windowFrame = ( ( frame . origin . x , frame . origin . y ) , ( frame . size . width , frame . size . height ) ) <EOL> defaults . setObject_forKey_ ( windowFrame , "<STR_LIT>" ) <EOL> def restorePosition ( self ) : <EOL> defaults = NSUserDefaults . standardUserDefaults ( ) <EOL> frame = defaults . arrayForKey_ ( "<STR_LIT>" ) <EOL> if not frame : <EOL> frame = defaultWindowFrame <EOL> self . window . setFrame_display_ ( frame , YES ) <EOL> class LoadDelegate ( NSObject , protocols . WebFrameLoadDelegate ) : <EOL> def webView_didFailLoadWithError_forFrame_ ( self , webView , error , frame ) : <EOL> print "<STR_LIT>" , error <EOL> def webView_didFailProvisionalLoadWithError_forFrame_ ( self , webView , error , frame ) : <EOL> print "<STR_LIT>" , error <EOL> def webView_didFinishLoadForFrame_ ( self , webView , frame ) : <EOL> pass <EOL> def webView_didClearWindowObject_forFrame_ ( self , webView , window , frame ) : <EOL> if not frame . parentFrame ( ) : <EOL> window . setValue_forKey_ ( self , "<STR_LIT>" ) <EOL> def webView_didReceiveTitle_forFrame_ ( self , webView , title , frame ) : <EOL> if not frame . parentFrame ( ) : <EOL> NSApp ( ) . delegate ( ) . window . setTitle_ ( title ) <EOL> def isKeyExcludedFromWebScript_ ( self , key ) : <EOL> return YES <EOL> def isSelectorExcludedFromWebScript_ ( self , sel ) : <EOL> if str ( sel ) in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] : <EOL> return NO <EOL> else : <EOL> return YES <EOL> def activate ( self ) : <EOL> NSApp ( ) . activateIgnoringOtherApps_ ( YES ) <EOL> def bounce ( self , forever ) : <EOL> NSApp ( ) . requestUserAttention_ ( NSCriticalRequest if forever else NSInformationRequest ) <EOL> def registerCommand ( self , name , handler ) : <EOL> NSApp ( ) . commands += [ ( name , handler ) ] <EOL> def unregisterCommand ( self , name , handler ) : <EOL> pass <EOL> def registerHotKey ( self , hotKeyName , handler ) : <EOL> keyCode , flags = hotKeyToVirtualCodes ( hotKeyName ) <EOL> key = RegisterEventHotKey ( keyCode , flags , ( <NUM_LIT:0> , <NUM_LIT:0> ) , GetApplicationEventTarget ( ) , <NUM_LIT:0> ) <EOL> app = DevonApp . sharedApplication ( ) <EOL> app . hotKeys [ HotKeyAddress ( key ) ] = ( key , handler ) <EOL> def unregisterHotKey ( self , hotKeyName , handler ) : <EOL> app = DevonApp . sharedApplication ( ) <EOL> for address in app . hotKeys : <EOL> key , theHandler = app . hotKeys [ address ] <EOL> if handler == theHandler : <EOL> key . UnregisterEventHotKey ( ) <EOL> class UIDelegate ( NSObject , protocols . WebUIDelegate ) : <EOL> def webView_runJavaScriptAlertPanelWithMessage_initiatedByFrame_ ( self , webView , message , frame ) : <EOL> print message <EOL> def main ( ) : <EOL> thread . start_new_thread ( serve , ( ) ) <EOL> app = DevonApp . sharedApplication ( ) <EOL> delegate = AppDelegate . alloc ( ) . init ( ) <EOL> app . setDelegate_ ( delegate ) <EOL> app . run ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> print "<STR_LIT>" <EOL> main ( ) </s>
