Show
Ignore:
Timestamp:
10/05/08 22:50:54 (16 years ago)
Author:
jerome
Message:

Moved the progress report code to its own module.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/tool.py

    r3432 r3434  
    4242from pykota import config, storage, logger 
    4343from pykota.version import __version__, __author__, __years__, __gplblurb__ 
    44  
    45 class Percent : 
    46     """A class to display progress.""" 
    47     def __init__(self, app, size=None) : 
    48         """Initializes the engine.""" 
    49         self.isatty = sys.stdout.isatty() 
    50         self.app = app 
    51         self.size = None 
    52         if size : 
    53             self.setSize(size) 
    54         self.previous = None 
    55         self.before = time.time() 
    56  
    57     def setSize(self, size) : 
    58         """Sets the total size.""" 
    59         self.number = 0 
    60         self.size = size 
    61         if size : 
    62             self.factor = 100.0 / float(size) 
    63  
    64     def display(self, msg) : 
    65         """Displays the value.""" 
    66         if self.isatty : 
    67             self.app.display(msg) 
    68             sys.stdout.flush() 
    69  
    70     def oneMore(self) : 
    71         """Increments internal counter.""" 
    72         if self.size : 
    73             self.number += 1 
    74             percent = "%.02f" % (float(self.number) * self.factor) 
    75             if percent != self.previous : # optimize for large number of items 
    76                 self.display("\r%s%%" % percent) 
    77                 self.previous = percent 
    78  
    79     def done(self) : 
    80         """Displays the 'done' message.""" 
    81         after = time.time() 
    82         if self.size : 
    83             try : 
    84                 speed = self.size / ((after - self.before) + 0.00000000001) # adds an epsilon to avoid an user's problem I can't reproduce... 
    85             except ZeroDivisionError : 
    86                 speed = 1 # Fake value in case of division by zero, shouldn't happen anyway with the epsilon above... 
    87             self.display("\r100.00%%\r        \r%s. %s : %.2f %s.\n" \ 
    88                      % (_("Done"), _("Average speed"), speed, _("entries per second"))) 
    89         else : 
    90             self.display("\r100.00%%\r        \r%s.\n" % _("Done")) 
    9144 
    9245class Tool : 
     
    342295        for c in list(invalidchars) : 
    343296            if c in name : 
    344                 return 0 
    345         return 1 
     297                return False 
     298        return True 
    346299 
    347300    def _checkUserPQuota(self, userpquota) : 
     
    466419 
    467420        # indicates that a warning needs to be sent 
    468         warned = 0 
     421        warned = False 
    469422 
    470423        # first we check any group the user is a member of 
     
    480433                            return action 
    481434                        elif action == "WARN" : 
    482                             warned = 1 
     435                            warned = True 
    483436 
    484437        # Then we check the user's account balance 
     
    515468        else : 
    516469            # Then check the user quota on current printer and all its parents. 
    517             policyallowed = 0 
     470            policyallowed = False 
    518471            for upquota in [ userpquota ] + userpquota.ParentPrintersUserPQuota : 
    519472                action = self._checkUserPQuota(upquota) 
     
    521474                    return action 
    522475                elif action == "WARN" : 
    523                     warned = 1 
     476                    warned = True 
    524477                elif action == "POLICY_ALLOW" : 
    525                     policyallowed = 1 
     478                    policyallowed = True 
    526479            if warned : 
    527480                return "WARN"