1 | import tempfile |
---|
2 | |
---|
3 | from gi.repository import GObject |
---|
4 | from gi.repository import Gio |
---|
5 | from gi.repository import GLib |
---|
6 | |
---|
7 | # Avoid "Fatal Python error: GC object already tracked" |
---|
8 | # http://stackoverflow.com/questions/7496629/gstreamer-appsrc-causes-random-crashes |
---|
9 | GObject.threads_init() |
---|
10 | |
---|
11 | |
---|
12 | class FileTransfer(GObject.GObject): |
---|
13 | _CHUNK_SIZE = 10240 |
---|
14 | |
---|
15 | def __init__(self, input_stream, output_stream): |
---|
16 | GObject.GObject.__init__(self) |
---|
17 | |
---|
18 | self._input_stream = input_stream |
---|
19 | self._output_stream = output_stream |
---|
20 | self._pending_buffers = [] |
---|
21 | |
---|
22 | def start(self): |
---|
23 | self._input_stream.read_bytes_async( |
---|
24 | self._CHUNK_SIZE, GLib.PRIORITY_LOW, |
---|
25 | None, self.__read_async_cb, None) |
---|
26 | |
---|
27 | def __read_async_cb(self, input_stream, result, user_data=None): |
---|
28 | data = input_stream.read_bytes_finish(result) |
---|
29 | print 'Read Size:', data, 'Type:', type(data) |
---|
30 | print 'Result:', result, 'Propagate ERROR:', result.propagate_error() |
---|
31 | |
---|
32 | if data is None: |
---|
33 | # TODO: an error occured. Report something |
---|
34 | print 'An error occured' |
---|
35 | elif data.get_size() == 0: |
---|
36 | # We read the file completely |
---|
37 | print 'Closing the File.' |
---|
38 | self._input_stream.close(None) |
---|
39 | else: |
---|
40 | self._pending_buffers.append(data) |
---|
41 | self._input_stream.read_bytes_async( |
---|
42 | self._CHUNK_SIZE, GLib.PRIORITY_LOW, |
---|
43 | None, self.__read_async_cb, None) |
---|
44 | self._write_next_buffer() |
---|
45 | |
---|
46 | def __write_async_cb(self, output_stream, result, user_data=None): |
---|
47 | size = self._output_stream.write_bytes_finish(result) |
---|
48 | print 'Size written:', size |
---|
49 | |
---|
50 | if not self._pending_buffers and \ |
---|
51 | not self._output_stream.has_pending() and \ |
---|
52 | not self._input_stream.has_pending(): |
---|
53 | output_stream.close(None) |
---|
54 | print 'DONE' |
---|
55 | loop.quit() |
---|
56 | else: |
---|
57 | self._write_next_buffer() |
---|
58 | |
---|
59 | def _write_next_buffer(self): |
---|
60 | |
---|
61 | if self._pending_buffers and not self._output_stream.has_pending(): |
---|
62 | data = self._pending_buffers.pop(0) |
---|
63 | self._output_stream.write_bytes_async( |
---|
64 | data, GLib.PRIORITY_LOW, None, self.__write_async_cb, |
---|
65 | None) |
---|
66 | |
---|
67 | |
---|
68 | test_file_name = '/home/humitos/mozilla.pdf' |
---|
69 | test_input_stream = Gio.File.new_for_path(test_file_name).read(None) |
---|
70 | PATH = tempfile.mkstemp()[1] |
---|
71 | print PATH |
---|
72 | |
---|
73 | test_output_stream = Gio.File.new_for_path(PATH)\ |
---|
74 | .append_to(Gio.FileCreateFlags.PRIVATE, None) |
---|
75 | |
---|
76 | splicer = FileTransfer(test_input_stream, test_output_stream) |
---|
77 | splicer.start() |
---|
78 | |
---|
79 | loop = GObject.MainLoop() |
---|
80 | loop.run() |
---|