Skip to content

Commit

Permalink
Merge branch 'main' into bld/make-non-essential-packages-optional
Browse files Browse the repository at this point in the history
  • Loading branch information
mdickinson authored Jul 4, 2024
2 parents b7eee4d + b559b37 commit 52507d6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
9 changes: 6 additions & 3 deletions apptools/preferences/preferences_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import logging

# Enthought library imports.
from traits.api import HasTraits, Instance, Str
from traits.api import HasTraits, Instance, Str, observe

# Local imports.
from .i_preferences import IPreferences
Expand Down Expand Up @@ -99,8 +99,11 @@ def _anytrait_changed(self, trait_name, old, new):
# If the change refers to a trait defined on this class, then
# the trait is not a preference trait and we do nothing.

def _preferences_changed(self, old, new):
""" Static trait change handler. """
@observe("preferences", post_init=True)
def _update_preferences_listeners(self, event):
"""Update listeners when the preferences object changes."""

old, new = event.old, event.new

# Stop listening to the old preferences node.
if old is not None:
Expand Down
27 changes: 26 additions & 1 deletion apptools/preferences/tests/test_preferences_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ class AcmeUIPreferencesHelper(PreferencesHelper):
def test_scoped_preferences(self):
""" scoped preferences """

p = set_default_preferences(ScopedPreferences())
p = set_default_preferences(
ScopedPreferences(application_preferences_filename=self.tmpfile)
)

# Set a preference value in the default scope.
p.set("default/acme.ui.bgcolor", "blue")
Expand Down Expand Up @@ -605,3 +607,26 @@ class AcmeUIPreferencesHelper(PreferencesHelper):
# attempt to create instance from invalid value
with self.assertRaises(TraitError):
AcmeUIPreferencesHelper(preferences_path="acme.ui")

def test_preferences_not_written_on_helper_creation(self):

class AppPreferencesHelper(PreferencesHelper):
#: The node that contains the preferences.
preferences_path = "app"

#: The user's favourite colour
color = Str()

default_preferences = Preferences(name="default")
default_preferences.set("app.color", "red")

application_preferences = Preferences(name="application")
preferences = ScopedPreferences(
scopes=[application_preferences, default_preferences]
)
self.assertIsNone(application_preferences.get("app.color"))

# Then creation of the helper should not cause the application
# preferences to change.
AppPreferencesHelper(preferences=preferences)
self.assertIsNone(application_preferences.get("app.color"))
12 changes: 8 additions & 4 deletions apptools/preferences/tests/test_scoped_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ class ScopedPreferencesTestCase(PreferencesTestCase):
def setUp(self):
""" Prepares the test fixture before each test method is called. """

self.preferences = ScopedPreferences()
# A temporary directory that can safely be written to.
self.tmpdir = tempfile.mkdtemp()

self.preferences = ScopedPreferences(
application_preferences_filename=os.path.join(
self.tmpdir, "preferences.ini"
)
)

# The filename of the example preferences file.
self.example = os.fspath(files(PKG) / "example.ini")

# A temporary directory that can safely be written to.
self.tmpdir = tempfile.mkdtemp()

def tearDown(self):
""" Called immediately after each test method has been called. """

Expand Down
1 change: 1 addition & 0 deletions docs/releases/upcoming/343.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Don't write to preferences on ``PreferencesHelper`` creation. (#343)

0 comments on commit 52507d6

Please sign in to comment.