#!/usr/bin/env python # -*- mode: python; coding: utf-8 -*- # Copyright © 2001, 2002, 2007 Translation Project. # Copyright © 1996, 1997, 1998, 1999, 2000 Progiciels Bourbeau-Pinard inc. # François Pinard , 1996. """\ Construct matrices showing the availability of PO files: 'tmp-matrix.texi', 'tmp-matrix.html' and 'tmp-matrix.xml'. """ import os, sys, string sys.path.insert(0, sys.path[0]+'/../lib') import config, registry try: del os.environ['LANGUAGE'] except KeyError:pass try: del os.environ['LANG'] except KeyError:pass os.umask(0002) def main(*arguments): inventory = Inventory() Html('tmp-matrix.html', inventory) Texinfo('tmp-matrix.texi', inventory) Xml('tmp-matrix.xml', inventory) def team_nick(team): if len(team.name) == 5: return team.name[3:] return team.name def percentage(counts): if counts[2] == 0: return 0 return 100 * counts[1] / counts[2] def ignored(filename): if filename[-9:]==".htaccess": return 1 return 0 class Inventory: """\ Make an inventory of all `.pot' and `.po' files. """ def __init__(self): # Preset results. self.domains = {} self.teams = {} self.pofiles = {} # Read list of POT files. for file in os.listdir('%s' % config.pots_path): try: hints = registry.Hints(file) except (KeyError,ValueError): if not ignored(file): sys.stderr.write("* Dubious file: %s/%s'\n" % (config.pots_path, file)) else: self.domains[hints.domain.name] = [0, 0, 0] # Read PO file statistics. import data postats = data.load_postats() for key in postats.keys(): domain, version_name, team = key version = registry.version(version_name) try: version.set_sort_key() except: continue if ((not self.pofiles.has_key((domain, team)) or version > self.pofiles[domain, team][0])): translated = postats[key][2] if translated > 0: # Drop the mtime element. self.pofiles[domain, team] = version, postats[key][:6] # Precompute various totals. for key in self.pofiles.keys(): domain, team = key version, (translator, mailto, translated, total, translated_length, total_length) = self.pofiles[key] if not self.domains.has_key(domain): self.domains[domain] = [0, 0, 0] counts = self.domains[domain] counts[0] = counts[0] + 1 counts[1] = counts[1] + translated_length counts[2] = counts[2] + total_length if not self.teams.has_key(team): self.teams[team] = [0, 0, 0] counts = self.teams[team] counts[0] = counts[0] + 1 counts[1] = counts[1] + translated_length counts[2] = counts[2] + total_length self.pofiles[key] = version, 100 * translated_length / total_length for domain in self.domains.keys(): counts = self.domains[domain] if counts[0] > 0: estimated = counts[2]/counts[0] counts[2] = counts[2] * len(self.teams) / counts[0] for team in self.teams.keys(): if not self.pofiles.has_key((domain, team)): counts = self.teams[team] counts[2] = counts[2] + estimated counts = [0, 0, 0] for team in self.teams.keys(): counts[0] = counts[0] + self.teams[team][0] counts[1] = counts[1] + self.teams[team][1] counts[2] = counts[2] + self.teams[team][2] self.total_all = counts # Get a sorted list of teams. def presort(name): try: team = registry.team(name) except KeyError: sys.stderr.write("* Team `%s' does not exist\n" % name) return None, None else: return string.lower(team_nick(team)), team def postsort((nick, team)): return team pairs = map(presort, self.teams.keys()) pairs.sort() self.all_teams = filter(None, map(postsort, pairs)) # Get a sorted list of domains. def presort(name): try: domain = registry.domain(name) except KeyError: sys.stderr.write("* Domain `%s' does not exist\n" % name) return None, None else: return string.lower(domain.name), domain def postsort((nick, domain)): return domain pairs = map(presort, self.domains.keys()) pairs.sort() self.all_domains = filter(None, map(postsort, pairs)) class Html: """\ Produce the availability matrix in HTML format. From an idea David Sven had on 1997-01-22. """ def __init__(self, output, inventory): write = self.prologue(output, 'The translation matrix') write(' \n') # Row of team codes at the top. write(' \n' ' \n' ' \n' ' \n') for team in inventory.all_teams: write(' \n' % (team.name, team_nick(team))) write(' \n' ' \n' ' \n') # Body of the matrix. write(' \n' ' \n' ' \n' ' \n') for team in inventory.all_teams: counts = inventory.teams[team.name] write(' \n' % percentage(counts)) write(' \n') write(' \n') for domain in inventory.all_domains: counts = inventory.domains[domain.name] write(' \n' ' \n' % (domain.name, domain.name)) write(' \n' % percentage(counts)) for team in inventory.all_teams: pair = inventory.pofiles.get((domain.name, team.name)) if pair is not None: version, percent = pair write(' \n' % (config.pos_dir, team.name, domain.name, version.name, team.name, percent)) elif team.name in domain.ext: write(' \n') else: write(' \n') write(' \n' % counts[0]) write(' \n') write(' \n') write(' \n' ' \n') counts = inventory.total_all write(' \n' % percentage(counts)) for team in inventory.all_teams: write(' \n' % inventory.teams[team.name][0]) write(' \n' % counts[0]) write(' \n' ' \n') # Row of team codes at the bottom. write(' \n' ' \n' ' \n' ' \n') for team in inventory.all_teams: write(' \n' % (team.name, team_nick(team))) write(' \n') write(' \n' ' \n') write('
Domain %sCount
 Pct%d%% 
