~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Aaron Bentley
  • Date: 2008-10-21 14:08:31 UTC
  • mto: This revision was merged to the branch mainline in revision 3789.
  • Revision ID: aaron@aaronbentley.com-20081021140831-a8sqdr5sg8y82z4e
Switch from dict to Registry for plugin_cmds

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
from bzrlib.option import Option
54
54
 
55
55
 
56
 
plugin_cmds = {}
 
56
class CommandRegistry(registry.Registry):
 
57
 
 
58
    def register(self, cmd, decorate=False):
 
59
        """Utility function to help register a command
 
60
 
 
61
        :param cmd: Command subclass to register
 
62
        :param decorate: If true, allow overriding an existing command
 
63
            of the same name; the old command is returned by this function.
 
64
            Otherwise it is an error to try to override an existing command.
 
65
        """
 
66
        k = cmd.__name__
 
67
        if k.startswith("cmd_"):
 
68
            k_unsquished = _unsquish_command_name(k)
 
69
        else:
 
70
            k_unsquished = k
 
71
        try:
 
72
            previous = self.get(k_unsquished)
 
73
        except KeyError:
 
74
            previous = _builtin_commands().get(k_unsquished)
 
75
        try:
 
76
            registry.Registry.register(self, k_unsquished, cmd,
 
77
                                       override_existing=decorate)
 
78
        except KeyError:
 
79
            trace.log_error('Two plugins defined the same command: %r' % k)
 
80
            trace.log_error('Not loading the one in %r' %
 
81
                            sys.modules[cmd.__module__])
 
82
            trace.log_error('Previously this command was registered from %r' %
 
83
                            sys.modules[previous.__module__])
 
84
        return previous
 
85
 
 
86
 
 
87
plugin_cmds = CommandRegistry()
57
88
 
58
89
 
59
90
def register_command(cmd, decorate=False):
60
 
    """Utility function to help register a command
61
 
 
62
 
    :param cmd: Command subclass to register
63
 
    :param decorate: If true, allow overriding an existing command
64
 
        of the same name; the old command is returned by this function.
65
 
        Otherwise it is an error to try to override an existing command.
66
 
    """
67
91
    global plugin_cmds
68
 
    k = cmd.__name__
69
 
    if k.startswith("cmd_"):
70
 
        k_unsquished = _unsquish_command_name(k)
71
 
    else:
72
 
        k_unsquished = k
73
 
    if k_unsquished not in plugin_cmds:
74
 
        plugin_cmds[k_unsquished] = cmd
75
 
        ## trace.mutter('registered plugin command %s', k_unsquished)
76
 
        if decorate and k_unsquished in builtin_command_names():
77
 
            return _builtin_commands()[k_unsquished]
78
 
    elif decorate:
79
 
        result = plugin_cmds[k_unsquished]
80
 
        plugin_cmds[k_unsquished] = cmd
81
 
        return result
82
 
    else:
83
 
        trace.log_error('Two plugins defined the same command: %r' % k)
84
 
        trace.log_error('Not loading the one in %r' % sys.modules[cmd.__module__])
85
 
        trace.log_error('Previously this command was registered from %r' %
86
 
                        sys.modules[plugin_cmds[k_unsquished].__module__])
 
92
    return plugin_cmds.register(cmd, decorate)
87
93
 
88
94
 
89
95
def _squish_command_name(cmd):
118
124
    """Return name->class mapping for all commands."""
119
125
    d = _builtin_commands()
120
126
    if plugins_override:
121
 
        d.update(plugin_cmds)
 
127
        d.update(plugin_cmds.iteritems())
122
128
    return d
123
129
 
124
130