Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | # Text progress bar library, like curl or scp. |
michael@0 | 2 | |
michael@0 | 3 | from datetime import datetime, timedelta |
michael@0 | 4 | import math |
michael@0 | 5 | import sys |
michael@0 | 6 | |
michael@0 | 7 | if sys.platform.startswith('win'): |
michael@0 | 8 | from terminal_win import Terminal |
michael@0 | 9 | else: |
michael@0 | 10 | from terminal_unix import Terminal |
michael@0 | 11 | |
michael@0 | 12 | class NullProgressBar(object): |
michael@0 | 13 | def update(self, current, data): pass |
michael@0 | 14 | def poke(self): pass |
michael@0 | 15 | def finish(self, complete=True): pass |
michael@0 | 16 | def beginline(self): pass |
michael@0 | 17 | def message(self, msg): sys.stdout.write(msg + '\n') |
michael@0 | 18 | def update_granularity(self): return timedelta.max |
michael@0 | 19 | |
michael@0 | 20 | class ProgressBar(object): |
michael@0 | 21 | def __init__(self, limit, fmt): |
michael@0 | 22 | assert self.conservative_isatty() |
michael@0 | 23 | |
michael@0 | 24 | self.prior = None |
michael@0 | 25 | self.atLineStart = True |
michael@0 | 26 | self.counters_fmt = fmt # [{str:str}] Describtion of how to lay out each |
michael@0 | 27 | # field in the counters map. |
michael@0 | 28 | self.limit = limit # int: The value of 'current' equal to 100%. |
michael@0 | 29 | self.limit_digits = int(math.ceil(math.log10(self.limit))) # int: max digits in limit |
michael@0 | 30 | self.t0 = datetime.now() # datetime: The start time. |
michael@0 | 31 | |
michael@0 | 32 | # Compute the width of the counters and build the format string. |
michael@0 | 33 | self.counters_width = 1 # [ |
michael@0 | 34 | for layout in self.counters_fmt: |
michael@0 | 35 | self.counters_width += self.limit_digits |
michael@0 | 36 | self.counters_width += 1 # | (or ']' for the last one) |
michael@0 | 37 | |
michael@0 | 38 | self.barlen = 64 - self.counters_width |
michael@0 | 39 | |
michael@0 | 40 | def update_granularity(self): |
michael@0 | 41 | return timedelta(seconds=0.1) |
michael@0 | 42 | |
michael@0 | 43 | def update(self, current, data): |
michael@0 | 44 | # Record prior for poke. |
michael@0 | 45 | self.prior = (current, data) |
michael@0 | 46 | self.atLineStart = False |
michael@0 | 47 | |
michael@0 | 48 | # Build counters string. |
michael@0 | 49 | sys.stdout.write('\r[') |
michael@0 | 50 | for layout in self.counters_fmt: |
michael@0 | 51 | Terminal.set_color(layout['color']) |
michael@0 | 52 | sys.stdout.write(('%' + str(self.limit_digits) + 'd') % data[layout['value']]) |
michael@0 | 53 | Terminal.reset_color() |
michael@0 | 54 | if layout != self.counters_fmt[-1]: |
michael@0 | 55 | sys.stdout.write('|') |
michael@0 | 56 | else: |
michael@0 | 57 | sys.stdout.write('] ') |
michael@0 | 58 | |
michael@0 | 59 | # Build the bar. |
michael@0 | 60 | pct = int(100.0 * current / self.limit) |
michael@0 | 61 | sys.stdout.write('%3d%% ' % pct) |
michael@0 | 62 | |
michael@0 | 63 | barlen = int(1.0 * self.barlen * current / self.limit) - 1 |
michael@0 | 64 | bar = '=' * barlen + '>' + ' ' * (self.barlen - barlen - 1) |
michael@0 | 65 | sys.stdout.write(bar + '|') |
michael@0 | 66 | |
michael@0 | 67 | # Update the bar. |
michael@0 | 68 | dt = datetime.now() - self.t0 |
michael@0 | 69 | dt = dt.seconds + dt.microseconds * 1e-6 |
michael@0 | 70 | sys.stdout.write('%6.1fs' % dt) |
michael@0 | 71 | Terminal.clear_right() |
michael@0 | 72 | |
michael@0 | 73 | # Force redisplay, since we didn't write a \n. |
michael@0 | 74 | sys.stdout.flush() |
michael@0 | 75 | |
michael@0 | 76 | def poke(self): |
michael@0 | 77 | if not self.prior: |
michael@0 | 78 | return |
michael@0 | 79 | self.update(*self.prior) |
michael@0 | 80 | |
michael@0 | 81 | def finish(self, complete=True): |
michael@0 | 82 | final_count = self.limit if complete else self.prior[0] |
michael@0 | 83 | self.update(final_count, self.prior[1]) |
michael@0 | 84 | sys.stdout.write('\n') |
michael@0 | 85 | |
michael@0 | 86 | def beginline(self): |
michael@0 | 87 | if not self.atLineStart: |
michael@0 | 88 | sys.stdout.write('\n') |
michael@0 | 89 | self.atLineStart = True |
michael@0 | 90 | |
michael@0 | 91 | def message(self, msg): |
michael@0 | 92 | self.beginline() |
michael@0 | 93 | sys.stdout.write(msg) |
michael@0 | 94 | sys.stdout.write('\n') |
michael@0 | 95 | |
michael@0 | 96 | @staticmethod |
michael@0 | 97 | def conservative_isatty(): |
michael@0 | 98 | """ |
michael@0 | 99 | Prefer erring on the side of caution and not using terminal commands if |
michael@0 | 100 | the current output stream may be a file. We explicitly check for the |
michael@0 | 101 | Android platform because terminal commands work poorly over ADB's |
michael@0 | 102 | redirection. |
michael@0 | 103 | """ |
michael@0 | 104 | try: |
michael@0 | 105 | import android |
michael@0 | 106 | return False |
michael@0 | 107 | except ImportError: |
michael@0 | 108 | return sys.stdout.isatty() |
michael@0 | 109 | return False |