<commit_before>import os
import re
import json

from slugify import slugify

from zou.app import app

from zou.app.models.entity import Entity
from zou.app.models.entity_type import EntityType
from zou.app.models.task_type import TaskType
from zou.app.models.task import Task
from zou.app.models.department import Department

from zou.app.services import (
    assets_service,
    entities_service,
    files_service,
    shots_service,
    projects_service,
    tasks_service
)
from zou.app.services.exception import (
    MalformedFileTreeException,
    WrongFileTreeFileException,
    WrongPathFormatException,
    TaskNotFoundException
)


def get_file_path(
    task,
    mode="working",
    software=None,
    output_type=None,
    name="",
    version=1,
    sep=os.sep
):
    file_name = get_file_name(
        task,
        mode=mode,
        software=software,
        output_type=output_type,
        name=name,
        version=version,
    )
    folder = get_folder_path(
        task,
        mode,
        software=software,
        output_type=output_type,
        name=name,
        sep=sep
    )

    return join_path(folder, file_name, sep)


def get_file_name(
    task,
    mode="working",
    software=None,
    output_type=None,
    name="",
    version=1
):
    entity = tasks_service.get_entity(task["entity_id"])
    project = get_project(entity)
    tree = get_tree_from_project(project)

    file_name = get_file_name_root(
        tree,
        mode,
        entity,
        task,
        software,
        output_type,
        name
    )
    file_name = add_version_suffix_to_file_name(file_name, version)

    return u"%s" % file_name


def get_instance_file_name(
    asset_instance,
    output_type,
    mode="output",
    name="",
    version=1
):
    shot = tasks_service.get_entity(asset_instance["entity_id"])
    asset = tasks_service.get_entity(asset_instance["asset_id"])
    project = get_project(shot)
    tree = get_tree_from_project(project)

    file_name = get_file_name_root(
        tree,
        mode,
        shot,
        None,
        None,
        output_type,
        name,
        asset_instance=asset_instance,
        asset=asset
    )
    file_name = add_version_suffix_to_file_name(file_name, version)

    return u"%s" % file_name


def get_folder_path(
    task,
    mode="working",
    software=None,
    output_type=None,
    name="",
    sep=os.sep
):
    entity = tasks_service.get_entity(task["entity_id"])
    project = get_project(entity)
    tree = get_tree_from_project(project)
    root_path = get_root_path(tree, mode, sep)
    style = tree[mode]["folder_path"].get("style", "")

    folder_template = get_folder_path_template(tree, mode, entity)
    folder_path = update_variable(
        folder_template,
        entity,
        task,
        software,
        output_type,
        name,
        style
    )
    folder_path = change_folder_path_separators(folder_path, sep)

    return join_path(root_path, folder_path, "")


def get_instance_folder_path(
    asset_instance,
    output_type,
    mode="output",
    sep=os.sep,
):
    shot = tasks_service.get_entity(asset_instance["entity_id"])
    asset = tasks_service.get_entity(asset_instance["asset_id"])
    project = get_project(shot)
    tree = get_tree_from_project(project)
    root_path = get_root_path(tree, mode, sep)
    style = tree[mode]["folder_path"].get("style", "")

    folder_template = get_folder_path_template(
        tree,
        mode,
        asset_instance
    )

    folder_path = update_variable(
        folder_template,
        shot,
        None,
        None,
        output_type,
        "",
        style,
        asset_instance=asset_instance,
        asset=asset
    )
    folder_path = change_folder_path_separators(folder_path, sep)

    return join_path(root_path, folder_path, "")


def get_project(entity):
    return projects_service.get_project(entity["project_id"])


def get_tree_from_project(project):
    return project["file_tree"]


def get_tree_from_file(tree_name):
    try:
        tree_path = os.path.join(
            app.config["FILE_TREE_FOLDER"], "%s.json" % tree_name)
        tree_string = open(tree_path).read()
    except IOError:
        raise WrongFileTreeFileException(
            "File Tree file not found: %s." % tree_path
        )
    return json.loads(tree_string)


def get_folder_path_template(tree, mode, entity):
    try:
        if entity["type"] == "AssetInstance":
            return tree[mode]["folder_path"]["instance"]
        elif shots_service.is_shot(entity):
            return tree[mode]["folder_path"]["shot"]
        elif shots_service.is_sequence(entity):
            return tree[mode]["folder_path"]["sequence"]
        elif shots_service.is_scene(entity):
            return tree[mode]["folder_path"]["scene"]
        else:
            return tree[mode]["folder_path"]["asset"]
    except KeyError:
        raise MalformedFileTreeException


