From 29724818764af6b4d30e845d9280947584078aed Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 29 May 2016 10:39:14 +0200 Subject: fix(remote): Add CallableRemoteProgress That way, the base type doesn't need any adjustment. Related to #450 --- git/remote.py | 5 +++-- git/util.py | 29 +++++++++++++++++++---------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/git/remote.py b/git/remote.py index 946d0165..6e3d65d4 100644 --- a/git/remote.py +++ b/git/remote.py @@ -24,7 +24,8 @@ from git.util import ( LazyMixin, Iterable, IterableList, - RemoteProgress + RemoteProgress, + CallableRemoteProgress ) from git.util import ( join_path, @@ -65,7 +66,7 @@ def to_progress_instance(progress): """ # new API only needs progress as a function if callable(progress): - return RemoteProgress(progress) + return CallableRemoteProgress(progress) # where None is passed create a parser that eats the progress elif progress is None: diff --git a/git/util.py b/git/util.py index 021018d1..55ad8a03 100644 --- a/git/util.py +++ b/git/util.py @@ -39,7 +39,7 @@ from gitdb.util import ( # NOQA __all__ = ("stream_copy", "join_path", "to_native_path_windows", "to_native_path_linux", "join_path_native", "Stats", "IndexFileSHA1Writer", "Iterable", "IterableList", "BlockingLockFile", "LockFile", 'Actor', 'get_user_id', 'assure_directory_exists', - 'RemoteProgress', 'rmtree', 'WaitGroup', 'unbare_repo') + 'RemoteProgress', 'CallableRemoteProgress', 'rmtree', 'WaitGroup', 'unbare_repo') #{ Utility Methods @@ -160,7 +160,6 @@ def finalize_process(proc, **kwargs): class RemoteProgress(object): - """ Handler providing an interface to parse progress information emitted by git-push and git-fetch and to dispatch callbacks allowing subclasses to react to the progress. @@ -174,12 +173,11 @@ class RemoteProgress(object): DONE_TOKEN = 'done.' TOKEN_SEPARATOR = ', ' - __slots__ = ("_cur_line", "_seen_ops", "__progress_function") + __slots__ = ("_cur_line", "_seen_ops") re_op_absolute = re.compile(r"(remote: )?([\w\s]+):\s+()(\d+)()(.*)") re_op_relative = re.compile(r"(remote: )?([\w\s]+):\s+(\d+)% \((\d+)/(\d+)\)(.*)") - def __init__(self, progress_function=None): - self.__progress_function = progress_function if progress_function else self.update + def __init__(self): self._seen_ops = list() self._cur_line = None @@ -268,10 +266,10 @@ class RemoteProgress(object): # END end message handling message = message.strip(self.TOKEN_SEPARATOR) - self.__progress_function(op_code, - cur_count and float(cur_count), - max_count and float(max_count), - message) + self.update(op_code, + cur_count and float(cur_count), + max_count and float(max_count), + message) # END for each sub line return failed_lines @@ -314,7 +312,18 @@ class RemoteProgress(object): You may read the contents of the current line in self._cur_line""" pass - + + +class CallableRemoteProgress(RemoteProgress): + """An implementation forwarding updates to any callable""" + __slots__ = ('_callable') + + def __init__(self, fn): + self._callable = fn + + def update(self, *args, **kwargs): + self._callable(*args, **kwargs) + class Actor(object): -- cgit v1.2.1