Bob Ippolito avatar Bob Ippolito committed 921c4ed

stdin reader example

Comments (0)

Files changed (1)

Examples/Scripts/stdinreader.py

+from Foundation import *
+from PyObjCTools import AppHelper
+
+class FileObserver(NSObject):
+    def initWithFileDescriptor_readCallback_errorCallback_(self,
+            fileDescriptor, readCallback, errorCallback):
+        self = self.init()
+        self.readCallback = readCallback
+        self.errorCallback = errorCallback
+        self.fileHandle = NSFileHandle.alloc().initWithFileDescriptor_(
+            fileDescriptor)
+        self.nc = NSNotificationCenter.defaultCenter()
+        self.nc.addObserver_selector_name_object_(
+            self,
+            'fileHandleReadCompleted:',
+            NSFileHandleReadCompletionNotification,
+            self.fileHandle)
+        self.fileHandle.readInBackgroundAndNotify()
+        return self
+
+    def fileHandleReadCompleted_(self, aNotification):
+        ui = aNotification.userInfo()
+        newData = ui.objectForKey_(NSFileHandleNotificationDataItem)
+        if newData is None:
+            if self.errorCallback is not None:
+                self.errorCallback(self, ui.objectForKey_(NSFileHandleError))
+            self.close()
+        else:
+            self.fileHandle.readInBackgroundAndNotify()
+            if self.readCallback is not None:
+                self.readCallback(self, newData.bytes()[:])
+
+    def close(self):
+        self.nc.removeObserver_(self)
+        if self.fileHandle is not None:
+            self.fileHandle.closeFile()
+            self.fileHandle = None
+        # break cycles in case these functions are closed over
+        # an instance of us
+        self.readCallback = None
+        self.errorCallback = None
+
+    def __del__(self):
+        # Without this, if a notification fires after we are GC'ed
+        # then the app will crash because NSNotificationCenter
+        # doesn't retain observers.  In this example, it doesn't
+        # matter, but it's worth pointing out.
+        self.close()
+
+def prompt():
+    sys.stdout.write("write something: ")
+    sys.stdout.flush()
+
+def gotLine(observer, aLine):
+    if aLine:
+        print "you wrote:", aLine.rstrip()
+        prompt()
+    else:
+        print ""
+        AppHelper.stopEventLoop()
+
+def gotError(observer, err):
+    print "error:", err
+    AppHelper.stopEventLoop()
+
+if __name__ == '__main__':
+    import sys
+    observer = FileObserver.alloc().initWithFileDescriptor_readCallback_errorCallback_(
+        sys.stdin.fileno(), gotLine, gotError)
+    prompt()
+    AppHelper.runConsoleEventLoop(installInterrupt=True)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.