From e49341ceeff3f9149b585e379bdb1218272ed669 Mon Sep 17 00:00:00 2001 From: Akihiro Motoki Date: Wed, 8 Mar 2017 00:26:40 +0000 Subject: [PATCH] zanata_stats: Use logger method for messages - Utilizes logging instead of oslo_log and catagorizs log messages into debug, error, info, and warning logs. - Implements --debug option to also see debug logs (by default, debug logs are not seen to output). Change-Id: Ib2b29896ca596f24cfb0c18602334435be6c2e13 --- tools/zanata/zanata_stats.py | 56 +++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/tools/zanata/zanata_stats.py b/tools/zanata/zanata_stats.py index dda752c..4d8f8a7 100755 --- a/tools/zanata/zanata_stats.py +++ b/tools/zanata/zanata_stats.py @@ -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)