Exemplo n.º 1
0
    def migrate_settings(settings, version=None):
        def mig_value(x):
            if x == "Position (index)":
                return INDEX
            if x == "Source position (index)":
                return INSTANCEID
            return x

        if not version:
            operations = ("augment", "merge", "combine")
            oper = operations[settings["merging"]]
            settings["attr_pairs"] = ([
                (mig_value(settings[f"attr_{oper}_data"]),
                 mig_value(settings[f"attr_{oper}_extra"]))
            ])
            for oper in operations:
                del settings[f"attr_{oper}_data"]
                del settings[f"attr_{oper}_extra"]

        if not version or version < 2 and "attr_pairs" in settings:
            attr_pairs = settings.pop("attr_pairs")
            attr_pairs = [
                tuple((var, 100) if isinstance(var, str) else var
                      for var in pair) for pair in attr_pairs
            ]
            context = ContextHandler().new_context()
            context.variables1 = \
                dict(var for var, _ in attr_pairs if var[1] > 100)
            context.variables2 = \
                dict(var for _, var in attr_pairs if var[1] > 100)
            context.values["attr_pairs"] = attr_pairs
            settings["context_settings"] = [context]
Exemplo n.º 2
0
    def migrate_settings(settings, version=None):
        def mig_value(x):
            if x == "Position (index)":
                return INDEX
            if x == "Source position (index)":
                return INSTANCEID
            return x

        if not version:
            operations = ("augment", "merge", "combine")
            oper = operations[settings["merging"]]
            settings["attr_pairs"] = (True, True, [
                (mig_value(settings[f"attr_{oper}_data"]),
                 mig_value(settings[f"attr_{oper}_extra"]))
            ])
            for oper in operations:
                del settings[f"attr_{oper}_data"]
                del settings[f"attr_{oper}_extra"]

        if not version or version < 2 and "attr_pairs" in settings:
            data_exists, extra_exists, attr_pairs = settings.pop("attr_pairs")
            if not (data_exists and extra_exists):
                settings["context_settings"] = []
                return

            mapper = {0: (INDEX, 100), 1: (INSTANCEID, 100)}
            context = ContextHandler().new_context()
            context.values["attr_pairs"] = [
                tuple(mapper.get(var, (var, 100)) for var in pair)
                for pair in attr_pairs
            ]
            context.variables1 = {}
            context.variables2 = {}
            settings["context_settings"] = [context]
Exemplo n.º 3
0
 def test_about_pack_settings_signal(self):
     handler = ContextHandler()
     handler.bind(SimpleWidget)
     widget = SimpleWidget()
     handler.initialize(widget)
     fn = Mock()
     widget.settingsAboutToBePacked.connect(fn)
     handler.pack_data(widget)
     self.assertEqual(1, fn.call_count)
     handler.update_defaults(widget)
     self.assertEqual(2, fn.call_count)
Exemplo n.º 4
0
 def test_close_context(self):
     handler = ContextHandler()
     handler.bind(SimpleWidget)
     widget = SimpleWidget()
     widget.storeSpecificSettings = Mock()
     handler.initialize(widget)
     widget.schema_only_setting = 0xD06F00D
     widget.current_context = handler.new_context()
     handler.close_context(widget)
     self.assertEqual(widget.schema_only_setting, 0xD06F00D)
Exemplo n.º 5
0
    def test_fast_save(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)

        widget = SimpleWidget()
        handler.initialize(widget)

        context = widget.current_context = handler.new_context()
        handler.fast_save(widget, 'context_setting', 55)
        self.assertEqual(context.values['context_setting'], 55)
Exemplo n.º 6
0
    def test_find_or_create_context(self):
        widget = SimpleWidget()
        handler = ContextHandler()
        handler.match = lambda context, i: (context.i == i) * 2
        handler.clone_context = lambda context, i: copy(context)

        c1, c2, c3, c4, c5, c6, c7, c8, c9 = (Context(i=i)
                                              for i in range(1, 10))

        # finding a perfect match in global_contexts should copy it to
        # the front of context_settings (and leave globals as-is)
        widget.context_settings = [c2, c5]
        handler.global_contexts = [c3, c7]
        context, new = handler.find_or_create_context(widget, 7)
        self.assertEqual(context.i, 7)
        self.assertEqual([c.i for c in widget.context_settings], [7, 2, 5])
        self.assertEqual([c.i for c in handler.global_contexts], [3, 7])

        # finding a perfect match in context_settings should move it to
        # the front of the list
        widget.context_settings = [c2, c5]
        handler.global_contexts = [c3, c7]
        context, new = handler.find_or_create_context(widget, 5)
        self.assertEqual(context.i, 5)
        self.assertEqual([c.i for c in widget.context_settings], [5, 2])
        self.assertEqual([c.i for c in handler.global_contexts], [3, 7])
