Christophe (OpenERP) has proposed merging
lp:~openerp-dev/openerp-command/auto-discovery-chs into lp:openerp-command.
Requested reviews:
OpenERP Core Team (openerp)
For more details, see:
https://code.launchpad.net/~openerp-dev/openerp-command/auto-discovery-chs/+merge/136360
--
https://code.launchpad.net/~openerp-dev/openerp-command/auto-discovery-chs/+merge/136360
Your team OpenERP R&D Team is subscribed to branch
lp:~openerp-dev/openerp-command/auto-discovery-chs.
=== modified file 'oe'
--- oe 2012-01-31 17:55:23 +0000
+++ oe 2012-11-27 10:39:21 +0000
@@ -1,5 +1,6 @@
#! /usr/bin/env python2
if __name__ == '__main__':
+ import sys
import openerpcommand.main
- openerpcommand.main.run()
+ sys.exit(openerpcommand.main.run())
=== modified file 'openerpcommand/__init__.py'
--- openerpcommand/__init__.py 2012-11-05 11:27:01 +0000
+++ openerpcommand/__init__.py 2012-11-27 10:39:21 +0000
@@ -58,4 +58,25 @@
# Client-side commands. TODO one per .py file.
for x in command_list_client:
x(subparsers)
+
+ from . import commands # auto import commands
+
+ def register_classes(classes):
+ abstracts = set()
+ for class_ in classes:
+ # check if class is abstract
+ attrs = [getattr(class_, attr) for attr in dir(class_) if not attr.startswith('__')]
+ if any(getattr(attr, '__isabstractmethod__', False) for attr in attrs):
+ abstracts.add(class_)
+ else:
+ subparser = subparsers.add_parser(class_.__name__.lower(), description=class_.__doc__)
+ class_.configure_parser(subparser)
+ subparser.set_defaults(run=class_._run)
+
+ for abstract in abstracts:
+ register_classes(abstract.__subclasses__())
+
+ from . import command
+ register_classes(command.Command.__subclasses__())
+
return parser
=== added file 'openerpcommand/command.py'
--- openerpcommand/command.py 1970-01-01 00:00:00 +0000
+++ openerpcommand/command.py 2012-11-27 10:39:21 +0000
@@ -0,0 +1,48 @@
+import abc
+import os
+
+def required_or_default(name, h):
+ """
+ Helper to define `argparse` arguments. If the name is the environment,
+ the argument is optional and draw its value from the environment if not
+ supplied on the command-line. If it is not in the environment, make it
+ a mandatory argument.
+ """
+ if os.environ.get('OPENERP_' + name.upper()):
+ d = {'default': os.environ['OPENERP_' + name.upper()]}
+ else:
+ d = {'required': True}
+ d['help'] = h + '. The environment variable OPENERP_' + name.upper() + ' can be used instead.'
+ return d
+
+class Command(object):
+ __metaclass__ = abc.ABCMeta
+
+ @classmethod
+ def _run(cls, args):
+ c = cls()
+ c.run(args)
+
+ @abc.abstractmethod
+ def run(self, args):
+ raise NotImplementedError()
+
+ @classmethod
+ def configure_parser(cls, parser):
+ parser.add_argument('-d', '--database', metavar='DATABASE',
+ **required_or_default('DATABASE', 'the database to connect to'))
+ parser.add_argument('-u', '--user', metavar='USER',
+ **required_or_default('USER', 'the user login or ID. When using '
+ 'RPC, providing an ID avoid the login() step'))
+ parser.add_argument('-p', '--password', metavar='PASSWORD',
+ **required_or_default('PASSWORD', 'the user password')) # TODO read it from the command line or from file.
+
+
+class RemoteCommand(Command):
+ @classmethod
+ def configure_parser(cls, parser):
+ parser.add_argument('-H', '--host', metavar='HOST',
+ **required_or_default('HOST', 'the server host'))
+ parser.add_argument('-P', '--port', metavar='PORT',
+ **required_or_default('PORT', 'the server port'))
+ super(RemoteCommand, cls).configure_parser(parser)
=== added directory 'openerpcommand/commands'
=== added file 'openerpcommand/commands/__init__.py'
--- openerpcommand/commands/__init__.py 1970-01-01 00:00:00 +0000
+++ openerpcommand/commands/__init__.py 2012-11-27 10:39:21 +0000
@@ -0,0 +1,13 @@
+import os
+import imp
+
+HERE = os.path.dirname(__file__)
+
+# XXX also search commands in other directory? (/usr/share, ~/.oe/) ?
+
+for x in os.listdir(HERE):
+ if not x.endswith('.py') or x.startswith(('_', '.')):
+ continue
+ m, _ = os.path.splitext(x)
+ i = imp.find_module(m, [HERE])
+ imp.load_module('openerpcommand.commands.' + m, *i)
=== added file 'openerpcommand/commands/test.py'
--- openerpcommand/commands/test.py 1970-01-01 00:00:00 +0000
+++ openerpcommand/commands/test.py 2012-11-27 10:39:21 +0000
@@ -0,0 +1,12 @@
+
+from .. import command
+
+class Test(command.Command):
+ def run(self, args):
+ print "test is run"
+
+ @property
+ def name(self):
+ return "test"
+
+print "test imported"
_______________________________________________
Mailing list: https://launchpad.net/~openerp-dev-gtk
Post to : [email protected]
Unsubscribe : https://launchpad.net/~openerp-dev-gtk
More help : https://help.launchpad.net/ListHelp