<commit_before>import os
import logging
from functools import lru_cache
from uuid import UUID
from io import StringIO
from .format import Writer
from django.template import Context, Template, TemplateDoesNotExist
from django.template.loader import get_template
from arches.app.models.models import GraphModel
from arches.app.models.resource import Resource
from arches.app.models.system_settings import settings
from pathlib import Path


logger = logging.getLogger(__name__)


class HtmlWriter(Writer):
    def __init__(self, **kwargs):
        super(HtmlWriter, self).__init__(**kwargs)

    @staticmethod
    def get_graphids_with_export_template():
        """
        get a list of graphids that have html export templates available.
        """
        valid_graphs = []
        existing_graphids = GraphModel.objects.filter(isresource=True).values_list("graphid", flat=True)
        for existing_graphid in existing_graphids:
            if HtmlWriter.has_html_template(existing_graphid):
                valid_graphs.append(str(existing_graphid))

        return valid_graphs

    @staticmethod
    @lru_cache(maxsize=None)
    def has_html_template(graphid=None):
        """
        Check an html template for the graph is available
        """
        result = False
        try:
            temp = get_template(f"html_export/{graphid}.htm")
            result = True
        except TemplateDoesNotExist as e:
            pass
        return result

    def load_html_template(self, graphid=None):
        template = None
        try:
            template = get_template(f"html_export/{graphid}.htm")
        except TemplateDoesNotExist as e:
            pass
        return template

    def fetch_resource_objects_list(self, resourceinstanceids=None, user=None, allowed_graph_ids=None):
        """
        returns a dict containing graph_id keyed lists containing json ready resource objects

        {
            "<graph_id>": [
                {<disambiguated resource object>}
            ],
            "<graph_id>": [
                {<disambiguated resource object>}
            ],
            ...
            ...
        }

        """

        perm = "read_nodegroup"
        resources = Resource.objects.filter(pk__in=resourceinstanceids)
        compact = True
        hide_empty_nodes = False
        resource_lists = {}
        for resource in resources:
            gid = str(resource.graph_id)<commit_msg>handles htm and html template file extensions<commit_after>import os
import logging
from functools import lru_cache
from uuid import UUID
from io import StringIO
from .format import Writer
from django.template import Context, Template, TemplateDoesNotExist
from django.template.loader import get_template
from arches.app.models.models import GraphModel
from arches.app.models.resource import Resource
from arches.app.models.system_settings import settings
from pathlib import Path


logger = logging.getLogger(__name__)


class HtmlWriter(Writer):
    def __init__(self, **kwargs):
        super(HtmlWriter, self).__init__(**kwargs)

    @staticmethod
    def get_graphids_with_export_template():
        """
        get a list of graphids that have html export templates available.
        """
        valid_graphs = []
        existing_graphids = GraphModel.objects.filter(isresource=True).values_list("graphid", flat=True)
        for existing_graphid in existing_graphids:
            if HtmlWriter.has_html_template(existing_graphid):
                valid_graphs.append(str(existing_graphid))

        return valid_graphs

    @staticmethod
    @lru_cache(maxsize=None)
    def has_html_template(graphid=None):
        """
        Check an html template for the graph is available
        """
        result = False
        for extension in ('htm', 'html'):
            try:
                path = os.path.join("html_export", str(graphid))
                get_template(f"{path}.{extension}")
                result = True
            except TemplateDoesNotExist as e:
                pass
        return result

    def load_html_template(self, graphid=None):
        template = None
        for extension in ('htm', 'html'):
            try:
                path = os.path.join("html_export", str(graphid))
                template = get_template(f"{path}.{extension}")
                return template
            except TemplateDoesNotExist as e:
                pass
        return template

    def fetch_resource_objects_list(self, resourceinstanceids=None, user=None, allowed_graph_ids=None):
        """
        returns a dict containing graph_id keyed lists containing json ready resource objects

        {
            "<graph_id>": [
                {<disambiguated resource object>}
            ],
            "<graph_id>": [
                {<disambiguated resource object>}
            ],
            ...
            ...
        }

        """

        perm = "read_nodegroup"
        resources = Resource.objects.filter(pk__in=resourceinstanceids)
        compact = True
        hide_empty_nodes = False
        resource_lists = {}
        for resource in resources:
            gid = str(resource.graph_id)
{'fraction_non_alphanumeric': 0.07963572950978492, 'fraction_numerical': 0.0, 'mean_word_length': 2.315350032113038, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2141654', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1323, 'n_words': 350}