diff options
| author | Raphael Collet <rco@openerp.com> | 2016-09-02 15:58:39 +0200 |
|---|---|---|
| committer | Raphael Collet <rco@openerp.com> | 2016-09-02 17:28:12 +0200 |
| commit | 9e64f9f9514194d74e166b032add8511182f2677 (patch) | |
| tree | 5086710c04c6b923be18a6d1d97b3f0f3a2a9c4a /odoo/cli/command.py | |
| parent | 11812b0b9e82b2b1b9041d8250ce01e7b4a5f7ee (diff) | |
[REF] openerp: move `openerp` to `odoo`
Diffstat (limited to 'odoo/cli/command.py')
| -rw-r--r-- | odoo/cli/command.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/odoo/cli/command.py b/odoo/cli/command.py new file mode 100644 index 00000000000..2f2fffa18b4 --- /dev/null +++ b/odoo/cli/command.py @@ -0,0 +1,64 @@ +import logging +import sys +import os +from os.path import join as joinpath, isdir + +import openerp +from openerp.modules import get_modules, get_module_path + +commands = {} + +class CommandType(type): + def __init__(cls, name, bases, attrs): + super(CommandType, cls).__init__(name, bases, attrs) + name = getattr(cls, name, cls.__name__.lower()) + cls.name = name + if name != 'command': + commands[name] = cls + +class Command(object): + """Subclass this class to define new openerp subcommands """ + __metaclass__ = CommandType + + def run(self, args): + pass + +class Help(Command): + """Display the list of available commands""" + def run(self, args): + print "Available commands:\n" + names = commands.keys() + padding = max([len(k) for k in names]) + 2 + for k in sorted(names): + name = k.ljust(padding, ' ') + doc = (commands[k].__doc__ or '').strip() + print " %s%s" % (name, doc) + print "\nUse '%s <command> --help' for individual command help." % sys.argv[0].split(os.path.sep)[-1] + +def main(): + args = sys.argv[1:] + + # The only shared option is '--addons-path=' needed to discover additional + # commands from modules + if len(args) > 1 and args[0].startswith('--addons-path=') and not args[1].startswith("-"): + # parse only the addons-path, do not setup the logger... + openerp.tools.config._parse_config([args[0]]) + args = args[1:] + + # Default legacy command + command = "server" + + # TODO: find a way to properly discover addons subcommands without importing the world + # Subcommand discovery + if len(args) and not args[0].startswith("-"): + logging.disable(logging.CRITICAL) + for module in get_modules(): + if isdir(joinpath(get_module_path(module), 'cli')): + __import__('openerp.addons.' + module) + logging.disable(logging.NOTSET) + command = args[0] + args = args[1:] + + if command in commands: + o = commands[command]() + o.run(args) |
