<commit_before>"""
Li-aung "Lewis" Yip
<EMAIL>
"""

import xml.etree.ElementTree as ET
import os

import unshortenit

from file_handling import fetch_url
from mod_file import Mod_File
from mod_pack import Mod_Pack


URL_BASE = "http://download.nodecdn.net/containers/atl/"


def atlauncher_to_catherder(pack_name, pack_version, download_cache_folder, install_folder):
    os.chdir(download_cache_folder)
    config_xml_file_path = fetch_atlauncher_config_xml(pack_name, pack_version)

    # Process Configs.xml and pull out list of files to download.
    tree = ET.parse(config_xml_file_path)
    root = tree.getroot()
    mods = root.findall("./mods/mod")
    libs = root.findall("./libraries/library")

    # From config file, pull out minecraft version number (need this for creating 'dependency' folder, i.e. ./mods/1.7.10/.
    minecraft_version = root.find("./pack/minecraft").text

    mp = Mod_Pack(pack_name, pack_version, download_cache_folder, install_folder, minecraft_version)



    # Build list of mod files that need to be downloaded.
    for mod in mods:
        f = Mod_File()

        if 'server' in mod.attrib.keys() and mod.attrib['server'] == 'no':
            f['required_on_server'] = False
        else:
            f['required_on_server'] = True

        f['name'] = mod.attrib['name']
        f['download_url_primary'] = expand_atlauncher_url(mod.attrib['url'], mod.attrib['download'])
        f['download_md5'] = mod.attrib['md5']
        f['install_filename'] = mod.attrib['file']
        f['required_on_client'] = True
        f['description'] = mod.attrib['description']

        install_types_folders = {
            'mods': 'mods/',
            'forge': './',
            'dependency': 'mods/' + mp['minecraft_version'] + "/",
            'ic2lib': 'mods/ic2/',
            'flan': 'mods/Flan/',  # not tested
            'denlib': 'mods/denlib/',  # not tested
            'plugins': 'plugins/',  # not tested
            'coremods': 'coremods/',  # not tested
            'jarmod': 'jarmods/',  #not tested
            'disabled': 'disabledmods/',  #not tested
            'bin': 'bin/',  #not tested
            'natives': 'natives/'  #not tested
        }

        t = mod.attrib['type']
        fn = f['install_filename']
        url = f['download_url_primary']

        if t in install_types_folders.keys():
            f['install_method'] = 'copy'
            f['install_path'] = install_types_folders[t]
        elif t == 'extract':
            f['install_method'] = 'unzip'
            e = mod.attrib['extractto']
            if e == 'root':
                f['install_path'] = './'
            elif e == 'mods':
                f['install_path'] = 'mods/'
            else:
                print (
                    "WARNING - didn't know what to do with file {f} of type {t} extractto type {e}.".format(f=fn, t=t,
                                                                                                            e=e))
        else:
            print ("WARNING - didn't know what to do with file {f} of type {t}.".format(f=fn, t=t))
            continue
        print ('Adding mod {m} to download list - {u}'.format(m=fn, u=url))
        mp.mod_files.append(f)

    for lib in libs:
        # print lib.attrib

        f = Mod_File()

        # TODO - clean up common code with 'mod' loop above.
        f['download_url_primary'] = expand_atlauncher_url(lib.attrib['url'], lib.attrib['download'])

        f['download_md5'] = lib.attrib['md5']
        f['install_filename'] = lib.attrib['file']
        f['name'] = lib.attrib['name']
        f['install_method'] = 'copy'
        f['description'] = 'Library.'
        if 'server' in lib.attrib.keys():
            f['required_on_server'] = True
            # noinspection PyUnusedLocal
            [dir_path, filename] = os.path.split('libraries/' + lib.attrib['server'])
            f['install_path'] = dir_path
            print (<commit_msg>ATLauncher XML file - libraries don't have 'name'.<commit_after>"""
Li-aung "Lewis" Yip
<EMAIL>
"""

import xml.etree.ElementTree as ET
import os

import unshortenit

from file_handling import fetch_url
from mod_file import Mod_File
from mod_pack import Mod_Pack


URL_BASE = "http://download.nodecdn.net/containers/atl/"


def atlauncher_to_catherder(pack_name, pack_version, download_cache_folder, install_folder):
    os.chdir(download_cache_folder)
    config_xml_file_path = fetch_atlauncher_config_xml(pack_name, pack_version)

    # Process Configs.xml and pull out list of files to download.
    tree = ET.parse(config_xml_file_path)
    root = tree.getroot()
    mods = root.findall("./mods/mod")
    libs = root.findall("./libraries/library")

    # From config file, pull out minecraft version number (need this for creating 'dependency' folder, i.e. ./mods/1.7.10/.
    minecraft_version = root.find("./pack/minecraft").text

    mp = Mod_Pack(pack_name, pack_version, download_cache_folder, install_folder, minecraft_version)



    # Build list of mod files that need to be downloaded.
    for mod in mods:
        f = Mod_File()

        if 'server' in mod.attrib.keys() and mod.attrib['server'] == 'no':
            f['required_on_server'] = False
        else:
            f['required_on_server'] = True

        f['name'] = mod.attrib['name']
        f['download_url_primary'] = expand_atlauncher_url(mod.attrib['url'], mod.attrib['download'])
        f['download_md5'] = mod.attrib['md5']
        f['install_filename'] = mod.attrib['file']
        f['required_on_client'] = True
        f['description'] = mod.attrib['description']

        install_types_folders = {
            'mods': 'mods/',
            'forge': './',
            'dependency': 'mods/' + mp['minecraft_version'] + "/",
            'ic2lib': 'mods/ic2/',
            'flan': 'mods/Flan/',  # not tested
            'denlib': 'mods/denlib/',  # not tested
            'plugins': 'plugins/',  # not tested
            'coremods': 'coremods/',  # not tested
            'jarmod': 'jarmods/',  #not tested
            'disabled': 'disabledmods/',  #not tested
            'bin': 'bin/',  #not tested
            'natives': 'natives/'  #not tested
        }

        t = mod.attrib['type']
        fn = f['install_filename']
        url = f['download_url_primary']

        if t in install_types_folders.keys():
            f['install_method'] = 'copy'
            f['install_path'] = install_types_folders[t]
        elif t == 'extract':
            f['install_method'] = 'unzip'
            e = mod.attrib['extractto']
            if e == 'root':
                f['install_path'] = './'
            elif e == 'mods':
                f['install_path'] = 'mods/'
            else:
                print (
                    "WARNING - didn't know what to do with file {f} of type {t} extractto type {e}.".format(f=fn, t=t,
                                                                                                            e=e))
        else:
            print ("WARNING - didn't know what to do with file {f} of type {t}.".format(f=fn, t=t))
            continue
        print ('Adding mod {m} to download list - {u}'.format(m=fn, u=url))
        mp.mod_files.append(f)

    for lib in libs:
        # print lib.attrib

        f = Mod_File()

        # TODO - clean up common code with 'mod' loop above.
        f['download_url_primary'] = expand_atlauncher_url(lib.attrib['url'], lib.attrib['download'])

        f['download_md5'] = lib.attrib['md5']
        f['install_filename'] = lib.attrib['file']
        f['name'] = 'a library'
        f['install_method'] = 'copy'
        f['description'] = 'Library.'
        if 'server' in lib.attrib.keys():
            f['required_on_server'] = True
            # noinspection PyUnusedLocal
            [dir_path, filename] = os.path.split('libraries/' + lib.attrib['server'])
            f['install_path'] = dir_path
            print (
{'fraction_non_alphanumeric': 0.1395407519555892, 'fraction_numerical': 0.0025233409033560434, 'mean_word_length': 2.118410700236035, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6497794', 'n_tokens_mistral': 2513, 'n_tokens_neox': 2440, 'n_words': 567}