<commit_before>from core import db
from core.responses import success, user_error
from flask import abort, Blueprint, request

blueprint = Blueprint('layer', __name__)

def child_label_of(lhs, rhs):
    """Is the lhs label a child of the rhs label"""
    if lhs.startswith(rhs):
        return True
    #   Interpretations have a slightly different hierarchy
    if 'Interp' in lhs and 'Interp' in rhs:
        lhs_reg, lhs_comment = lhs.split('Interp')
        rhs_reg, rhs_comment = rhs.split('Interp')
        if lhs_reg.startswith(rhs_reg) or (
            lhs_reg == rhs_reg and lhs_comment.startswith(rhs_comment)):
            return True
    
    return False


@blueprint.route('/layer/<name>/<label>/<version>', methods=['PUT'])
def add(name, label, version):
    """Add the layer node and all of its children to the db"""
    layer = request.json

    if not isinstance(layer, dict):
        return user_error('invalid format')

    for key in layer.keys():
        # terms layer has a special attribute
        if not child_label_of(key, label) and key != 'referenced':
            return user_error('label mismatch: %s, %s' % (label, key))

    to_save = []
    #   Make sure to add *a* layer for all children
    for node_key in child_keys(label, version):
        sublayer = {}
        if 'referenced' in layer:   #   @todo: be more granular
            sublayer['referenced'] = layer['referenced']
        #   Determine which keys of the parent the child layer needs
        for layer_key in layer.keys():
            if child_label_of(layer_key, node_key):
                sublayer[layer_key] = layer[layer_key]

        to_save.append({<commit_msg>Put interp check first; remove duplicate check<commit_after>from core import db
from core.responses import success, user_error
from flask import abort, Blueprint, request

blueprint = Blueprint('layer', __name__)

def child_label_of(lhs, rhs):
    """Is the lhs label a child of the rhs label"""
    #   Interpretations have a slightly different hierarchy
    if 'Interp' in lhs and 'Interp' in rhs:
        lhs_reg, lhs_comment = lhs.split('Interp')
        rhs_reg, rhs_comment = rhs.split('Interp')
        if lhs_reg.startswith(rhs_reg):
            return True

    #   Handle Interps with shared prefix as well as non-interps
    if lhs.startswith(rhs):
        return True
    
    return False


@blueprint.route('/layer/<name>/<label>/<version>', methods=['PUT'])
def add(name, label, version):
    """Add the layer node and all of its children to the db"""
    layer = request.json

    if not isinstance(layer, dict):
        return user_error('invalid format')

    for key in layer.keys():
        # terms layer has a special attribute
        if not child_label_of(key, label) and key != 'referenced':
            return user_error('label mismatch: %s, %s' % (label, key))

    to_save = []
    #   Make sure to add *a* layer for all children
    for node_key in child_keys(label, version):
        sublayer = {}
        if 'referenced' in layer:   #   @todo: be more granular
            sublayer['referenced'] = layer['referenced']
        #   Determine which keys of the parent the child layer needs
        for layer_key in layer.keys():
            if child_label_of(layer_key, node_key):
                sublayer[layer_key] = layer[layer_key]

        to_save.append({
{'fraction_non_alphanumeric': 0.10077519379844961, 'fraction_numerical': 0.0, 'mean_word_length': 2.799546998867497, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '7353709', 'n_tokens_mistral': 1005, 'n_tokens_neox': 998, 'n_words': 335}