Skip to content

Commit

Permalink
Flatten titles in SectionContainer.filter()
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzeman committed Jun 5, 2015
1 parent 8a9f4d1 commit aaf68ba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
30 changes: 30 additions & 0 deletions plex/core/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
import re
import unicodedata

def flatten(text):
if text is None:
return None

# Normalize `text` to ascii
text = normalize(text)

# Remove special characters
text = re.sub('[^A-Za-z0-9\s]+', '', text)

# Merge duplicate spaces
text = ' '.join(text.split())

# Convert to lower-case
return text.lower()

def normalize(text):
if text is None:
return None

# Normalize unicode characters
if type(text) is unicode:
text = unicodedata.normalize('NFKD', text)

# Ensure text is ASCII, ignore unknown characters
return text.encode('ascii', 'ignore')

def to_iterable(value):
if value is None:
return None
Expand Down
8 changes: 4 additions & 4 deletions plex/objects/library/container.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from plex.core.helpers import to_iterable
from plex.core.helpers import flatten, to_iterable
from plex.objects.core.base import Property
from plex.objects.container import Container
from plex.objects.library.section import Section
Expand Down Expand Up @@ -61,8 +61,8 @@ def filter(self, types=None, keys=None, titles=None):
titles = to_iterable(titles)

if titles:
# Normalize titles
titles = [x.lower() for x in titles]
# Flatten titles
titles = [flatten(x) for x in titles]

for section in self:
if not self.filter_passes(types, section.type):
Expand All @@ -71,7 +71,7 @@ def filter(self, types=None, keys=None, titles=None):
if not self.filter_passes(keys, section.key):
continue

if not self.filter_passes(titles, section.title.lower()):
if not self.filter_passes(titles, flatten(section.title)):
continue

yield section

0 comments on commit aaf68ba

Please sign in to comment.