<commit_before>from __future__ import (
    print_function, division, absolute_import, unicode_literals
)
from fontTools.misc.py23 import unichr

import collections
import re
try:
    import unicodedata2 as unicodedata
except ImportError:
    import unicodedata
from ufo2ft.featureWriters import BaseFeatureWriter


class KernFeatureWriter(BaseFeatureWriter):
    """Generates a kerning feature based on glyph class definitions.

    Uses the kerning rules contained in an UFO's kerning data, as well as glyph
    classes from parsed feature text. Class-based rules are set based on the
    existing rules for their key glyphs.

    Uses class attributes to match glyph class names in feature text as kerning
    classes, which can be overridden.
    """

    features = ["kern"]
    leftFeaClassRe = r"@MMK_L_(.+)"
    rightFeaClassRe = r"@MMK_R_(.+)"
    options = dict(
        ignoreMarks=True,
    )

    def set_context(self, font):
        ctx = super(KernFeatureWriter, self).set_context(font)

        ctx.kerning = dict(font.kerning)
        ctx.groups = dict(font.groups)

        fealines = []
        if font.features.text:
            for line in font.features.text.splitlines():
                comment_start = line.find('#')
                if comment_start >= 0:
                    line = line[:comment_start]
                line = line.strip()
                if line:
                    fealines.append(line)
        ctx.featxt = '\n'.join(fealines)

        ctx.ltrScripts = collections.OrderedDict()
        ctx.rtlScripts = collections.OrderedDict()
        for script, lang in re.findall(
                r'languagesystem\s+([a-z]{4})\s+([A-Z]+|dflt)\s*;',
                ctx.featxt):
            if self._scriptIsRtl(script):
                ctx.rtlScripts.setdefault(script, []).append(lang)
            else:
                ctx.ltrScripts.setdefault(script, []).append(lang)

        # kerning classes found in existing feature text and UFO groups
        ctx.leftFeaClasses = {}
        ctx.rightFeaClasses = {}
        ctx.leftUfoClasses = {}
        ctx.rightUfoClasses = {}

        # kerning rule collections, mapping pairs to values
        ctx.glyphPairKerning = {}
        ctx.leftClassKerning = {}
        ctx.rightClassKerning = {}
        ctx.classPairKerning = {}

    def _write(self):

        self._collectFeaClasses()
        self._collectFeaClassKerning()

        self._cleanupMissingGlyphs()
        self._correctUfoClassNames()
        self._collectUfoKerning()

        self._removeConflictingKerningRules()

        # write the glyph classes
        lines = []
        self._addGlyphClasses(lines)
        lines.append("")
<commit_msg>[kernFeatureWriter] set_context must return updated namespace<commit_after>from __future__ import (
    print_function, division, absolute_import, unicode_literals
)
from fontTools.misc.py23 import unichr

import collections
import re
try:
    import unicodedata2 as unicodedata
except ImportError:
    import unicodedata
from ufo2ft.featureWriters import BaseFeatureWriter


class KernFeatureWriter(BaseFeatureWriter):
    """Generates a kerning feature based on glyph class definitions.

    Uses the kerning rules contained in an UFO's kerning data, as well as glyph
    classes from parsed feature text. Class-based rules are set based on the
    existing rules for their key glyphs.

    Uses class attributes to match glyph class names in feature text as kerning
    classes, which can be overridden.
    """

    features = ["kern"]
    leftFeaClassRe = r"@MMK_L_(.+)"
    rightFeaClassRe = r"@MMK_R_(.+)"
    options = dict(
        ignoreMarks=True,
    )

    def set_context(self, font):
        ctx = super(KernFeatureWriter, self).set_context(font)

        ctx.kerning = dict(font.kerning)
        ctx.groups = dict(font.groups)

        fealines = []
        if font.features.text:
            for line in font.features.text.splitlines():
                comment_start = line.find('#')
                if comment_start >= 0:
                    line = line[:comment_start]
                line = line.strip()
                if line:
                    fealines.append(line)
        ctx.featxt = '\n'.join(fealines)

        ctx.ltrScripts = collections.OrderedDict()
        ctx.rtlScripts = collections.OrderedDict()
        for script, lang in re.findall(
                r'languagesystem\s+([a-z]{4})\s+([A-Z]+|dflt)\s*;',
                ctx.featxt):
            if self._scriptIsRtl(script):
                ctx.rtlScripts.setdefault(script, []).append(lang)
            else:
                ctx.ltrScripts.setdefault(script, []).append(lang)

        # kerning classes found in existing feature text and UFO groups
        ctx.leftFeaClasses = {}
        ctx.rightFeaClasses = {}
        ctx.leftUfoClasses = {}
        ctx.rightUfoClasses = {}

        # kerning rule collections, mapping pairs to values
        ctx.glyphPairKerning = {}
        ctx.leftClassKerning = {}
        ctx.rightClassKerning = {}
        ctx.classPairKerning = {}

        return ctx

    def _write(self):

        self._collectFeaClasses()
        self._collectFeaClassKerning()

        self._cleanupMissingGlyphs()
        self._correctUfoClassNames()
        self._collectUfoKerning()

        self._removeConflictingKerningRules()

        # write the glyph classes
        lines = []
        self._addGlyphClasses(lines)
        lines.append("")

{'fraction_non_alphanumeric': 0.09294636296840558, 'fraction_numerical': 0.002204261572373255, 'mean_word_length': 2.2980012113870383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '125364', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1526, 'n_words': 381}