From 4c73e9cd66c77934f8a262b0c1bab9c2f15449ba Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 12 Oct 2009 17:03:01 +0200 Subject: refs now take repo as first argument and derive from LazyMixin to allow them to dynamically retrieve their objects Improved way commits are returned by refs as they now use the path to be sure they always point to the ref even if it changes - previously it would use the sha intead so it would not update after being cached on the ref object --- lib/git/utils.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'lib/git/utils.py') diff --git a/lib/git/utils.py b/lib/git/utils.py index c204c432..39994bd5 100644 --- a/lib/git/utils.py +++ b/lib/git/utils.py @@ -24,3 +24,28 @@ def is_git_dir(d): (os.path.islink(headref) and os.readlink(headref).startswith('refs')) return False + + +class LazyMixin(object): + __slots__ = tuple() + + def __getattr__(self, attr): + """ + Whenever an attribute is requested that we do not know, we allow it + to be created and set. Next time the same attribute is reqeusted, it is simply + returned from our dict/slots. + """ + self._set_cache_(attr) + # will raise in case the cache was not created + return object.__getattribute__(self, attr) + + def _set_cache_(self, attr): + """ This method should be overridden in the derived class. + It should check whether the attribute named by attr can be created + and cached. Do nothing if you do not know the attribute or call your subclass + + The derived class may create as many additional attributes as it deems + necessary in case a git command returns more information than represented + in the single attribute.""" + pass + -- cgit v1.2.1 From f4fa1cb3c3e84cad8b74edb28531d2e27508be26 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Tue, 13 Oct 2009 19:35:49 +0200 Subject: Added base for all iteratable objects --- lib/git/utils.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'lib/git/utils.py') diff --git a/lib/git/utils.py b/lib/git/utils.py index 39994bd5..96ec15b9 100644 --- a/lib/git/utils.py +++ b/lib/git/utils.py @@ -27,6 +27,12 @@ def is_git_dir(d): class LazyMixin(object): + """ + Base class providing an interface to lazily retrieve attribute values upon + first access. If slots are used, memory will only be reserved once the attribute + is actually accessed and retrieved the first time. All future accesses will + return the cached value as stored in the Instance's dict or slot. + """ __slots__ = tuple() def __getattr__(self, attr): @@ -49,3 +55,35 @@ class LazyMixin(object): in the single attribute.""" pass + +class Iterable(object): + """ + Defines an interface for iterable items which is to assure a uniform + way to retrieve and iterate items within the git repository + """ + __slots__ = tuple() + + @classmethod + def list_items(cls, repo, *args, **kwargs): + """ + Find all items of this type - subclasses can specify args and kwargs differently. + If no args are given, subclasses are obliged to return all items if no additional + arguments arg given. + + Note: Favor the iter_items method as it will + + Returns: + list(Item,...) list of item instances + """ + return list(cls.iter_items, repo, *args, **kwargs) + + + @classmethod + def iter_items(cls, repo, *args, **kwargs): + """ + For more information about the arguments, see find_all + Return: + iterator yielding Items + """ + raise NotImplementedError("To be implemented by Subclass") + -- cgit v1.2.1 From 6acec357c7609fdd2cb0f5fdb1d2756726c7fe98 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Tue, 13 Oct 2009 21:26:19 +0200 Subject: renamed find_all to list_all, changed commit to use iterable interface in preparation for command changes --- lib/git/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/git/utils.py') diff --git a/lib/git/utils.py b/lib/git/utils.py index 96ec15b9..f84c247d 100644 --- a/lib/git/utils.py +++ b/lib/git/utils.py @@ -75,13 +75,13 @@ class Iterable(object): Returns: list(Item,...) list of item instances """ - return list(cls.iter_items, repo, *args, **kwargs) + return list(cls.iter_items(repo, *args, **kwargs)) @classmethod def iter_items(cls, repo, *args, **kwargs): """ - For more information about the arguments, see find_all + For more information about the arguments, see list_items Return: iterator yielding Items """ -- cgit v1.2.1