#!/usr/bin/env python from __future__ import division __requires__="TurboGears" import sys import shutil import logging from os import path, mkdir from turbogears import config from transifex.util import header, load_config from transifex.model import * from turbogears.database import metadata, session from turbogears.database import bind_meta_data log = logging.getLogger(__name__) def populate_stats(): """ Init stats table with random values """ import random log.info("Initializing Testing modules...") seed_locales = ['el', 'en', 'en_US', 'fr', 'pt'] for m in Module.query(): for b in m.branches: total = random.randrange(200,800) for l in seed_locales: translated = random.randrange(0,total) fuzzy = random.randrange(0,total-translated) untranslated = total-translated-fuzzy stat = TranslationStatistics(locale=l, module=m, branch=b, total=total, translated=translated, fuzzy=fuzzy, untranslated=untranslated) session.save(stat) log.info(' - %s' % (l)) log.info("Done.\n") def list_stats(): """ List translation statistics on the command line. """ for m in Module.query().filter_by(disabled=False): print "\nModule %s" % m.name for b in m.branches: print "\nBranch %s" % b.name stats = TranslationStatistics.byResource(m, b) stats = stats.order_by(TranslationStatistics.locale) if stats.count(): print "Locale\tTotal\tTranslat\tFuzzy\t\tUntranslated" for s in stats: print "%s\t%s\t%s (%s%%)\t%s (%s%%)\t%s (%s%%)" % ( s.locale, s.total, s.translated, 100*s.translated//s.total, s.fuzzy, 100*s.fuzzy//s.total, s.untranslated, 100*s.untranslated//s.total,) print "" def usage(name): print '''Command-line script for translation statistics administration. Usage: %(name)s [OPTIONS...] Options: -h, --help\t\tPrint this help message and exit -i, --init\t\tInitialize some statistics for testing purposes -l, --list\t\tList stats (might be a looong list) -d, --delete\t\tDelete all stats for all modules (caution!) ''' % {'name': name} # -v, --verbose\t\tIncrease verbosity def main(): import getopt try: opts, args = getopt.getopt(sys.argv[1:], "hidl", ["help", "init", "delete", "list"]) except getopt.GetoptError: # print help information and exit: usage(sys.argv[0]) sys.exit(2) configfile = None force = False test = False verbose = False load_config(configfile) bind_meta_data() logging.getLogger('sqlalchemy.engine').setLevel(logging.ERROR) logging.getLogger('sqlalchemy.orm.unitofwork').setLevel(logging.ERROR) for o, a in opts: if o in ("-h", "--help"): usage(sys.argv[0]) sys.exit(2) if o in ("-i", "--init"): print header("Populating statistics table...") session.begin() populate_stats() session.commit() print "\nDone.\n" sys.exit(2) if o in ("-d", "--delete"): print header("Deleting %s stats..." % TranslationStatistics.query().count()) session.execute(project_trans_stats_table.delete()) print "Making sure: ...found %s stats." % \ TranslationStatistics.query().count() print "\nDone.\n" if o in ("-l", "--list"): list_stats() if __name__ == "__main__": sys.exit(main())