%s%d%%' '%d%%ext %d
Sums%d%%%d%d
Team %s 
\n') self.epilogue(write) def prologue(self, file, title): write = open(file, 'w').write write('\n') write('\n' '\n' ' \n' ' %s\n' % title) write(' \n' ' \n' ' \n' '\n') write('\n' '
\n' '
\n') write('

%s

\n' % title) return write def epilogue(self, write): import commands write('
\n') write('
\n') write(' \n' '\n' '\n') class Texinfo: """\ Produce the availability matrix in Texinfo format. """ def __init__(self, output, inventory): separator = '-' * 70 + '\n' write = open(output, 'w').write write(separator) write('@example\n') team_count = len(inventory.all_teams) skip = team_count / ((team_count + 15) / 16) + 1 for count in range(0, team_count, skip): if count + skip < team_count: self.make_page(write, inventory, count == 0, 0, inventory.all_teams[count:count+skip]) else: self.make_page(write, inventory, count == 0, 1, inventory.all_teams[count:team_count]) write('@end example\n') write(separator) def make_page(self, write, inventory, first, last, team_list): if not first: write('\n') write('@group\n') if first: insert = 'Ready PO files' else: insert = '' write('%-16s %s\n' % (insert, string.join(map(team_nick, team_list)))) write('%-16s.-%s.\n' % ('', '-' * (len(team_list) * 3))) for domain in inventory.all_domains: write('%-16s| ' % domain.name) for team in team_list: if inventory.pofiles.has_key((domain.name, team.name)): write('[] ') elif team.name in domain.ext: write('E ') else: write(' ') if last: counts = inventory.domains[domain.name] write('| %2d\n' % counts[0]) else: write('|\n') write("%-16s`-%s'\n" % ('', '-' * (len(team_list) * 3))) if last: insert = '%4d teams' % len(inventory.all_teams) else: insert = '' write('%-16s %s\n' % (insert, string.join(map(team_nick, team_list)))) if last: insert = '%4d domains' % len(inventory.all_domains) else: insert = '' write('%-16s ' % insert) for team in team_list: counts = inventory.teams[team.name] write(' %2d' % counts[0]) if last: counts = inventory.total_all write(' %3d\n' % counts[0]) else: write('\n') write('@end group\n') class Xml: """\ Produce the matrix in XML format. Before changing the output format of this function, check with Bruno Haible. """ def __init__(self, output, inventory): write = open(output, 'w').write write('\n'); write('\n'); write(' \n'); for domain in inventory.all_domains: write(' \n' % (domain.name)); write(' \n'); write(' \n'); for team in inventory.all_teams: write(' \n' % (team.name)); write(' \n'); write(' \n'); for key in inventory.pofiles.keys(): domain, team = key pair = inventory.pofiles.get((domain, team)) if pair is not None: version, percent = pair write(' %s\n' % (domain, team, percent)) for domain in inventory.all_domains: for e in domain.ext: write(' \n' % (domain.name, e)) write(' \n'); write('\n'); if __name__ == '__main__': apply(main, tuple(sys.argv[1:]))