<commit_before>"""

"""

import os
import glob
import importlib
import mimetypes
from bson import ObjectId
from flask import request
from modularodm import fields
from mako.lookup import TemplateLookup

import furl
import requests

from framework.exceptions import PermissionsError
from framework.mongo import StoredObject
from framework.routing import process_rules
from framework.guid.model import GuidStoredObject

from website import settings
from website.addons.base import exceptions
from website.addons.base import serializer
from website.project.model import Node

lookup = TemplateLookup(
    directories=[
        settings.TEMPLATES_PATH
    ]
)

STATUS_EXCEPTIONS = {
    410: exceptions.FileDeletedError,
    404: exceptions.FileDoesntExistError
}


def _is_image(filename):
    mtype, _ = mimetypes.guess_type(filename)
    return mtype and mtype.startswith('image')


class AddonConfig(object):

    def __init__(self, short_name, full_name, owners, categories,
                 added_default=None, added_mandatory=None,
                 node_settings_model=None, user_settings_model=None, include_js=None, include_css=None,
                 widget_help=None, views=None, configs=None, models=None,
                 has_hgrid_files=False, get_hgrid_data=None, max_file_size=None, high_max_file_size=None,
                 accept_extensions=True,
                 node_settings_template=None, user_settings_template=None,
                 **kwargs):

        self.models = models
        self.settings_models = {}

        if node_settings_model:
            node_settings_model.config = self
            self.settings_models['node'] = node_settings_model

        if user_settings_model:
            user_settings_model.config = self
            self.settings_models['user'] = user_settings_model

        self.short_name = short_name
        self.full_name = full_name
        self.owners = owners
        self.categories = categories

        self.added_default = added_default or []
        self.added_mandatory = added_mandatory or []
        if set(self.added_mandatory).difference(self.added_default):
            raise ValueError('All mandatory targets must also be defaults.')

        self.include_js = self._include_to_static(include_js or {})
        self.include_css = self._include_to_static(include_css or {})

        self.widget_help = widget_help

        self.views = views or []
        self.configs = configs or []

        self.has_hgrid_files = has_hgrid_files
        # WARNING: get_hgrid_data can return None if the addon is added but has no credentials.
        self.get_hgrid_data = get_hgrid_data  # if has_hgrid_files and not get_hgrid_data rubeus.make_dummy()
        self.max_file_size = max_file_size
        self.high_max_file_size = high_max_file_size
        self.accept_extensions = accept_extensions

        # Build template lookup
        template_path = os.path.join('website', 'addons', short_name, 'templates')
        if os.path.exists(template_path):
            self.template_lookup = TemplateLookup(
                directories=[
                    template_path,
                    settings.TEMPLATES_PATH,
                ]
            )
        else:
            self.template_lookup = None

        # Provide the path the the user_settings template
        self.user_settings_template = user_settings_template
        addon_user_settings_path = os.path.join(
            template_path,
            '{}_user_settings.mako'.format(self.short_name)
        )

        if user_settings_template:
            # If USER_SETTINGS_TEMPLATE is defined, use that path.
            self.user_settings_template = user_settings_template
        elif os.path.exists(addon_user_settings_path):
            # An implicit template exists
            self.user_settings_template = os.path.join(
                os.path.pardir,
                'addons',
                self.short_name,
                'templates',
                '{}_user_settings.mako'.format(self.short_name),
            )
        else:
            # Use the default template (for OAuth addons)
            self.user_settings_template = os.path.join(
                'project',
                'addon',
                'user_settings_default.mako',
            )

        self.node_settings_template = node_settings_template

    def _static_url(self, filename):
        """Build static URL for file; use the current addon if relative path,
        else the global static directory.

        :param str filename: Local path to file
        :return str: Static URL for file

        """
        if filename.startswith('/'):
            return filename
        return '/static/addons/{addon}/{filename}'.format(
            addon=self.short_name,
            filename=filename,
        )

    def _include_to_static(self, include):
        """

        """
        # TODO: minify static assets
        return {
            key: [
                self._static_url(item)<commit_msg>Allow for default node_settings template in addon init<commit_after>"""

"""

import os
import glob
import importlib
import mimetypes
from bson import ObjectId
from flask import request
from modularodm import fields
from mako.lookup import TemplateLookup

import furl
import requests

from framework.exceptions import PermissionsError
from framework.mongo import StoredObject
from framework.routing import process_rules
from framework.guid.model import GuidStoredObject

from website import settings
from website.addons.base import exceptions
from website.addons.base import serializer
from website.project.model import Node

lookup = TemplateLookup(
    directories=[
        settings.TEMPLATES_PATH
    ]
)

STATUS_EXCEPTIONS = {
    410: exceptions.FileDeletedError,
    404: exceptions.FileDoesntExistError
}


def _is_image(filename):
    mtype, _ = mimetypes.guess_type(filename)
    return mtype and mtype.startswith('image')


class AddonConfig(object):

    def __init__(self, short_name, full_name, owners, categories,
                 added_default=None, added_mandatory=None,
                 node_settings_model=None, user_settings_model=None, include_js=None, include_css=None,
                 widget_help=None, views=None, configs=None, models=None,
                 has_hgrid_files=False, get_hgrid_data=None, max_file_size=None, high_max_file_size=None,
                 accept_extensions=True,
                 node_settings_template=None, user_settings_template=None,
                 **kwargs):

        self.models = models
        self.settings_models = {}

        if node_settings_model:
            node_settings_model.config = self
            self.settings_models['node'] = node_settings_model

        if user_settings_model:
            user_settings_model.config = self
            self.settings_models['user'] = user_settings_model

        self.short_name = short_name
        self.full_name = full_name
        self.owners = owners
        self.categories = categories

        self.added_default = added_default or []
        self.added_mandatory = added_mandatory or []
        if set(self.added_mandatory).difference(self.added_default):
            raise ValueError('All mandatory targets must also be defaults.')

        self.include_js = self._include_to_static(include_js or {})
        self.include_css = self._include_to_static(include_css or {})

        self.widget_help = widget_help

        self.views = views or []
        self.configs = configs or []

        self.has_hgrid_files = has_hgrid_files
        # WARNING: get_hgrid_data can return None if the addon is added but has no credentials.
        self.get_hgrid_data = get_hgrid_data  # if has_hgrid_files and not get_hgrid_data rubeus.make_dummy()
        self.max_file_size = max_file_size
        self.high_max_file_size = high_max_file_size
        self.accept_extensions = accept_extensions

        # Build template lookup
        template_path = os.path.join('website', 'addons', short_name, 'templates')
        if os.path.exists(template_path):
            self.template_lookup = TemplateLookup(
                directories=[
                    template_path,
                    settings.TEMPLATES_PATH,
                ]
            )
        else:
            self.template_lookup = None

        # Provide the path the the user_settings template
        self.user_settings_template = user_settings_template
        addon_user_settings_path = os.path.join(
            template_path,
            '{}_user_settings.mako'.format(self.short_name)
        )

        if user_settings_template:
            # If USER_SETTINGS_TEMPLATE is defined, use that path.
            self.user_settings_template = user_settings_template
        elif os.path.exists(addon_user_settings_path):
            # An implicit template exists
            self.user_settings_template = os.path.join(
                os.path.pardir,
                'addons',
                self.short_name,
                'templates',
                '{}_user_settings.mako'.format(self.short_name),
            )
        else:
            # Use the default template (for OAuth addons)
            self.user_settings_template = os.path.join(
                'project',
                'addon',
                'user_settings_default.mako',
            )

        # Provide the path the the node_settings template
        self.node_settings_template = node_settings_template
        addon_node_settings_path = os.path.join(
            template_path,
            '{}_node_settings.mako'.format(self.short_name)
        )
        if node_settings_template:
            # If NODE_SETTINGS_TEMPLATE is defined, use that path.
            self.node_settings_template = node_settings_template
        elif os.path.exists(addon_node_settings_path):
            # An implicit template exists
            self.node_settings_template = os.path.join(
                os.path.pardir,
                'addons',
                self.short_name,
                'templates',
                '{}_node_settings.mako'.format(self.short_name),
            )
        else:
            # Use the default template (for OAuth addons)
            self.node_settings_template = os.path.join(
                'project',
                'addon',
                'node_settings_default.mako',
            )

    def _static_url(self, filename):
        """Build static URL for file; use the current addon if relative path,
        else the global static directory.

        :param str filename: Local path to file
        :return str: Static URL for file

        """
        if filename.startswith('/'):
            return filename
        return '/static/addons/{addon}/{filename}'.format(
            addon=self.short_name,
            filename=filename,
        )

    def _include_to_static(self, include):
        """

        """
        # TODO: minify static assets
        return {
            key: [
                self._static_url(item)
{'fraction_non_alphanumeric': 0.07266278646780648, 'fraction_numerical': 0.0010913059294288831, 'mean_word_length': 2.2429961663226186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4368625', 'n_tokens_mistral': 3043, 'n_tokens_neox': 2956, 'n_words': 670}