[IMP] cli first command testjs
[odoo/odoo.git] / openerp / cli / __init__.py
1 import logging
2 import sys
3
4 import openerp
5 from openerp import tools
6 from openerp.modules import module
7
8 _logger = logging.getLogger(__name__)
9
10 commands = {}
11
12 class CommandType(type):
13     def __init__(cls, name, bases, attrs):
14         super(CommandType, cls).__init__(name, bases, attrs)
15         name = getattr(cls, name, cls.__name__.lower())
16         cls.name = name
17         if name != 'command':
18             commands[name] = cls
19
20 class Command(object):
21     """Subclass this class to define new openerp subcommands """
22     __metaclass__ = CommandType
23
24     def run(self, args):
25         pass
26
27 class Help(Command):
28     def run(self, args):
29         print "Available commands:\n"
30         for k, v in commands.items():
31             print "    %s" % k
32
33 import server
34
35 def main():
36     args = sys.argv[1:]
37
38     # The only shared option is '--addons-path=' needed to discover additional
39     # commands from modules
40     if len(args) > 1 and args[0].startswith('--addons-path=') and not args[1].startswith("-"):
41         tools.config.parse_config([args[0]])
42         args = args[1:]
43
44     # Default legacy command
45     command = "server"
46
47     # Subcommand discovery
48     if len(args) and not args[0].startswith("-"):
49         for m in module.get_modules():
50             m = 'openerp.addons.' + m
51             __import__(m)
52             #try:
53             #except Exception, e:
54             #    raise
55             #    print e
56         command = args[0]
57         args = args[1:]
58
59     if command in commands:
60         o = commands[command]()
61         o.run(args)
62
63 # vim:et:ts=4:sw=4: