[pitivi] shortcuts: Make newly added shortcuts available



commit 8b3a691a39c1fd512727be26d9793129290f2676
Author: HarishFulara07 <harish14143 iiitd ac in>
Date:   Wed Jun 20 02:32:09 2018 +0530

    shortcuts: Make newly added shortcuts available
    
    Closes #2211

 pitivi/shortcuts.py     | 13 +++++++------
 tests/test_shortcuts.py | 21 ++++++++++++---------
 2 files changed, 19 insertions(+), 15 deletions(-)
---
diff --git a/pitivi/shortcuts.py b/pitivi/shortcuts.py
index 1ec1d6b2..f91b0bba 100644
--- a/pitivi/shortcuts.py
+++ b/pitivi/shortcuts.py
@@ -43,7 +43,7 @@ class ShortcutsManager(GObject.Object):  # pylint: disable=too-many-instance-att
         self.titles = {}
         self.config_path = os.path.sep.join([xdg_config_home(),
                                              "shortcuts.conf"])
-        self.__loaded = self.__load()
+        self.__loaded_actions = list(self.__load())
 
     @property
     def groups(self):
@@ -53,17 +53,17 @@ class ShortcutsManager(GObject.Object):  # pylint: disable=too-many-instance-att
     def __load(self):
         """Loads the shortcuts from the config file and sets them.
 
-        Returns:
-            bool: Whether the config file exists.
+        Yields:
+            string: The shortcuts loaded from the config file.
         """
         if not os.path.isfile(self.config_path):
-            return False
+            return
 
         for line in open(self.config_path, "r"):
             action_name, accelerators = line.split(":", 1)
             accelerators = accelerators.strip("\n").split(",")
             self.app.set_accels_for_action(action_name, accelerators)
-        return True
+            yield action_name
 
     def save(self):
         """Saves the accelerators for each action to the config file.
@@ -92,7 +92,7 @@ class ShortcutsManager(GObject.Object):  # pylint: disable=too-many-instance-att
         """
         self.default_accelerators[action] = accelerators
         self.titles[action] = title
-        if not self.__loaded:
+        if action not in self.__loaded_actions:
             self.app.set_accels_for_action(action, accelerators)
 
         if title:
@@ -163,6 +163,7 @@ class ShortcutsManager(GObject.Object):  # pylint: disable=too-many-instance-att
         self.__groups.append((position, action_prefix))
         self.__groups.sort()
 
+    # pylint: disable=redefined-argument-from-local
     def reset_accels(self, action=None):
         """Resets accelerators to their default values.
 
diff --git a/tests/test_shortcuts.py b/tests/test_shortcuts.py
index 5100fd71..49c10ad4 100644
--- a/tests/test_shortcuts.py
+++ b/tests/test_shortcuts.py
@@ -17,7 +17,6 @@
 # Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 # Boston, MA 02110-1301, USA.
 """Test the keyboard shortcuts."""
-import os
 import tempfile
 from unittest import mock
 
@@ -71,17 +70,21 @@ class TestShortcutsManager(common.TestCase):
             manager.register_group("general", "General group", position=0)
             manager.add("prefix.action1", ["<Primary>P"], "Action one")
             self.assertEqual(app.set_accels_for_action.call_count, 1)
+            # Save the shortcut to the config file.
+            manager.save()
+            app.set_accels_for_action.reset_mock()
 
-            # Create the temporary shortcuts.conf file
-            # and test that add is not calling set_accels_for_action()
-            open(os.path.sep.join([temp_dir, "shortcuts.conf"]), "w").close()
             manager2 = ShortcutsManager(app)
+            # Previously saved shortcut is read from the config file
+            # and 'set_accels_for_action' is called.
+            self.assertEqual(app.set_accels_for_action.call_count, 1)
             manager2.register_group("other", "Other group", position=0)
-
-            manager2.add("prefix.action4", ["<Primary>W"],
-                         "Action addition not invoking set_accels_for_action")
-            # No. of calls to set_accels_for_action should be unchanged now
-            # because calling set_accels_for_action isn't allowed with .conf existing
+            app.set_accels_for_action.reset_mock()
+            manager2.add("prefix.action1", ["<Primary>P"], "Action one")
+            # Above shortcut has been already loaded from the config file
+            # and hence 'set_accels_for_action' is not called.
+            self.assertEqual(app.set_accels_for_action.call_count, 0)
+            manager2.add("prefix.action2", ["<Primary>W"], "Action two")
             self.assertEqual(app.set_accels_for_action.call_count, 1)
 
     def test_load_save(self):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]