# Conversion between a textual description and a more efficient database. # -*- mode: python; coding: utf-8 -*- # Copyright © 2001, 2002, 2003, 2004, 2007 Translation Project. # Copyright © 1998, 1999, 2000 Progiciels Bourbeau-Pinard inc. # François Pinard , 1998. import os, sys, string import config, data def quote(text): if '&' in text: text = text.replace('&', '&') return text ### Encode a clear-text file into a database. def encode_database(): sgml = read_sgml_file(config.progs_path + '/registry/registry.sgml') # Convert all domains and build a dictionary. domains = {} for domain in sgml[1][1:]: name = domain[1] info = {'name': name, 'package': None, 'potcopyright': None, 'ref': [], 'mailto': [], 'nomailto': [], 'keep': [], 'disclaim': 0, 'autosend': 0, 'note': [], 'url': [], 'ext': [], 'remark': [], } for item in domain[2:]: tag = item[0] if tag == 'ref': info[tag].append((item[1], item[2][1])) elif tag in ('keep', 'mailto', 'nomailto', 'remark', 'url', 'ext', 'note'): info[tag].append(item[1]) elif tag == 'autosend': if len(item) == 3: info[tag] = item[1]['option'] else: info[tag] = 1 elif tag in ('package', 'potcopyright'): info[tag] = item[1] else: info[tag] = 1 domains[name] = info # Prepare a list of domains. domain_list = domains.keys() domain_list.sort() # Convert all teams and build a dictionary. teams = {} for team in sgml[2][1:]: code = team[2][1] info = {'language': team[1], 'code': code, 'mailto': [], 'announceto': None, 'charset': None, 'leader': None, 'ref': [], 'remark': [], 'translator': {}, 'translators': None, # Sorted keys of translators. 'suppresspot': 0, } for item in team[3:]: tag = item[0] if tag == 'ref': info[tag].append((item[1], item[2][1])) elif tag in ('mailto', 'remark'): info[tag].append(item[1]) elif tag == 'translator': # Convert one translator in that team. trans = {'name': [item[1]], 'mailto': [], 'url': [], 'disclaimer': None, 'autosend': 0, 'do': [], 'remark': [], } info['translator'][item[1]] = trans for item in item[2:]: tag = item[0] if tag == 'alias': trans['name'].append(item[1]) info['translator'][item[1]] = trans elif tag in ('do', 'remark', 'url'): if len(item) == 1: raise "*** Empty %s tag." % item[0] if tag == 'do': e = domains[item[1]]['ext'] if info['code'] in e: raise "External assignment", (item[1], info['code']) trans[tag].append(item[1]) elif tag == 'disclaimer': if len(item) > 1: trans[tag] = item[1] or '*' else: trans[tag] = '*' elif tag == 'mailto': if len(item) == 3: trans[tag].append(item[2]) trans['showmail'] = item[1]['show'] elif len(item) == 2: trans[tag].append(item[1]) else: raise "*** Empty mailto tag." elif tag == 'autosend': if len(item) == 3: trans[tag] = item[1]['option'] else: trans[tag] = 1 else: trans[tag] = 1 elif tag == 'suppresspot': info[tag] = 1 else: info[tag] = item[1] # Prepare a list of translators in that team (aliases included). items = info['translator'].keys() items.sort() info['translators'] = items teams[code] = info # Prepare a list of teams. items = map(team_presort, teams.values()) items.sort() team_list = map(team_postsort, items) data.save_registry((domains, domain_list, teams, team_list)) def team_presort(team): return team['language'], team['code'] def team_postsort((language, code)): return code def read_sgml_file(name): stack = [] current = [] attrs = {} # Avoid docbk30, which raises some unanalysed interference. # Also request UTF-8 processing for line in os.popen('SGML_CATALOG_FILES= SP_ENCODING=UTF-8 SP_CHARSET_FIXED=YES nsgmls %s' % name).readlines(): if line[0] == '(': stack.append(current) current = [string.lower(line[1:-1])] if attrs: current.append(attrs) attrs = {} continue if line[0] == ')': element = tuple(current) current = stack[-1] del stack[-1] current.append(element) continue if line[0] == '-': line = line[1:-1] line = string.replace(line, '\\n', '\n') line = string.replace(line, '\\011', '\t') line = string.rstrip(line) current.append(line) continue if line[0] == 'A': attr = line[1:].split() if attr[1] == "IMPLIED": continue if attr[1] == "TOKEN": attrs[attr[0].lower()] = attr[2].lower() continue raise ValueError, "Unsupported attribute %s" % `attr` continue if line[0] == 'C': return current[0] raise ValueError, "SGML in '%s' is not conformant.\n" % name ### Decode the database to produce clear text. def decode_database(): import registry write = sys.stdout.write write("""\ """) for domain in registry.domain_list(): write('\n') write(domain_description(domain)) write("""\ """) for team in registry.team_list(): write('\n') write(team_description(team)) write("""\ """) def domain_description(domain): import registry lines = [] write = lines.append if not domain.package: write(' %s\n' % domain.name) else: write(' %s%s\n' % (domain.name, domain.package)) if domain.potcopyright: write(' %s\n' % domain.potcopyright) for ref in domain.ref: write(' %s%s\n' % (ref[0], quote(ref[1]))) for mailto in domain.mailto: write(' %s\n' % mailto) if domain.nomailto: for nomailto in domain.nomailto: write(' %s\n' % nomailto) if domain.keep: write(' %s\n' % string.join(domain.keep, '')) if domain.disclaim: disclaim = '' else: disclaim = '' if domain.autosend: if domain.autosend == 'compress': autosend = "" else: autosend = '' else: autosend = '' if disclaim or autosend: write(' %s%s\n' % (disclaim, autosend)) for url in domain.url: write(' %s\n' % url) for note in domain.note: write(' %s\n' % note) if domain.ext: write("".join([' ']+domain.ext)+"\n") for remark in domain.remark: write(' %s\n' % remark) return string.join(lines, '') def team_description(team): import registry lines = [] write = lines.append write(' %s%s\n' % (team.language, team.code)) for mailto in team.mailto: write(' %s\n' % mailto) if team.announceto: write(' %s\n' % team.announceto) if team.charset: write(' %s\n' % team.charset) if team.suppresspot: write(' \n') if team.leader: write(' %s\n' % team.leader.name[0]) for ref in team.ref: write(' %s%s\n' % (ref[0], quote(ref[1]))) for remark in team.remark: write(' %s\n' % remark) for name in team.translators: translator = registry.Translator(team, name) if translator.name[0] == name: write('\n') write(translator_description(translator)) return string.join(lines, '') def translator_description(translator): import registry lines = [] write = lines.append write(' %s\n' % translator.name[0]) for alias in translator.name[1:]: write(' %s\n' % alias) for url in translator.mailto: # XXX transitional if not hasattr(translator, 'showmail'): translator.showmail = None if translator.showmail is None: show = '' else: show = ' show='+translator.showmail write(' %s\n' % (show, url)) for url in translator.url: write(' %s\n' % url) if translator.disclaimer: if translator.disclaimer == '*': disclaimer = '' else: disclaimer = '' + translator.disclaimer else: disclaimer = '' if translator.autosend: if translator.autosend == 'compress': autosend = "" else: autosend = '' else: autosend = '' if disclaimer or autosend: write(' %s%s\n' % (disclaimer, autosend)) if translator.do: line = ' ' for do in translator.do: if len(line) + 4 + len(do.name) > 76: write(line + '\n') line = ' ' line = '%s%s' % (line, do.name) write(line + '\n') for remark in translator.remark: write(' %s\n' % remark) return string.join(lines, '')