def get_file_name_template(tree, mode, entity):
    try:
        if entity["type"] == "AssetInstance":
            return tree[mode]["file_name"]["instance"]
        elif shots_service.is_shot(entity):
            return tree[mode]["file_name"]["shot"]
        elif shots_service.is_sequence(entity):
            return tree[mode]["file_name"]["sequence"]
        elif shots_service.is_scene(entity):
            return tree[mode]["file_name"]["scene"]
        else:
            return tree[mode]["file_name"]["asset"]
    except KeyError:
        raise MalformedFileTreeException


def get_file_name_root(
    tree,
    mode,
    entity,
    task,
    software,
    output_type,
    name,
    asset_instance=None,
    asset=None
):
    if asset_instance is None:
        file_name_template = get_file_name_template(tree, mode, entity)
    else:
        file_name_template = get_file_name_template(tree, mode, asset_instance)

    file_name = update_variable(
        file_name_template,
        entity,
        task,
        software,
        output_type,
        name,
        asset_instance=asset_instance,
        asset=asset
    )
    file_name = slugify(file_name, separator="_")
    file_name = apply_style(file_name, tree[mode]["file_name"].get("style", ""))
    return file_name


def add_version_suffix_to_file_name(file_name, version=1):
    file_name = "%s_v%s" % (file_name, str(version).zfill(3))
    return file_name


def change_folder_path_separators(folder_path, sep):
    return folder_path.replace("/", sep)


def get_root_path(tree, mode, sep):
    try:
        mountpoint = tree[mode]["mountpoint"]
        root = tree[mode]["root"]
    except KeyError:
        raise MalformedFileTreeException(
            "Can't find given mode (%s) in given tree." % mode
        )
    return "%s%s%s%s" % (mountpoint, sep, root, sep)


def update_variable(
    template,
    entity,
    task,
    software=None,
    output_type=None,
    name="",
    style="lowercase",
    asset_instance=None,
    asset=None
):
    variables = re.findall('<(\w*)>', template)

    render = template
    for variable in variables:
        data = get_folder_from_datatype(
            variable,
            entity,
            task,
            software,
            output_type,
            name,
            asset_instance,
            asset
        )
        render = render.replace(
            "<%s>" % variable,
            apply_style(slugify(data, separator="_"), style)
        )

    return render


def get_folder_from_datatype(
    datatype,
    entity,
    task,
    software=None,
    output_type=None,
    name="",
    instance=None,
    asset=None
):
    if datatype == "Project":
        folder = get_folder_from_project(entity)
    elif datatype == "Task":
        folder = get_folder_from_task(task)
    elif datatype == "TaskType":
        folder = get_folder_from_task_type(task)
    elif datatype == "Department":
        folder = get_folder_from_department(task)
    elif datatype == "Shot":
        folder = get_folder_from_shot(entity)
    elif datatype == "TemporalEntity":
        folder = get_folder_from_temporal_entity(entity)
    elif datatype == "TemporalEntityType":
        folder = get_folder_from_temporal_entity_type(entity)
    elif datatype == "AssetType":
        if asset is None:
            folder = get_folder_from_asset_type(entity)
        else:
            folder = get_folder_from_asset_type(asset)
    elif datatype == "Sequence":
        folder = get_folder_from_sequence(entity)
    elif datatype == "Episode":
        folder = get_folder_from_episode(entity)
    elif datatype == "Asset":
        if asset is None:
            folder = get_folder_from_asset(entity)
        else:
            folder = get_folder_from_asset(asset)
    elif datatype == "Software":
        folder = get_folder_from_software(software)
    elif datatype == "OutputType":
        folder = get_folder_from_output_type(output_type)
    elif datatype == "Scene":
        folder = get_folder_from_scene(entity)
    elif datatype == "Instance":
        folder = get_folder_from_instance(instance)
    elif datatype == "Name":
        folder = name
    else:
        raise MalformedFileTreeException("Unknown data type: %s." % datatype)

    return folder


def get_folder_from_project(entity):
    project = get_project(entity)
    return project["name"]


def get_folder_from_task(task):
    return task["name"]


def get_folder_from_shot(shot):
    return shot["name"]


def get_folder_from_output_type(output_type):
    if output_type is None:
        output_type = files_service.get_or_create_output_type("Geometry")

    return output_type["name"].lower()


def get_folder_from_department(task):
    folder = ""
    department = tasks_service.get_department_from_task(task["id"])
    folder = department["name"]
    return folder


def get_folder_from_task_type(task):
    folder = ""
    task_type = tasks_service.get_task_type(task["task_type_id"])
    if task_type is not None:
        folder = task_type["name"]
    return folder


