Esempio n. 1
0
    def read(self, size=-1):
        """Read at most `size` bytes from the file (less if there
        isn't enough data).

        The bytes are returned as an instance of :class:`str`. If
        `size` is negative or omitted all data is read.

        :Parameters:
          - `size` (optional): the number of bytes to read
        """
        if size:
            remainder = int(self.length) - self.__position
            if size < 0 or size > remainder:
                size = remainder

            data = self.__buffer
            chunk_number = (len(data) + self.__position) / self.chunk_size

            while len(data) < size:
                chunk = yield self.__chunks.find_one({"files_id": self._id,
                                                      "n": chunk_number})
                if not chunk:
                    raise CorruptGridFile("no chunk #%d" % chunk_number)

                if not data:
                    data += chunk["data"][self.__position % self.chunk_size:]
                else:
                    data += chunk["data"]

                chunk_number += 1

            self.__position += size
            to_return = data[:size]
            self.__buffer = data[size:]
            defer.returnValue(to_return)
Esempio n. 2
0
 def next(self):
     if self.__current_chunk >= self.__max_chunk:
         raise StopIteration
     chunk = yield self.__chunks.find_one({"files_id": self.__id,
                                     "n": self.__current_chunk})
     if not chunk:
         raise CorruptGridFile("no chunk #%d" % self.__current_chunk)
     self.__current_chunk += 1
     defer.returnValue(str(chunk["data"]))
Esempio n. 3
0
 def __next__(self):
     if self.__current_chunk >= self.__max_chunk:
         raise StopIteration
     chunk = yield self.__chunks.find_one({"files_id": self.__id,
                                           "n": self.__current_chunk})
     if not chunk:
         raise CorruptGridFile("TxMongo: no chunk #{0}".format(self.__current_chunk))
     self.__current_chunk += 1
     defer.returnValue(bytes(chunk["data"]))
Esempio n. 4
0
        def process(chunk):
            if not chunk:
                raise CorruptGridFile("TxMongo: no chunk #{0}".format(state.chunk_number))

            if not state.data:
                state.data += chunk["data"][self.__position % self.chunk_size:]
            else:
                state.data += chunk["data"]

            state.chunk_number += 1
Esempio n. 5
0
 def ok(chunk):
     if not chunk:
         raise CorruptGridFile("TxMongo: no chunk #{0}".format(self.__current_chunk))
     self.__current_chunk += 1
     return bytes(chunk["data"])