Logo Search packages:      
Sourcecode: calibre version File versions

def calibre::devices::prs500::driver::PRS500::put_file (   self,
  infile,
  path,
  replace_file = False,
  end_session = True 
)

Put infile onto the devoce at path
@param infile: An open file object. infile must have a name attribute.
    If you are using a StringIO object set its name attribute manually.
@param path: The path on the device at which to put infile.
    It should point to an existing directory.
@param replace_file: If True and path points to a file that already exists, it is replaced

Definition at line 677 of file driver.py.

00677                                                                           :
        """
        Put infile onto the devoce at path
        @param infile: An open file object. infile must have a name attribute.
            If you are using a StringIO object set its name attribute manually.
        @param path: The path on the device at which to put infile.
            It should point to an existing directory.
        @param replace_file: If True and path points to a file that already exists, it is replaced
        """
        pos = infile.tell()
        infile.seek(0, 2)
        bytes = infile.tell() - pos
        start_pos = pos
        infile.seek(pos)
        cp = self.card_prefix(False)
        path = path.replace('card:/', cp if cp else '')
        exists, dest = self._exists(path)
        if exists:
            if dest.is_dir:
                if not path.endswith("/"):
                    path += "/"
                path += os.path.basename(infile.name)
                return self.put_file(infile, path, replace_file=replace_file, end_session=False)
            else:
                if not replace_file:
                    raise PathError("Cannot write to " + \
                                    path + " as it already exists", path=path)
                _file = self.path_properties(path, end_session=False)
                if _file.file_size > bytes:
                    self.del_file(path, end_session=False)
                    self.touch(path, end_session=False)
        else:  self.touch(path, end_session=False)
        chunk_size = 512 * self.bulk_write_max_packet_size
        data_left = True
        res = self.send_validated_command(FileOpen(path, mode=FileOpen.WRITE))
        if res.code != 0:
            raise ProtocolError("Unable to open " + path + \
                        " for writing. Response code: " + hex(res.code))
        _id = self._bulk_read(20, data_type=IdAnswer, \
                                command_number=FileOpen.NUMBER)[0].id

        while data_left:
            data = array('B')
            try:
                # Cannot use data.fromfile(infile, chunk_size) as it
                # doesn't work in windows w/ python 2.5.1
                ind = infile.read(chunk_size)
                data.fromstring(ind)
                if len(ind) < chunk_size:
                    raise EOFError
            except EOFError:
                data_left = False
            res = self.send_validated_command(FileIO(_id, pos, len(data), \
                                mode=FileIO.WNUMBER))
            if res.code != 0:
                raise ProtocolError("Unable to write to " + \
                                    path + ". Response code: " + hex(res.code))
            self._bulk_write(data)
            pos += len(data)
            if self.report_progress:
                self.report_progress( int(100*(pos-start_pos)/(1.*bytes)) )
        self.send_validated_command(FileClose(_id))
        # Ignore res.code as cant do anything if close fails
        _file = self.path_properties(path, end_session=False)
        if _file.file_size != pos:
            raise ProtocolError("Copying to device failed. The file " +\
                    "on the device is larger by " + \
                    str(_file.file_size - pos) + " bytes")

    @safe
    def del_file(self, path, end_session=True):


Generated by  Doxygen 1.6.0   Back to index