HEX
Server: LiteSpeed
System: Linux CentOS-79-64-minimal 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: vishn3436 (5293)
PHP: 8.0.15
Disabled: NONE
Upload Files
File: //lib/python2.7/site-packages/packagekit/progress.py
# Licensed under the GNU General Public License Version 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2008
#    Richard Hughes <richard@hughsie.com>

try:
    from collections import Iterable
except ImportError:
    from collections.abc import Iterable


class PackagekitProgress(Iterable):
    '''
    Progress class there controls the total progress of a transaction
    the transaction is divided in n milestones. the class contains
    the current step (milestone n -> n+1) and the percentage of the whole transaction

    Usage:

    from packagekit import PackagekitProgress

    steps = [10, 30, 50, 70] # Milestones in %
    progress = PackagekitProgress(steps)
    for milestone in progress:
        # do the action is this step
        for i in range(100):
            # do some action
            print "progress : %s " % milestone
    '''

    #TODO: Add support for elapsed/remaining time

    def __init__(self, steps=None):
        super(PackagekitProgress, self).__init__()
        if not steps:
            self.reset()
        else:
            self.set_steps(steps)

    def set_steps(self, steps):
        '''
        Set the steps for the whole transaction
        @param steps: list of int representing the percentage of each step in the transaction
        '''
        self.reset()
        self.steps = steps
        self.current_step = 0

    def reset(self):
        self.percent = 0
        self.steps = []
        self.current_step = 0

    def step(self):
        '''
        Step to the next step in the transaction
        '''
        if self.current_step < len(self.steps)-1:
            self.current_step += 1
            self.percent = self.steps[self.current_step]
        else:
            self.current_step = len(self.steps)
            self.percent = 100

    def __iter__(self):
        while self.current_step < len(self.steps):
            yield self.percent
            self.step()

        raise StopIteration

    def _update_percent(self):
        '''
        Increment percentage based on current step
        '''
        if self.current_step == 0:
            startpct = 0
        else:
            startpct = self.steps[self.current_step-1]
        if self.current_step < len(self.steps)-1:
            endpct = self.steps[self.current_step+1]
        else:
            endpct = 100
        incr = endpct -startpct
        self.percent = startpct + incr