def test_save(self):
        """ save """

        p = self.preferences

        # Get the application scope.
        application = p.node('application/')

        tmp = join(self.tmpdir, 'test.ini')
        application.filename = tmp

        # Set a value.
        p.set('acme.ui.bgcolor', 'red')

        # Save all scopes.
        p.save()

        # Make sure a file was written.
        self.assertEqual(True, os.path.exists(tmp))

        # Load the 'ini' file into a new preferences node and make sure the
        # preference is in there.
        p = Preferences()
        p.load(tmp)

        self.assertEqual('red', p.get('acme.ui.bgcolor'))

        # Cleanup.
        os.remove(tmp)

        return
Example #2
0
    def test_save(self):
        """ save """

        p = self.preferences

        # Get the application scope.
        application = p.node('application/')

        tmp = join(self.tmpdir, 'test.ini')
        application.filename = tmp

        # Set a value.
        p.set('acme.ui.bgcolor', 'red')

        # Save all scopes.
        p.save()

        # Make sure a file was written.
        self.assertEqual(True, os.path.exists(tmp))

        # Load the 'ini' file into a new preferences node and make sure the
        # preference is in there.
        p = Preferences()
        p.load(tmp)

        self.assertEqual('red', p.get('acme.ui.bgcolor'))

        # Cleanup.
        os.remove(tmp)

        return
Example #3
0
    def test_explicit_preferences(self):
        """ explicit preferences """

        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
            width = Int
            ratio = Float
            visible = Bool

        acme_ui = AcmeUI()
        acme_ui.on_trait_change(listener)

        # Create an empty preferences node and use that in some of the
        # bindings!
        preferences = Preferences()

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

        # Make sure the object was initialized properly.
        self.assertEqual('', acme_ui.bgcolor)
        self.assertEqual(50, acme_ui.width)
        self.assertEqual(0.0, acme_ui.ratio)
        self.assertEqual(True, acme_ui.visible)

        return
    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
Example #5
0
    def setUp(self):
        """ Prepares the test fixture before each test method is called. """

        self.preferences = Preferences()

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

        # A temporary directory that can safely be written to.
        self.tmpdir = tempfile.mkdtemp()
Example #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
Example #7
0
    def test_save(self):
        """ save """

        p = self.preferences

        # Load the preferences from an 'ini' file.
        p.load(self.example)

        # Make sure it was all loaded!
        self.assertEqual('blue', p.get('acme.ui.bgcolor'))
        self.assertEqual('50', p.get('acme.ui.width'))
        self.assertEqual('1.0', p.get('acme.ui.ratio'))
        self.assertEqual('True', p.get('acme.ui.visible'))
        self.assertEqual('acme ui', p.get('acme.ui.description'))
        self.assertEqual('[1, 2, 3, 4]', p.get('acme.ui.offsets'))
        self.assertEqual("['joe', 'fred', 'jane']", p.get('acme.ui.names'))
        self.assertEqual('splash', p.get('acme.ui.splash_screen.image'))
        self.assertEqual('red', p.get('acme.ui.splash_screen.fgcolor'))

        # Make a change.
        p.set('acme.ui.bgcolor', 'yellow')
        
        # Save it to another file.
        tmp = join(self.tmpdir, 'tmp.ini')
        p.save(tmp)

        try:
            # Load it into a new node.
            p = Preferences()
            p.load(tmp)
            
            # Make sure it was all loaded!
            self.assertEqual('yellow', p.get('acme.ui.bgcolor'))
            self.assertEqual('50', p.get('acme.ui.width'))
            self.assertEqual('1.0', p.get('acme.ui.ratio'))
            self.assertEqual('True', p.get('acme.ui.visible'))
            self.assertEqual('acme ui', p.get('acme.ui.description'))
            self.assertEqual('[1, 2, 3, 4]', p.get('acme.ui.offsets'))
            self.assertEqual("['joe', 'fred', 'jane']", p.get('acme.ui.names'))
            self.assertEqual('splash', p.get('acme.ui.splash_screen.image'))
            self.assertEqual('red', p.get('acme.ui.splash_screen.fgcolor'))

        finally:
            # Clean up!
            os.remove(tmp)

        return
