mirror of
https://github.com/pkkid/python-plexapi
synced 2025-02-16 12:58:26 +00:00
Use breadth-first search when using rtag
(#912)
This commit is contained in:
parent
398c058f18
commit
82aede202d
2 changed files with 17 additions and 4 deletions
|
@ -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):
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Add table
Reference in a new issue