Guest User

Untitled

a guest
Jan 14th, 2010
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.27 KB | None | 0 0
  1. #may be missing some imports code taken from http://ubuntuforums.org/showthread.php?t=169551 by jdong
  2. import time
  3. import os
  4.  
  5. def _supports_progress(f):
  6.     if not hasattr(f, 'isatty'):
  7.         return False
  8.     if not f.isatty():
  9.         return False
  10.     if os.environ.get('TERM') == 'dumb':
  11.         # e.g. emacs compile window
  12.         return False
  13.     return True
  14.  
  15.  
  16.  
  17. def ProgressBar(to_file=sys.stderr, **kwargs):
  18.     """Abstract factory"""
  19.     if _supports_progress(to_file):
  20.         return TTYProgressBar(to_file=to_file, **kwargs)
  21.     else:
  22.         return DotsProgressBar(to_file=to_file, **kwargs)
  23.    
  24.  
  25. class ProgressBarStack(object):
  26.     """A stack of progress bars."""
  27.  
  28.     def __init__(self,
  29.                  to_file=sys.stderr,
  30.                  show_pct=False,
  31.                  show_spinner=True,
  32.                  show_eta=False,
  33.                  show_bar=True,
  34.                  show_count=True,
  35.                  to_messages_file=sys.stdout,
  36.                  klass=None):
  37.         """Setup the stack with the parameters the progress bars should have."""
  38.         self._to_file = to_file
  39.         self._show_pct = show_pct
  40.         self._show_spinner = show_spinner
  41.         self._show_eta = show_eta
  42.         self._show_bar = show_bar
  43.         self._show_count = show_count
  44.         self._to_messages_file = to_messages_file
  45.         self._stack = []
  46.         self._klass = klass or TTYProgressBar
  47.  
  48.     def top(self):
  49.         if len(self._stack) != 0:
  50.             return self._stack[-1]
  51.         else:
  52.             return None
  53.  
  54.     def bottom(self):
  55.         if len(self._stack) != 0:
  56.             return self._stack[0]
  57.         else:
  58.             return None
  59.  
  60.     def get_nested(self):
  61.         """Return a nested progress bar."""
  62.         if len(self._stack) == 0:
  63.             func = self._klass
  64.         else:
  65.             func = self.top().child_progress
  66.         new_bar = func(to_file=self._to_file,
  67.                        show_pct=self._show_pct,
  68.                        show_spinner=self._show_spinner,
  69.                        show_eta=self._show_eta,
  70.                        show_bar=self._show_bar,
  71.                        show_count=self._show_count,
  72.                        to_messages_file=self._to_messages_file,
  73.                        _stack=self)
  74.         self._stack.append(new_bar)
  75.         return new_bar
  76.  
  77.     def return_pb(self, bar):
  78.         """Return bar after its been used."""
  79.         self._stack.pop()
  80.  
  81.  
  82. class _BaseProgressBar(object):
  83.  
  84.     def __init__(self,
  85.                  to_file=sys.stderr,
  86.                  show_pct=False,
  87.                  show_spinner=False,
  88.                  show_eta=True,
  89.                  show_bar=True,
  90.                  show_count=True,
  91.                  to_messages_file=sys.stdout,
  92.                  _stack=None):
  93.         object.__init__(self)
  94.         self.to_file = to_file
  95.         self.to_messages_file = to_messages_file
  96.         self.last_msg = None
  97.         self.last_cnt = None
  98.         self.last_total = None
  99.         self.show_pct = show_pct
  100.         self.show_spinner = show_spinner
  101.         self.show_eta = show_eta
  102.         self.show_bar = show_bar
  103.         self.show_count = show_count
  104.         self._stack = _stack
  105.         # seed throttler
  106.         self.MIN_PAUSE = 0.5 # seconds
  107.         now = time.time()
  108.         # starting now
  109.         self.start_time = now
  110.         # next update should not throttle
  111.         self.last_update = now - self.MIN_PAUSE - 1
  112.  
  113.     def finished(self):
  114.         """Return this bar to its progress stack."""
  115.         self.clear()
  116.         assert self._stack is not None
  117.         self._stack.return_pb(self)
  118.  
  119.     def note(self, fmt_string, *args, **kwargs):
  120.         """Record a note without disrupting the progress bar."""
  121.         self.clear()
  122.         self.to_messages_file.write(fmt_string % args)
  123.         self.to_messages_file.write('\n')
  124.  
  125.     def child_progress(self, **kwargs):
  126.         return ChildProgress(**kwargs)
  127.  
  128.  
  129. class DummyProgress(_BaseProgressBar):
  130.     """Progress-bar standin that does nothing.
  131.  
  132.    This can be used as the default argument for methods that
  133.    take an optional progress indicator."""
  134.     def tick(self):
  135.         pass
  136.  
  137.     def update(self, msg=None, current=None, total=None):
  138.         pass
  139.  
  140.     def child_update(self, message, current, total):
  141.         pass
  142.  
  143.     def clear(self):
  144.         pass
  145.        
  146.     def note(self, fmt_string, *args, **kwargs):
  147.         """See _BaseProgressBar.note()."""
  148.  
  149.     def child_progress(self, **kwargs):
  150.         return DummyProgress(**kwargs)
  151.  
  152. class DotsProgressBar(_BaseProgressBar):
  153.  
  154.     def __init__(self, **kwargs):
  155.         _BaseProgressBar.__init__(self, **kwargs)
  156.         self.last_msg = None
  157.         self.need_nl = False
  158.        
  159.     def tick(self):
  160.         self.update()
  161.        
  162.     def update(self, msg=None, current_cnt=None, total_cnt=None):
  163.         if msg and msg != self.last_msg:
  164.             if self.need_nl:
  165.                 self.to_file.write('\n')
  166.            
  167.             self.to_file.write(msg + ': ')
  168.             self.last_msg = msg
  169.         self.need_nl = True
  170.         self.to_file.write('.')
  171.        
  172.     def clear(self):
  173.         if self.need_nl:
  174.             self.to_file.write('\n')
  175.        
  176.     def child_update(self, message, current, total):
  177.         self.tick()
  178.    
  179. class TTYProgressBar(_BaseProgressBar):
  180.     """Progress bar display object.
  181.  
  182.    Several options are available to control the display.  These can
  183.    be passed as parameters to the constructor or assigned at any time:
  184.  
  185.    show_pct
  186.        Show percentage complete.
  187.    show_spinner
  188.        Show rotating baton.  This ticks over on every update even
  189.        if the values don't change.
  190.    show_eta
  191.        Show predicted time-to-completion.
  192.    show_bar
  193.        Show bar graph.
  194.    show_count
  195.        Show numerical counts.
  196.  
  197.    The output file should be in line-buffered or unbuffered mode.
  198.    """
  199.     SPIN_CHARS = r'/-\|'
  200.  
  201.  
  202.     def __init__(self, **kwargs):
  203.         #from bzrlib.osutils import terminal_width
  204.         #TODO: Determine terminal width
  205.         _BaseProgressBar.__init__(self, **kwargs)
  206.         self.spin_pos = 0
  207.         self.width = 80
  208.         self.start_time = time.time()
  209.         self.last_updates = deque()
  210.         self.child_fraction = 0
  211.    
  212.  
  213.     def throttle(self):
  214.         """Return True if the bar was updated too recently"""
  215.         # time.time consistently takes 40/4000 ms = 0.01 ms.
  216.         # but every single update to the pb invokes it.
  217.         # so we use time.time which takes 20/4000 ms = 0.005ms
  218.         # on the downside, time.time() appears to have approximately
  219.         # 10ms granularity, so we treat a zero-time change as 'throttled.'
  220.        
  221.         now = time.time()
  222.         interval = now - self.last_update
  223.         # if interval > 0
  224.         if interval < self.MIN_PAUSE:
  225.             return True
  226.  
  227.         self.last_updates.append(now - self.last_update)
  228.         self.last_update = now
  229.         return False
  230.        
  231.  
  232.     def tick(self):
  233.         self.update(self.last_msg, self.last_cnt, self.last_total,
  234.                     self.child_fraction)
  235.  
  236.     def child_update(self, message, current, total):
  237.         if current is not None and total != 0:
  238.             child_fraction = float(current) / total
  239.             if self.last_cnt is None:
  240.                 pass
  241.             elif self.last_cnt + child_fraction <= self.last_total:
  242.                 self.child_fraction = child_fraction
  243.         if self.last_msg is None:
  244.             self.last_msg = ''
  245.         self.tick()
  246.  
  247.  
  248.     def update(self, msg, current_cnt=None, total_cnt=None,
  249.                child_fraction=0):
  250.         """Update and redraw progress bar."""
  251.  
  252.         if current_cnt < 0:
  253.             current_cnt = 0
  254.            
  255.         if current_cnt > total_cnt:
  256.             total_cnt = current_cnt
  257.        
  258.         ## # optional corner case optimisation
  259.         ## # currently does not seem to fire so costs more than saved.
  260.         ## # trivial optimal case:
  261.         ## # NB if callers are doing a clear and restore with
  262.         ## # the saved values, this will prevent that:
  263.         ## # in that case add a restore method that calls
  264.         ## # _do_update or some such
  265.         ## if (self.last_msg == msg and
  266.         ##     self.last_cnt == current_cnt and
  267.         ##     self.last_total == total_cnt and
  268.         ##     self.child_fraction == child_fraction):
  269.         ##     return
  270.  
  271.         old_msg = self.last_msg
  272.         # save these for the tick() function
  273.         self.last_msg = msg
  274.         self.last_cnt = current_cnt
  275.         self.last_total = total_cnt
  276.         self.child_fraction = child_fraction
  277.  
  278.         # each function call takes 20ms/4000 = 0.005 ms,
  279.         # but multiple that by 4000 calls -> starts to cost.
  280.         # so anything to make this function call faster
  281.         # will improve base 'diff' time by up to 0.1 seconds.
  282.         if old_msg == self.last_msg and self.throttle():
  283.             return
  284.  
  285.         if self.show_eta and self.start_time and self.last_total:
  286.             eta = get_eta(self.start_time, self.last_cnt + self.child_fraction,
  287.                     self.last_total, last_updates = self.last_updates)
  288.             eta_str = " " + str_tdelta(eta)
  289.         else:
  290.             eta_str = ""
  291.  
  292.         if self.show_spinner:
  293.             spin_str = self.SPIN_CHARS[self.spin_pos % 4] + ' '            
  294.         else:
  295.             spin_str = ''
  296.  
  297.         # always update this; it's also used for the bar
  298.         self.spin_pos += 1
  299.  
  300.         if self.show_pct and self.last_total and self.last_cnt:
  301.             pct = 100.0 * ((self.last_cnt + self.child_fraction) / self.last_total)
  302.             pct_str = ' (%5.1f%%)' % pct
  303.         else:
  304.             pct_str = ''
  305.  
  306.         if not self.show_count:
  307.             count_str = ''
  308.         elif self.last_cnt is None:
  309.             count_str = ''
  310.         elif self.last_total is None:
  311.             count_str = ' %i' % (self.last_cnt)
  312.         else:
  313.             # make both fields the same size
  314.             t = '%i' % (self.last_total)
  315.             c = '%*i' % (len(t), self.last_cnt)
  316.             count_str = ' ' + c + '/' + t
  317.  
  318.         if self.show_bar:
  319.             # progress bar, if present, soaks up all remaining space
  320.             cols = self.width - 1 - len(self.last_msg) - len(spin_str) - len(pct_str) \
  321.                    - len(eta_str) - len(count_str) - 3
  322.  
  323.             if self.last_total:
  324.                 # number of markers highlighted in bar
  325.                 markers = int(round(float(cols) *
  326.                               (self.last_cnt + self.child_fraction) / self.last_total))
  327.                 bar_str = '[' + ('=' * markers).ljust(cols) + '] '
  328.             elif False:
  329.                 # don't know total, so can't show completion.
  330.                 # so just show an expanded spinning thingy
  331.                 m = self.spin_pos % cols
  332.                 ms = (' ' * m + '*').ljust(cols)
  333.                
  334.                 bar_str = '[' + ms + '] '
  335.             else:
  336.                 bar_str = ''
  337.         else:
  338.             bar_str = ''
  339.  
  340.         m = spin_str + self.last_msg + bar_str + count_str + pct_str + eta_str
  341.  
  342.         assert len(m) < self.width
  343.         self.to_file.write('\r' + m.ljust(self.width - 1))
  344.         #self.to_file.flush()
  345.            
  346.     def clear(self):        
  347.         self.to_file.write('\r%s\r' % (' ' * (self.width - 1)))
  348.         #self.to_file.flush()        
  349.  
  350.  
  351. class ChildProgress(_BaseProgressBar):
  352.     """A progress indicator that pushes its data to the parent"""
  353.     def __init__(self, _stack, **kwargs):
  354.         _BaseProgressBar.__init__(self, _stack=_stack, **kwargs)
  355.         self.parent = _stack.top()
  356.         self.current = None
  357.         self.total = None
  358.         self.child_fraction = 0
  359.         self.message = None
  360.  
  361.     def update(self, msg, current_cnt=None, total_cnt=None):
  362.         self.current = current_cnt
  363.         self.total = total_cnt
  364.         self.message = msg
  365.         self.child_fraction = 0
  366.         self.tick()
  367.  
  368.     def child_update(self, message, current, total):
  369.         if current is None or total == 0:
  370.             self.child_fraction = 0
  371.         else:
  372.             self.child_fraction = float(current) / total
  373.         self.tick()
  374.  
  375.     def tick(self):
  376.         if self.current is None:
  377.             count = None
  378.         else:
  379.             count = self.current+self.child_fraction
  380.             if count > self.total:
  381.                 count = self.total
  382.         self.parent.child_update(self.message, count, self.total)
  383.  
  384.     def clear(self):
  385.         pass
  386.  
  387.     def note(self, *args, **kwargs):
  388.         self.parent.note(*args, **kwargs)
  389.  
  390.  
  391. def str_tdelta(delt):
  392.     if delt is None:
  393.         return "-:--:--"
  394.     delt = int(round(delt))
  395.     return '%d:%02d:%02d' % (delt/3600,
  396.                              (delt/60) % 60,
  397.                              delt % 60)
  398.  
  399.  
  400. def get_eta(start_time, current, total, enough_samples=3, last_updates=None, n_recent=10):
  401.     if start_time is None:
  402.         return None
  403.  
  404.     if not total:
  405.         return None
  406.  
  407.     if current < enough_samples:
  408.         return None
  409.  
  410.     if current > total:
  411.         return None                     # wtf?
  412.     elapsed = time.time() - start_time
  413.  
  414.     if elapsed < 5.0:                   # not enough time to estimate
  415.         return None
  416.    
  417.     total_duration = float(elapsed) * float(total) / float(current)
  418.  
  419.     assert total_duration >= elapsed
  420.  
  421.     if last_updates and len(last_updates) >= n_recent:
  422.         while len(last_updates) > n_recent:
  423.             last_updates.popleft()
  424.         avg = sum(last_updates) / float(len(last_updates))
  425.         time_left = avg * (total - current)
  426.  
  427.         old_time_left = total_duration - elapsed
  428.  
  429.         # We could return the average, or some other value here
  430.         return (time_left + old_time_left) / 2
  431.  
  432.     return total_duration - elapsed
  433.  
  434.  
  435. class ProgressPhase(object):
  436.     """Update progress object with the current phase"""
  437.     def __init__(self, message, total, pb):
  438.         object.__init__(self)
  439.         self.pb = pb
  440.         self.message = message
  441.         self.total = total
  442.         self.cur_phase = None
  443.  
  444.     def next_phase(self):
  445.         if self.cur_phase is None:
  446.             self.cur_phase = 0
  447.         else:
  448.             self.cur_phase += 1
  449.         assert self.cur_phase < self.total
  450.         self.pb.update(self.message, self.cur_phase, self.total)
Advertisement
Add Comment
Please, Sign In to add comment