add 'contributions summarize' command

Change-Id: Ib6aca3be27fbc99a6be9ecb735659869726c5e5e
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2018-04-30 19:21:26 -04:00
parent d2e2c8fb68
commit 668a5f7052
3 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,37 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import collections
from goal_tools.who_helped import summarize
from goal_tools.tests import base
class TestSummarizeBy(base.TestCase):
_data = [
{'a': 'A', 'b': 'B'},
{'a': 'A', 'b': 'C'},
]
def test_one_column(self):
results = summarize._summarize_by(['a'], self._data)
expected = collections.Counter({('A',): 2})
self.assertEqual(expected, results)
def test_two_columns(self):
results = summarize._summarize_by(['a', 'b'], self._data)
expected = collections.Counter({
('A', 'B'): 1,
('A', 'C'): 1,
})
self.assertEqual(expected, results)

View File

@ -0,0 +1,63 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import collections
import csv
import logging
from cliff import lister
LOG = logging.getLogger(__name__)
def _summarize_by(by_names, data_source):
counts = collections.Counter()
counts.update(
tuple(row[by] for by in by_names)
for row in data_source
)
return counts
class SummarizeContributions(lister.Lister):
"Summarize a contribution report."
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'--by', '-b',
action='append',
default=[],
help='columns to summarize by',
)
parser.add_argument(
'contribution_list',
nargs='+',
help='name(s) of files containing contribution details',
)
return parser
def take_action(self, parsed_args):
if not parsed_args.by:
raise RuntimeError('No --by values specified')
def rows():
for filename in parsed_args.contribution_list:
LOG.debug('reading %s', filename)
with open(filename, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
yield from reader
counts = _summarize_by(parsed_args.by, rows())
items = reversed(sorted(counts.items(), key=lambda x: x[1]))
columns = tuple(parsed_args.by) + ('Count',)
return (columns, (cols + (count,) for cols, count in items))

View File

@ -30,6 +30,7 @@ console_scripts =
who_helped =
contributions list = goal_tools.who_helped.contributions:ListContributions
contributions summarize = goal_tools.who_helped.summarize:SummarizeContributions
member show = goal_tools.who_helped.members:ShowMember
changes query = goal_tools.who_helped.changes:QueryChanges
cache remove = goal_tools.who_helped.cache:CacheRemove