~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 20:43:05 UTC
  • mto: This revision was merged to the branch mainline in revision 3789.
  • Revision ID: aaron@aaronbentley.com-20081021204305-4yf2n78r6b61yo91
Enable lazy-loading of commands

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
68
68
 
69
69
class CommandRegistry(registry.Registry):
70
70
 
 
71
    @staticmethod
 
72
    def _get_name(command_name):
 
73
        if command_name.startswith("cmd_"):
 
74
            return _unsquish_command_name(command_name)
 
75
        else:
 
76
            return command_name
 
77
 
71
78
    def register(self, cmd, decorate=False):
72
79
        """Utility function to help register a command
73
80
 
77
84
            Otherwise it is an error to try to override an existing command.
78
85
        """
79
86
        k = cmd.__name__
80
 
        if k.startswith("cmd_"):
81
 
            k_unsquished = _unsquish_command_name(k)
82
 
        else:
83
 
            k_unsquished = k
 
87
        k_unsquished = self._get_name(k)
84
88
        try:
85
89
            previous = self.get(k_unsquished)
86
90
        except KeyError:
97
101
                            sys.modules[previous.__module__])
98
102
        return previous
99
103
 
 
104
    def register_lazy(self, command_name, aliases, module_name):
 
105
        """Register a command without loading its module.
 
106
 
 
107
        :param command_name: The primary name of the command.
 
108
        :param aliases: A list of aliases for the command.
 
109
        :module_name: The module that the command lives in.
 
110
        """
 
111
        key = self._get_name(command_name)
 
112
        registry.Registry.register_lazy(self, key, module_name, command_name,
 
113
                                        info=CommandInfo(aliases))
 
114
 
100
115
 
101
116
plugin_cmds = CommandRegistry()
102
117