#!/usr/bin/env python # Copyright (C) 2009 Sebastian Pipping # Licensed under GPL 2 or later import xml.etree.ElementTree as ET import codecs from extradata import * # local from feedextractors import * # local from sharedutils import * # local def to_ascii(o, current_encoding='utf-8'): if not isinstance(o, basestring): return o if isinstance(o, unicode): s = o else: s = unicode(o, current_encoding) return codecs.encode(s, 'ascii', 'ignore') def append_feed(feed_uri, overlay_object): feed = ET.Element('feed') feed.text = feed_uri overlay_object.append(feed) a = ET.parse(open('layman-global.txt')) overlays = a.getroot() for overlay in overlays: repo_name = overlay.attrib['name'] extra_data = TRANSITION_DATA_EXTRA.get(repo_name, {}) # Move 'description' tag description = overlay.find('description') overlay.remove(description) overlay.insert(0, description) # Transform 'overlay' tag overlay.tag = 'repo' del overlay.attrib['name'] name = ET.Element('name') name.text = repo_name overlay.insert(0, name) # Transform 'link' tag link = overlay.find('link') if link != None: link.tag = 'homepage' # Transform 'contact' attribute owner = ET.Element('owner') overlay.append(owner) email = ET.Element('email') email.text = extra_data.get('owner', {}).\ get('email', overlay.attrib['contact']) del overlay.attrib['contact'] owner.append(email) # Transform 'src' and 'type' attribute source = ET.Element('source') source.text = overlay.attrib['src'] del overlay.attrib['src'] source.attrib['type'] = overlay.attrib['type'] del overlay.attrib['type'] overlay.append(source) # Extend by quality label try: overlay.attrib['quality'] = extra_data['quality'] except KeyError: pass # Extend by owner type if repo_name in TRANSITION_DATA_PROJECTS: owner.attrib['type'] = 'project' # Extend by owner name try: maint_name = extra_data['owner']['name'] name = ET.Element('name') name.text = maint_name owner.append(name) except KeyError: pass # Extend by feed URIs for fe in FEED_EXTRACTORS: uri = fe['regex'].sub(fe['format'], source.text) if uri != source.text: append_feed(uri, overlay) break try: feed_uris = extra_data['feeds'] except KeyError: feed_uris = () for uri in feed_uris: append_feed(uri, overlay) # Explicify defaults if 'status' not in overlay.attrib: overlay.attrib['status'] = 'unofficial' if 'quality' not in overlay.attrib: overlay.attrib['quality'] = 'experimental' # Add note on file being a generated one overlay.insert(0, ET.Comment('NOTE: This file is generated, do not edit.')) # Transform 'overlays' tag overlays.tag = 'repositories' # Sort overlays overlays[:] = sorted(overlays[:], key=lambda x: x.find('name').text.lower()) # Extend by format version overlays.attrib['version'] = '1.0' recurse_print(overlays) indent(overlays) repositories_xml = open('repositories.xml', 'w') repositories_xml.write("""\ """) a.write(repositories_xml, encoding='utf-8') repositories_xml.close()