# Path: Lib/glyphsLib/builder/constants.py
# GLYPHLIB_PREFIX = GLYPHS_PREFIX + "Glyphs."
# 
# Path: Lib/glyphsLib/builder/glyph.py
# SCRIPT_LIB_KEY = GLYPHLIB_PREFIX + "script"
# ORIGINAL_WIDTH_KEY = GLYPHLIB_PREFIX + "originalWidth"
# BACKGROUND_WIDTH_KEY = GLYPHLIB_PREFIX + "backgroundWidth"
# def _clone_layer(layer, paths=None, components=None):
# def to_ufo_glyph(self, ufo_glyph, layer, glyph, do_color_layers=True):  # noqa: C901
# def to_glyphs_glyph(self, ufo_glyph, ufo_layer, master):  # noqa: C901
# def to_ufo_glyph_height_and_vertical_origin(self, ufo_glyph, layer):
# def _get_typo_ascender_descender(master):
# def to_ufo_glyph_background(self, glyph, layer):
# def _to_glyphs_color(color):
# def to_glyphs_glyph_height_and_vertical_origin(self, ufo_glyph, master, layer):


from collections import defaultdict
from glyphsLib import classes
from .constants import GLYPHLIB_PREFIX
from .glyph import BRACKET_GLYPH_RE
import os
import re

def to_ufo_groups(self):
    # Build groups once and then apply to all UFOs.
    groups = defaultdict(list)

    # Classes usually go to the feature file, unless we have our custom flag
    group_names = None
    if UFO_GROUPS_NOT_IN_FEATURE_KEY in self.font.userData.keys():
        group_names = set(self.font.userData[UFO_GROUPS_NOT_IN_FEATURE_KEY])
    if group_names:
        for gsclass in self.font.classes.values():
            if gsclass.name in group_names:
                if gsclass.code:
                    groups[gsclass.name] = gsclass.code.split(" ")
                else:
                    # Empty group: using split like above would produce ['']
                    groups[gsclass.name] = []

    # Rebuild kerning groups from `left/rightKerningGroup`s
    # Use the original list of kerning groups as a base, to recover
    #  - the original ordering
    #  - the kerning groups of glyphs that were not in the font (which can be
    #    stored in a UFO but not by Glyphs)
    recovered = set()
    orig_groups = self.font.userData.get(UFO_ORIGINAL_KERNING_GROUPS_KEY)
    if orig_groups:
        for group, glyphs in orig_groups.items():
            if not glyphs:
                # Restore empty group
                groups[group] = []

# Path: Lib/glyphsLib/builder/constants.py
# GLYPHLIB_PREFIX = GLYPHS_PREFIX + "Glyphs."
# 
# Path: Lib/glyphsLib/builder/glyph.py
# SCRIPT_LIB_KEY = GLYPHLIB_PREFIX + "script"
# ORIGINAL_WIDTH_KEY = GLYPHLIB_PREFIX + "originalWidth"
# BACKGROUND_WIDTH_KEY = GLYPHLIB_PREFIX + "backgroundWidth"
# def _clone_layer(layer, paths=None, components=None):
# def to_ufo_glyph(self, ufo_glyph, layer, glyph, do_color_layers=True):  # noqa: C901
# def to_glyphs_glyph(self, ufo_glyph, ufo_layer, master):  # noqa: C901
# def to_ufo_glyph_height_and_vertical_origin(self, ufo_glyph, layer):
# def _get_typo_ascender_descender(master):
# def to_ufo_glyph_background(self, glyph, layer):
# def _to_glyphs_color(color):
# def to_glyphs_glyph_height_and_vertical_origin(self, ufo_glyph, master, layer):


# Path: Lib/glyphsLib/builder/groups.py
from collections import defaultdict
from glyphsLib import classes
from .constants import GLYPHLIB_PREFIX
from .glyph import BRACKET_GLYPH_RE
import os
import re


def to_ufo_groups(self):
    # Build groups once and then apply to all UFOs.
    groups = defaultdict(list)

    # Classes usually go to the feature file, unless we have our custom flag
    group_names = None
    if UFO_GROUPS_NOT_IN_FEATURE_KEY in self.font.userData.keys():
        group_names = set(self.font.userData[UFO_GROUPS_NOT_IN_FEATURE_KEY])
    if group_names:
        for gsclass in self.font.classes.values():
            if gsclass.name in group_names:
                if gsclass.code:
                    groups[gsclass.name] = gsclass.code.split(" ")
                else:
                    # Empty group: using split like above would produce ['']
                    groups[gsclass.name] = []

    # Rebuild kerning groups from `left/rightKerningGroup`s
    # Use the original list of kerning groups as a base, to recover
    #  - the original ordering
    #  - the kerning groups of glyphs that were not in the font (which can be
    #    stored in a UFO but not by Glyphs)
    recovered = set()
    orig_groups = self.font.userData.get(UFO_ORIGINAL_KERNING_GROUPS_KEY)
    if orig_groups:
        for group, glyphs in orig_groups.items():
            if not glyphs:
                # Restore empty group
                groups[group] = []

            for glyph_name in glyphs: