Move rescaleProgress+enumProgress to mceditlib.util.progress
This commit is contained in:
parent
084613ebf8
commit
800ab23427
@ -10,6 +10,7 @@ import shutil
|
|||||||
|
|
||||||
from mceditlib.anvil.worldfolder import AnvilWorldFolder
|
from mceditlib.anvil.worldfolder import AnvilWorldFolder
|
||||||
from mceditlib.exceptions import ChunkNotPresent
|
from mceditlib.exceptions import ChunkNotPresent
|
||||||
|
from mceditlib.util.progress import rescaleProgress, enumProgress
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
#
|
#
|
||||||
@ -360,46 +361,11 @@ class RevisionHistory(object):
|
|||||||
currentNode.invalid = True
|
currentNode.invalid = True
|
||||||
shutil.rmtree(currentNode.worldFolder.filename, ignore_errors=True)
|
shutil.rmtree(currentNode.worldFolder.filename, ignore_errors=True)
|
||||||
|
|
||||||
|
|
||||||
def copyToFolder(destFolder, sourceNode, presaveNode=None):
|
def copyToFolder(destFolder, sourceNode, presaveNode=None):
|
||||||
for status in copyToFolderIter(destFolder, sourceNode, presaveNode):
|
for status in copyToFolderIter(destFolder, sourceNode, presaveNode):
|
||||||
pass
|
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):
|
def copyToFolderIter(destFolder, sourceNode, presaveNode=None):
|
||||||
# Progress counts:
|
# Progress counts:
|
||||||
|
44
src/mceditlib/util/progress.py
Normal file
44
src/mceditlib/util/progress.py
Normal 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
|
Reference in New Issue
Block a user