OpenSite/Guides/Save and reload settings

From BathAfrica Documentation
Revision as of 14:01, 29 October 2026 by BathAfrica (talk | contribs) (Add rewritten practical lesson with steps, code and expected results)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
OpenSite 0.0.1 · DevelopmentSource scope and availability


Save selected user-setting state to a fresh file and reload it.

Stable identifier
opensite.help.save-and-reload-settings

Prerequisites

Embedded OpenSite Python and a writable practice directory. The script restores the original language setting on completion.

Steps

Read the starting value

Store the original language tag before making the lesson change.

Save a new settings file

Set the tag to French and save to a fresh temporary folder. The format is opensite-settings, not a document model.

Change and reload

Change to English, reload the saved file and confirm that the stored tag returns to fr.

Restore the session

The finally block restores the original tag. A language tag selects available catalog text; it does not download translations.

Expected result

The reloaded value is fr; the original setting is restored afterwards.

Limitations

Do not put credentials in settings. File success does not make an arbitrary exported value secret-safe. Settings-file persistence and engineering-document persistence are distinct.

Complete example

from pathlib import Path
from tempfile import mkdtemp
import opensite

original = opensite.execute("settings.get", {"key": "locale.language"})["value"]
path = Path(mkdtemp(prefix="opensite-settings-")) / "preferences.json"
try:
    opensite.execute("settings.set", {"key": "locale.language", "value": "fr"})
    opensite.settings_save(str(path))
    opensite.execute("settings.set", {"key": "locale.language", "value": "en"})
    opensite.settings_load(str(path))
    restored = opensite.execute("settings.get", {"key": "locale.language"})["value"]
    assert restored == "fr"
    print("Stored language:", restored)
finally:
    opensite.execute("settings.set", {"key": "locale.language", "value": original})
# Selecting a language does not install a missing translation catalog.