summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorJohan Dahlin <johan@src.gnome.org>2008-07-29 07:21:16 +0000
committerJohan Dahlin <johan@src.gnome.org>2008-07-29 07:21:16 +0000
commit7242e335e4c0c4ab94d1e83cd49085618c692e1e (patch)
treebbc9d219e90a334f0a2f943500a4342b763387dc /examples
parente999b5d16cc345beaf81314916b2584bd3a5ee22 (diff)
downloadpygobject-7242e335e4c0c4ab94d1e83cd49085618c692e1e.tar.gz
pygobject-7242e335e4c0c4ab94d1e83cd49085618c692e1e.tar.xz
pygobject-7242e335e4c0c4ab94d1e83cd49085618c692e1e.zip
Refactor the example a bit, use start/stop to access loop so it's easier to reuse it in a real application
svn path=/trunk/; revision=894
Diffstat (limited to 'examples')
-rw-r--r--examples/gio/downloader.py31
1 files changed, 19 insertions, 12 deletions
diff --git a/examples/gio/downloader.py b/examples/gio/downloader.py
index 4da4f53..528016d 100644
--- a/examples/gio/downloader.py
+++ b/examples/gio/downloader.py
@@ -10,13 +10,11 @@ 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)
+ self.uri = uri
+ self.loop = None
+ self.gfile = gio.File(self.uri)
+ self.output = self.get_output_filename()
+ self.fd = None
def get_output_filename(self):
basename = self.gfile.get_basename()
@@ -37,28 +35,37 @@ class Downloader(object):
stream = gfile.read_finish(result)
except gio.Error, e:
print 'ERROR: %s' % (e.message,)
- self.loop.quit()
+ self.stop()
return
stream.read_async(4096, self.stream_read_callback)
def data_read(self, data):
+ if self.fd is None:
+ self.fd = open(self.output, 'w')
self.fd.write(data)
self.total += len(data)
def data_finished(self):
- print '%d bytes read' % (self.total,)
- self.loop.quit()
+ print '%d bytes read.' % (self.total,)
+ self.fd.close()
+ self.stop()
- def run(self):
+ def start(self):
+ print 'Downloading %s -> %s' % (self.uri, self.output)
+ self.gfile.read_async(self.read_callback)
+ self.loop = glib.MainLoop()
self.loop.run()
+ def stop(self):
+ self.loop.quit()
+
def main(args):
if len(args) < 2:
print 'Needs a URI'
return 1
d = Downloader(args[1])
- d.run()
+ d.start()
if __name__ == '__main__':
sys.exit(main(sys.argv))