'Load the index, automatically removing incorrectly sized thumbnails and pruning to fit max_size'

def _load_index(self):

    try:
        os.makedirs(self.location)
    except OSError as err:
        if (err.errno != errno.EEXIST):
            self.log(u'Failed    to    make    thumbnail    cache    dir:', as_unicode(err))
    self.total_size = 0
    self.items = OrderedDict()
    order = self._read_order()
    def listdir(*args):
        try:
            return os.listdir(os.path.join(*args))
        except EnvironmentError:
            return ()
    entries = (u'/'.join((parent, subdir, entry)) for parent in listdir(self.location) for subdir in listdir(self.location, parent) for entry in listdir(self.location, parent, subdir))
    invalidate = set()
    try:
        with open(os.path.join(self.location, u'invalidate'), u'rb') as f:
            raw = f.read()
    except EnvironmentError as err:
        if (getattr(err, u'errno', None) != errno.ENOENT):
            self.log(u'Failed    to    read    thumbnail    invalidate    data:', as_unicode(err))
    else:
        try:
            os.remove(os.path.join(self.location, u'invalidate'))
        except EnvironmentError as err:
            self.log(u'Failed    to    remove    thumbnail    invalidate    data:', as_unicode(err))
        else:
            def record(line):
                try:
                    (uuid, book_id) = line.partition(u'    ')[0::2]
                    book_id = int(book_id)
                    return (uuid, book_id)
                except Exception:
                    return None
            invalidate = {record(x) for x in raw.splitlines()}
    items = []
    try:
        for entry in entries:
            try:
                (uuid, name) = entry.split(u'/')[0::2]
                (book_id, timestamp, size, thumbnail_size) = name.split(u'-')
                (book_id, timestamp, size) = (int(book_id), float(timestamp), int(size))
                thumbnail_size = tuple(map(int, thumbnail_size.partition(u'x')[0::2]))
            except (ValueError, TypeError, IndexError, KeyError, AttributeError):
                continue
            key = (uuid, book_id)
            path = os.path.join(self.location, entry)
            if ((self.thumbnail_size == thumbnail_size) and (key not in invalidate)):
                items.append((key, Entry(path, size, timestamp, thumbnail_size)))
                self.total_size += size
            else:
                self._do_delete(path)
    except EnvironmentError as err:
        self.log(u'Failed    to    read    thumbnail    cache    dir:', as_unicode(err))
    self.items = OrderedDict(sorted(items, key=(lambda x: order.get(hash(x[0]), 0))))
    self._apply_size()
