|
1 | 1 | # -*- coding: utf-8 -*-
|
| 2 | +"""Repository and contributor stats logic.""" |
2 | 3 | from __future__ import unicode_literals
|
3 | 4 |
|
4 |
| -from datetime import datetime |
| 5 | +import datetime |
5 | 6 |
|
6 |
| -from .. import users |
| 7 | +import dateutil.tz |
7 | 8 |
|
8 |
| -from ..models import GitHubCore |
| 9 | +from .. import models |
| 10 | +from .. import users |
9 | 11 |
|
10 | 12 |
|
11 | 13 | def alternate_week(week):
|
| 14 | + """Map GitHub 'short' data to usable data. |
| 15 | +
|
| 16 | + .. note:: This is not meant for public consumption |
| 17 | +
|
| 18 | + :param dict week: |
| 19 | + The week's statistical data from GitHub |
| 20 | + :returns: |
| 21 | + Huamnized week statistical data |
| 22 | + :rtype: |
| 23 | + dict |
| 24 | + """ |
| 25 | + start_of_week = datetime.datetime.utcfromtimestamp(int(week['w'])) |
12 | 26 | return {
|
13 |
| - 'start of week': datetime.fromtimestamp(int(week['w'])), |
| 27 | + 'start of week': start_of_week.replace(tzinfo=dateutil.tz.tzutc()), |
14 | 28 | 'additions': week['a'],
|
15 | 29 | 'deletions': week['d'],
|
16 | 30 | 'commits': week['c'],
|
17 | 31 | }
|
18 | 32 |
|
19 | 33 |
|
20 |
| -class ContributorStats(GitHubCore): |
| 34 | +class ContributorStats(models.GitHubCore): |
| 35 | + """Representation of a user's contributor statistics to a repository. |
| 36 | +
|
| 37 | + See also http://developer.github.com/v3/repos/statistics/ |
| 38 | +
|
| 39 | + This object has the following attributes: |
| 40 | +
|
| 41 | + .. attribute:: author |
| 42 | +
|
| 43 | + A :class:`~github3.users.ShortUser` representing the contributor |
| 44 | + whose stats this object represents. |
| 45 | +
|
| 46 | + .. attribute:: total |
| 47 | +
|
| 48 | + The total number of commits authored by :attr:`author`. |
| 49 | +
|
| 50 | + .. attribute:: weeks |
| 51 | +
|
| 52 | + A list of dictionaries containing weekly statistical data. |
| 53 | +
|
| 54 | + .. attribute:: alternate_weeks |
| 55 | +
|
| 56 | + .. note:: |
| 57 | +
|
| 58 | + :mod:`github3` generates this data for a more humane interface |
| 59 | + to the data in :attr:`weeks`. |
| 60 | +
|
| 61 | + A list of dictionaries that provide an easier to remember set of |
| 62 | + keys as well as a :class:`~datetime.datetime` object representing the |
| 63 | + start of the week. The dictionary looks vaguely like: |
21 | 64 |
|
22 |
| - """This object provides easy access to information returned by the |
23 |
| - statistics section of the API. |
| 65 | + .. code-block:: python |
24 | 66 |
|
25 |
| - See http://developer.github.com/v3/repos/statistics/ for specifics. |
| 67 | + { |
| 68 | + 'start of week': datetime(2013, 5, 5, 5, 0, tzinfo=tzutc()) |
| 69 | + 'additions': 100, |
| 70 | + 'deletions': 150, |
| 71 | + 'commits': 5, |
| 72 | + } |
26 | 73 |
|
27 | 74 | """
|
28 | 75 |
|
29 | 76 | def _update_attributes(self, stats_object):
|
30 |
| - #: Contributor in particular that this relates to |
31 |
| - self.author = self._class_attribute( |
32 |
| - stats_object, 'author', users.ShortUser, self |
33 |
| - ) |
34 |
| - #: Total number of commits authored by ``author``. |
35 |
| - self.total = self._get_attribute(stats_object, 'total') |
36 |
| - #: List of weekly dictionaries. |
| 77 | + self.author = users.ShortUser(stats_object['author'], self) |
| 78 | + self.total = stats_object['total'] |
37 | 79 | self.weeks = self._get_attribute(stats_object, 'weeks', [])
|
38 |
| - #: Alternative collection of weekly dictionaries |
39 |
| - #: This provides a datetime object and easy to remember keys for each |
40 |
| - #: element in the list. |
41 |
| - #: 'w' -> 'start of week', 'a' -> 'Number of additions', |
42 |
| - #: 'd' -> 'Number of deletions', 'c' -> 'Number of commits' |
43 |
| - self.alt_weeks = self.weeks |
44 |
| - if self.alt_weeks: |
45 |
| - self.alt_weeks = [alternate_week(w) for w in self.weeks] |
| 80 | + alt_weeks = self.weeks |
| 81 | + if alt_weeks: |
| 82 | + alt_weeks = [alternate_week(w) for w in self.weeks] |
| 83 | + self.alternate_weeks = self.alt_weeks = alt_weeks |
46 | 84 |
|
47 | 85 | def _repr(self):
|
48 | 86 | return '<Contributor Statistics [{0}]>'.format(self.author)
|
0 commit comments