From 8430529e1a9fb28d8586d24ee507a8195c370fa5 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 8 Oct 2009 14:34:29 +0200 Subject: Renamed lazy.py to base.py to have a file for base classes - lazy not yet changed to allow proper rename tracking --- lib/git/tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/git/tree.py') diff --git a/lib/git/tree.py b/lib/git/tree.py index 1b546f85..06c1a158 100644 --- a/lib/git/tree.py +++ b/lib/git/tree.py @@ -5,7 +5,7 @@ # the BSD License: http://www.opensource.org/licenses/bsd-license.php import os -from lazy import LazyMixin +from base import LazyMixin import blob class Tree(LazyMixin): -- cgit v1.2.1 From 9ee31065abea645cbc2cf3e54b691d5983a228b2 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 11 Oct 2009 11:01:12 +0200 Subject: Intermediate commit: commit,tree and blob objects now derive from object - test is in place which still fails on purpose. Need to integrate tags which can be objects or just a special form of a ref --- lib/git/tree.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'lib/git/tree.py') diff --git a/lib/git/tree.py b/lib/git/tree.py index 06c1a158..6215f875 100644 --- a/lib/git/tree.py +++ b/lib/git/tree.py @@ -5,25 +5,22 @@ # the BSD License: http://www.opensource.org/licenses/bsd-license.php import os -from base import LazyMixin import blob +import base -class Tree(LazyMixin): +class Tree(base.Object): + + type = "tree" + def __init__(self, repo, id, mode=None, path=None): - LazyMixin.__init__(self) - self.repo = repo - self.id = id + super(Tree, self).__init__(repo, id) self.mode = mode self.path = path self._contents = None def __bake__(self): - # Ensure the treeish references directly a tree - treeish = self.id - if not treeish.endswith(':'): - treeish = treeish + ':' - # Read the tree contents. + super(Tree, self).__bake__() self._contents = {} for line in self.repo.git.ls_tree(self.id).splitlines(): obj = self.content_from_string(self.repo, line) -- cgit v1.2.1 From 9374a916588d9fe7169937ba262c86ad710cfa74 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 11 Oct 2009 16:49:05 +0200 Subject: converted all spaces to tabs ( 4 spaces = 1 tab ) just to allow me and my editor to work with the files properly. Can convert it back for releaes --- lib/git/tree.py | 188 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 94 insertions(+), 94 deletions(-) (limited to 'lib/git/tree.py') diff --git a/lib/git/tree.py b/lib/git/tree.py index 6215f875..3d4deb16 100644 --- a/lib/git/tree.py +++ b/lib/git/tree.py @@ -9,97 +9,97 @@ import blob import base class Tree(base.Object): - - type = "tree" - - def __init__(self, repo, id, mode=None, path=None): - super(Tree, self).__init__(repo, id) - self.mode = mode - self.path = path - self._contents = None - - def __bake__(self): - # Read the tree contents. - super(Tree, self).__bake__() - self._contents = {} - for line in self.repo.git.ls_tree(self.id).splitlines(): - obj = self.content_from_string(self.repo, line) - if obj is not None: - self._contents[obj.path] = obj - - @staticmethod - def content_from_string(repo, text): - """ - Parse a content item and create the appropriate object - - ``repo`` - is the Repo - - ``text`` - is the single line containing the items data in `git ls-tree` format - - Returns - ``git.Blob`` or ``git.Tree`` - """ - try: - mode, typ, id, path = text.expandtabs(1).split(" ", 3) - except: - return None - - if typ == "tree": - return Tree(repo, id=id, mode=mode, path=path) - elif typ == "blob": - return blob.Blob(repo, id=id, mode=mode, path=path) - elif typ == "commit": - return None - else: - raise(TypeError, "Invalid type: %s" % typ) - - def __div__(self, file): - """ - Find the named object in this tree's contents - - Examples:: - - >>> Repo('/path/to/python-git').tree/'lib' - - >>> Repo('/path/to/python-git').tree/'README.txt' - - - Returns - ``git.Blob`` or ``git.Tree`` or ``None`` if not found - """ - return self.get(file) - - @property - def basename(self): - os.path.basename(self.path) - - def __repr__(self): - return '' % self.id - - # Implement the basics of the dict protocol: - # directories/trees can be seen as object dicts. - def __getitem__(self, key): - return self._contents[key] - - def __iter__(self): - return iter(self._contents) - - def __len__(self): - return len(self._contents) - - def __contains__(self, key): - return key in self._contents - - def get(self, key): - return self._contents.get(key) - - def items(self): - return self._contents.items() - - def keys(self): - return self._contents.keys() - - def values(self): - return self._contents.values() + + type = "tree" + + def __init__(self, repo, id, mode=None, path=None): + super(Tree, self).__init__(repo, id) + self.mode = mode + self.path = path + self._contents = None + + def __bake__(self): + # Read the tree contents. + super(Tree, self).__bake__() + self._contents = {} + for line in self.repo.git.ls_tree(self.id).splitlines(): + obj = self.content_from_string(self.repo, line) + if obj is not None: + self._contents[obj.path] = obj + + @staticmethod + def content_from_string(repo, text): + """ + Parse a content item and create the appropriate object + + ``repo`` + is the Repo + + ``text`` + is the single line containing the items data in `git ls-tree` format + + Returns + ``git.Blob`` or ``git.Tree`` + """ + try: + mode, typ, id, path = text.expandtabs(1).split(" ", 3) + except: + return None + + if typ == "tree": + return Tree(repo, id=id, mode=mode, path=path) + elif typ == "blob": + return blob.Blob(repo, id=id, mode=mode, path=path) + elif typ == "commit": + return None + else: + raise(TypeError, "Invalid type: %s" % typ) + + def __div__(self, file): + """ + Find the named object in this tree's contents + + Examples:: + + >>> Repo('/path/to/python-git').tree/'lib' + + >>> Repo('/path/to/python-git').tree/'README.txt' + + + Returns + ``git.Blob`` or ``git.Tree`` or ``None`` if not found + """ + return self.get(file) + + @property + def basename(self): + os.path.basename(self.path) + + def __repr__(self): + return '' % self.id + + # Implement the basics of the dict protocol: + # directories/trees can be seen as object dicts. + def __getitem__(self, key): + return self._contents[key] + + def __iter__(self): + return iter(self._contents) + + def __len__(self): + return len(self._contents) + + def __contains__(self, key): + return key in self._contents + + def get(self, key): + return self._contents.get(key) + + def items(self): + return self._contents.items() + + def keys(self): + return self._contents.keys() + + def values(self): + return self._contents.values() -- cgit v1.2.1 From 7a7eedde7f5d5082f7f207ef76acccd24a6113b1 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 11 Oct 2009 18:06:18 +0200 Subject: put Tree and Blob onto a new base class suitable to deal with IndexObjects --- lib/git/tree.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'lib/git/tree.py') diff --git a/lib/git/tree.py b/lib/git/tree.py index 3d4deb16..90f1b72d 100644 --- a/lib/git/tree.py +++ b/lib/git/tree.py @@ -8,14 +8,13 @@ import os import blob import base -class Tree(base.Object): +class Tree(base.IndexObject): type = "tree" + __slots__ = "_contents" - def __init__(self, repo, id, mode=None, path=None): - super(Tree, self).__init__(repo, id) - self.mode = mode - self.path = path + def __init__(self, repo, id, mode=None, path=None, size=None): + super(Tree, self).__init__(repo, id, mode, path, size) self._contents = None def __bake__(self): @@ -71,9 +70,6 @@ class Tree(base.Object): """ return self.get(file) - @property - def basename(self): - os.path.basename(self.path) def __repr__(self): return '' % self.id -- cgit v1.2.1 From 15b9129ec639112e94ea96b6a395ad9b149515d1 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 11 Oct 2009 19:07:03 +0200 Subject: lazymixin system now supports per-attribute baking, it is up to the class whether it bakes more. This also leads to more efficient use of memory as values are only cached and set when required - the baking system does not require an own tracking variable anymore, and values are only to be cached once - then python will natively find the cache without involving any additional overhead. This works by using __getattr__ instead of __get_attribute__ which would always be called --- lib/git/tree.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'lib/git/tree.py') diff --git a/lib/git/tree.py b/lib/git/tree.py index 90f1b72d..db4a3e22 100644 --- a/lib/git/tree.py +++ b/lib/git/tree.py @@ -13,18 +13,19 @@ class Tree(base.IndexObject): type = "tree" __slots__ = "_contents" - def __init__(self, repo, id, mode=None, path=None, size=None): - super(Tree, self).__init__(repo, id, mode, path, size) - self._contents = None - - def __bake__(self): - # Read the tree contents. - super(Tree, self).__bake__() - self._contents = {} - for line in self.repo.git.ls_tree(self.id).splitlines(): - obj = self.content_from_string(self.repo, line) - if obj is not None: - self._contents[obj.path] = obj + def __init__(self, repo, id, mode=None, path=None): + super(Tree, self).__init__(repo, id, mode, path) + + def _set_cache_(self, attr): + if attr == "_contents": + # Read the tree contents. + self._contents = {} + for line in self.repo.git.ls_tree(self.id).splitlines(): + obj = self.content_from_string(self.repo, line) + if obj is not None: + self._contents[obj.path] = obj + else: + super(Tree, self)._set_cache_(attr) @staticmethod def content_from_string(repo, text): -- cgit v1.2.1 From 9a119924bd314934158515a1a5f5877be63f6f91 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 11 Oct 2009 20:21:22 +0200 Subject: fixed issue in Ref.name implementation which would not handle components properly --- lib/git/tree.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/git/tree.py') diff --git a/lib/git/tree.py b/lib/git/tree.py index db4a3e22..597668ae 100644 --- a/lib/git/tree.py +++ b/lib/git/tree.py @@ -47,11 +47,11 @@ class Tree(base.IndexObject): return None if typ == "tree": - return Tree(repo, id=id, mode=mode, path=path) + return Tree(repo, id, mode, path) elif typ == "blob": - return blob.Blob(repo, id=id, mode=mode, path=path) + return blob.Blob(repo, id, mode, path) elif typ == "commit": - return None + return None else: raise(TypeError, "Invalid type: %s" % typ) -- cgit v1.2.1