def get_folder_from_asset(asset):
    folder = ""
    if asset is not None:
        folder = asset["name"]
    return folder


def get_folder_from_sequence(entity):
    if shots_service.is_shot(entity) or shots_service.is_scene(entity):
        sequence = shots_service.get_sequence_from_shot(entity)
        sequence_name = sequence["name"]
    elif shots_service.is_sequence(entity):
        sequence_name = entity["name"]
    else:
        sequence_name = ""

    if "Seq" in sequence_name:
        sequence_number = sequence.name[3:]
        sequence_name = "S%s" % sequence_number.zfill(3)
    return sequence_name


def get_folder_from_episode(entity):
    if shots_service.is_shot(entity) or shots_service.is_scene(entity):
        sequence = shots_service.get_sequence_from_shot(entity)
    elif shots_service.is_sequence(entity):
        sequence = entity

    try:
        episode = shots_service.get_episode_from_sequence(sequence)
        episode_name = episode["name"]
    except:
        episode_name = "e001"

    return episode_name


def get_folder_from_temporal_entity(entity):
    if entity is not None:
        entity_type = entities_service.get_entity(entity["id"])
        folder = entity_type["name"]
    else:
        raise MalformedFileTreeException("Given temporal entity is null.")
    return folder


def get_folder_from_temporal_entity_type(entity):
    if entity is not None:
        entity_type = entities_service.get_entity_type_by_id(
            entity["entity_type_id"]
        )
        folder = entity_type["name"].lower() + "s"
    else:
        raise MalformedFileTreeException("Given temporal entity type is null.")
    return folder


def get_folder_from_asset_type(asset):
    if asset is not None:
        asset_type = assets_service.get_asset_type(asset["entity_type_id"])
        folder = asset_type["name"]
    else:
        raise MalformedFileTreeException("Given asset is null.")
    return folder


def get_folder_from_software(software):
    if software is None:
        software = files_service.get_or_create_software("3dsmax", "max", ".max")
    return software["name"]


def get_folder_from_scene(scene):
    folder = ""
    if scene is not None:
        folder = scene["name"]
    return folder


def get_folder_from_instance(instance):
    if instance is not None:<commit_msg>File template: do not add s at to temporal type<commit_after>import os
import re
import json

from slugify import slugify

from zou.app import app

from zou.app.models.entity import Entity
from zou.app.models.entity_type import EntityType
from zou.app.models.task_type import TaskType
from zou.app.models.task import Task
from zou.app.models.department import Department

from zou.app.services import (
    assets_service,
    entities_service,
    files_service,
    shots_service,
    projects_service,
    tasks_service
)
from zou.app.services.exception import (
    MalformedFileTreeException,
    WrongFileTreeFileException,
    WrongPathFormatException,
    TaskNotFoundException
)


def get_file_path(
    task,
    mode="working",
    software=None,
    output_type=None,
    name="",
    version=1,
    sep=os.sep
):
    file_name = get_file_name(
        task,
        mode=mode,
        software=software,
        output_type=output_type,
        name=name,
        version=version,
    )
    folder = get_folder_path(
        task,
        mode,
        software=software,
        output_type=output_type,
        name=name,
        sep=sep
    )

    return join_path(folder, file_name, sep)


def get_file_name(
    task,
    mode="working",
    software=None,
    output_type=None,
    name="",
    version=1
):
    entity = tasks_service.get_entity(task["entity_id"])
    project = get_project(entity)
    tree = get_tree_from_project(project)

    file_name = get_file_name_root(
        tree,
        mode,
        entity,
        task,
        software,
        output_type,
        name
    )
    file_name = add_version_suffix_to_file_name(file_name, version)

    return u"%s" % file_name


def get_instance_file_name(
    asset_instance,
    output_type,
    mode="output",
    name="",
    version=1
):
    shot = tasks_service.get_entity(asset_instance["entity_id"])
    asset = tasks_service.get_entity(asset_instance["asset_id"])
    project = get_project(shot)
    tree = get_tree_from_project(project)

    file_name = get_file_name_root(
        tree,
        mode,
        shot,
        None,
        None,
        output_type,
        name,
        asset_instance=asset_instance,
        asset=asset
    )
    file_name = add_version_suffix_to_file_name(file_name, version)

    return u"%s" % file_name


def get_folder_path(
    task,
    mode="working",
    software=None,
    output_type=None,
    name="",
    sep=os.sep
):
    entity = tasks_service.get_entity(task["entity_id"])
    project = get_project(entity)
    tree = get_tree_from_project(project)
    root_path = get_root_path(tree, mode, sep)
    style = tree[mode]["folder_path"].get("style", "")

    folder_template = get_folder_path_template(tree, mode, entity)
    folder_path = update_variable(
        folder_template,
        entity,
        task,
        software,
        output_type,
        name,
        style
    )
    folder_path = change_folder_path_separators(folder_path, sep)

    return join_path(root_path, folder_path, "")


