Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: A Better API for Derived Settings #36192

Merged

Conversation

kdmccormick
Copy link
Member

@kdmccormick kdmccormick commented Jan 29, 2025

Description

The Python API for declaring derived settings was confusing to the uninitiated
reader, and also prone to spelling mistakes. This replaces the API with one
that is more readable and more concise, and updates the implementation of
derive_settings to properly derive settings declared using the new API.

BREAKING CHANGE: The derived and derived_collection_entry function are
replaced with the Derived class. We do not expect those functions to have
been used outside of edx-platform, but if they are, this commit will cause them
to loudly ImportError.

Note that there should be NO change in behavior to the derive_settings
function, which we DO know to be used by some external edx-platform plugins.

Before

from openedx.core.lib.derived import derived, derived_collection_entry, derive_settings

# Define a derived default
MY_DERIVED_SETTING = lambda settings: "compute something based on" + settings.ANOTHER_SETTING

# Register that derived default. If this is misspelled, it will silently fail.
derived("MY_DERIVED_SETTING")

# Define a derived default within a collection
MY_DICT_SETTING = {
    "my_list_with_a_derived_value": [
        "value0",
        "value1",
        lambda settings: "compute value2 based on" settings.YET_ANOTHER_SETTING
    ],
   "my_static_list": ["value0", "value1", "value2"],
}

# Register that derived default within a collection. If any of these are misspelled, it will silently fail.
derived_collection_entry("MY_DICT_SETTING", "my_list_with_a_derived_value", 2)
...

derive_settings(__name__)  # This actually renders the derived settings

After

from openedx.core.lib.derived import Derived, derive_settings

# Define a derived default (no registration required)
MY_DERIVED_SETTING = Derived(lambda settings: "compute something based on" + settings.ANOTHER_SETTING)

# Define a derived default within a collection (no registration required)
MY_DICT_SETTING = {
    "my_list_with_a_derived_value": [
        "value0",
        "value1",
        Derived(lambda settings: "compute value2 based on" settings.YET_ANOTHER_SETTING),
    ],
   "my_static_list": ["value0", "value1", "value2"],
}
...

derive_settings(__name__)  # This actually renders the derived settings (same as before)

Supporting info

Part of:

Builds on a previous settings refactoring:

Next up:

Testing instructions

First, download prod-like YML files to ensure that this works in a non-Tutor environment...

Then, download and save this to diff_settings.sh in the root of edx-platform:

#!/usr/bin/env bash
# Usage: ./diff_settings.sh GIT_REF_A GIT_REF_B
# Example: ./diff_settings.sh upstream/master myusername/myrefactorbranch
# Will fail if git state is not clean.

set -xeuo pipefail  # Be verbose and strict

REF_A=$1
REF_B=$2
DIR_A=dump_settings_a
DIR_B=dump_settings_b

SETTINGS_AND_CFG=( \
"envs.tutor.production,$LMS_CFG,$CMS_CFG" \
"envs.tutor.development,$LMS_CFG,$CMS_CFG" \
"envs.production,prodlike-lms.yml,prodlike-cms.yml" \
)

rm -rf "$DIR_A" "$DIR_B"
mkdir "$DIR_A" "$DIR_B"

for settings_and_cfg in "${SETTINGS_AND_CFG[@]}" ; do
    settings="${settings_and_cfg%%,*}"
    cfg="${settings_and_cfg#*,}"

    lms_cfg="${cfg%%,*}"
    lms_module="lms.${settings}"
    lms_outfile="${lms_module}__${lms_cfg//\//_}.json"
    git checkout "$REF_A"
    DJANGO_SETTINGS_MODULE="$lms_module" LMS_CFG="$lms_cfg" ./manage.py lms dump_settings > "$DIR_A/$lms_outfile"
    git checkout "$REF_B"
    DJANGO_SETTINGS_MODULE="$lms_module" LMS_CFG="$lms_cfg" ./manage.py lms dump_settings > "$DIR_B/$lms_outfile"

    cms_cfg="${cfg#*,}"
    if [[ -n "$cms_cfg" ]]; then
        cms_module="cms.${settings}"
        cms_outfile="${cms_module}__${cms_cfg//\//_}.json"
        git checkout "$REF_A"
        DJANGO_SETTINGS_MODULE="$cms_module" CMS_CFG="$cms_cfg" ./manage.py lms dump_settings > "$DIR_A/$cms_outfile"
        git checkout "$REF_B"
        DJANGO_SETTINGS_MODULE="$cms_module" CMS_CFG="$cms_cfg" ./manage.py lms dump_settings > "$DIR_B/$cms_outfile"
    fi
done

diff "$DIR_A" "$DIR_B" && echo "No difference!"

Finally, from within a Tutor LMS or CMS shell, run ./diff_settings.sh master kdmccormick/use-derived-tooling

Result should be "No difference!"

@kdmccormick kdmccormick force-pushed the kdmccormick/derived-better-2 branch 4 times, most recently from 7b56894 to 11dbd37 Compare February 3, 2025 15:07
@kdmccormick kdmccormick changed the title refactor: A Better API for Derived Settings feat!: A Better API for Derived Settings Feb 3, 2025
@kdmccormick kdmccormick requested a review from feanil February 3, 2025 16:22
@kdmccormick kdmccormick marked this pull request as ready for review February 3, 2025 16:23
@kdmccormick
Copy link
Member Author

kdmccormick commented Feb 3, 2025

@feanil , this is ready for review.

@dianakhuang , I have confirmed this PR using the redacted LMS yaml file. If you're able to provide the redacted CMS yaml, I can confirm with that as well.

@dianakhuang
Copy link
Contributor

@kdmccormick working on a redacted studio file right now, so I'll try to get you one by the end of today.

@kdmccormick
Copy link
Member Author

Tyty @dianakhuang -- I will keep this open until I hear from you.

Copy link
Contributor

@feanil feanil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, much easier to manage.

@kdmccormick
Copy link
Member Author

Confirmed with CMS settings. Will rebase and merge.

@kdmccormick kdmccormick force-pushed the kdmccormick/derived-better-2 branch from 11dbd37 to 2426dc7 Compare February 4, 2025 19:19
@kdmccormick kdmccormick merged commit 3227566 into openedx:master Feb 4, 2025
49 checks passed
@kdmccormick kdmccormick deleted the kdmccormick/derived-better-2 branch February 4, 2025 19:57
@edx-pipeline-bot
Copy link
Contributor

2U Release Notice: This PR has been deployed to the edX staging environment in preparation for a release to production.

@edx-pipeline-bot
Copy link
Contributor

2U Release Notice: This PR has been deployed to the edX production environment.

1 similar comment
@edx-pipeline-bot
Copy link
Contributor

2U Release Notice: This PR has been deployed to the edX production environment.

justinhynes added a commit that referenced this pull request Feb 5, 2025
A tiny update to the directions in devstack.py to enable theming based on recent changes after #36192 was merged.
justinhynes added a commit to edx/devstack that referenced this pull request Feb 5, 2025
Updates theming config example in our docs after recent changes to edx-platform (see openedx/edx-platform#36192).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants