From a6bca603b9f3852a784afc76ea1d0a24306f3672 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Thu, 15 Mar 2012 22:08:50 +0100 Subject: GitModifier: add __getitem__ and keys() so it can be used as dictonary --- gbp/git/modifier.py | 16 ++++++++++++++++ gbp/git/repository.py | 3 ++- tests/test_GitModifier.py | 10 +++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/gbp/git/modifier.py b/gbp/git/modifier.py index c77bb4d..51d9693 100644 --- a/gbp/git/modifier.py +++ b/gbp/git/modifier.py @@ -76,3 +76,19 @@ class GitModifier(object): """ return self._get_env('committer') + def __getitem__(self, key): + if key in self.keys(): + return self.__dict__[key] + else: + raise KeyError + + def keys(self): + return [ 'name', 'email', 'date' ] + + def items(self): + items = [] + for key in self.keys(): + val = self.__dict__[key] + if val: + items.append((key, val)) + return items diff --git a/gbp/git/repository.py b/gbp/git/repository.py index 3daf2da..03b166c 100644 --- a/gbp/git/repository.py +++ b/gbp/git/repository.py @@ -935,6 +935,7 @@ class GitRepository(object): @type author: C{dict} with keys I{name}, I{email}, I{date} @param committer: committer information to use for commit @type committer: C{dict} with keys I{name}, I{email}, I{date} + or L{GitModifier} @param create_missing_branch: create I{branch} as detached branch if it doesn't already exist. @type create_missing_branch: C{bool} @@ -987,7 +988,7 @@ class GitRepository(object): @param msg: commit message @param parents: parents of this commit @param author: authorship information - @type author: C{dict} with keys 'name' and 'email' + @type author: C{dict} with keys 'name' and 'email' or L{GitModifier} @param committer: comitter information @type committer: C{dict} with keys 'name' and 'email' """ diff --git a/tests/test_GitModifier.py b/tests/test_GitModifier.py index 1a5b27c..38944d8 100644 --- a/tests/test_GitModifier.py +++ b/tests/test_GitModifier.py @@ -9,9 +9,10 @@ def test_author(): Methods tested: - L{gbp.git.GitModifier.get_author_env} - L{gbp.git.GitModifier.get_committer_env} + - L{gbp.git.GitModifier.keys} >>> import gbp.git - >>> modifier = gbp.git.GitModifier("foo", "bar") + >>> modifier = gbp.git.GitModifier('foo', 'bar') >>> modifier.name 'foo' >>> modifier.email @@ -24,4 +25,11 @@ def test_author(): Traceback (most recent call last): ... GitModifierError: Neither comitter nor author + >>> modifier.keys() + ['name', 'email', 'date'] + >>> modifier['name'] + 'foo' + >>> modifier['email'] + 'bar' + >>> modifier['date'] """ -- cgit v1.2.3