コード例 #1
0
ファイル: deuce.py プロジェクト: painterjd/deuce-client
    def FinalizeFile(self, vault, file_id):
        """Finalize the file in the vault

        :param vault: vault containing the file
        :param file_id: file_id of the file to finalize

        :returns: True on success
        """
        if file_id not in vault.files:
            raise KeyError('file_id must specify a file in the provided Vault')

        url = api_v1.get_file_path(vault.vault_id, file_id)
        self.ReInit(self.sslenabled, url)
        self.__update_headers()
        headers = {}
        headers.update(self.Headers)
        headers['X-File-Length'] = len(vault.files[file_id])
        self.__log_request_data(fn='Finalize File')
        res = requests.post(self.Uri, headers=headers)
        self.__log_response_data(res, jsondata=True, fn='Finalize File')
        if res.status_code in (200, 204):
            return True
        else:
            raise RuntimeError(
                'Failed to finalize file. Details: {0}'
                .format(res.json()))
コード例 #2
0
ファイル: deuce.py プロジェクト: painterjd/deuce-client
    def DownloadFile(self, vault, file_id, output_file, chunk_size=512 * 1024):
        """Download a file

        :param vault: vault to download the file from
        :param file_id: file id within the vault to download
        :param output_file: local fully qualified (absolute) file name to
                            store the file in
        :returns: True on success
        """
        url = api_v1.get_file_path(vault.vault_id, file_id)
        self.ReInit(self.sslenabled, url)
        self.__update_headers()
        self.__log_request_data(fn='Download File')
        res = requests.get(self.Uri, headers=self.Headers, stream=True)
        if res.status_code == 200:
            try:
                downloaded_bytes = 0
                download_start_time = datetime.datetime.utcnow()
                with open(output_file, 'wb') as output:
                    for chunk in res.iter_content(chunk_size=chunk_size):
                        output.write(chunk)
                        downloaded_bytes = downloaded_bytes + len(chunk)
                        res.raise_for_status()
                download_end_time = datetime.datetime.utcnow()

                download_time = download_end_time - download_start_time
                download_rate = downloaded_bytes / download_time\
                    .total_seconds()

                log = logging.getLogger(__name__)
                log.info('Downloaded {0:} bytes in {1:} seconds for {2:} bps, '
                         '{3:} kbps, {4:} mbps'
                         .format(downloaded_bytes, download_time,
                                 download_rate,
                                 download_rate / 1024,
                                 download_rate / 1024 / 1024))

                # succeeded in downloading the file
                return True

            except Exception as ex:
                raise RuntimeError(
                    'Failed while Downloading File. '
                    'Error: {0:} '.format(ex))
        else:
            raise RuntimeError(
                'Failed to Download File. '
                'Error ({0:}): {1:}'.format(res.status_code, res.text))
コード例 #3
0
ファイル: deuce.py プロジェクト: painterjd/deuce-client
    def DeleteFile(self, vault, file_id):
        """Delete a file

        :param vault: vault to download the file from
        :param file_id: file id within the vault to be deleted
        """
        url = api_v1.get_file_path(vault.vault_id, file_id)
        self.ReInit(self.sslenabled, url)
        self.__update_headers()
        self.__log_request_data(fn='Delete File')
        res = requests.delete(self.Uri, headers=self.Headers)
        self.__log_response_data(res, jsondata=False, fn='Delete File')
        if res.status_code == 204:
            return True
        else:
            raise RuntimeError(
                'Failed to Delete File. '
                'Error ({0:}): {1:}'.format(res.status_code, res.text))
コード例 #4
0
ファイル: deuce.py プロジェクト: BenjamenMeyer/deuce-client
    def ListFiles(self, vault, marker=None, limit=None):
        """List files in the Vault

        :param vault: vault to list the files from
        :param marker: fileid within the list to start at
        :param limit: the maximum number of entries to retrieve
        :returns: a list of file ids in the vault
        """
        url = api_v1.get_files_path(vault.vault_id)

        query_args = {}

        if marker and limit:
            query_args = {
                'marker': marker,
                'limit': limit,
            }

        elif marker:
            query_args = {
                'marker': marker
            }
        elif limit:
            query_args = {
                'limit': limit
            }
        else:
            pass

        ret_url = set_qs_on_url(url, query_args)
        self.ReInit(self.sslenabled, ret_url)
        self.__update_headers()
        self.__log_request_data(fn='List Files')
        res = requests.get(self.Uri, headers=self.Headers)
        self.__log_response_data(res, jsondata=True, fn='List Files')

        if res.status_code == 200:
            if 'x-next-batch' in res.headers:
                parsed_url = urlparse(res.headers['x-next-batch'])

                qs = parse_qs(parsed_url[4])
                vault.files.marker = qs['marker'][0]
            else:
                vault.files.marker = None

            return_list = []
            for file_id in res.json():
                return_list.append(file_id)

                file_uri = api_v1.get_file_path(vault.vault_id, file_id)
                self.ReInit(self.sslenabled, url)
                file_url = self.Uri

                kw = {
                    'project_id': self.project_id,
                    'vault_id': vault.vault_id,
                    'file_id': file_id,
                    'url': file_url
                }
                vault.files[file_id] = api_file.File(**kw)

            return return_list

        else:
            raise RuntimeError(
                'Failed to List Files in the Vault. '
                'Error ({0:}): {1:}'.format(res.status_code, res.text))