Exemplo n.º 1
0
    def __init__(self, plugins=None, **traits):
        """ Constructor.

        We allow the caller to specify an initial list of plugins, but the list
        itself is not part of the public API. To add and remove plugins after
        after construction, use the 'add_plugin' and 'remove_plugin' methods
        respectively. The application is also iterable, so to iterate over the
        plugins use 'for plugin in application: ...'.

        """

        super(Application, self).__init__(**traits)

        # fixme: We have to initialize the application home here (i.e. we can't
        # wait until the 'home' trait is accessed) because the scoped
        # preferences uses 'ETSConfig.application' home as the name of the
        # default preferences file.
        self._initialize_application_home()

        # Set the default preferences node used by the preferences package.
        # This allows 'PreferencesHelper' and 'PreferenceBinding' instances to
        # be used as more convenient ways to access preferences.
        #
        # fixme: This is another sneaky global!
        set_default_preferences(self.preferences)

        # We allow the caller to specify an initial list of plugins, but the
        # list itself is not part of the public API. To add and remove plugins
        # after construction, use the 'add_plugin' and 'remove_plugin' methods
        # respectively. The application is also iterable, so to iterate over
        # the plugins use 'for plugin in application: ...'.
        if plugins is not None:
            map(self.add_plugin, plugins)

        return
Exemplo n.º 2
0
    def __init__(self, plugins=None, **traits):
        """ Constructor.

        We allow the caller to specify an initial list of plugins, but the list
        itself is not part of the public API. To add and remove plugins after
        after construction, use the 'add_plugin' and 'remove_plugin' methods
        respectively. The application is also iterable, so to iterate over the
        plugins use 'for plugin in application: ...'.

        """

        super().__init__(**traits)

        # fixme: We have to initialize the application home here (i.e. we can't
        # wait until the 'home' trait is accessed) because the scoped
        # preferences uses 'ETSConfig.application' home as the name of the
        # default preferences file.
        self._initialize_application_home()

        # Set the default preferences node used by the preferences package.
        # This allows 'PreferencesHelper' and 'PreferenceBinding' instances to
        # be used as more convenient ways to access preferences.
        #
        # fixme: This is another sneaky global!
        set_default_preferences(self.preferences)

        # We allow the caller to specify an initial list of plugins, but the
        # list itself is not part of the public API. To add and remove plugins
        # after construction, use the 'add_plugin' and 'remove_plugin' methods
        # respectively. The application is also iterable, so to iterate over
        # the plugins use 'for plugin in application: ...'.
        if plugins is not None:
            for plugin in plugins:
                self.add_plugin(plugin)
    def test_package_global_default_preferences(self):
        """ package global default preferences """

        from apptools.preferences.api import get_default_preferences
        from apptools.preferences.api import set_default_preferences

        set_default_preferences(self.preferences)
        self.assertEqual(self.preferences, get_default_preferences())
    def test_package_global_default_preferences(self):
        """ package global default preferences """

        from apptools.preferences.api import get_default_preferences
        from apptools.preferences.api import set_default_preferences

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

        return
Exemplo n.º 5
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.º 6
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 = os.fspath(files(PKG) / "example.ini")
Exemplo n.º 7
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
 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('mayavi.tests', 'test_preference.ini')
     self.preferences.load(pref_file)
     self.pref = _TestPreference()
     self.mirror = PreferencesMirror()
     self.mirror.preferences = self.pref
Exemplo n.º 9
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('mayavi.tests',
                                   'test_preference.ini')
     self.preferences.load(pref_file)
     self.pref = _TestPreference()
     self.mirror = PreferencesMirror()
     self.mirror.preferences = self.pref
    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 = os.fspath(files(PKG) / "example.ini")

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

        # Path to a temporary file
        self.tmpfile = os.path.join(self.tmpdir, "tmp.ini")

        push_exception_handler(reraise_exceptions=True)
        self.addCleanup(pop_exception_handler)
Exemplo n.º 11
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.assertTrue(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.assertTrue(acme_ui.visible)

        # Clean up!
        os.remove(tmp)
        os.rmdir(tmpdir)
Exemplo n.º 12
0
# Thanks for using Enthought open source!
""" An example of using the preferences manager. """

# Enthought library imports.
from traits.api import Color, Int
from traitsui.api import View

# Local imports.
from apptools.preferences.api import Preferences
from apptools.preferences.api import get_default_preferences
from apptools.preferences.api import set_default_preferences
from apptools.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 = ''
    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
    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.º 15
0
from traits.etsconfig.api import ETSConfig
from apptools.preferences.api import ScopedPreferences, set_default_preferences, get_default_preferences 
import os
import sys

ETSConfig.company = 'Infobiotics'

preferences = ScopedPreferences(filename=os.path.join(ETSConfig.get_application_data(create=True), 'preferences.ini'))
set_default_preferences(preferences) # allows use of Preferences, PreferencesHelper and bind_preference without explicitly passing preferences
assert preferences == get_default_preferences()

from infobiotics.mcss.mcss_preferences import PREFERENCES_PATH as MCSS_PREFERENCES_PATH
from infobiotics.mcsscmaes.mcsscmaes_preferences import PREFERENCES_PATH as MCSSCMAES_PREFERENCES_PATH
from infobiotics.pmodelchecker.pmodelchecker_preferences import PREFERENCES_PATH as PMODELCHECKER_PREFERENCES_PATH
from infobiotics.pmodelchecker.prism.prism_preferences import PREFERENCES_PATH as PRISM_PREFERENCES_PATH
from infobiotics.pmodelchecker.mc2.mc2_preferences import PREFERENCES_PATH as MC2_PREFERENCES_PATH
from infobiotics.pmodelchecker.mc2.mc2_preferences import MC2_MCSS_PREFERENCES_PATH
from infobiotics.poptimizer.poptimizer_preferences import PREFERENCES_PATH as POPTIMIZER_PREFERENCES_PATH

DEFAULT_MCSS_EXECUTABLE = 'default/'+MCSS_PREFERENCES_PATH+'.executable'
DEFAULT_MCSSCMAES_EXECUTABLE = 'default/'+MCSSCMAES_PREFERENCES_PATH+'.executable'
DEFAULT_PMODELCHECKER_EXECUTABLE = 'default/'+PMODELCHECKER_PREFERENCES_PATH+'.executable'
DEFAULT_PRISM_EXECUTABLE = 'default/'+PRISM_PREFERENCES_PATH+'.executable'
DEFAULT_MC2_EXECUTABLE = 'default/'+MC2_PREFERENCES_PATH+'.executable'
DEFAULT_MC2_MCSS_EXECUTABLE = 'default/'+MC2_MCSS_PREFERENCES_PATH+'.executable'
DEFAULT_POPTIMIZER_EXECUTABLE = 'default/'+POPTIMIZER_PREFERENCES_PATH+'.executable'

if sys.platform.startswith('win'):
    preferences.set(DEFAULT_MCSS_EXECUTABLE, 'mcss.exe'),
    preferences.set(DEFAULT_MCSSCMAES_EXECUTABLE, 'mcss-cmaes.exe'),
    preferences.set(DEFAULT_PMODELCHECKER_EXECUTABLE, 'pmodelchecker.exe'),
Exemplo n.º 16
0

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

# Local imports.
from apptools.preferences.api import Preferences, PreferencesHelper
from apptools.preferences.api import get_default_preferences
from apptools.preferences.api import set_default_preferences
from apptools.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 = ''