From 44e2ee6a6c1918a46bff9a64ad313780f315215e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Mu=C5=82a?= Date: Fri, 22 Jan 2021 23:26:05 +0100 Subject: [PATCH] Port update_inlinedoc.py to new ElementTree --- update_inlinedoc.py | 50 +++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/update_inlinedoc.py b/update_inlinedoc.py index ea9ea65..ed2c02c 100644 --- a/update_inlinedoc.py +++ b/update_inlinedoc.py @@ -1,11 +1,12 @@ -import os +from pathlib import Path import sys +import os import textwrap import xml.etree.ElementTree as ET def main(): - dstpath = 'src/inline_doc.h' + dstpath = Path('src/inline_doc.h') app = Application(dstpath) app.run() @@ -26,14 +27,12 @@ def run(self): content += '\n' + self.__format_file(path, name) oldcontent = None - if os.path.exists(self.dstpath): - with open(self.dstpath, 'rt') as f: - oldcontent = f.read() + if self.dstpath.exists(): + oldcontent = self.dstpath.read_text() if content != oldcontent: - with open(self.dstpath, 'wt') as f: - print("Creating %s" % self.dstpath) - f.write(content) + print("Creating %s" % self.dstpath) + self.dstpath.write_text(content) def __format_file(self, path, name): @@ -45,11 +44,10 @@ def __format_file(self, path, name): def __get_files(self): - rootdir = 'docs' - for name in sorted(os.listdir(rootdir)): - if name.endswith('.rst') and name != 'index.rst': - path = os.path.join(rootdir, name) - name = name[:-4] + '_doc' + rootdir = Path('docs') + for path in sorted(rootdir.glob("*.rst")): + if path.name != 'index.rst': + name = path.stem + '_doc' yield (path, name) @@ -64,7 +62,7 @@ def __init__(self, xml_string, name): def format(self): self.lines = [] - for node in self.xml.getiterator('document')[0]: + for node in next(self.xml.iter('document')): if node.tag == 'title': self.format_title(node) elif node.tag == 'paragraph': @@ -90,24 +88,22 @@ def format_paragraph(self, node): def format_bullet_list(self, node): - for child in node.iterfind('list_item'): - par = child.getchildren()[0] - assert par.tag == 'paragraph' - - text = self.normalize(par) + for list_item in node.iter('list_item'): + for paragraph in list_item.iter('paragraph'): - lines = textwrap.wrap(text, width=(WIDTH - 2)) - for i, line in enumerate(lines): - if i == 0: - prefix = '- ' - else: - prefix = ' ' + text = self.normalize(paragraph) + lines = textwrap.wrap(text, width=(WIDTH - 2)) + for i, line in enumerate(lines): + if i == 0: + prefix = '- ' + else: + prefix = ' ' - self.lines.append(prefix + line) + self.lines.append(prefix + line) def normalize(self, node): - t = ET.tostring(node, method='text') + t = ET.tostring(node, method='text', encoding='unicode') t = t.split() return ' '.join(t)