"Fossies" - the Fresh Open Source Software Archive

Member "roundup-2.0.0/roundup/support.py" (1 Jan 2020, 3385 Bytes) of package /linux/www/roundup-2.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "support.py": 1.6.1_vs_2.0.0.

    1 """Implements various support classes and functions used in a number of
    2 places in Roundup code.
    3 """
    4 
    5 from __future__ import print_function
    6 __docformat__ = 'restructuredtext'
    7 
    8 import os, time, sys
    9 
   10 
   11 class TruthDict:
   12     '''Returns True for valid keys, False for others.
   13     '''
   14     def __init__(self, keys):
   15         if keys:
   16             self.keys = {}
   17             for col in keys:
   18                 self.keys[col] = 1
   19 
   20     def __getitem__(self, name):
   21         if hasattr(self, 'keys'):
   22             return name in self.keys
   23         else:
   24             return True
   25 
   26 
   27 def ensureParentsExist(dest):
   28     if not os.path.exists(os.path.dirname(dest)):
   29         os.makedirs(os.path.dirname(dest))
   30 
   31 
   32 class PrioList:
   33     '''Manages a sorted list.
   34 
   35     Currently only implements method 'append' and iteration from a
   36     full list interface.
   37     Implementation: We manage a "sorted" status and sort on demand.
   38     Appending to the list will require re-sorting before use.
   39     >>> p = PrioList()
   40     >>> for i in 5,7,1,-1:
   41     ...  p.append(i)
   42     ...
   43     >>> for k in p:
   44     ...  print k
   45     ...
   46     -1
   47     1
   48     5
   49     7
   50 
   51     '''
   52     def __init__(self):
   53         self.list = []
   54         self.sorted = True
   55 
   56     def append(self, item):
   57         self.list.append(item)
   58         self.sorted = False
   59 
   60     def __iter__(self):
   61         if not self.sorted:
   62             self.list.sort()
   63             self.sorted = True
   64         return iter(self.list)
   65 
   66 
   67 class Progress:
   68     '''Progress display for console applications.
   69 
   70     See __main__ block at end of file for sample usage.
   71     '''
   72     def __init__(self, info, sequence):
   73         self.info = info
   74         self.sequence = iter(sequence)
   75         self.total = len(sequence)
   76         self.start = self.now = time.time()
   77         self.num = 0
   78         self.stepsize = self.total // 100 or 1
   79         self.steptimes = []
   80         self.display()
   81 
   82     def __iter__(self): return self
   83 
   84     def __next__(self):
   85         self.num += 1
   86 
   87         if self.num > self.total:
   88             print(self.info, 'done', ' '*(75-len(self.info)-6))
   89             sys.stdout.flush()
   90             return next(self.sequence)
   91 
   92         if self.num % self.stepsize:
   93             return next(self.sequence)
   94 
   95         self.display()
   96         return next(self.sequence)
   97     # Python 2 compatibility:
   98     next = __next__
   99 
  100     def display(self):
  101         # figure how long we've spent - guess how long to go
  102         now = time.time()
  103         steptime = now - self.now
  104         self.steptimes.insert(0, steptime)
  105         if len(self.steptimes) > 5:
  106             self.steptimes.pop()
  107         steptime = sum(self.steptimes) / len(self.steptimes)
  108         self.now = now
  109         eta = steptime * ((self.total - self.num)/self.stepsize)
  110 
  111         # tell it like it is (or might be)
  112         if now - self.start > 3:
  113             M = eta / 60
  114             H = M / 60
  115             M = M % 60
  116             S = eta % 60
  117             if self.total:
  118                 s = '%s %2d%% (ETA %02d:%02d:%02d)' % (self.info,
  119                     self.num * 100. / self.total, H, M, S)
  120             else:
  121                 s = '%s 0%% (ETA %02d:%02d:%02d)' % (self.info, H, M, S)
  122         elif self.total:
  123             s = '%s %2d%%' % (self.info, self.num * 100. / self.total)
  124         else:
  125             s = '%s %d done' % (self.info, self.num)
  126         sys.stdout.write(s + ' '*(75-len(s)) + '\r')
  127         sys.stdout.flush()
  128 
  129 # vim: set et sts=4 sw=4 :