summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJohan Dahlin <johan@gnome.org>2008-07-28 22:06:26 +0000
committerJohan Dahlin <johan@src.gnome.org>2008-07-28 22:06:26 +0000
commit05461cad1d58f4c0de73a63a69f260a01286e08e (patch)
tree159dd7ab7b5f46d648e42a346eedbba471c247fd /examples
parent6151bbd9f69c6131be611267c887eb130607e569 (diff)
downloadgobject-introspection-05461cad1d58f4c0de73a63a69f260a01286e08e.tar.gz
Add a new example
2008-07-29 Johan Dahlin <johan@gnome.org> * examples/gio/downloader.py: Add a new example * gio/ginputstream.override: Use a string internally instead of a PyStringObject when in read_async. Create a new python string in finish and honor the number of bytes read. svn path=/trunk/; revision=892
Diffstat (limited to 'examples')
-rw-r--r--examples/gio/downloader.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/examples/gio/downloader.py b/examples/gio/downloader.py
new file mode 100644
index 00000000..36c803bb
--- /dev/null
+++ b/examples/gio/downloader.py
@@ -0,0 +1,61 @@
+# Example GIO based Asynchronous downloader
+
+import sys
+
+import glib
+import glib.option
+import gio
+
+
+class Downloader(object):
+ def __init__(self, uri):
+ self.total = 0
+ self.gfile = gio.File(uri)
+ self.loop = glib.MainLoop()
+
+ output = self.get_output_filename()
+ self.fd = open(output, 'w')
+ print 'Downloading %s -> %s' % (uri, output)
+
+ self.gfile.read_async(self.read_callback)
+
+ def get_output_filename(self):
+ basename = self.gfile.get_basename()
+ if basename == '/':
+ basename = 'index.html'
+ return basename
+
+ def stream_read_callback(self, stream, result):
+ data = stream.read_finish(result)
+ if not data:
+ self.data_finished()
+ return
+ self.data_read(data)
+ stream.read_async(4096, self.stream_read_callback)
+
+
+ def read_callback(self, gfile, result):
+ stream = gfile.read_finish(result)
+ stream.read_async(4096, self.stream_read_callback)
+
+ def data_read(self, data):
+ self.fd.write(data)
+ self.total += len(data)
+
+ def data_finished(self):
+ print '%d bytes read' % (self.total,)
+ self.loop.quit()
+
+ def run(self):
+ self.loop.run()
+
+def main(args):
+ if len(args) < 2:
+ print 'Needs a URI'
+ return 1
+
+ d = Downloader(args[1])
+ d.run()
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv))