Move rescaleProgress+enumProgress to mceditlib.util.progress

This commit is contained in:
David Vierra 2015-09-10 15:49:17 -10:00
parent 084613ebf8
commit 800ab23427
2 changed files with 46 additions and 36 deletions

View File

@ -10,6 +10,7 @@ import shutil
from mceditlib.anvil.worldfolder import AnvilWorldFolder
from mceditlib.exceptions import ChunkNotPresent
from mceditlib.util.progress import rescaleProgress, enumProgress
log = logging.getLogger(__name__)
#
@ -360,46 +361,11 @@ class RevisionHistory(object):
currentNode.invalid = True
shutil.rmtree(currentNode.worldFolder.filename, ignore_errors=True)
def copyToFolder(destFolder, sourceNode, presaveNode=None):
for status in copyToFolderIter(destFolder, sourceNode, presaveNode):
pass
def rescaleProgress(iterable, start, end):
"""
Given an iterable that yields (current, maximum, status) tuples, rescales current and maximum
to fit within the range [start, end]. `current` is assumed to start at zero.
:param iterable:
:param start:
:param end:
:return:
"""
d = end - start
for current, maximum, status in iterable:
yield start + current * d / maximum, end, status
def enumProgress(collection, start, end=None):
"""
Iterate through a collection, yielding (progress, value) tuples. `progress` is the value
between `start` and `end` proportional to the progress through the collection.
:param collection:
:param progress:
:return:
"""
if end is None:
end = start
start = 0
if len(collection) == 0:
return
progFraction = (end - start) / len(collection)
for i, value in enumerate(collection):
yield start + i * progFraction, value
def copyToFolderIter(destFolder, sourceNode, presaveNode=None):
# Progress counts:

View File

@ -0,0 +1,44 @@
"""
progress
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import logging
log = logging.getLogger(__name__)
def rescaleProgress(iterable, start, end):
"""
Given an iterable that yields (current, maximum, status) tuples, rescales current and maximum
to fit within the range [start, end]. `current` is assumed to start at zero.
:param iterable:
:param start:
:param end:
:return:
"""
d = end - start
for current, maximum, status in iterable:
yield start + current * d / maximum, end, status
def enumProgress(collection, start, end=None):
"""
Iterate through a collection, yielding (progress, value) tuples. `progress` is the value
between `start` and `end` proportional to the progress through the collection.
:param collection:
:param progress:
:return:
"""
if end is None:
end = start
start = 0
if len(collection) == 0:
return
progFraction = (end - start) / len(collection)
for i, value in enumerate(collection):
yield start + i * progFraction, value