<commit_before>from collections import OrderedDict
from datetime import datetime, timezone
import json

from .load_data_from_file import load_data_from_file
from .get_old_dict_values import get_old_dict_values
from .log import log
from .paths import make_course_path


def load_previous(course_path):
    try:
        with open(course_path, 'r', encoding='utf-8') as infile:
            return json.load(infile)
    except FileNotFoundError:
        return None
    except json.JSONDecodeError:
        log("Error decoding old jsonified course %s", course_path)


def sort_diff(diff, ignore_revisions):
    ordered_diff = OrderedDict()
    for key in sorted(diff.keys()):
        if key not in ignore_revisions:
            ordered_diff[key] = diff[key]
    return ordered_diff


def check_for_revisions(course, ignore_revisions):
    prior = load_previous(make_course_path(course['clbid']))

    if not prior:
        return None

    if 'revisions' in prior:
        revisions = prior['revisions']
        del prior['revisions']
    else:
        return None

    if prior == course:
        return course.get('revisions', None)

    diff = get_old_dict_values(prior, course)
    if not diff:
        return None

    ordered_diff = sort_diff(diff, ignore_revisions=ignore_revisions)

    ordered_diff['_updated'] = datetime.now(timezone.utc)
    revisions.append(ordered_diff)

    return revisions<commit_msg>fix an early-return bug in the revision checker<commit_after>from collections import OrderedDict
from datetime import datetime, timezone
import json

from .load_data_from_file import load_data_from_file
from .get_old_dict_values import get_old_dict_values
from .log import log
from .paths import make_course_path


def load_previous(course_path):
    try:
        with open(course_path, 'r', encoding='utf-8') as infile:
            return json.load(infile)
    except FileNotFoundError:
        return None
    except json.JSONDecodeError:
        log("Error decoding old jsonified course %s", course_path)


def sort_diff(diff, ignore_revisions):
    ordered_diff = OrderedDict()
    for key in sorted(diff.keys()):
        if key not in ignore_revisions:
            ordered_diff[key] = diff[key]
    return ordered_diff


def check_for_revisions(course, ignore_revisions):
    prior = load_previous(make_course_path(course['clbid']))

    if not prior:
        return None

    if 'revisions' in prior:
        revisions = prior['revisions']
        del prior['revisions']
    else:
        revisions = []

    if prior == course:
        return course.get('revisions', None)

    diff = get_old_dict_values(prior, course)
    if not diff:
        return None

    ordered_diff = sort_diff(diff, ignore_revisions=ignore_revisions)

    ordered_diff['_updated'] = datetime.now(timezone.utc)
    revisions.append(ordered_diff)

    return revisions
{'fraction_non_alphanumeric': 0.0796367446734195, 'fraction_numerical': 0.0006985679357317499, 'mean_word_length': 3.4472049689440993, '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': '501971', 'n_tokens_mistral': 890, 'n_tokens_neox': 836, 'n_words': 229}