From c6671c64668d67018cc89b364887dd056a498277 Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 17 Nov 2013 10:16:59 -0800 Subject: [PATCH] Finish .../stats/... (#203) --- github/GithubObject.py | 4 +++ github/Repository.py | 22 +++++++++--- github/StatsCodeFrequency.py | 61 +++++++++++++++++++++++++++++++++ github/StatsCommitActivity.py | 64 +++++++++++++++++++++++++++++++++++ github/StatsContributor.py | 4 +-- github/StatsParticipation.py | 56 ++++++++++++++++++++++++++++++ github/StatsPunchCard.py | 48 ++++++++++++++++++++++++++ github/tests/Repository.py | 21 +++++++++--- 8 files changed, 270 insertions(+), 10 deletions(-) create mode 100755 github/StatsCodeFrequency.py create mode 100755 github/StatsCommitActivity.py create mode 100755 github/StatsParticipation.py create mode 100755 github/StatsPunchCard.py diff --git a/github/GithubObject.py b/github/GithubObject.py index 0bd1bda1..8c2042fb 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -172,6 +172,10 @@ class GithubObject(object): def _makeListOfStringsAttribute(value): return GithubObject.__makeSimpleListAttribute(value, (str, unicode)) + @staticmethod + def _makeListOfIntsAttribute(value): + return GithubObject.__makeSimpleListAttribute(value, int) + @staticmethod def _makeListOfListOfStringsAttribute(value): return GithubObject.__makeSimpleListAttribute(value, list) diff --git a/github/Repository.py b/github/Repository.py index 0c2ceedd..6a791b5f 100644 --- a/github/Repository.py +++ b/github/Repository.py @@ -62,6 +62,10 @@ import github.Permissions import github.Event import github.Legacy import github.StatsContributor +import github.StatsCommitActivity +import github.StatsCodeFrequency +import github.StatsParticipation +import github.StatsPunchCard class Repository(github.GithubObject.CompletableGithubObject): @@ -1711,6 +1715,7 @@ class Repository(github.GithubObject.CompletableGithubObject): def get_stats_commit_activity(self): """ :calls: `GET /repos/:owner/:repo/stats/commit_activity `_ + :rtype: None or list of :class:`github.StatsCommitActivity.StatsCommitActivity` """ headers, data = self._requester.requestJsonAndCheck( "GET", @@ -1719,11 +1724,15 @@ class Repository(github.GithubObject.CompletableGithubObject): if data == {}: return None else: - return data # @todo Return something structured + return [ + github.StatsCommitActivity.StatsCommitActivity(self._requester, headers, attributes, completed=True) + for attributes in data + ] def get_stats_code_frequency(self): """ :calls: `GET /repos/:owner/:repo/stats/code_frequency `_ + :rtype: None or list of :class:`github.StatsCodeFrequency.StatsCodeFrequency` """ headers, data = self._requester.requestJsonAndCheck( "GET", @@ -1732,11 +1741,15 @@ class Repository(github.GithubObject.CompletableGithubObject): if data == {}: return None else: - return data # @todo Return something structured + return [ + github.StatsCodeFrequency.StatsCodeFrequency(self._requester, headers, attributes, completed=True) + for attributes in data + ] def get_stats_participation(self): """ :calls: `GET /repos/:owner/:repo/stats/participation `_ + :rtype: None or :class:`github.StatsParticipation.StatsParticipation` """ headers, data = self._requester.requestJsonAndCheck( "GET", @@ -1745,11 +1758,12 @@ class Repository(github.GithubObject.CompletableGithubObject): if data == {}: return None else: - return data # @todo Return something structured + return github.StatsParticipation.StatsParticipation(self._requester, headers, data, completed=True) def get_stats_punch_card(self): """ :calls: `GET /repos/:owner/:repo/stats/punch_card `_ + :rtype: None or :class:`github.StatsPunchCard.StatsPunchCard` """ headers, data = self._requester.requestJsonAndCheck( "GET", @@ -1758,7 +1772,7 @@ class Repository(github.GithubObject.CompletableGithubObject): if data == {}: return None else: - return data # @todo Return something structured + return github.StatsPunchCard.StatsPunchCard(self._requester, headers, data, completed=True) def get_subscribers(self): """ diff --git a/github/StatsCodeFrequency.py b/github/StatsCodeFrequency.py new file mode 100755 index 00000000..d6eb53e2 --- /dev/null +++ b/github/StatsCodeFrequency.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- + +############################ Copyrights and license ############################ +# # +# Copyright 2013 Vincent Jacques # +# # +# This file is part of PyGithub. http://jacquev6.github.com/PyGithub/ # +# # +# PyGithub is free software: you can redistribute it and/or modify it under # +# the terms of the GNU Lesser General Public License as published by the Free # +# Software Foundation, either version 3 of the License, or (at your option) # +# any later version. # +# # +# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # +# details. # +# # +# You should have received a copy of the GNU Lesser General Public License # +# along with PyGithub. If not, see . # +# # +################################################################################ + +import github.GithubObject + + +class StatsCodeFrequency(github.GithubObject.NonCompletableGithubObject): + """ + This class represents statistics of code frequency. The reference can be found here http://developer.github.com/v3/repos/statistics/#get-the-number-of-additions-and-deletions-per-week + """ + + @property + def week(self): + """ + :type: datetime.datetime + """ + return self._week.value + + @property + def additions(self): + """ + :type: int + """ + return self._additions.value + + @property + def deletions(self): + """ + :type: int + """ + return self._deletions.value + + def _initAttributes(self): + self._week = github.GithubObject.NotSet + self._additions = github.GithubObject.NotSet + self._deletions = github.GithubObject.NotSet + + def _useAttributes(self, attributes): + self._week = self._makeTimestampAttribute(attributes[0]) + self._additions = self._makeIntAttribute(attributes[1]) + self._deletions = self._makeIntAttribute(attributes[2]) diff --git a/github/StatsCommitActivity.py b/github/StatsCommitActivity.py new file mode 100755 index 00000000..e5491ae2 --- /dev/null +++ b/github/StatsCommitActivity.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- + +############################ Copyrights and license ############################ +# # +# Copyright 2013 Vincent Jacques # +# # +# This file is part of PyGithub. http://jacquev6.github.com/PyGithub/ # +# # +# PyGithub is free software: you can redistribute it and/or modify it under # +# the terms of the GNU Lesser General Public License as published by the Free # +# Software Foundation, either version 3 of the License, or (at your option) # +# any later version. # +# # +# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # +# details. # +# # +# You should have received a copy of the GNU Lesser General Public License # +# along with PyGithub. If not, see . # +# # +################################################################################ + +import github.GithubObject + + +class StatsCommitActivity(github.GithubObject.NonCompletableGithubObject): + """ + This class represents statistics of commit activity. The reference can be found here http://developer.github.com/v3/repos/statistics/#get-the-last-year-of-commit-activity-data + """ + + @property + def week(self): + """ + :type: datetime.datetime + """ + return self._week.value + + @property + def total(self): + """ + :type: int + """ + return self._total.value + + @property + def days(self): + """ + :type: list of int + """ + return self._days.value + + def _initAttributes(self): + self._week = github.GithubObject.NotSet + self._total = github.GithubObject.NotSet + self._days = github.GithubObject.NotSet + + def _useAttributes(self, attributes): + if "week" in attributes: # pragma no branch + self._week = self._makeTimestampAttribute(attributes["week"]) + if "total" in attributes: # pragma no branch + self._total = self._makeIntAttribute(attributes["total"]) + if "days" in attributes: # pragma no branch + self._days = self._makeListOfIntsAttribute(attributes["days"]) diff --git a/github/StatsContributor.py b/github/StatsContributor.py index 03ec3eb8..f6202d24 100755 --- a/github/StatsContributor.py +++ b/github/StatsContributor.py @@ -39,7 +39,7 @@ class StatsContributor(github.GithubObject.NonCompletableGithubObject): @property def w(self): """ - :type: int + :type: datetime.datetime """ return self._w.value @@ -90,7 +90,7 @@ class StatsContributor(github.GithubObject.NonCompletableGithubObject): @property def total(self): """ - :type: string + :type: int """ return self._total.value diff --git a/github/StatsParticipation.py b/github/StatsParticipation.py new file mode 100755 index 00000000..e609e6cf --- /dev/null +++ b/github/StatsParticipation.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- + +############################ Copyrights and license ############################ +# # +# Copyright 2013 Vincent Jacques # +# # +# This file is part of PyGithub. http://jacquev6.github.com/PyGithub/ # +# # +# PyGithub is free software: you can redistribute it and/or modify it under # +# the terms of the GNU Lesser General Public License as published by the Free # +# Software Foundation, either version 3 of the License, or (at your option) # +# any later version. # +# # +# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # +# details. # +# # +# You should have received a copy of the GNU Lesser General Public License # +# along with PyGithub. If not, see . # +# # +################################################################################ + +import github.GithubObject + +import github.NamedUser + + +class StatsParticipation(github.GithubObject.NonCompletableGithubObject): + """ + This class represents statistics of participation. The reference can be found here http://developer.github.com/v3/repos/statistics/#get-the-weekly-commit-count-for-the-repo-owner-and-everyone-else + """ + + @property + def all(self): + """ + :type: list of int + """ + return self._all.value + + @property + def owner(self): + """ + :type: list of int + """ + return self._owner.value + + def _initAttributes(self): + self._all = github.GithubObject.NotSet + self._owner = github.GithubObject.NotSet + + def _useAttributes(self, attributes): + if "all" in attributes: # pragma no branch + self._all = self._makeListOfIntsAttribute(attributes["all"]) + if "owner" in attributes: # pragma no branch + self._owner = self._makeListOfIntsAttribute(attributes["owner"]) diff --git a/github/StatsPunchCard.py b/github/StatsPunchCard.py new file mode 100755 index 00000000..61745aef --- /dev/null +++ b/github/StatsPunchCard.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- + +############################ Copyrights and license ############################ +# # +# Copyright 2013 Vincent Jacques # +# # +# This file is part of PyGithub. http://jacquev6.github.com/PyGithub/ # +# # +# PyGithub is free software: you can redistribute it and/or modify it under # +# the terms of the GNU Lesser General Public License as published by the Free # +# Software Foundation, either version 3 of the License, or (at your option) # +# any later version. # +# # +# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # +# details. # +# # +# You should have received a copy of the GNU Lesser General Public License # +# along with PyGithub. If not, see . # +# # +################################################################################ + +import github.GithubObject + +import github.NamedUser + + +class StatsPunchCard(github.GithubObject.NonCompletableGithubObject): + """ + This class represents the punch card. The reference can be found here http://developer.github.com/v3/repos/statistics/#get-the-number-of-commits-per-hour-in-each-day + """ + + def get(self, day, hour): + """ + Get a specific element + :param day: int + :param hour: int + :rtype: int + """ + return self._dict[(day, hour)] + + def _initAttributes(self): + self._dict = {} + + def _useAttributes(self, attributes): + for day, hour, commits in attributes: + self._dict[(day, hour)] = commits diff --git a/github/tests/Repository.py b/github/tests/Repository.py index 1b7ac2e3..9218dcd5 100644 --- a/github/tests/Repository.py +++ b/github/tests/Repository.py @@ -475,7 +475,20 @@ class Repository(Framework.TestCase): self.assertEqual(s.weeks[0].w, datetime.datetime(2012, 2, 12)) self.assertTrue(seenJacquev6) - self.repo.get_stats_commit_activity() - self.repo.get_stats_code_frequency() - self.repo.get_stats_participation() - self.repo.get_stats_punch_card() + stats = self.repo.get_stats_commit_activity() + self.assertEqual(stats[0].week, datetime.datetime(2012, 11, 18, 0, 0)) + self.assertEqual(stats[0].total, 29) + self.assertEqual(stats[0].days, [0, 7, 3, 9, 7, 3, 0]) + + stats = self.repo.get_stats_code_frequency() + self.assertEqual(stats[0].week, datetime.datetime(2012, 2, 12, 0, 0)) + self.assertEqual(stats[0].additions, 3853) + self.assertEqual(stats[0].deletions, -2098) + + stats = self.repo.get_stats_participation() + self.assertEqual(stats.owner, [1, 36, 8, 0, 0, 8, 18, 0, 0, 0, 0, 7, 20, 6, 9, 0, 4, 11, 20, 16, 0, 3, 0, 16, 0, 0, 6, 1, 4, 0, 1, 6, 0, 0, 12, 10, 0, 0, 0, 1, 44, 0, 20, 10, 0, 0, 0, 0, 0, 10, 0, 0]) + self.assertEqual(stats.all, [4, 36, 8, 0, 0, 10, 20, 0, 0, 0, 0, 11, 20, 6, 9, 0, 4, 14, 21, 16, 0, 3, 0, 20, 0, 0, 8, 1, 9, 16, 1, 15, 1, 0, 12, 12, 0, 4, 6, 15, 116, 20, 20, 11, 0, 0, 0, 0, 0, 10, 0, 0]) + + stats = self.repo.get_stats_punch_card() + self.assertEqual(stats.get(4, 12), 7) + self.assertEqual(stats.get(6, 18), 2)