Exemplo n.º 7
0
 def test_about_pack_settings_signal(self):
     handler = ContextHandler()
     handler.bind(SimpleWidget)
     widget = SimpleWidget()
     handler.initialize(widget)
     fn = Mock()
     widget.settingsAboutToBePacked.connect(fn)
     handler.pack_data(widget)
     self.assertEqual(1, fn.call_count)
     handler.update_defaults(widget)
     self.assertEqual(2, fn.call_count)
Exemplo n.º 8
0
 def test_close_context(self):
     handler = ContextHandler()
     handler.bind(SimpleWidget)
     widget = SimpleWidget()
     widget.storeSpecificSettings = Mock()
     handler.initialize(widget)
     widget.schema_only_setting = 0xD06F00D
     widget.current_context = handler.new_context()
     handler.close_context(widget)
     self.assertEqual(widget.schema_only_setting, 0xD06F00D)
    def test_fast_save(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)

        widget = SimpleWidget()
        handler.initialize(widget)

        context = widget.current_context = handler.new_context()
        handler.fast_save(widget, "context_setting", 55)
        self.assertEqual(context.values["context_setting"], 55)
        self.assertEqual(
            handler.known_settings["context_setting"].default,
            SimpleWidget.context_setting.default,
        )
Exemplo n.º 10
0
    def test_find_or_create_context(self):
        widget = SimpleWidget()
        handler = ContextHandler()
        handler.match = lambda context, i: (context.i == i) * 2
        handler.clone_context = lambda context, i: copy(context)

        c1, c2, c3, c4, c5, c6, c7, c8, c9 = (Context(i=i)
                                              for i in range(1, 10))

        # finding a perfect match in global_contexts should copy it to
        # the front of context_settings (and leave globals as-is)
        widget.context_settings = [c2, c5]
        handler.global_contexts = [c3, c7]
        context, new = handler.find_or_create_context(widget, 7)
        self.assertEqual(context.i, 7)
        self.assertEqual([c.i for c in widget.context_settings], [7, 2, 5])
        self.assertEqual([c.i for c in handler.global_contexts], [3, 7])

        # finding a perfect match in context_settings should move it to
        # the front of the list
        widget.context_settings = [c2, c5]
        handler.global_contexts = [c3, c7]
        context, new = handler.find_or_create_context(widget, 5)
        self.assertEqual(context.i, 5)
        self.assertEqual([c.i for c in widget.context_settings], [5, 2])
        self.assertEqual([c.i for c in handler.global_contexts], [3, 7])
Exemplo n.º 11
0
    def test_migrates_settings_removes_incompatible(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)

        widget = SimpleWidget()

        contexts = [Context(foo=i) for i in (13, 13, 0, 1, 13, 2, 13)]

        def migrate_context(context, _):
            if context.foo == 13:
                raise IncompatibleContext()

        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.initialize(widget, dict(context_settings=contexts))
            contexts = widget.context_settings
            self.assertEqual(len(contexts), 3)
            self.assertTrue(
                all(context.foo == i
                    for i, context in enumerate(contexts)))
Exemplo n.º 12
0
    def test_fast_save(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)

        widget = SimpleWidget()
        handler.initialize(widget)

        context = widget.current_context = handler.new_context()
        handler.fast_save(widget, 'context_setting', 55)
        self.assertEqual(context.values['context_setting'], 55)
        self.assertEqual(handler.known_settings['context_setting'].default,
                         SimpleWidget.context_setting.default)
Exemplo n.º 13
0
    def test_pack_settings_stores_version(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)

        widget = SimpleWidget()
        handler.initialize(widget)
        widget.context_setting = [DummyContext() for _ in range(3)]

        settings = handler.pack_data(widget)
        self.assertIn("context_settings", settings)
        for c in settings["context_settings"]:
            self.assertIn(VERSION_KEY, c.values)
Exemplo n.º 14
0
    def test_initialize(self):
        handler = ContextHandler()
        handler.provider = Mock()
        handler.widget_class = SimpleWidget

        # Context settings from data
        widget = SimpleWidget()
        context_settings = [DummyContext()]
        handler.initialize(widget, {'context_settings': context_settings})
        self.assertTrue(hasattr(widget, 'context_settings'))
        self.assertEqual(widget.context_settings, context_settings)

        # Default (global) context settings
        widget = SimpleWidget()
        handler.initialize(widget)
        self.assertTrue(hasattr(widget, 'context_settings'))
        self.assertEqual(widget.context_settings, handler.global_contexts)
Exemplo n.º 15
0
    def test_write_defaults_stores_version(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)
        widget = SimpleWidget()
        widget.current_context = None
        widget.context_settings = [DummyContext() for _ in range(3)]
        handler.update_defaults(widget)

        f = BytesIO()
        f.close = lambda: None
        with patch("builtins.open", Mock(return_value=f)):
            handler.write_defaults()
            f.seek(0)
            pickle.load(f)  # settings
            contexts = pickle.load(f)
            for c in contexts:
                self.assertEqual(c.values.get("__version__", 0xBAD), 1)
Exemplo n.º 16
0
    def test_pack_settings_stores_version(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)

        widget = SimpleWidget()
        handler.initialize(widget)
        widget.context_setting = [DummyContext() for _ in range(3)]

        settings = handler.pack_data(widget)
        self.assertIn("context_settings", settings)
        for c in settings["context_settings"]:
            self.assertIn(VERSION_KEY, c.values)
