refactor report base out of the summarize command

Change-Id: Ic66686f5123d435e9ae1c1c935d08bad4b4deb96
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2018-05-01 13:40:44 -04:00
parent ca8dbf916e
commit 5af6614932
2 changed files with 57 additions and 27 deletions

View File

@ -0,0 +1,54 @@
# 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 csv
import logging
from cliff import lister
LOG = logging.getLogger(__name__)
class ContributionsReportBase(lister.Lister):
"Base class for commands that report about contributions."
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'--role',
default=[],
action='append',
help='filter to only include specific roles (may be repeated)',
)
parser.add_argument(
'contribution_list',
nargs='+',
help='name(s) of files containing contribution details',
)
return parser
def get_contributions(self, parsed_args):
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
data = rows()
roles = parsed_args.role
if roles:
data = (d for d in data if d['Role'] in roles)
return data

View File

@ -11,12 +11,10 @@
# under the License.
import collections
import csv
import logging
from cliff import lister
from goal_tools.who_helped import contributions
from goal_tools.who_helped import report
LOG = logging.getLogger(__name__)
@ -35,7 +33,7 @@ def _count_distinct(by_names, to_count, data_source):
return {k: len(v) for k, v in counts.items()}
class SummarizeContributions(lister.Lister):
class SummarizeContributions(report.ContributionsReportBase):
"Summarize a contribution report."
def get_parser(self, prog_name):
@ -56,17 +54,6 @@ class SummarizeContributions(lister.Lister):
help=('combination of unique values to count '
'(may be repeated), defaults to counting each contribution'),
)
parser.add_argument(
'--role',
default=[],
action='append',
help='filter to only include specific roles (may be repeated)',
)
parser.add_argument(
'contribution_list',
nargs='+',
help='name(s) of files containing contribution details',
)
return parser
def take_action(self, parsed_args):
@ -77,18 +64,7 @@ class SummarizeContributions(lister.Lister):
to_count = parsed_args.count[:]
to_count_column = ', '.join(to_count) or 'Count'
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
data = rows()
roles = parsed_args.role
if roles:
data = (d for d in data if d['Role'] in roles)
data = self.get_contributions(parsed_args)
counts = _count_distinct(group_by, to_count, data)