forked from openai/openai-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_progress.py
More file actions
52 lines (37 loc) · 1.16 KB
/
upload_progress.py
File metadata and controls
52 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import io
class CancelledError(Exception):
def __init__(self, msg):
self.msg = msg
Exception.__init__(self, msg)
def __str__(self):
return self.msg
__repr__ = __str__
class BufferReader(io.BytesIO):
def __init__(self, buf=b"", desc=None):
self._len = len(buf)
io.BytesIO.__init__(self, buf)
self._progress = 0
self._callback = progress(len(buf), desc=desc)
def __len__(self):
return self._len
def read(self, n=-1):
chunk = io.BytesIO.read(self, n)
self._progress += len(chunk)
if self._callback:
try:
self._callback(self._progress)
except Exception as e: # catches exception from the callback
raise CancelledError("The upload was cancelled: {}".format(e))
return chunk
def progress(total, desc):
import tqdm # type: ignore
meter = tqdm.tqdm(total=total, unit_scale=True, desc=desc)
def incr(progress):
meter.n = progress
if progress == total:
meter.close()
else:
meter.refresh()
return incr
def MB(i):
return int(i // 1024**2)