summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2012-07-03 21:47:54 +0200
committerGuido Günther <agx@sigxcpu.org>2012-07-27 13:45:49 +0200
commitf4da9649c81a18c5c3aee0e5cedcf3bab85eb033 (patch)
tree46c6976ba28d9e9f22ffcdab0d5f40e170c1efea
parent4db02e5726393455945a9070af41344bc0456e30 (diff)
GitModifier: More flexible date handling
Allow to pass in the date as datetime object, timestamp or git raw date. and allow to retrieve these values. This make constructing GitModifiers from python simpler.
-rw-r--r--gbp/git/modifier.py59
-rw-r--r--tests/test_GitModifier.py31
2 files changed, 85 insertions, 5 deletions
diff --git a/gbp/git/modifier.py b/gbp/git/modifier.py
index 51d9693b..ad5ee7a1 100644
--- a/gbp/git/modifier.py
+++ b/gbp/git/modifier.py
@@ -20,8 +20,9 @@ Someone who modifiers something in git
like committing changes or authoring a patch
"""
-from gbp.git.errors import GitError
+import calendar, datetime
+from gbp.git.errors import GitError
class GitModifierError(GitError):
"""Exception thrown by L{GitModifier}"""
@@ -31,9 +32,33 @@ class GitModifierError(GitError):
class GitModifier(object):
"""Stores authorship/comitter information"""
def __init__(self, name=None, email=None, date=None):
+ """
+ @param name: the modifier's name
+ @type name: C{str}
+ @param email: the modifier's email
+ @type email: C{str}
+ @param date: the date of the modification
+ @type date: C{str} (git raw date), C{int} (timestamp) or I{datetime} object
+ """
self.name = name
self.email = email
- self.date = date
+ self._parse_date(date)
+
+ def _parse_date(self, date):
+ self._offset = '+0000'
+ self._date = None
+
+ if isinstance(date, basestring):
+ timestamp, offset = date.split()
+ self._date = datetime.datetime.utcfromtimestamp(int(timestamp))
+ self._offset = offset
+ elif type(date) in [ type(0), type(0.0) ]:
+ self._date = datetime.datetime.utcfromtimestamp(date)
+ elif isinstance(date, datetime.datetime):
+ self._date = date
+ elif date != None:
+ raise ValueError("Date '%s' not timestamp, "
+ "datetime object or git raw date" % date)
def _get_env(self, who):
"""Get author or comitter information as env var dictionary"""
@@ -50,6 +75,30 @@ class GitModifier(object):
extra_env['GIT_%s_DATE' % who] = self.date
return extra_env
+ def get_date(self):
+ """Return date as a git raw date"""
+ if self._date:
+ return "%s %s" % (calendar.timegm(self._date.utctimetuple()),
+ self._offset)
+ else:
+ return None
+
+ def set_date(self, date):
+ """Set date from timestamp, git raw date or datetime object"""
+ self._parse_date(date)
+
+ date = property(get_date, set_date)
+
+ @property
+ def datetime(self):
+ """Return the date as datetime object"""
+ return self._date
+
+ @property
+ def tz_offset(self):
+ """Return the date's UTC offset"""
+ return self._offset
+
def get_author_env(self):
"""
Get env vars for authorship information
@@ -77,10 +126,10 @@ class GitModifier(object):
return self._get_env('committer')
def __getitem__(self, key):
- if key in self.keys():
- return self.__dict__[key]
+ if key == 'date':
+ return self.date
else:
- raise KeyError
+ return self.__dict__[key]
def keys(self):
return [ 'name', 'email', 'date' ]
diff --git a/tests/test_GitModifier.py b/tests/test_GitModifier.py
index 38944d8c..25c6531a 100644
--- a/tests/test_GitModifier.py
+++ b/tests/test_GitModifier.py
@@ -33,3 +33,34 @@ def test_author():
'bar'
>>> modifier['date']
"""
+
+def test_date():
+ """
+ Methods tested:
+ - L{gbp.git.GitModifier.__init__}
+
+ Properties tested:
+ - L{gbp.git.GitModifier.date}
+ - L{gbp.git.GitModifier.datetime}
+ - L{gbp.git.GitModifier.tz_offset}
+
+ >>> import gbp.git
+ >>> import datetime
+ >>> modifier = gbp.git.GitModifier('foo', 'bar', 1)
+ >>> modifier.date
+ '1 +0000'
+ >>> modifier.date = '1 +0400'
+ >>> modifier.date
+ '1 +0400'
+ >>> modifier['date']
+ '1 +0400'
+ >>> modifier.datetime
+ datetime.datetime(1970, 1, 1, 0, 0, 1)
+ >>> modifier.date = datetime.datetime(1970, 1, 1, 0, 0, 1)
+ >>> modifier.date
+ '1 +0000'
+ >>> modifier.datetime
+ datetime.datetime(1970, 1, 1, 0, 0, 1)
+ >>> modifier.tz_offset
+ '+0000'
+ """