"""
Statically ports utilities from ubelt needed by xdoctest.
"""


def test_liberate_ubelt():
    import ubelt as ub

    # Uses netharn closer until it is ported to a standalone module
    from liberator import Liberator
    lib = Liberator()

    from ubelt import util_import
    lib.add_dynamic(util_import.split_modpath)
    lib.add_dynamic(util_import.modpath_to_modname)
    lib.add_dynamic(util_import.modname_to_modpath)
    lib.add_dynamic(util_import.import_module_from_name)
    lib.add_dynamic(util_import.import_module_from_path)
    lib.add_dynamic(util_import._pkgutil_modname_to_modpath)
    lib.add_dynamic(util_import._importlib_import_modpath)
    lib.add_dynamic(util_import.is_modname_importable)

    lib.expand(['ubelt'])
    text = lib.current_sourcecode()
    print(text)

    # if 0:
    #     import redbaron
    #     new_baron = redbaron.RedBaron(text)
    #     new_names = [n.name for n in new_baron.node_list if n.type in ['class', 'def']]
    #     import xdoctest
    #     old_baron = redbaron.RedBaron(open(xdoctest.utils.util_import.__file__, 'r').read())
    #     old_names = [n.name for n in old_baron.node_list if n.type in ['class', 'def']]
    #     set(old_names) - set(new_names)
    #     set(new_names) - set(old_names)

    prefix = ub.codeblock(
        '''
        # -*- coding: utf-8 -*-
        """
        This file was autogenerated based on code in ubelt
        """
        from __future__ import print_function, division, absolute_import, unicode_literals
        ''')

    final_text = prefix + '\n' + text + '\n'
    print(final_text)

    # fpath = ub.expandpath('~/code/xdoctest/xdoctest/utils/util_import.py')
    # open(fpath, 'w').write()