def get_instance_folder_path(
    asset_instance,
    output_type,
    mode="output",
    sep=os.sep,
):
    shot = tasks_service.get_entity(asset_instance["entity_id"])
    asset = tasks_service.get_entity(asset_instance["asset_id"])
    project = get_project(shot)
    tree = get_tree_from_project(project)
    root_path = get_root_path(tree, mode, sep)
    style = tree[mode]["folder_path"].get("style", "")

    folder_template = get_folder_path_template(
        tree,
        mode,
        asset_instance
    )

    folder_path = update_variable(
        folder_template,
        shot,
        None,
        None,
        output_type,
        "",
        style,
        asset_instance=asset_instance,
        asset=asset
    )
    folder_path = change_folder_path_separators(folder_path, sep)

    return join_path(root_path, folder_path, "")


def get_project(entity):
    return projects_service.get_project(entity["project_id"])


def get_tree_from_project(project):
    return project["file_tree"]


def get_tree_from_file(tree_name):
    try:
        tree_path = os.path.join(
            app.config["FILE_TREE_FOLDER"], "%s.json" % tree_name)
        tree_string = open(tree_path).read()
    except IOError:
        raise WrongFileTreeFileException(
            "File Tree file not found: %s." % tree_path
        )
    return json.loads(tree_string)


def get_folder_path_template(tree, mode, entity):
    try:
        if entity["type"] == "AssetInstance":
            return tree[mode]["folder_path"]["instance"]
        elif shots_service.is_shot(entity):
            return tree[mode]["folder_path"]["shot"]
        elif shots_service.is_sequence(entity):
            return tree[mode]["folder_path"]["sequence"]
        elif shots_service.is_scene(entity):
            return tree[mode]["folder_path"]["scene"]
        else:
            return tree[mode]["folder_path"]["asset"]
    except KeyError:
        raise MalformedFileTreeException


def get_file_name_template(tree, mode, entity):
    try:
        if entity["type"] == "AssetInstance":
            return tree[mode]["file_name"]["instance"]
        elif shots_service.is_shot(entity):
            return tree[mode]["file_name"]["shot"]
        elif shots_service.is_sequence(entity):
            return tree[mode]["file_name"]["sequence"]
        elif shots_service.is_scene(entity):
            return tree[mode]["file_name"]["scene"]
        else:
            return tree[mode]["file_name"]["asset"]
    except KeyError:
        raise MalformedFileTreeException


def get_file_name_root(
    tree,
    mode,
    entity,
    task,
    software,
    output_type,
    name,
    asset_instance=None,
    asset=None
):
    if asset_instance is None:
        file_name_template = get_file_name_template(tree, mode, entity)
    else:
        file_name_template = get_file_name_template(tree, mode, asset_instance)

    file_name = update_variable(
        file_name_template,
        entity,
        task,
        software,
        output_type,
        name,
        asset_instance=asset_instance,
        asset=asset
    )
    file_name = slugify(file_name, separator="_")
    file_name = apply_style(file_name, tree[mode]["file_name"].get("style", ""))
    return file_name


def add_version_suffix_to_file_name(file_name, version=1):
    file_name = "%s_v%s" % (file_name, str(version).zfill(3))
    return file_name


def change_folder_path_separators(folder_path, sep):
    return folder_path.replace("/", sep)


def get_root_path(tree, mode, sep):
    try:
        mountpoint = tree[mode]["mountpoint"]
        root = tree[mode]["root"]
    except KeyError:
        raise MalformedFileTreeException(
            "Can't find given mode (%s) in given tree." % mode
        )
    return "%s%s%s%s" % (mountpoint, sep, root, sep)


def update_variable(
    template,
    entity,
    task,
    software=None,
    output_type=None,
    name="",
    style="lowercase",
    asset_instance=None,
    asset=None
):
    variables = re.findall('<(\w*)>', template)

    render = template
    for variable in variables:
        data = get_folder_from_datatype(
            variable,
            entity,
            task,
            software,
            output_type,
            name,
            asset_instance,
            asset
        )
        render = render.replace(
            "<%s>" % variable,
            apply_style(slugify(data, separator="_"), style)
        )

    return render


