# Path: dataimporter/fixer.py
# class WowToolsFixer:
#     """Base class for Wowtools-based data fixers."""
#     load_files = False
# 
#     def __init__(self, *args, build=None):
#         self.build = build
#         self._store_init(*args)
#         if self.load_files:
#             self.wt_files = {
#                 int(e['ID']): e for e in self.wt_get_table('files')
#             }
# 
#     def _store_init(self, *args):
#         raise NotImplementedError
# 
#     def run(self):
#         raise NotImplementedError
# 
#     def wt_get_table(self, table_name):
#         return wowtools.get_table(table_name, self.build)
# 
#     def get_icon_name(self, icon_id: int):
#         assert self.load_files
#         icon_name = str(icon_id)
#         if icon_id in self.wt_files:
#             icon_path = self.wt_files[icon_id]['Path']
#             if 'encrypted' not in icon_path:
#                 icon_name = icon_path.split('/')[-1].rsplit('.', 1)[0].lower()
#                 icon_name = icon_name.replace(' ', '-')
# 
#         return icon_name
# 
# Path: dataimporter/tools.py
# def changelog(*args, **kwargs):
#     print(*args, **kwargs, file=sys.stderr)
# 
# def genid():
#     return binascii.b2a_hex(os.urandom(4)).decode('ascii')
# 
# def filter_del(L, cond, deleted=None, format_deleted=None):
#     kept = [item for item in L if cond(item)]
#     if deleted is not None:
#         if format_deleted is None:
#             format_deleted = lambda x: x  # noqa
#         deleted.extend(format_deleted(item) for item in L if not cond(item))
#     return kept
# 
# def sort_try_respect_order(L, order_list, key='name'):
#     d = {k: v for v, k in enumerate(order_list)}
#     L.sort(key=lambda k: d.get(k[key], float('inf')))
# 
# def iscat(dct, supercat=None, cat=None, subcat=None, *, error_absent=False):
#     if supercat is not None:
#         res = find_or_create_item(
#             dct['supercats'], supercat, 'cats', error_absent
#         )
#     if cat is not None:
#         res = find_or_create_item(res['cats'], cat, 'subcats', error_absent)
#     if subcat is not None:
#         res = find_or_create_item(
#             res['subcats'], subcat, 'items', error_absent
#         )
#     return res
# 
# def list_find(L, pred):
#     return next(item for item in L if pred(item))


import collections
from .fixer import WowToolsFixer
from .tools import (changelog, genid, filter_del, sort_try_respect_order,
                    iscat, list_find)
    def del_empty(self):
        # Delete empty subcategories
        deleted_subc = []
        for supercat in self.achievs['supercats']:
            for cat in supercat['cats']:
                cat['subcats'] = filter_del(
                    cat['subcats'], lambda item: bool(item['items']),
                    deleted_subc,
                    lambda item: '  - {} > {} > {}'.format(
                        supercat['name'], cat['name'], item['name'])
                )
        if deleted_subc:
            changelog("* Deleted sub-categories:\n{}"
                      .format('\n'.join(deleted_subc)))

        # Delete empty categories
        deleted_cats = []
        for supercat in self.achievs['supercats']:
            supercat['cats'] = filter_del(
                supercat['cats'], lambda item: bool(item['subcats']),
                deleted_cats,
                lambda item: '  - {} > {}'.format(
                    supercat['name'], item['name'])
            )
        if deleted_cats:
            changelog("* Deleted categories:\n{}"
                      .format('\n'.join(deleted_cats)))

        # Delete empty supercategories
        deleted_supcats = []

# Path: dataimporter/fixer.py
# class WowToolsFixer:
#     """Base class for Wowtools-based data fixers."""
#     load_files = False
# 
#     def __init__(self, *args, build=None):
#         self.build = build
#         self._store_init(*args)
#         if self.load_files:
#             self.wt_files = {
#                 int(e['ID']): e for e in self.wt_get_table('files')
#             }
# 
#     def _store_init(self, *args):
#         raise NotImplementedError
# 
#     def run(self):
#         raise NotImplementedError
# 
#     def wt_get_table(self, table_name):
#         return wowtools.get_table(table_name, self.build)
# 
#     def get_icon_name(self, icon_id: int):
#         assert self.load_files
#         icon_name = str(icon_id)
#         if icon_id in self.wt_files:
#             icon_path = self.wt_files[icon_id]['Path']
#             if 'encrypted' not in icon_path:
#                 icon_name = icon_path.split('/')[-1].rsplit('.', 1)[0].lower()
#                 icon_name = icon_name.replace(' ', '-')
# 
#         return icon_name
# 
# Path: dataimporter/tools.py
# def changelog(*args, **kwargs):
#     print(*args, **kwargs, file=sys.stderr)
# 
# def genid():
#     return binascii.b2a_hex(os.urandom(4)).decode('ascii')
# 
# def filter_del(L, cond, deleted=None, format_deleted=None):
#     kept = [item for item in L if cond(item)]
#     if deleted is not None:
#         if format_deleted is None:
#             format_deleted = lambda x: x  # noqa
#         deleted.extend(format_deleted(item) for item in L if not cond(item))
#     return kept
# 
# def sort_try_respect_order(L, order_list, key='name'):
#     d = {k: v for v, k in enumerate(order_list)}
#     L.sort(key=lambda k: d.get(k[key], float('inf')))
# 
# def iscat(dct, supercat=None, cat=None, subcat=None, *, error_absent=False):
#     if supercat is not None:
#         res = find_or_create_item(
#             dct['supercats'], supercat, 'cats', error_absent
#         )
#     if cat is not None:
#         res = find_or_create_item(res['cats'], cat, 'subcats', error_absent)
#     if subcat is not None:
#         res = find_or_create_item(
#             res['subcats'], subcat, 'items', error_absent
#         )
#     return res
# 
# def list_find(L, pred):
#     return next(item for item in L if pred(item))


# Path: dataimporter/achievements.py
import collections
from .fixer import WowToolsFixer
from .tools import (changelog, genid, filter_del, sort_try_respect_order,
                    iscat, list_find)

    def del_empty(self):
        # Delete empty subcategories
        deleted_subc = []
        for supercat in self.achievs['supercats']:
            for cat in supercat['cats']:
                cat['subcats'] = filter_del(
                    cat['subcats'], lambda item: bool(item['items']),
                    deleted_subc,
                    lambda item: '  - {} > {} > {}'.format(
                        supercat['name'], cat['name'], item['name'])
                )
        if deleted_subc:
            changelog("* Deleted sub-categories:\n{}"
                      .format('\n'.join(deleted_subc)))

        # Delete empty categories
        deleted_cats = []
        for supercat in self.achievs['supercats']:
            supercat['cats'] = filter_del(
                supercat['cats'], lambda item: bool(item['subcats']),
                deleted_cats,
                lambda item: '  - {} > {}'.format(
                    supercat['name'], item['name'])
            )
        if deleted_cats:
            changelog("* Deleted categories:\n{}"
                      .format('\n'.join(deleted_cats)))

        # Delete empty supercategories
        deleted_supcats = []

        self.achievs['supercats'] = filter_del(