<commit_before># -*- coding: utf-8 -*-
"""
PlexLibrary
"""
from plexapi import log, utils
from plexapi import X_PLEX_CONTAINER_SIZE
from plexapi.media import MediaTag
from plexapi.exceptions import BadRequest, NotFound


class Library(object):

    def __init__(self, server, data):
        self.server = server
        self.identifier = data.attrib.get('identifier')
        self.mediaTagVersion = data.attrib.get('mediaTagVersion')
        self.title1 = data.attrib.get('title1')
        self.title2 = data.attrib.get('title2')

    def __repr__(self):
        return '<Library:%s>' % self.title1.encode('utf8')

    def sections(self):
        items = []
        SECTION_TYPES = {
            MovieSection.TYPE: MovieSection,
            ShowSection.TYPE: ShowSection,
            MusicSection.TYPE: MusicSection,
        }
        path = '/library/sections'
        for elem in self.server.query(path):
            stype = elem.attrib['type']
            if stype in SECTION_TYPES:
                cls = SECTION_TYPES[stype]
                items.append(cls(self.server, elem, path))
        return items

    def section(self, title=None):
        for item in self.sections():
            if item.title == title:
                return item
        raise NotFound('Invalid library section: %s' % title)

    def all(self):
        return utils.listItems(self.server, '/library/all')

    def onDeck(self):
        return utils.listItems(self.server, '/library/onDeck')

    def recentlyAdded(self):
        return utils.listItems(self.server, '/library/recentlyAdded')

    def get(self, title):
        return utils.findItem(self.server, '/library/all', title)

    def getByKey(self, key):
        return utils.findKey(self.server, key)
        
    def search(self, title, libtype=None, **kwargs):
        """ Searching within a library section is much more powerful. It seems certain attributes on
            the media objects can be targeted to filter this search down a bit, but I havent found the
            documentation for it. For example: "studio=Comedy%20Central" or "year=1999" "title=Kung Fu"
            all work. Other items such as actor=<id> seem to work, but require you already know the id
            of the actor.
        """
        # TODO: FIGURE THIS OUT!
        args = {}
        if title: args['title'] = title
        if libtype: args['type'] = utils.searchType(libtype)
        for attr, value in kwargs.items():
            args[attr] = value
        query = '/library/all%s' % utils.joinArgs(args)
        return utils.listItems(self.server, query)
    
    def cleanBundles(self):
        self.server.query('/library/clean/bundles')

    def emptyTrash(self):
        for section in self.sections():<commit_msg>Add note to library.search() that its basically not tested.<commit_after># -*- coding: utf-8 -*-
"""
PlexLibrary
"""
from plexapi import log, utils
from plexapi import X_PLEX_CONTAINER_SIZE
from plexapi.media import MediaTag
from plexapi.exceptions import BadRequest, NotFound


class Library(object):

    def __init__(self, server, data):
        self.server = server
        self.identifier = data.attrib.get('identifier')
        self.mediaTagVersion = data.attrib.get('mediaTagVersion')
        self.title1 = data.attrib.get('title1')
        self.title2 = data.attrib.get('title2')

    def __repr__(self):
        return '<Library:%s>' % self.title1.encode('utf8')

    def sections(self):
        items = []
        SECTION_TYPES = {
            MovieSection.TYPE: MovieSection,
            ShowSection.TYPE: ShowSection,
            MusicSection.TYPE: MusicSection,
        }
        path = '/library/sections'
        for elem in self.server.query(path):
            stype = elem.attrib['type']
            if stype in SECTION_TYPES:
                cls = SECTION_TYPES[stype]
                items.append(cls(self.server, elem, path))
        return items

    def section(self, title=None):
        for item in self.sections():
            if item.title == title:
                return item
        raise NotFound('Invalid library section: %s' % title)

    def all(self):
        return utils.listItems(self.server, '/library/all')

    def onDeck(self):
        return utils.listItems(self.server, '/library/onDeck')

    def recentlyAdded(self):
        return utils.listItems(self.server, '/library/recentlyAdded')

    def get(self, title):
        return utils.findItem(self.server, '/library/all', title)

    def getByKey(self, key):
        return utils.findKey(self.server, key)
        
    def search(self, title, libtype=None, **kwargs):
        """ Searching within a library section is much more powerful. It seems certain attributes on the media
            objects can be targeted to filter this search down a bit, but I havent found the documentation for
            it. For example: "studio=Comedy%20Central" or "year=1999" "title=Kung Fu" all work. Other items
            such as actor=<id> seem to work, but require you already know the id of the actor.
            TLDR: This is untested but seems to work. Use library section search when you can.
        """
        args = {}
        if title: args['title'] = title
        if libtype: args['type'] = utils.searchType(libtype)
        for attr, value in kwargs.items():
            args[attr] = value
        query = '/library/all%s' % utils.joinArgs(args)
        return utils.listItems(self.server, query)
    
    def cleanBundles(self):
        self.server.query('/library/clean/bundles')

    def emptyTrash(self):
        for section in self.sections():
{'fraction_non_alphanumeric': 0.10896748838871026, 'fraction_numerical': 0.004644515898535191, 'mean_word_length': 2.4161073825503356, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5748996', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1593, 'n_words': 458}