<commit_before>from __future__ import unicode_literals
def process_media_attribute(attribute, resp, val):
    if val:
        val = interpolate_media_path(val)
        resp['corrections'][attribute] = val
    else:
        val = None
    return val


def interpolate_media_path(val):
    if not val:
        return val

    if val.startswith('jr://'):
        pass
    elif val.startswith('/file/'):
        val = 'jr:/' + val
    elif val.startswith('file/'):
        val = 'jr://' + val
    elif val.startswith('/'):
        val = 'jr://file' + val
    else:
        val = 'jr://file/' + val

    return val


def handle_media_edits(request, item, should_edit, resp, lang, app):
    if 'corrections' not in resp:
        resp['corrections'] = {}
    for attribute in ('media_image', 'media_audio'):
        if should_edit(attribute):
            old_value = getattr(item, attribute, {})[lang]
            media_path = process_media_attribute(attribute, resp, request.POST.get(attribute))
            item._set_media(attribute, lang, media_path)
            # remove the entry from app multimedia mappings if media is being removed now
            # This does not remove the multimedia but just it's reference in mapping
            if old_value and not media_path:
                app.multimedia_map.pop(old_value, None)<commit_msg>fix possible error in case of missing lang key<commit_after>from __future__ import unicode_literals
def process_media_attribute(attribute, resp, val):
    if val:
        val = interpolate_media_path(val)
        resp['corrections'][attribute] = val
    else:
        val = None
    return val


def interpolate_media_path(val):
    if not val:
        return val

    if val.startswith('jr://'):
        pass
    elif val.startswith('/file/'):
        val = 'jr:/' + val
    elif val.startswith('file/'):
        val = 'jr://' + val
    elif val.startswith('/'):
        val = 'jr://file' + val
    else:
        val = 'jr://file/' + val

    return val


def handle_media_edits(request, item, should_edit, resp, lang, app):
    if 'corrections' not in resp:
        resp['corrections'] = {}
    for attribute in ('media_image', 'media_audio'):
        if should_edit(attribute):
            old_value = getattr(item, attribute, {}).get(lang, None)
            media_path = process_media_attribute(attribute, resp, request.POST.get(attribute))
            item._set_media(attribute, lang, media_path)
            # remove the entry from app multimedia mappings if media is being removed now
            # This does not remove the multimedia but just it's reference in mapping
            if old_value and not media_path:
                app.multimedia_map.pop(old_value, None)
{'fraction_non_alphanumeric': 0.1084470675027665, 'fraction_numerical': 0.0, 'mean_word_length': 2.4993548387096776, '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': '5991167', 'n_tokens_mistral': 813, 'n_tokens_neox': 784, 'n_words': 234}