Use reStructuredText-style docstrings
[stgit.git] / stgit / lib / git / person.py
blob3f782b0078ff9250df2d4b1aa0008c6b9f5ac97d
1 import re
3 from stgit.compat import environ_get
4 from stgit.config import config
6 from .base import Immutable
7 from .date import Date
10 class Person(Immutable):
11 """Represents an author or committer in a Git commit object.
13 Contains :attr:`name`, :attr:`email` and :attr:`timestamp`.
15 """
17 def __init__(self, name, email, date):
18 self.name = name
19 self.email = email
20 self.date = date
22 @property
23 def name_email(self):
24 return '%s <%s>' % (self.name, self.email)
26 def set_name(self, name):
27 return self._replace(name=name)
29 def set_email(self, email):
30 return self._replace(email=email)
32 def set_date(self, date):
33 return self._replace(date=date)
35 def _replace(self, **kws):
36 return type(self)(
37 kws.get('name', self.name),
38 kws.get('email', self.email),
39 kws.get('date', self.date),
42 def __repr__(self):
43 return '%s %s' % (self.name_email, self.date)
45 @classmethod
46 def parse(cls, s):
47 m = re.match(r'^([^<]*)<([^>]*)>\s+(\d+\s+[+-]\d{4})$', s)
48 name = m.group(1).strip()
49 email = m.group(2)
50 date = Date(m.group(3))
51 return cls(name, email, date)
53 @classmethod
54 def user(cls):
55 if not hasattr(cls, '_user'):
56 cls._user = cls(
57 config.get('user.name'), config.get('user.email'), date=None
59 return cls._user
61 @classmethod
62 def author(cls):
63 if not hasattr(cls, '_author'):
64 user = cls.user()
65 cls._author = cls(
66 environ_get('GIT_AUTHOR_NAME', user.name),
67 environ_get('GIT_AUTHOR_EMAIL', user.email),
68 Date.maybe(environ_get('GIT_AUTHOR_DATE')),
70 return cls._author
72 @classmethod
73 def committer(cls):
74 if not hasattr(cls, '_committer'):
75 user = cls.user()
76 cls._committer = cls(
77 environ_get('GIT_COMMITTER_NAME', user.name),
78 environ_get('GIT_COMMITTER_EMAIL', user.email),
79 Date.maybe(environ_get('GIT_COMMITTER_DATE')),
81 return cls._committer