Use breadth-first search when using rtag (#912)

This commit is contained in:
JonnyWong16 2022-05-16 19:46:10 -07:00 committed by GitHub
parent 398c058f18
commit 82aede202d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View file

@ -282,9 +282,9 @@ class PlexObject(object):
kwargs['etag'] = cls.TAG
if cls and cls.TYPE and 'type' not in kwargs:
kwargs['type'] = cls.TYPE
# rtag to iter on a specific root tag
# rtag to iter on a specific root tag using breadth-first search
if rtag:
data = next(data.iter(rtag), [])
data = next(utils.iterXMLBFS(data, rtag), [])
# loop through all data elements to find matches
items = []
for elem in data:
@ -304,9 +304,9 @@ class PlexObject(object):
def listAttrs(self, data, attr, rtag=None, **kwargs):
""" Return a list of values from matching attribute. """
results = []
# rtag to iter on a specific root tag
# rtag to iter on a specific root tag using breadth-first search
if rtag:
data = next(data.iter(rtag), [])
data = next(utils.iterXMLBFS(data, rtag), [])
for elem in data:
kwargs['%s__exists' % attr] = True
if self._checkAttrs(elem, **kwargs):

View file

@ -9,6 +9,7 @@ import time
import unicodedata
import warnings
import zipfile
from collections import deque
from datetime import datetime
from getpass import getpass
from threading import Event, Thread
@ -472,3 +473,15 @@ def deprecated(message, stacklevel=2):
return func(*args, **kwargs)
return wrapper
return decorator
def iterXMLBFS(root, tag=None):
""" Iterate through an XML tree using a breadth-first search.
If tag is specified, only return nodes with that tag.
"""
queue = deque([root])
while queue:
node = queue.popleft()
if tag is None or node.tag == tag:
yield node
queue.extend(list(node))