comparison src/plugins/plugin_misc_file.py @ 1607:4741e2f5eed2

plugin file: progressFinished and progressError are now sent on SatFile.close. eof Deferred attribute is removed, as it is not used and bring unnecessary complication
author Goffi <goffi@goffi.org>
date Mon, 16 Nov 2015 00:24:49 +0100
parents 33728a2f17bf
children 3ec7511dbf28
comparison
equal deleted inserted replaced
1606:de785fcf9a7b 1607:4741e2f5eed2
66 self.host = host 66 self.host = host
67 self.uid = uid or unicode(uuid.uuid4()) 67 self.uid = uid or unicode(uuid.uuid4())
68 self._file = open(path, mode) 68 self._file = open(path, mode)
69 self.size = size 69 self.size = size
70 self.profile = profile 70 self.profile = profile
71 self.eof = defer.Deferred()
72 self.host.registerProgressCb(self.uid, self.getProgress, profile) 71 self.host.registerProgressCb(self.uid, self.getProgress, profile)
73 self.host.bridge.progressStarted(self.uid, self.profile) 72 self.host.bridge.progressStarted(self.uid, self.profile)
74 self.eof.addCallback(lambda ignore: self.host.bridge.progressFinished(self.uid, self.profile))
75 self.eof.addErrback(lambda failure: self.host.bridge.progressError(self.uid, unicode(failure), self.profile))
76 73
77 def close(self): 74 def close(self):
75 position = self._file.tell()
78 self._file.close() 76 self._file.close()
77 if not self.size or self.size == position:
78 self.host.bridge.progressFinished(self.uid, self.profile)
79 else:
80 self.host.bridge.progressError(self.uid, u"size doesn't match", self.profile)
79 self.host.removeProgressCb(self.uid, self.profile) 81 self.host.removeProgressCb(self.uid, self.profile)
80 82
81 def flush(self): 83 def flush(self):
82 self._file.flush() 84 self._file.flush()
83 85
84 def write(self, buf): 86 def write(self, buf):
85 self._file.write(buf) 87 self._file.write(buf)
86 88
87 def read(self, size=-1): 89 def read(self, size=-1):
88 read = self._file.read(size) 90 read = self._file.read(size)
89 if not read:
90 self.eof.callback(None)
91 return read 91 return read
92 92
93 def seek(self, offset, whence=os.SEEK_SET): 93 def seek(self, offset, whence=os.SEEK_SET):
94 self._file.seek(offset, whence) 94 self._file.seek(offset, whence)
95 95