<commit_before>import io
import json
import logging
import os
import re

import six

from jirafs import constants, utils
from jirafs.plugin import MacroPlugin, PluginValidationError
from jirafs.readers import GitRevisionReader, WorkingCopyReader


class JiraFieldManager(dict):
    FIELD_MATCHER = re.compile(
        '^%s$' % (
            constants.TICKET_FILE_FIELD_TEMPLATE.replace(
                '{field_name}', '([\w_]+)'
            )
        )
    )

    def __init__(self, data=None, names=None):
        if names is None:
            self._data, self._names = self.get_fields_from_string(data)
        else:
            self._data = data
            self._names = names
        super(JiraFieldManager, self).__init__(self._data)

    def __sub__(self, other):
        differing = {}
        for k, v in other.items_transformed():
            if self.get_transformed(k) != v:
                tx = self.get_transformed(k)
                differing[k] = (v, tx, self[k], )

        return differing

    def get_human_name_for_field(self, field):
        try:
            return self._names[field]
        except KeyError:
            return field

    def get_macro_plugins(self):
        if not hasattr(self, '_macro_plugins'):
            config = self.folder.get_config()
            plugins = []

            if not config.has_section(constants.CONFIG_PLUGINS):
                return plugins

            installed_plugins = utils.get_installed_plugins(MacroPlugin)

            for name, status in config.items(constants.CONFIG_PLUGINS):
                if not utils.convert_to_boolean(status):
                    # This plugin is not turned on.
                    continue
                if name not in installed_plugins:
                    # This plugin is not installed.
                    self.folder.log(
                        "Macro plugin '%s' is not available; "
                        "this is probably because this plugin is not a "
                        "macro.",
                        (name, ),
                        level=logging.DEBUG
                    )
                    continue

                plugin = installed_plugins[name](self.folder, name)

                try:
                    plugin.validate()
                except PluginValidationError as e:
                    self.folder.log(
                        "Plugin '%s' did not pass validation; "
                        "not loading: %s.",
                        (name, e,),
                    )

                plugins.append(plugin)

            self._macro_plugins = plugins

        return self._macro_plugins

    def _process_field_macros(self, data):
        macro_plugins = self.get_macro_plugins()

        for cls in macro_plugins:
            if isinstance(data, six.string_types):
                data = cls.process_text_data(data)
            else:
                continue

        return data

    def items_transformed(self):
        for k, v in self.items():
            result = self._process_field_macros(v)
            yield k, result

    def get_transformed(self, field_name, default=None):
        try:
            return self._process_field_macros(self[field_name])
        except KeyError:
            return default

    @classmethod
    def create(cls, folder, revision=None, path=None):
        if revision and path:
            raise TypeError(
                'You may specify a git revision or a local path; not both.'
            )

        if revision:
            return GitRevisionJiraFieldManager(folder, revision)
        else:
            return WorkingCopyJiraFieldManager(folder, path)

    def get_requested_per_ticket_fields(self):
        return constants.FILE_FIELDS

    def get_used_per_ticket_fields(self):
        raise NotImplementedError()

    def set_data_value(self, data, field_name, raw_value):<commit_msg>Only allow field macros to run in file-based fields; not in the jirafieldmanager.<commit_after>import io
import json
import logging
import os
import re

import six

from jirafs import constants, utils
from jirafs.plugin import MacroPlugin, PluginValidationError
from jirafs.readers import GitRevisionReader, WorkingCopyReader


class JiraFieldManager(dict):
    FIELD_MATCHER = re.compile(
        '^%s$' % (
            constants.TICKET_FILE_FIELD_TEMPLATE.replace(
                '{field_name}', '([\w_]+)'
            )
        )
    )

    def __init__(self, data=None, names=None):
        if names is None:
            self._data, self._names = self.get_fields_from_string(data)
        else:
            self._data = data
            self._names = names
        super(JiraFieldManager, self).__init__(self._data)

    def __sub__(self, other):
        differing = {}
        for k, v in other.items_transformed():
            if self.get_transformed(k) != v:
                tx = self.get_transformed(k)
                differing[k] = (v, tx, self[k], )

        return differing

    def get_human_name_for_field(self, field):
        try:
            return self._names[field]
        except KeyError:
            return field

    def get_macro_plugins(self):
        if not hasattr(self, '_macro_plugins'):
            config = self.folder.get_config()
            plugins = []

            if not config.has_section(constants.CONFIG_PLUGINS):
                return plugins

            installed_plugins = utils.get_installed_plugins(MacroPlugin)

            for name, status in config.items(constants.CONFIG_PLUGINS):
                if not utils.convert_to_boolean(status):
                    # This plugin is not turned on.
                    continue
                if name not in installed_plugins:
                    # This plugin is not installed.
                    self.folder.log(
                        "Macro plugin '%s' is not available; "
                        "this is probably because this plugin is not a "
                        "macro.",
                        (name, ),
                        level=logging.DEBUG
                    )
                    continue

                plugin = installed_plugins[name](self.folder, name)

                try:
                    plugin.validate()
                except PluginValidationError as e:
                    self.folder.log(
                        "Plugin '%s' did not pass validation; "
                        "not loading: %s.",
                        (name, e,),
                    )

                plugins.append(plugin)

            self._macro_plugins = plugins

        return self._macro_plugins

    def _process_field_macros(self, data):
        macro_plugins = self.get_macro_plugins()

        for cls in macro_plugins:
            if isinstance(data, six.string_types):
                data = cls.process_text_data(data)
            else:
                continue

        return data

    def items_transformed(self):
        for k, v in self.items():
            if k in self.get_requested_per_ticket_fields():
                result = self._process_field_macros(v)
                yield k, result
            else:
                yield k, v

    def get_transformed(self, field_name, default=None):
        if field_name not in self.get_requested_per_ticket_fields():
            return self[field_name]

        try:
            return self._process_field_macros(self[field_name])
        except KeyError:
            return default

    @classmethod
    def create(cls, folder, revision=None, path=None):
        if revision and path:
            raise TypeError(
                'You may specify a git revision or a local path; not both.'
            )

        if revision:
            return GitRevisionJiraFieldManager(folder, revision)
        else:
            return WorkingCopyJiraFieldManager(folder, path)

    def get_requested_per_ticket_fields(self):
        return constants.FILE_FIELDS

    def get_used_per_ticket_fields(self):
        raise NotImplementedError()

    def set_data_value(self, data, field_name, raw_value):
{'fraction_non_alphanumeric': 0.07495338719701677, 'fraction_numerical': 0.0, 'mean_word_length': 1.6441012159053565, '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': '1044766', 'n_tokens_mistral': 2185, 'n_tokens_neox': 2051, 'n_words': 540}