diff options
author | 2020-01-25 23:01:18 +0100 | |
---|---|---|
committer | 2020-01-25 23:41:54 +0100 | |
commit | 6064d4f06db32ffe8fa0a6e09f186102c4f1ff9f (patch) | |
tree | cef5c266744ef7f5617ee80e0e0b361fa169af5c | |
parent | setup.py: Get some cruft out, use setuptools (diff) | |
download | metagen-6064d4f06db32ffe8fa0a6e09f186102c4f1ff9f.tar.gz metagen-6064d4f06db32ffe8fa0a6e09f186102c4f1ff9f.tar.bz2 metagen-6064d4f06db32ffe8fa0a6e09f186102c4f1ff9f.zip |
Migrate XML writer from dead jaxml to lxml.etree
Needed for migration to Python 3
Signed-off-by: Sebastian Pipping <sping@gentoo.org>
-rwxr-xr-x | metagen/metagenerator.py | 58 | ||||
-rwxr-xr-x | setup.py | 4 |
2 files changed, 48 insertions, 14 deletions
diff --git a/metagen/metagenerator.py b/metagen/metagenerator.py index fd00caa..e733997 100755 --- a/metagen/metagenerator.py +++ b/metagen/metagenerator.py @@ -1,21 +1,26 @@ #!/usr/bin/python3 - import sys +from xml.dom import minidom -import jaxml +from lxml import etree +from lxml.etree import Element, SubElement from portage.output import red -class MyMetadata(jaxml.XML_document): +class MyMetadata: """Create Gentoo Linux metadata.xml""" + class _Maintainer: + def __init__(self, type_=None, email=None, name=None, description=None): + self.type_ = type_ + self.email = email + self.name = name + self.description = description + def __init__(self): - jaxml.XML_document.__init__(self, "1.0", "UTF-8") - self._indentstring("\t") - self._text('<!DOCTYPE pkgmetadata SYSTEM ' + - '"http://www.gentoo.org/dtd/metadata.dtd">') - self.pkgmetadata() + self._maintainers = [] + self._long_description = None def set_maintainer(self, emails, names, descs, types): """Set maintainer(s)'s email, name, desc""" @@ -27,26 +32,51 @@ class MyMetadata(jaxml.XML_document): i = 0 for e in emails: - self._push("maintainer_level") - self.maintainer(type=types[i]).email(e) + maintainer = self._Maintainer(type_=types[i], email=e) if names: if len(names) > len(emails): print(red("!!! Nbr names > nbr emails")) sys.exit(1) if i <= len(names) -1: - self.name(names[i]) + maintainer.name = names[i] if descs: if len(descs) > len(emails): print(red("!!! Nbr descs > nbr emails")) sys.exit(1) if i <= len(descs) -1: - self.description(descs[i]) - self._pop("maintainer_level") + maintainer.description = descs[i] i += 1 + self._maintainers.append(maintainer) def set_longdescription(self, longdesc): """Set package's long description.""" - self.longdescription(longdesc) + self._long_description = longdesc + + def __str__(self): + doctype = '<!DOCTYPE pkgmetadata SYSTEM "http://www.gentoo.org/dtd/metadata.dtd">' + root = Element('pkgmetadata') + + for maintainer_data in self._maintainers: + maintainer_element = SubElement(root, 'maintainer') + maintainer_element.set('type', maintainer_data.type_) + if maintainer_data.email: + SubElement(maintainer_element, 'email').text = maintainer_data.email + if maintainer_data.name: + SubElement(maintainer_element, 'name').text = maintainer_data.name + if maintainer_data.description: + SubElement(maintainer_element, 'description').text = maintainer_data.description + + if self._long_description: + long_description = SubElement(root, 'longdescription') + long_description.text = self._long_description + + xml_text = etree.tostring(root, xml_declaration=True, doctype=doctype) + + # Re-write indentation to tabulators + # (for backwards compatibility and smaller diffs with existing files) + reparsed = minidom.parseString(xml_text) + return reparsed.toprettyxml(indent='\t', encoding='UTF-8').decode() + def do_tests(): from metagen import meta_unittest @@ -56,6 +56,10 @@ def main(): url=url, license = license, + install_requires = [ + 'lxml', + ], + packages = packages, data_files = data_files, package_data = package_data, |