<commit_before>__author__ = "<NAME>"
__license__ = "MIT"
__version__ = "0.0.1"

# -------------------------------------------------------------------------------------------------------------------- #
# IMPORTS

# Modules:
import lxml.etree as et
import xmltodict
import datetime
import os
import shutil

# RiBuild Modules:
import delphin_6_automation.simulation.nosql.db_templates.delphin_entry as delphin_db
import delphin_6_automation.simulation.nosql.db_templates.result_entry as result_db
import delphin_6_automation.simulation.nosql.database_collections as collections
from delphin_6_automation.simulation.database_interactions import general_interactions as interactions

# -------------------------------------------------------------------------------------------------------------------- #
# DELPHIN FUNCTIONS AND CLASSES


def dp6_to_dict(path: str) -> dict:
    """
    Converts a Delphin 6 project file to a python dict
    :param path: Path to the Delphin project file
    :return: Dictionary with the project file information
    """

    xml_string = et.tostring(et.parse(path).getroot())
    xml_dict = xmltodict.parse(xml_string, encoding='UTF-8')

    return dict(xml_dict)


def upload_to_database(delphin_file: str,  queue_priority: int) -> delphin_db.Delphin.id:
    """
    Uploads a Delphin file to a database
    :param delphin_file: Path to a Delphin 6 project file
    :param queue_priority: Queue priority for the simulation
    :return: Database entry id
    """

    entry = delphin_db.Delphin()
    entry.materials = collections.material_db
    entry.weather = collections.weather_db
    entry.result_db = collections.raw_result_db
    entry.queue_priority = queue_priority

    delphin_dict = dp6_to_dict(delphin_file)
    entry.dp6_file = delphin_dict

    if len(delphin_dict['DelphinProject']['Discretization']) > 2:
        entry.dimensions = 3
    elif len(delphin_dict['DelphinProject']['Discretization']) > 1:
        entry.dimensions = 2
    else:
        entry.dimensions = 1

    entry.save()

    return entry.id


def mongo_document_to_dp6(document_id: str, path: str) -> bool:
    """
    Converts a database entry to Delphin 6 project file
    :param document_id: Database entry id.
    :param path: Path to where the files should be written.
    :return: True
    """

    delphin_document = delphin_db.Delphin.objects(id=document_id).first()
    delphin_dict = dict(delphin_document.dp6_file)
    xmltodict.unparse(delphin_dict, output=open(path + '/' + document_id + '.d6p', 'w'), pretty=True)

    return True


def download_from_database(document_id: str, path: str) -> bool:
    """
    Downloads a Delphin project file from the database with all of its materials and weather.
    :param document_id: Database entry id
    :param path: Path where the files should be written.
    :return: True
    """

    mongo_document_to_dp6(document_id, path)
    material_list = interactions.gather_material_list(document_id)
    weather_list = interactions.gather_weather_list(document_id)

    interactions.download_materials(material_list, path)
    interactions.download_weather(weather_list, path)

    return True


def d6o_to_dict(path: str, filename: str)-> tuple:
    """
    Converts a Delphin results file into a dict
    :param path: path to folder
    :param filename: file name with extension
    :return: converted result dict
    """

    # Helper functions
    def d6o_1d(d6o_lines):
        result_dict_ = dict()
        result_dict_['D6OARLZ'] = lines[0].split(' ')[-1].strip()

        meta_dict_ = {}

        for i in range(1, 14):
            line = lines[i].split('=')
            name = line[0].strip().lower()
<commit_msg>added todo to download_from_database<commit_after>__author__ = "<NAME>"
__license__ = "MIT"
__version__ = "0.0.1"

# -------------------------------------------------------------------------------------------------------------------- #
# IMPORTS

# Modules:
import lxml.etree as et
import xmltodict
import datetime
import os
import shutil

# RiBuild Modules:
import delphin_6_automation.simulation.nosql.db_templates.delphin_entry as delphin_db
import delphin_6_automation.simulation.nosql.db_templates.result_entry as result_db
import delphin_6_automation.simulation.nosql.database_collections as collections
from delphin_6_automation.simulation.database_interactions import general_interactions as interactions

# -------------------------------------------------------------------------------------------------------------------- #
# DELPHIN FUNCTIONS AND CLASSES


def dp6_to_dict(path: str) -> dict:
    """
    Converts a Delphin 6 project file to a python dict
    :param path: Path to the Delphin project file
    :return: Dictionary with the project file information
    """

    xml_string = et.tostring(et.parse(path).getroot())
    xml_dict = xmltodict.parse(xml_string, encoding='UTF-8')

    return dict(xml_dict)


def upload_to_database(delphin_file: str,  queue_priority: int) -> delphin_db.Delphin.id:
    """
    Uploads a Delphin file to a database
    :param delphin_file: Path to a Delphin 6 project file
    :param queue_priority: Queue priority for the simulation
    :return: Database entry id
    """

    entry = delphin_db.Delphin()
    entry.materials = collections.material_db
    entry.weather = collections.weather_db
    entry.result_db = collections.raw_result_db
    entry.queue_priority = queue_priority

    delphin_dict = dp6_to_dict(delphin_file)
    entry.dp6_file = delphin_dict

    if len(delphin_dict['DelphinProject']['Discretization']) > 2:
        entry.dimensions = 3
    elif len(delphin_dict['DelphinProject']['Discretization']) > 1:
        entry.dimensions = 2
    else:
        entry.dimensions = 1

    entry.save()

    return entry.id


def mongo_document_to_dp6(document_id: str, path: str) -> bool:
    """
    Converts a database entry to Delphin 6 project file
    :param document_id: Database entry id.
    :param path: Path to where the files should be written.
    :return: True
    """

    delphin_document = delphin_db.Delphin.objects(id=document_id).first()
    delphin_dict = dict(delphin_document.dp6_file)
    xmltodict.unparse(delphin_dict, output=open(path + '/' + document_id + '.d6p', 'w'), pretty=True)

    return True


def download_from_database(document_id: str, path: str) -> bool:
    """
    Downloads a Delphin project file from the database with all of its materials and weather.
    :param document_id: Database entry id
    :param path: Path where the files should be written.
    :return: True
    """

    mongo_document_to_dp6(document_id, path)
    material_list = interactions.gather_material_list(document_id)
    weather_list = interactions.gather_weather_list(document_id)

    # TODO - Update download weather & and materials
    #interactions.download_materials(material_list, path)
    #interactions.download_weather(weather_list, path)

    return True


def d6o_to_dict(path: str, filename: str)-> tuple:
    """
    Converts a Delphin results file into a dict
    :param path: path to folder
    :param filename: file name with extension
    :return: converted result dict
    """

    # Helper functions
    def d6o_1d(d6o_lines):
        result_dict_ = dict()
        result_dict_['D6OARLZ'] = lines[0].split(' ')[-1].strip()

        meta_dict_ = {}

        for i in range(1, 14):
            line = lines[i].split('=')
            name = line[0].strip().lower()

{'fraction_non_alphanumeric': 0.14900751072961374, 'fraction_numerical': 0.009120171673819743, 'mean_word_length': 2.8123721881390593, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3318866', 'n_tokens_mistral': 2331, 'n_tokens_neox': 2250, 'n_words': 577}