Exemplo n.º 17
0
    def test_initialize(self):
        handler = ContextHandler()
        handler.provider = Mock()
        handler.widget_class = SimpleWidget

        # Context settings from data
        widget = SimpleWidget()
        context_settings = [DummyContext()]
        handler.initialize(widget, {'context_settings': context_settings})
        self.assertTrue(hasattr(widget, 'context_settings'))
        self.assertEqual(widget.context_settings, context_settings)

        # Default (global) context settings
        widget = SimpleWidget()
        handler.initialize(widget)
        self.assertTrue(hasattr(widget, 'context_settings'))
        self.assertEqual(widget.context_settings, handler.global_contexts)
Exemplo n.º 18
0
    def test_write_defaults_stores_version(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)
        widget = SimpleWidget()
        widget.current_context = None
        widget.context_settings = [DummyContext() for _ in range(3)]
        handler.update_defaults(widget)

        f = BytesIO()
        f.close = lambda: None
        with patch("builtins.open", Mock(return_value=f)):
            handler.write_defaults()
            f.seek(0)
            pickle.load(f)  # settings
            contexts = pickle.load(f)
            for c in contexts:
                self.assertEqual(c.values.get("__version__", 0xBAD), 1)
Exemplo n.º 19
0
    def test_initialize_migrates_contexts(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)

        widget = SimpleWidget()

        # Old settings without version
        contexts = [DummyContext() for _ in range(3)]
        migrate_context = Mock()
        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.initialize(widget, dict(context_settings=contexts))
        migrate_context.assert_has_calls([call(c, 0) for c in contexts])

        # Settings with version
        contexts = [DummyContext(version=i) for i in range(1, 4)]
        migrate_context = Mock()
        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.initialize(widget,
                               dict(context_settings=deepcopy(contexts)))
        migrate_context.assert_has_calls(
            [call(c, c.values[VERSION_KEY]) for c in contexts])
Exemplo n.º 20
0
    def test_initialize_migrates_contexts(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)

        widget = SimpleWidget()

        # Old settings without version
        contexts = [DummyContext() for _ in range(3)]
        migrate_context = Mock()
        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.initialize(widget, dict(context_settings=contexts))
        migrate_context.assert_has_calls([call(c, 0) for c in contexts])

        # Settings with version
        contexts = [DummyContext(version=i) for i in range(1, 4)]
        migrate_context = Mock()
        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.initialize(widget, dict(context_settings=deepcopy(contexts)))
        migrate_context.assert_has_calls([call(c, c.values[VERSION_KEY]) for c in contexts])
Exemplo n.º 21
0
    def test_read_defaults(self):
        contexts = [DummyContext() for _ in range(3)]

        handler = ContextHandler()
        handler.widget_class = SimpleWidget

        # Old settings without version
        migrate_context = Mock()
        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.read_defaults_file(create_defaults_file(contexts))
        self.assertSequenceEqual(handler.global_contexts, contexts)
        migrate_context.assert_has_calls([call(c, 0) for c in contexts])

        # Settings with version
        contexts = [DummyContext(version=i) for i in range(1, 4)]
        migrate_context.reset_mock()
        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.read_defaults_file(create_defaults_file(contexts))
        self.assertSequenceEqual(handler.global_contexts, contexts)
        migrate_context.assert_has_calls(
            [call(c, c.values[VERSION_KEY]) for c in contexts])
Exemplo n.º 22
0
    def test_migrates_settings_removes_incompatible(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)

        widget = SimpleWidget()

        contexts = [Context(foo=i) for i in (13, 13, 0, 1, 13, 2, 13)]

        def migrate_context(context, _):
            if context.foo == 13:
                raise IncompatibleContext()

        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.initialize(widget, dict(context_settings=contexts))
            contexts = widget.context_settings
            self.assertEqual(len(contexts), 3)
            self.assertTrue(
                all(context.foo == i for i, context in enumerate(contexts)))
Exemplo n.º 23
0
    def test_read_defaults(self):
        contexts = [DummyContext() for _ in range(3)]

        handler = ContextHandler()
        handler.widget_class = SimpleWidget

        # Old settings without version
        migrate_context = Mock()
        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.read_defaults_file(create_defaults_file(contexts))
        self.assertSequenceEqual(handler.global_contexts, contexts)
        migrate_context.assert_has_calls([call(c, 0) for c in contexts])

        # Settings with version
        contexts = [DummyContext(version=i) for i in range(1, 4)]
        migrate_context.reset_mock()
        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.read_defaults_file(create_defaults_file(contexts))
        self.assertSequenceEqual(handler.global_contexts, contexts)
        migrate_context.assert_has_calls([call(c, c.values[VERSION_KEY]) for c in contexts])
Exemplo n.º 24
0
 def open_context(self, widget, domain):
     ContextHandler.open_context(self, widget, domain,
                                 *self.encode_domain(domain))