Exemplo n.º 1
0
    def test_package_global_default_preferences(self):
        """ package global default preferences """

        from enthought.preferences.api import get_default_preferences
        from enthought.preferences.api import set_default_preferences

        set_default_preferences(self.preferences)
        self.assertEqual(self.preferences, get_default_preferences())

        return
Exemplo n.º 2
0
    def test_scoped_preferences(self):
        """ scoped preferences """

        p = set_default_preferences(ScopedPreferences())

        # Set a preference value in the default scope.
        p.set('default/acme.ui.bgcolor', 'blue')

        class AcmeUIPreferencesHelper(PreferencesHelper):
            """ A helper! """

            # The path to the preferences node that contains our preferences.
            preferences_path = 'acme.ui'

            # The traits that we want to initialize from preferences.
            bgcolor = Str

            # A trait for a preference that does not exist yet.
            name = Str

        helper = AcmeUIPreferencesHelper()

        # Make sure the trait is set!
        self.assertEqual('blue', helper.bgcolor)

        # And that the non-existent trait gets the default value.
        self.assertEqual('', helper.name)

        return
Exemplo n.º 3
0
    def test_scoped_preferences(self):
        """ scoped preferences """

        p = set_default_preferences(ScopedPreferences())

        # Set a preference value in the default scope.
        p.set('default/acme.ui.bgcolor', 'blue')

        class AcmeUIPreferencesHelper(PreferencesHelper):
            """ A helper! """

            # The path to the preferences node that contains our preferences.
            preferences_path = 'acme.ui'

            # The traits that we want to initialize from preferences.
            bgcolor = Str

            # A trait for a preference that does not exist yet.
            name = Str
            
        helper = AcmeUIPreferencesHelper()
        
        # Make sure the trait is set!
        self.assertEqual('blue', helper.bgcolor)

        # And that the non-existent trait gets the default value.
        self.assertEqual('', helper.name)
        
        return
Exemplo n.º 4
0
    def setUp(self):
        """ Prepares the test fixture before each test method is called. """

        self.preferences = set_default_preferences(Preferences())

        # The filename of the example preferences file.
        self.example = resource_filename(PKG, 'example.ini')

        return
Exemplo n.º 5
0
    def setUp(self):
        """ Prepares the test fixture before each test method is called. """

        self.preferences = set_default_preferences(Preferences())

        # The filename of the example preferences file.
        self.example = resource_filename(PKG, 'example.ini')
        
        return
Exemplo n.º 6
0
 def setUp(self):
     """Called before each test is run"""
     self.preferences = set_default_preferences(Preferences())
     # The filename of the example preferences file.
     pref_file = resource_filename('enthought.mayavi.tests',
                                   'test_preference.ini')
     self.preferences.load(pref_file)
     self.pref = TestPreference()
     self.mirror = PreferencesMirror()
     self.mirror.preferences = self.pref
Exemplo n.º 7
0

# Enthought library imports.
from enthought.traits.api import Color, Int, Float, Str
from enthought.traits.ui.api import View

# Local imports.
from enthought.preferences.api import Preferences, PreferencesHelper
from enthought.preferences.api import get_default_preferences
from enthought.preferences.api import set_default_preferences
from enthought.preferences.ui.api import PreferencesManager, PreferencesPage


# Create a preferences collection from a file and make it the default root
# preferences node for all preferences helpers etc.
set_default_preferences(Preferences(filename='example.ini'))


class AcmePreferencesPage(PreferencesPage):
    """ A preference page for the Acme preferences. """

    #### 'IPreferencesPage' interface #########################################

    # The page's category (e.g. 'General/Appearence'). The empty string means
    # that this is a top-level page.
    category = ''

    # The page's help identifier (optional). If a help Id *is* provided then
    # there will be a 'Help' button shown on the preference page.
    help_id = ''
