234 lines
7.9 KiB
Diff
234 lines
7.9 KiB
Diff
From 0c7d355a76236b0c687fb7c59cd974c2c7f35657 Mon Sep 17 00:00:00 2001
|
|
From: Robin Lee <cheeselee@fedoraproject.org>
|
|
Date: Thu, 10 Sep 2020 21:31:22 +0800
|
|
Subject: [PATCH] Replace usage of `getchildren` and `getiterator` with `list`
|
|
or `iter`
|
|
|
|
Methods getchildren() and getiterator() of classes ElementTree and Element in
|
|
the ElementTree module have been deprecated since Python 3.2 and removed since
|
|
Python 3.9.
|
|
|
|
Fixes #1258
|
|
Closes #1259
|
|
---
|
|
tests/__init__.py | 2 +-
|
|
zim/formats/__init__.py | 44 ++++++++++++++++++++---------------------
|
|
zim/gui/pageview.py | 2 +-
|
|
3 files changed, 24 insertions(+), 24 deletions(-)
|
|
|
|
diff --git tests/__init__.py tests/__init__.py
|
|
index 20e5b8df..88f87f87 100644
|
|
--- tests/__init__.py
|
|
+++ tests/__init__.py
|
|
@@ -559,7 +559,7 @@ def __init__(self):
|
|
tree = etree.ElementTree(file=root + '/tests/data/notebook-wiki.xml')
|
|
|
|
test_data = []
|
|
- for node in tree.getiterator(tag='page'):
|
|
+ for node in tree.iter(tag='page'):
|
|
name = node.attrib['name']
|
|
text = str(node.text.lstrip('\n'))
|
|
test_data.append((name, text))
|
|
diff --git zim/formats/__init__.py zim/formats/__init__.py
|
|
index 9e00c169..e1149f7f 100644
|
|
--- zim/formats/__init__.py
|
|
+++ zim/formats/__init__.py
|
|
@@ -262,7 +262,7 @@ def hascontent(self):
|
|
'''Returns True if the tree contains any content at all.'''
|
|
root = self._etree.getroot()
|
|
return root is not None and (
|
|
- bool(root.getchildren()) or (root.text and not root.text.isspace())
|
|
+ bool(list(root)) or (root.text and not root.text.isspace())
|
|
)
|
|
|
|
@property
|
|
@@ -284,14 +284,14 @@ def extend(self, tree):
|
|
myroot = self._etree.getroot()
|
|
otherroot = tree._etree.getroot()
|
|
if otherroot.text:
|
|
- children = myroot.getchildren()
|
|
+ children = list(myroot)
|
|
if children:
|
|
last = children[-1]
|
|
last.tail = (last.tail or '') + otherroot.text
|
|
else:
|
|
myroot.text = (myroot.text or '') + otherroot.text
|
|
|
|
- for element in otherroot.getchildren():
|
|
+ for element in iter(otherroot):
|
|
myroot.append(element)
|
|
|
|
return self
|
|
@@ -312,7 +312,7 @@ def tostring(self):
|
|
|
|
# HACK: Force sorting of attrib - else change in python3.8 breaks test cases
|
|
# Ensure all attrib are string, else ElementTree fails
|
|
- for element in self._etree.getiterator('*'):
|
|
+ for element in self._etree.iter('*'):
|
|
myattrib = element.attrib.copy()
|
|
element.attrib.clear()
|
|
for key in sorted(myattrib.keys()):
|
|
@@ -342,8 +342,8 @@ def iter_href(self):
|
|
from zim.notebook.page import HRef # XXX
|
|
seen = set()
|
|
for elt in itertools.chain(
|
|
- self._etree.getiterator(LINK),
|
|
- self._etree.getiterator(IMAGE)
|
|
+ self._etree.iter(LINK),
|
|
+ self._etree.iter(IMAGE)
|
|
):
|
|
href = elt.attrib.get('href')
|
|
if href and href not in seen:
|
|
@@ -359,7 +359,7 @@ def iter_tag_names(self):
|
|
@returns: yields an unordered list of tag names
|
|
'''
|
|
seen = set()
|
|
- for elt in self._etree.getiterator(TAG):
|
|
+ for elt in self._etree.iter(TAG):
|
|
name = elt.text
|
|
if not name in seen:
|
|
seen.add(name)
|
|
@@ -367,7 +367,7 @@ def iter_tag_names(self):
|
|
|
|
def _get_heading_element(self, level=1):
|
|
root = self._etree.getroot()
|
|
- children = root.getchildren()
|
|
+ children = list(root)
|
|
if root.text and not root.text.isspace():
|
|
return None
|
|
|
|
@@ -424,7 +424,7 @@ def remove_heading(self, level=-1):
|
|
'''
|
|
root = self._etree.getroot()
|
|
roottext = root.text and not root.text.isspace()
|
|
- children = root.getchildren()
|
|
+ children = list(root)
|
|
|
|
if children and not roottext:
|
|
first = children[0]
|
|
@@ -442,7 +442,7 @@ def cleanup_headings(self, offset=0, max=6):
|
|
and a max depth.
|
|
'''
|
|
path = []
|
|
- for heading in self._etree.getiterator('h'):
|
|
+ for heading in self._etree.iter('h'):
|
|
level = int(heading.attrib['level'])
|
|
# find parent header in path using old level
|
|
while path and path[-1][0] >= level:
|
|
@@ -461,11 +461,11 @@ def resolve_images(self, notebook=None, path=None):
|
|
adds a '_src_file' attribute to the elements with the full file path.
|
|
'''
|
|
if notebook is None:
|
|
- for element in self._etree.getiterator('img'):
|
|
+ for element in self._etree.iter('img'):
|
|
filepath = element.attrib['src']
|
|
element.attrib['_src_file'] = File(filepath)
|
|
else:
|
|
- for element in self._etree.getiterator('img'):
|
|
+ for element in self._etree.iter('img'):
|
|
filepath = element.attrib['src']
|
|
element.attrib['_src_file'] = notebook.resolve_file(element.attrib['src'], path)
|
|
|
|
@@ -473,7 +473,7 @@ def unresolve_images(self):
|
|
'''Undo effect of L{resolve_images()}, mainly intended for
|
|
testing.
|
|
'''
|
|
- for element in self._etree.getiterator('img'):
|
|
+ for element in self._etree.iter('img'):
|
|
if '_src_file' in element.attrib:
|
|
element.attrib.pop('_src_file')
|
|
|
|
@@ -481,7 +481,7 @@ def encode_urls(self, mode=URL_ENCODE_READABLE):
|
|
'''Calls encode_url() on all links that contain urls.
|
|
See zim.parsing for details. Modifies the parse tree.
|
|
'''
|
|
- for link in self._etree.getiterator('link'):
|
|
+ for link in self._etree.iter('link'):
|
|
href = link.attrib['href']
|
|
if href and is_url_re.match(href):
|
|
link.attrib['href'] = url_encode(href, mode=mode)
|
|
@@ -492,7 +492,7 @@ def decode_urls(self, mode=URL_ENCODE_READABLE):
|
|
'''Calls decode_url() on all links that contain urls.
|
|
See zim.parsing for details. Modifies the parse tree.
|
|
'''
|
|
- for link in self._etree.getiterator('link'):
|
|
+ for link in self._etree.iter('link'):
|
|
href = link.attrib['href']
|
|
if href and is_url_re.match(href):
|
|
link.attrib['href'] = url_decode(href, mode=mode)
|
|
@@ -502,7 +502,7 @@ def decode_urls(self, mode=URL_ENCODE_READABLE):
|
|
def count(self, text):
|
|
'''Returns the number of occurences of 'text' in this tree.'''
|
|
count = 0
|
|
- for element in self._etree.getiterator():
|
|
+ for element in self._etree.iter():
|
|
if element.text:
|
|
count += element.text.count(text)
|
|
if element.tail:
|
|
@@ -515,7 +515,7 @@ def countre(self, regex):
|
|
in this tree.
|
|
'''
|
|
count = 0
|
|
- for element in self._etree.getiterator():
|
|
+ for element in self._etree.iter():
|
|
if element.text:
|
|
newstring, n = regex.subn('', element.text)
|
|
count += n
|
|
@@ -535,7 +535,7 @@ def _get_element_ends_with_newline(self, element):
|
|
elif element.tag in ('li', 'h'):
|
|
return True # implicit newline
|
|
else:
|
|
- children = element.getchildren()
|
|
+ children = list(element)
|
|
if children:
|
|
return self._get_element_ends_with_newline(children[-1]) # recurs
|
|
elif element.text:
|
|
@@ -586,7 +586,7 @@ def findall(self, tag):
|
|
@param tag: tag name
|
|
@returns: yields L{Node} objects
|
|
'''
|
|
- for elt in self._etree.getiterator(tag):
|
|
+ for elt in self._etree.iter(tag):
|
|
yield Element.new_from_etree(elt)
|
|
|
|
def replace(self, tag, func):
|
|
@@ -932,7 +932,7 @@ def end(self, tag):
|
|
if len(self._stack) > 1 and not (
|
|
tag in (IMAGE, OBJECT, HEADDATA, TABLEDATA)
|
|
or (self._last.text and not self._last.text.isspace())
|
|
- or self._last.getchildren()
|
|
+ or bool(list(self._last))
|
|
):
|
|
# purge empty tags
|
|
if self._last.text and self._last.text.isspace():
|
|
@@ -940,7 +940,7 @@ def end(self, tag):
|
|
|
|
empty = self._stack.pop()
|
|
self._stack[-1].remove(empty)
|
|
- children = self._stack[-1].getchildren()
|
|
+ children = list(self._stack[-1])
|
|
if children:
|
|
self._last = children[-1]
|
|
if not self._last.tail is None:
|
|
@@ -1053,7 +1053,7 @@ def close(self):
|
|
def _append_to_previous(self, text):
|
|
'''Add text before current element'''
|
|
parent = self._stack[-2]
|
|
- children = parent.getchildren()[:-1]
|
|
+ children = list(parent)[:-1]
|
|
if children:
|
|
if children[-1].tail:
|
|
children[-1].tail = children[-1].tail + text
|
|
diff --git zim/gui/pageview.py zim/gui/pageview.py
|
|
index 84fe99b4..c2c29cc5 100644
|
|
--- zim/gui/pageview.py
|
|
+++ zim/gui/pageview.py
|
|
@@ -857,7 +857,7 @@ def force_line_start():
|
|
if not iter.starts_line():
|
|
self.insert_at_cursor('\n')
|
|
|
|
- for element in node.getchildren():
|
|
+ for element in iter(node):
|
|
if element.tag in ('p', 'div'):
|
|
# No force line start here on purpose
|
|
if 'indent' in element.attrib:
|