Last active
August 29, 2015 14:09
-
-
Save brain-zhang/3b2a6688598afdf90685 to your computer and use it in GitHub Desktop.
Revisions
-
brain-zhang renamed this gist
Nov 18, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
brain-zhang created this gist
Nov 18, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ #!/bin/env python # -*- coding: utf-8 -*- import xml.etree.ElementTree as ET class CommentedTreeBuilder(ET.XMLTreeBuilder): def __init__(self, html = 0, target = None): ET.XMLTreeBuilder.__init__(self, html, target) self._parser.CommentHandler = self.handle_comment def handle_comment(self, data): self._target.start(ET.Comment, {}) self._target.data( data ) self._target.end(ET.Comment) def genconfig(xml_path, tag, text): """ set tag's text, "<tag>text</tag>" """ xml_config_tree = ET.parse(xml_path, parser=CommentedTreeBuilder()) for elem in xml_config_tree.iter(tag=tag): elem.text = text xml_config_tree.write(xml_path, xml_declaration=True, encoding='utf-8') def genconfig_attr(xml_path, tag, attr, value): """ set attr's value of tag, "<tag attr=value></tag>" """ xml_config_tree = ET.parse(xml_path, parser=CommentedTreeBuilder()) for elem in xml_config_tree.iter(tag=tag): elem.attrib[attr] = value xml_config_tree.write(xml_path, xml_declaration=True, encoding='utf-8')