Example #8
0
    def test_flush(self):
        """ flush """

        p = self.preferences

        # A temporary .ini file for this test.
        tmp = join(self.tmpdir, 'tmp.ini')
        
        # This could be set in the constructor of course, its just here we
        # want to use the instance declared in 'setUp'.
        p.filename = tmp

        try:
            # Load the preferences from an 'ini' file.
            p.load(self.example)

            # Flush it.
            p.flush()

            # Load it into a new node.
            p = Preferences()
            p.load(tmp)
            
            # Make sure it was all loaded!
            self.assertEqual('blue', p.get('acme.ui.bgcolor'))
            self.assertEqual('50', p.get('acme.ui.width'))
            self.assertEqual('1.0', p.get('acme.ui.ratio'))
            self.assertEqual('True', p.get('acme.ui.visible'))
            self.assertEqual('acme ui', p.get('acme.ui.description'))
            self.assertEqual('[1, 2, 3, 4]', p.get('acme.ui.offsets'))
            self.assertEqual("['joe', 'fred', 'jane']", p.get('acme.ui.names'))
            self.assertEqual('splash', p.get('acme.ui.splash_screen.image'))
            self.assertEqual('red', p.get('acme.ui.splash_screen.fgcolor'))

        finally:
            # Clean up!
            os.remove(tmp)

        return
Example #9
0
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# RADPY IS NOT CERTIFIED AS A MEDICAL DEVICE.  IT IS INTENDED ONLY FOR RESEARCH
# PURPOSES.  ANY OTHER USE IS ENTIRELY AT THE DISCRETION AND RISK OF THE USER.
################################################################################

from enthought.preferences.api import PreferencesHelper, Preferences
from enthought.traits.api import List
from enthought.etsconfig.api import ETSConfig
from os.path import join

filename = join(ETSConfig.get_application_data(create=False), 'radpy',
                'preferences.ini')
PreferencesHelper.preferences = Preferences(filename=filename)


class BeamAnalysisPreferencesHelper(PreferencesHelper):
    """ A preferences helper for the BeamAnalysis plugin. """

    preferences_path = 'radpy.plugins.BeamAnalysis'
    match_traits = List
    def test_preferences_node_changed(self):
        """ preferences node changed """

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

        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
            width = Int
            ratio = Float
            visible = Bool
            description = Unicode
            offsets = List(Int)
            names = List(Str)

        helper = AcmeUIPreferencesHelper()

        # We only listen to some of the traits so the testing is easier.
        helper.on_trait_change(listener, ['bgcolor', 'width'])

        # Create a new preference node.
        p1 = Preferences()
        p1.load(self.example)
        p1.set('acme.ui.bgcolor', 'red')
        p1.set('acme.ui.width', 40)

        # Set the new preferences
        helper.preferences = p1

        # Test event handling.
        self.assertEqual(helper, listener.obj)
        self.assertEqual('width', listener.trait_name)
        self.assertEqual(50, listener.old)
        self.assertEqual(40, listener.new)

        # Test re-initialization.
        self.assertEqual(helper.bgcolor, 'red')
        self.assertEqual(helper.width, 40)

        # Test event handling.
        p1.set('acme.ui.bgcolor', 'black')
        self.assertEqual(helper, listener.obj)
        self.assertEqual('bgcolor', listener.trait_name)
        self.assertEqual('red', listener.old)
        self.assertEqual('black', listener.new)

        # This should not trigger any new changes since we are setting values
        # on the old preferences node.
        p.set('acme.ui.bgcolor', 'white')
        self.assertEqual(helper, listener.obj)
        self.assertEqual('bgcolor', listener.trait_name)
        self.assertEqual('red', listener.old)
        self.assertEqual('black', listener.new)

        return
    def test_preferences_node_changed(self):
        """ preferences node changed """
 
        p = self.preferences
        p.load(self.example)

        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
            width       = Int
            ratio       = Float
            visible     = Bool
            description = Unicode
            offsets     = List(Int)
            names       = List(Str)
            
        helper = AcmeUIPreferencesHelper()

        # We only listen to some of the traits so the testing is easier.
        helper.on_trait_change(listener, ['bgcolor', 'width'])

        # Create a new preference node.
        p1 = Preferences()
        p1.load(self.example)
        p1.set('acme.ui.bgcolor', 'red')
        p1.set('acme.ui.width', 40)

        # Set the new preferences
        helper.preferences = p1

        # Test event handling.
        self.assertEqual(helper, listener.obj)
        self.assertEqual('width', listener.trait_name)
        self.assertEqual(50, listener.old)
        self.assertEqual(40, listener.new)

        # Test re-initialization.
        self.assertEqual(helper.bgcolor, 'red')
        self.assertEqual(helper.width, 40)
    
        # Test event handling.
        p1.set('acme.ui.bgcolor', 'black')
        self.assertEqual(helper, listener.obj)
        self.assertEqual('bgcolor', listener.trait_name)
        self.assertEqual('red', listener.old)
        self.assertEqual('black', listener.new)
       
        # This should not trigger any new changes since we are setting values
        # on the old preferences node.
        p.set('acme.ui.bgcolor', 'white')
        self.assertEqual(helper, listener.obj)
        self.assertEqual('bgcolor', listener.trait_name)
        self.assertEqual('red', listener.old)
        self.assertEqual('black', listener.new)

        return
Example #12
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