def get_folder_from_datatype(
    datatype,
    entity,
    task,
    software=None,
    output_type=None,
    name="",
    instance=None,
    asset=None
):
    if datatype == "Project":
        folder = get_folder_from_project(entity)
    elif datatype == "Task":
        folder = get_folder_from_task(task)
    elif datatype == "TaskType":
        folder = get_folder_from_task_type(task)
    elif datatype == "Department":
        folder = get_folder_from_department(task)
    elif datatype == "Shot":
        folder = get_folder_from_shot(entity)
    elif datatype == "TemporalEntity":
        folder = get_folder_from_temporal_entity(entity)
    elif datatype == "TemporalEntityType":
        folder = get_folder_from_temporal_entity_type(entity)
    elif datatype == "AssetType":
        if asset is None:
            folder = get_folder_from_asset_type(entity)
        else:
            folder = get_folder_from_asset_type(asset)
    elif datatype == "Sequence":
        folder = get_folder_from_sequence(entity)
    elif datatype == "Episode":
        folder = get_folder_from_episode(entity)
    elif datatype == "Asset":
        if asset is None:
            folder = get_folder_from_asset(entity)
        else:
            folder = get_folder_from_asset(asset)
    elif datatype == "Software":
        folder = get_folder_from_software(software)
    elif datatype == "OutputType":
        folder = get_folder_from_output_type(output_type)
    elif datatype == "Scene":
        folder = get_folder_from_scene(entity)
    elif datatype == "Instance":
        folder = get_folder_from_instance(instance)
    elif datatype == "Name":
        folder = name
    else:
        raise MalformedFileTreeException("Unknown data type: %s." % datatype)

    return folder


def get_folder_from_project(entity):
    project = get_project(entity)
    return project["name"]


def get_folder_from_task(task):
    return task["name"]


def get_folder_from_shot(shot):
    return shot["name"]


def get_folder_from_output_type(output_type):
    if output_type is None:
        output_type = files_service.get_or_create_output_type("Geometry")

    return output_type["name"].lower()


def get_folder_from_department(task):
    folder = ""
    department = tasks_service.get_department_from_task(task["id"])
    folder = department["name"]
    return folder


def get_folder_from_task_type(task):
    folder = ""
    task_type = tasks_service.get_task_type(task["task_type_id"])
    if task_type is not None:
        folder = task_type["name"]
    return folder


def get_folder_from_asset(asset):
    folder = ""
    if asset is not None:
        folder = asset["name"]
    return folder


def get_folder_from_sequence(entity):
    if shots_service.is_shot(entity) or shots_service.is_scene(entity):
        sequence = shots_service.get_sequence_from_shot(entity)
        sequence_name = sequence["name"]
    elif shots_service.is_sequence(entity):
        sequence_name = entity["name"]
    else:
        sequence_name = ""

    if "Seq" in sequence_name:
        sequence_number = sequence.name[3:]
        sequence_name = "S%s" % sequence_number.zfill(3)
    return sequence_name


def get_folder_from_episode(entity):
    if shots_service.is_shot(entity) or shots_service.is_scene(entity):
        sequence = shots_service.get_sequence_from_shot(entity)
    elif shots_service.is_sequence(entity):
        sequence = entity

    try:
        episode = shots_service.get_episode_from_sequence(sequence)
        episode_name = episode["name"]
    except:
        episode_name = "e001"

    return episode_name


def get_folder_from_temporal_entity(entity):
    if entity is not None:
        entity_type = entities_service.get_entity(entity["id"])
        folder = entity_type["name"]
    else:
        raise MalformedFileTreeException("Given temporal entity is null.")
    return folder


def get_folder_from_temporal_entity_type(entity):
    if entity is not None:
        entity_type = entities_service.get_entity_type_by_id(
            entity["entity_type_id"]
        )
        folder = entity_type["name"].lower()
    else:
        raise MalformedFileTreeException("Given temporal entity type is null.")
    return folder


def get_folder_from_asset_type(asset):
    if asset is not None:
        asset_type = assets_service.get_asset_type(asset["entity_type_id"])
        folder = asset_type["name"]
    else:
        raise MalformedFileTreeException("Given asset is null.")
    return folder


def get_folder_from_software(software):
    if software is None:
        software = files_service.get_or_create_software("3dsmax", "max", ".max")
    return software["name"]


def get_folder_from_scene(scene):
    folder = ""
    if scene is not None:
        folder = scene["name"]
    return folder


def get_folder_from_instance(instance):
    if instance is not None:
{'fraction_non_alphanumeric': 0.09795357113969101, 'fraction_numerical': 0.0008897516783952115, 'mean_word_length': 2.8854494028912634, 'pattern_counts': {'":': 36, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '641564', 'n_tokens_mistral': 7675, 'n_tokens_neox': 7556, 'n_words': 1501}