Merge "zanata_stats: Use logger method for messages"

This commit is contained in:
Jenkins 2017-03-19 08:15:56 +00:00 committed by Gerrit Code Review
commit 4b42163c1a
1 changed files with 33 additions and 23 deletions

View File

@ -20,17 +20,17 @@ import csv
import datetime
import io
import json
import logging
import random
import re
import sys
from oslo_log import log as logging
import requests
import six
import yaml
ZANATA_URI = 'https://translate.openstack.org/rest/%s'
LOG = logging.getLogger(__name__)
LOG = logging.getLogger('zanata_stats')
ZANATA_VERSION_PATTERN = re.compile(r'^(master[-,a-z]*|stable-[a-z]+)$')
@ -52,9 +52,8 @@ class ZanataUtility(object):
req = requests.get(uri, headers=headers)
return req.text
except Exception as e:
print('exception happen', e)
LOG.warning('Error "%(error)s" while reading uri %(uri)s',
{'error': e, 'uri': uri})
LOG.error('Error "%(error)s" while reading uri %(uri)s',
{'error': e, 'uri': uri})
raise
def read_json_from_uri(self, uri):
@ -62,8 +61,8 @@ class ZanataUtility(object):
try:
return json.loads(data)
except Exception as e:
LOG.warning('Error "%(error)s" parsing json from uri %(uri)s',
{'error': e, 'uri': uri})
LOG.error('Error "%(error)s" parsing json from uri %(uri)s',
{'error': e, 'uri': uri})
raise
def get_projects(self):
@ -118,9 +117,9 @@ class LanguageTeam(object):
lang_notfound = [lang_code for lang_code in lang_list
if lang_code not in content]
if lang_notfound:
print('Language %s not tound in %s.' %
(', '.join(lang_notfound),
translation_team_uri))
LOG.error('Language %s not tound in %s.',
', '.join(lang_notfound),
translation_team_uri)
sys.exit(1)
return [cls(lang_code, team_info)
@ -234,8 +233,8 @@ class User(object):
def get_zanata_stats(start_date, end_date, language_teams, project_list,
version_list, user_list):
print('Getting Zanata contributors statistics (from %s to %s) ...' %
(start_date, end_date))
LOG.info('Getting Zanata contributors statistics (from %s to %s) ...',
start_date, end_date)
zanataUtil = ZanataUtility()
users = []
for team in language_teams:
@ -251,17 +250,17 @@ def get_zanata_stats(start_date, end_date, language_teams, project_list,
for user in users:
if user_list and user.user_id not in user_list:
continue
print('Getting %(project_id)s %(version)s '
'for user %(user_id)s %(user_lang)s'
% {'project_id': project_id,
'version': version,
'user_id': user.user_id,
'user_lang': user.lang})
statisticdata = zanataUtil.get_user_stats(
LOG.info('Getting %(project_id)s %(version)s '
'for user %(user_id)s %(user_lang)s',
{'project_id': project_id,
'version': version,
'user_id': user.user_id,
'user_lang': user.lang})
data = zanataUtil.get_user_stats(
project_id, version, user.user_id, start_date, end_date)
print('Got: %s' % statisticdata)
user.read_from_zanata_stats(statisticdata)
print('=> %s' % user)
LOG.debug('Got: %s', data)
user.read_from_zanata_stats(data)
LOG.debug('=> %s', user)
return users
@ -274,7 +273,7 @@ def write_stats_to_file(users, output_file, file_format,
_write_stats_to_csvfile(stats, output_file)
else:
_write_stats_to_jsonfile(stats, output_file)
print('Stats has been written to %s' % output_file)
LOG.info('Stats has been written to %s', output_file)
def _write_stats_to_csvfile(stats, output_file):
@ -342,10 +341,21 @@ def main():
parser.add_argument("-f", "--format",
default='csv', choices=['csv', 'json'],
help="Output file format.")
parser.add_argument("--debug",
action='store_true',
help="Enable debug message.")
parser.add_argument("user_yaml",
help="YAML file of the user list")
options = parser.parse_args()
logging_level = logging.DEBUG if options.debug else logging.INFO
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
handler = logging.StreamHandler()
handler.setLevel(logging_level)
handler.setFormatter(formatter)
LOG.setLevel(logging_level)
LOG.addHandler(handler)
language_teams = LanguageTeam.load_from_language_team_yaml(
options.user_yaml, options.lang)