forked from wolph/python-progressbar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
146 lines (120 loc) · 3.95 KB
/
utils.py
File metadata and controls
146 lines (120 loc) · 3.95 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
import math
def timedelta_to_seconds(delta):
'''Convert a timedelta to seconds with the microseconds as fraction
>>> from datetime import timedelta
>>> '%d' % timedelta_to_seconds(timedelta(days=1))
'86400'
>>> '%d' % timedelta_to_seconds(timedelta(seconds=1))
'1'
>>> '%.6f' % timedelta_to_seconds(timedelta(seconds=1, microseconds=1))
'1.000001'
>>> '%.6f' % timedelta_to_seconds(timedelta(microseconds=1))
'0.000001'
'''
# Only convert to float if needed
if delta.microseconds:
total = delta.microseconds * 1e-6
else:
total = 0
total += delta.seconds
total += delta.days * 60 * 60 * 24
return total
def scale_1024(x, n_prefixes):
'''Scale a number down to a suitable size, based on powers of 1024.
Returns the scaled number and the power of 1024 used.
Use to format numbers of bytes to KiB, MiB, etc.
>>> scale_1024(310, 3)
(310.0, 0)
>>> scale_1024(2048, 3)
(2.0, 1)
'''
power = min(int(math.log(x, 2) / 10), n_prefixes - 1)
scaled = float(x) / (2 ** (10 * power))
return scaled, power
def get_terminal_size(): # pragma: no cover
'''Get the current size of your terminal
Based on an activestate recipe: http://code.activestate.com/recipes/440694/
'''
import platform
system = platform.system().lower()
size = None
try:
# This works for Python 3, but not Pypy3. Probably the best method if
# it's supported so let's always try
import shutil
h, w = shutil.get_terminal_size((0, 0))
if h and w:
size = w, h
except:
pass
if not size:
if system == 'windows':
size = _get_terminal_size_windows()
if size is None:
# needed for window's python in cygwin's xterm!
size = _get_terminal_size_tput()
elif system in ('linux', 'darwin') or system.startswith('cygwin'):
size = _get_terminal_size_linux()
return size or (80, 25)
def _get_terminal_size_windows(): # pragma: no cover
res = None
try:
from ctypes import windll, create_string_buffer
# stdin handle is -10
# stdout handle is -11
# stderr handle is -12
h = windll.kernel32.GetStdHandle(-12)
csbi = create_string_buffer(22)
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
except:
return None
if res:
import struct
(_, _, _, _, _, left, top, right, bottom, _, _) = \
struct.unpack("hhhhHhhhhhh", csbi.raw)
w = right - left + 1
h = bottom - top + 1
return w, h
else:
return None
def _get_terminal_size_tput(): # pragma: no cover
# get terminal width src: http://stackoverflow.com/questions/263890/
try:
import subprocess
proc = subprocess.Popen(
['tput', 'w'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output = proc.communicate(input=None)
w = int(output[0])
proc = subprocess.Popen(
['tput', 'lines'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output = proc.communicate(input=None)
h = int(output[0])
return w, h
except:
return None
def _get_terminal_size_linux(): # pragma: no cover
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
import struct
size = struct.unpack(
'hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
except:
return None
return size
size = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not size:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
size = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not size:
try:
size = os.environ['LINES'], os.environ['COLUMNS']
except:
return None
return int(size[1]), int(size[0])