22
33"""Provides a base class for registering commands and filters."""
44
5+ # =============================================================================
6+ # >> IMPORTS
7+ # =============================================================================
8+ # Python
9+ from collections import defaultdict
10+
511
612# =============================================================================
713# >> CLASSES
@@ -18,7 +24,7 @@ class _BaseCommandManager(object):
1824 def __init__ (self ):
1925 """Initialize the command manager."""
2026 # This will store all created callback proxies
21- self ._callback_proxies = []
27+ self ._callback_proxies = defaultdict ( list )
2228
2329 def _prepare_command_names (self , names ):
2430 """Validate and prepare the given command names.
@@ -42,15 +48,17 @@ def _prepare_command_names(self, names):
4248
4349 return tuple (names )
4450
45- def _get_command_proxy (self , callback ):
51+ def _get_command_proxy (self , name , callback ):
4652 """Return the command proxy for the given callback.
4753
54+ :param str name:
55+ Name of the command.
4856 :param callable callback:
4957 The callback that should be used to search the proxy.
5058 :raise ValueError:
5159 Raised if the proxy wasn't found.
5260 """
53- for proxy in self ._callback_proxies :
61+ for proxy in self ._callback_proxies . get ( name , ()) :
5462 if proxy .callback == callback :
5563 return proxy
5664
@@ -73,14 +81,15 @@ def register_commands(self, names, callback, *args, **kwargs):
7381 """
7482 names = self ._prepare_command_names (names )
7583
76- if self ._callback_proxy is not None :
77- # Create a new callback proxy for this callback
78- callback = self ._callback_proxy (callback , * args , ** kwargs )
79- self ._callback_proxies .append (callback )
84+ if self ._callback_proxy is None :
85+ for name in names :
86+ self ._register_command (name , callback , * args , ** kwargs )
87+ else :
88+ proxy = self ._callback_proxy (callback , * args , ** kwargs )
8089
81- # Register all command names
82- for name in names :
83- self ._register_command (name , callback , * args , ** kwargs )
90+ for name in names :
91+ self . _callback_proxies [ name ]. append ( proxy )
92+ self ._register_command (name , proxy , * args , ** kwargs )
8493
8594 def _register_command (self , name , callback , * args , ** kwargs ):
8695 """Register a command.
@@ -106,10 +115,25 @@ def unregister_commands(self, names, callback):
106115 """
107116 names = self ._prepare_command_names (names )
108117
109- if self ._callback_proxy is not None :
110- callback = self ._get_command_proxy (callback )
111- self ._callback_proxies .remove (callback )
118+ if self ._callback_proxy is None :
119+ for name in names :
120+ self ._unregister_command (name , callback )
121+ else :
122+ for name in names :
123+ proxy = self ._get_command_proxy (name , callback )
124+ self ._callback_proxies [name ].remove (proxy )
125+
126+ if not self ._callback_proxies [name ]:
127+ del self ._callback_proxies [name ]
112128
113- # Unregister all command names
114- for name in names :
115- self ._get_command (name ).remove_callback (callback )
129+ self ._unregister_command (name , proxy )
130+
131+ def _unregister_command (self , name , callback ):
132+ """Unregister a command.
133+
134+ :param str name:
135+ Name of the command to unregister.
136+ :param callable callback:
137+ The callback that is assigned to the command.
138+ """
139+ self ._get_command (name ).remove_callback (callback )
0 commit comments