Exemplo n.º 8
0
    def test_load_and_save(self):
        """ load and save """
        
        p = self.preferences
        p.load(self.example)
        
        class AcmeUI(HasTraits):
            """ The Acme UI class! """

            # The traits that we want to initialize from preferences.
            bgcolor = Str('red')
            width   = Int(60)
            ratio   = Float(2.0)
            visible = Bool(False)

        acme_ui = AcmeUI()
        
        # Make some bindings.
        bind_preference(acme_ui, 'bgcolor', 'acme.ui.bgcolor')
        bind_preference(acme_ui, 'width',   'acme.ui.width')
        bind_preference(acme_ui, 'ratio',   'acme.ui.ratio')
        bind_preference(acme_ui, 'visible', 'acme.ui.visible')
            
        # Make sure the helper was initialized properly (with the values in
        # the loaded .ini file *not* the trait defaults!).
        self.assertEqual('blue', acme_ui.bgcolor)
        self.assertEqual(50, acme_ui.width)
        self.assertEqual(1.0, acme_ui.ratio)
        self.assertEqual(True, acme_ui.visible)

        # Make a change to one of the preference values.
        p.set('acme.ui.bgcolor', 'yellow') 
        self.assertEqual('yellow', acme_ui.bgcolor)
        self.assertEqual('yellow', p.get('acme.ui.bgcolor'))

        # Save the preferences to a different file.
        tmpdir = tempfile.mkdtemp()
        tmp = join(tmpdir, 'tmp.ini')
        p.save(tmp)

        # Load the preferences again from that file.
        p = set_default_preferences(Preferences())
        p.load(tmp)
        
        acme_ui = AcmeUI()
        
        # Make some bindings.
        bind_preference(acme_ui, 'bgcolor', 'acme.ui.bgcolor')
        bind_preference(acme_ui, 'width',   'acme.ui.width')
        bind_preference(acme_ui, 'ratio',   'acme.ui.ratio')
        bind_preference(acme_ui, 'visible', 'acme.ui.visible')
        
        # Make sure the helper was initialized properly (with the values in
        # the .ini file *not* the trait defaults!).
        self.assertEqual('yellow', acme_ui.bgcolor)
        self.assertEqual(50, acme_ui.width)
        self.assertEqual(1.0, acme_ui.ratio)
        self.assertEqual(True, acme_ui.visible)

        # Clean up!
        os.remove(tmp)
        os.removedirs(tmpdir)
        
        return
Exemplo n.º 9
0
    def test_load_and_save(self):
        """ load and save """

        p = self.preferences
        p.load(self.example)

        class AcmeUI(HasTraits):
            """ The Acme UI class! """

            # The traits that we want to initialize from preferences.
            bgcolor = Str('red')
            width = Int(60)
            ratio = Float(2.0)
            visible = Bool(False)

        acme_ui = AcmeUI()

        # Make some bindings.
        bind_preference(acme_ui, 'bgcolor', 'acme.ui.bgcolor')
        bind_preference(acme_ui, 'width', 'acme.ui.width')
        bind_preference(acme_ui, 'ratio', 'acme.ui.ratio')
        bind_preference(acme_ui, 'visible', 'acme.ui.visible')

        # Make sure the helper was initialized properly (with the values in
        # the loaded .ini file *not* the trait defaults!).
        self.assertEqual('blue', acme_ui.bgcolor)
        self.assertEqual(50, acme_ui.width)
        self.assertEqual(1.0, acme_ui.ratio)
        self.assertEqual(True, acme_ui.visible)

        # Make a change to one of the preference values.
        p.set('acme.ui.bgcolor', 'yellow')
        self.assertEqual('yellow', acme_ui.bgcolor)
        self.assertEqual('yellow', p.get('acme.ui.bgcolor'))

        # Save the preferences to a different file.
        tmpdir = tempfile.mkdtemp()
        tmp = join(tmpdir, 'tmp.ini')
        p.save(tmp)

        # Load the preferences again from that file.
        p = set_default_preferences(Preferences())
        p.load(tmp)

        acme_ui = AcmeUI()

        # Make some bindings.
        bind_preference(acme_ui, 'bgcolor', 'acme.ui.bgcolor')
        bind_preference(acme_ui, 'width', 'acme.ui.width')
        bind_preference(acme_ui, 'ratio', 'acme.ui.ratio')
        bind_preference(acme_ui, 'visible', 'acme.ui.visible')

        # Make sure the helper was initialized properly (with the values in
        # the .ini file *not* the trait defaults!).
        self.assertEqual('yellow', acme_ui.bgcolor)
        self.assertEqual(50, acme_ui.width)
        self.assertEqual(1.0, acme_ui.ratio)
        self.assertEqual(True, acme_ui.visible)

        # Clean up!
        os.remove(tmp)
        os.removedirs(tmpdir)

        return