def prompt_for_artwork(mediatype, medialabel, availableart, monitor):
    if not availableart:
        return None, None

    addon = pykodi.get_main_addon()
    arttypes = []
    for arttype, artlist in availableart.iteritems():
        if arttype.startswith('season.-1.'):
            # Ignore 'all' seasons artwork, as I can't set artwork for it with JSON
        label = arttype if not arttype.startswith('season.') else get_seasonlabel(arttype)
        for image in artlist:
            if image.get('existing'):
                arttypes.append({'arttype': arttype, 'label': label, 'count': len(artlist), 'url': image['url']})
        if arttype not in (at['arttype'] for at in arttypes):
            arttypes.append({'arttype': arttype, 'label': label, 'count': len(artlist)})
    arttypes.sort(key=lambda art: sort_arttype(art['arttype']))
    typeselectwindow = ArtworkTypeSelector('DialogSelect.xml', addon.path, arttypes=arttypes, medialabel=medialabel)
    hqpreview = addon.get_setting('highquality_preview')
    singletype = arttypes[0]['arttype'] if len(arttypes) == 1 else None
    selectedarttype = None
    selectedart = None
    while selectedart == None and not monitor.abortRequested():
        # The loop shows the first window if viewer backs out of the second
        if singletype:
            selectedarttype = singletype
            selectedarttype = typeselectwindow.prompt()
        if not selectedarttype:
        artlist = availableart[selectedarttype]
        if selectedarttype.startswith('season.'):
            stype = selectedarttype.rsplit('.', 1)[1]
            stype = mediatypes.artinfo[mediatypes.SEASON].get(stype)
            stype = mediatypes.artinfo[mediatype].get(selectedarttype)
        multi = stype['multiselect'] if stype else False
        artselectwindow = ArtworkSelector('DialogSelect.xml', addon.path, artlist=artlist, arttype=selectedarttype,
            medialabel=medialabel, multi=multi, hqpreview=hqpreview)
        selectedart = artselectwindow.prompt()
        if singletype and selectedart is None:
            selectedarttype = None
    return selectedarttype, selectedart
def remove_otherartwork(mediaitem):
    ''' Remove artwork not enabled in add-on settings. '''
    keep_types = pykodi.get_main_addon().get_setting('save_additional_arttypes')
    keep_types = [addon.strip() for addon in keep_types.split(',')]
    keep_types = dict(arttype.split(':', 2) if ':' in arttype else (arttype, sys.maxsize) for arttype in keep_types)
    finalart = {}

    for basetype in iter_base_arttypes(mediaitem['art']):
        if basetype in keep_types:
                max_allowed = int(keep_types[basetype])
            except ValueError:
                max_allowed = sys.maxsize
            max_allowed = mediatypes.artinfo[mediaitem['mediatype']].get(basetype, {}).get('autolimit', 0)
        finalart.update(_arrange_multiart(mediaitem['art'], basetype, limit=max_allowed))

    finalart.update((arttype, None) for arttype in mediaitem['art'] if arttype not in finalart)
    return finalart
import re
import xbmc
from collections import namedtuple
from os.path import split, basename, dirname

from devhelper import pykodi
from devhelper.pykodi import log

addon = pykodi.get_main_addon()

SortedDisplay = namedtuple('SortedDisplay', ['sort', 'display'])

def localize(messageid):
    if isinstance(messageid, basestring):
        return messageid
    if messageid >= 32000 and messageid < 33000:
        return addon.getLocalizedString(messageid)
    return xbmc.getLocalizedString(messageid)

def natural_sort(string, split_regex=re.compile(r'([0-9]+)')):
    return [int(text) if text.isdigit() else text.lower() for text in re.split(split_regex, string)]

# TODO: Load from advancedsettings.xml
moviestacking = [re.compile(r'(.*?)([ _.-]*(?:cd|dvd|p(?:ar)?t|dis[ck])[ _.-]*[0-9]+)(.*?)(\.[^.]+)$', re.IGNORECASE),
    re.compile(r'(.*?)([ _.-]*(?:cd|dvd|p(?:ar)?t|dis[ck])[ _.-]*[a-d])(.*?)(\.[^.]+)$', re.IGNORECASE),
    re.compile(r'(.*?)([ ._-]*[a-d])(.*?)(\.[^.]+)$', re.IGNORECASE)
def get_movie_path_list(stackedpath):
    """Returns a list of filenames that can be used to find a movie's supporting files.
    The list includes both possible filenames for stacks, and the VIDEO_TS/BDMV parent directory.
    If neither applies, returns a list of one item, the original path.