diff --git a/zanataclient/cmdbase.py b/zanataclient/cmdbase.py index 29bb244..7e7fb0d 100644 --- a/zanataclient/cmdbase.py +++ b/zanataclient/cmdbase.py @@ -61,6 +61,12 @@ def generate_zanatacmd(self, url, headers): sys.exit(1) return ZanataCommand(url, headers) + def check_essential(self, item, message): + if not item: + log.error(message) + sys.exit(1) + return item + class ListProjects(CommandsBase): def __init__(self, *args, **kargs): @@ -236,11 +242,7 @@ def get_files(self): filelist = [] tmlfolder = "" project_type = self.context_data.get('project_type') - - if not project_type: - log.error("Please specify PROJECT_TYPE with --project-type option or using zanata.xml") - sys.exit(1) - elif project_type != 'podir' and project_type != 'gettext': + if project_type != 'podir' and project_type != 'gettext': log.error("The project type is not correct, please use 'podir' and 'gettext' as project type") sys.exit(1) @@ -426,13 +428,15 @@ def get_importparam(self, project_type, folder): return import_param def log_message(self, project_id, project_version, username): - if not project_id: - log.error("Please specify PROJECT_ID with --project-id option or using zanata.xml") - sys.exit(1) - log.info("Project: %s" % project_id) - if not project_version: - log.error("Please specify PROJECT_VERSION with --project-version option or using zanata.xml") - sys.exit(1) - log.info("Version: %s" % project_version) + log.info("Project: %s" % self.check_essential( + project_id, "Please specify PROJECT_ID with --project-id option or using zanata.xml" + )) + log.info("Version: %s" % self.check_essential( + project_version, "Please specify PROJECT_VERSION with --project-version option or using zanata.xml" + )) + log.info("Project Type: %s" % self.check_essential( + self.context_data.get('project_type'), + "Please specify PROJECT_TYPE with --project-type option or using zanata.xml" + )) log.info("Username: %s" % username) log.info("Source language: en-US") diff --git a/zanataclient/pullcmd.py b/zanataclient/pullcmd.py index 7bfcbe8..8b3183a 100644 --- a/zanataclient/pullcmd.py +++ b/zanataclient/pullcmd.py @@ -49,12 +49,7 @@ def run(self): sys.exit(1) locale_map = self.context_data.get('locale_map') - - if self.context_data.has_key('project_type'): - command_type = self.context_data.get('project_type') - else: - log.error("The project type is unknown") - sys.exit(1) + command_type = self.context_data.get('project_type') if self.context_data.get('publican_po'): # Keep dir option for publican/po pull diff --git a/zanataclient/zanata.py b/zanataclient/zanata.py index cf632b3..17210ca 100644 --- a/zanataclient/zanata.py +++ b/zanataclient/zanata.py @@ -603,8 +603,8 @@ def pull(command_options, args, project_type=None): --project-version : id of the version (defaults to zanata.xml value) --transdir : translations will be written to this folder --lang : language list (defaults to zanata.xml locales) - --min-doc-percent : Accepts integer from 0 to 100. Only pull translation documents which are at least - minimum doc percentage (message based) completed. + --min-doc-percent : Only pull translation documents that have at least this percentage of messages translated. + Accepts an integer from 0 to 100. --noskeletons : omit po files when translations not found --disable-ssl-cert disable ssl certificate validation in 0.7.x python-httplib2 """ diff --git a/zanataclient/zanatacmd.py b/zanataclient/zanatacmd.py index 1d822e3..ded8375 100644 --- a/zanataclient/zanatacmd.py +++ b/zanataclient/zanatacmd.py @@ -192,8 +192,8 @@ def list_projects(self): if not projects: # As we are catching exceptions related to reaching server, - # we may be certain that there is NO projects created. - self.log.info("There is no projects on the server.") + # we may be certain that there is NO project created. + self.log.info("There are no projects on this server.") sys.exit(1) for project in projects: @@ -336,7 +336,7 @@ def push_trans_command(self, transfolder, project_id, iteration_id, lang_list, l else: lang = item - self.log.info("\nPushing %s translation for %s to server:" % (item, project_id)) + self.log.info("Pushing %s translation for %s to server:" % (item, project_id)) if project_type == "podir": folder = os.path.join(transfolder, item) @@ -562,8 +562,9 @@ def get_project_translation_stats(self, project_id, project_version, min_doc_per disqualify_locales = [alias for alias, locale in locale_map.items() for lang in disqualify_locales if lang == locale] if disqualify_locales: - self.log.info('Translation file for document %s for locales [%s] are skipped due to ' - 'insufficient completed percentage' % (doc, ', '.join(map(str, disqualify_locales)))) + self.log.info('Translation file for document %s for locales [%s] are skipped ' + 'because they are less than %s%% translated (--min-doc-percent setting)' % + (doc, ', '.join(map(str, disqualify_locales)), min_doc_percent)) qualify_lang_set = set(lang_list) - set(disqualify_locales) doc_locales_dict.update({doc: list(qualify_lang_set)}) finally: diff --git a/zanataclient/zanatalib/projectutils.py b/zanataclient/zanatalib/projectutils.py index 7a45ced..6194fe6 100644 --- a/zanataclient/zanatalib/projectutils.py +++ b/zanataclient/zanatalib/projectutils.py @@ -56,25 +56,43 @@ def get_iteration(self, version_id): class Stats(object): - def __init__(self, stat_dict): - self.detailed_stat = stat_dict.get('detailedStats') - self.references = stat_dict.get('refs') - self.stats = stat_dict.get('stats') + def __init__(self, stats): + self.stats_dict = stats + + def _get_doc_trans_percent(self, doc_name, stats_dict): + trans_percent = {} + for stat in stats_dict: + if stat.get('locale'): + trans_percent.update({ + stat['locale']: int((float(stat.get('translated', 0) * 100) / + float(stat.get('total', 0)))) + }) + return {doc_name: trans_percent} + + @property + def project_version(self): + return self.stats_dict.get('id') + + @property + def trans_stats_dict(self): + return self.stats_dict.get('stats') @property def trans_percent_dict(self): trans_percent = {} - if isinstance(self.detailed_stat, list): - for doc in self.detailed_stat: - if isinstance(doc, dict): - document = doc.get('id') - doc_stats = doc.get('stats') - trans_percent[document] = {} - if isinstance(doc_stats, list): - for stat in doc_stats: - if isinstance(stat, dict) and stat.get('locale'): - trans_percent[document].update({ - stat['locale']: int((float(stat.get('translated', 0) * 100) / - float(stat.get('total', 0)))) - }) + detailed_stats = self.stats_dict.get('detailedStats') + if isinstance(detailed_stats, list): + for doc in detailed_stats: + if isinstance(doc, dict) and doc.get('id') and doc.get('stats'): + trans_percent.update(self._get_doc_trans_percent(doc['id'], doc['stats'])) return trans_percent + + @property + def trans_stats_detail_dict(self): + trans_dict = {} + detailed_stats = self.stats_dict.get('detailedStats') + if isinstance(detailed_stats, list): + for doc in detailed_stats: + if isinstance(doc, dict) and doc.get('id') and doc.get('stats'): + trans_dict.update({doc['id']: doc['stats']}) + return trans_dict