|
1 | 1 | import asyncio |
2 | 2 | import json |
| 3 | +import logging |
3 | 4 | import math |
4 | 5 | import random |
5 | 6 | import shutil |
|
17 | 18 | from osparc_client import FilesApi as _FilesApi |
18 | 19 | from osparc_client import FileUploadCompletionBody, FileUploadData, UploadedPart |
19 | 20 | from tqdm.asyncio import tqdm |
| 21 | +from tqdm.contrib.logging import logging_redirect_tqdm |
20 | 22 |
|
21 | 23 | from . import ApiClient, File |
22 | 24 | from ._http_client import AsyncHttpClient |
|
28 | 30 | file_chunk_generator, |
29 | 31 | ) |
30 | 32 |
|
| 33 | +_logger = logging.getLogger(__name__) |
| 34 | + |
31 | 35 |
|
32 | 36 | class FilesApi(_FilesApi): |
33 | 37 | """Class for interacting with files""" |
@@ -113,41 +117,44 @@ async def upload_file_async( |
113 | 117 | ) |
114 | 118 |
|
115 | 119 | uploaded_parts: list[UploadedPart] = [] |
116 | | - print("- uploading chunks...") |
117 | 120 | async with AsyncHttpClient( |
118 | 121 | configuration=self.api_client.configuration, timeout=timeout_seconds |
119 | 122 | ) as session: |
120 | | - async for chunck, size in tqdm( |
121 | | - file_chunk_generator(file, chunk_size), total=n_urls |
122 | | - ): |
123 | | - index, url = next(url_iter) |
124 | | - uploaded_parts.append( |
125 | | - await self._upload_chunck( |
126 | | - http_client=session, |
127 | | - chunck=chunck, |
128 | | - chunck_size=size, |
129 | | - upload_link=url, |
130 | | - index=index, |
| 123 | + with logging_redirect_tqdm(): |
| 124 | + _logger.info("Uploading %i chunks", n_urls) |
| 125 | + async for chunck, size in tqdm( |
| 126 | + file_chunk_generator(file, chunk_size), |
| 127 | + total=n_urls, |
| 128 | + disable=(not _logger.isEnabledFor(logging.INFO)), |
| 129 | + ): |
| 130 | + index, url = next(url_iter) |
| 131 | + uploaded_parts.append( |
| 132 | + await self._upload_chunck( |
| 133 | + http_client=session, |
| 134 | + chunck=chunck, |
| 135 | + chunck_size=size, |
| 136 | + upload_link=url, |
| 137 | + index=index, |
| 138 | + ) |
131 | 139 | ) |
132 | | - ) |
133 | 140 |
|
134 | | - async with AsyncHttpClient( |
135 | | - configuration=self.api_client.configuration, |
136 | | - request_type="post", |
137 | | - url=links.abort_upload, |
138 | | - base_url=self.api_client.configuration.host, |
139 | | - follow_redirects=True, |
140 | | - auth=self._auth, |
141 | | - timeout=timeout_seconds, |
142 | | - ) as session: |
143 | | - print( |
144 | | - "- completing upload (this might take a couple of minutes)..." |
145 | | - ) |
146 | | - server_file: File = await self._complete_multipart_upload( |
147 | | - session, links.complete_upload, client_file, uploaded_parts |
148 | | - ) |
149 | | - print("- file upload complete") |
150 | | - return server_file |
| 141 | + async with AsyncHttpClient( |
| 142 | + configuration=self.api_client.configuration, |
| 143 | + request_type="post", |
| 144 | + url=links.abort_upload, |
| 145 | + base_url=self.api_client.configuration.host, |
| 146 | + follow_redirects=True, |
| 147 | + auth=self._auth, |
| 148 | + timeout=timeout_seconds, |
| 149 | + ) as session: |
| 150 | + _logger.info( |
| 151 | + "Completing upload (this might take a couple of minutes)..." |
| 152 | + ) |
| 153 | + server_file: File = await self._complete_multipart_upload( |
| 154 | + session, links.complete_upload, client_file, uploaded_parts |
| 155 | + ) |
| 156 | + _logger.info("File upload complete") |
| 157 | + return server_file |
151 | 158 |
|
152 | 159 | async def _complete_multipart_upload( |
153 | 160 | self, |
|
0 commit comments