summaryrefslogtreecommitdiff
path: root/alembic/__init__.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-04-28 15:47:51 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2010-04-28 15:47:51 -0400
commit54835cc4cd9c5ac65db888b5ec801f9fb12e29cd (patch)
tree709503a00dc53cd33efb7cb4a4c9be4d898555a1 /alembic/__init__.py
parent92e8f58ffc8709f332d29a7bc75dad212605377a (diff)
downloadalembic-54835cc4cd9c5ac65db888b5ec801f9fb12e29cd.tar.gz
- pushed the whole command line options thing into a single main() function
- command methods now accept a "config" plus positional + kw arguments, for easy API calling - main() provides context sensitive arguments and help for each command using inspect.getargspec()
Diffstat (limited to 'alembic/__init__.py')
-rw-r--r--alembic/__init__.py80
1 files changed, 71 insertions, 9 deletions
diff --git a/alembic/__init__.py b/alembic/__init__.py
index 649da28..568b000 100644
--- a/alembic/__init__.py
+++ b/alembic/__init__.py
@@ -1,18 +1,80 @@
-from alembic import options, command
+from alembic import config, command, util
+from optparse import OptionParser
+import inspect
+import os
__version__ = '0.1alpha'
-
-
def main(argv):
- parser = options.get_option_parser()
+ # TODO:
+ # OK, what's the super option parser library that
+ # allows <command> plus command-specfic sub-options,
+ # and derives everything from callables ?
+ # we're inventing here a bit.
+
+ commands = dict([
+ (fn.__name__, fn) for fn in
+ [getattr(command, n) for n in dir(command)]
+ if inspect.isfunction(fn) and
+ fn.__name__[0] != '_' and
+ fn.__module__ == 'alembic.command'
+ ])
+
+ parser = OptionParser(
+ "usage: %prog [options] <command> [command arguments]\n\n"
+ "Available Commands:\n" +
+ "\n".join(sorted([
+ util.format_opt(fn.__name__.replace('_', '-'), fn.__doc__)
+ for fn in commands.values()
+ ]))
+ )
+ parser.add_option("-c", "--config",
+ type="string",
+ default="alembic.ini",
+ help="Alternate config file")
+ parser.add_option("-t", "--template",
+ default='generic',
+ type="string",
+ help="Setup template for use with 'init'")
+ parser.add_option("-m", "--message",
+ type="string",
+ help="Message string to use with 'revision'")
+
+ cmd_line_options, cmd_line_args = parser.parse_args(argv[1:])
+
+ if len(cmd_line_args) < 1:
+ util.err("no command specified")
+
+ cmd = cmd_line_args.pop(0).replace('-', '_')
+
+ try:
+ cmd_fn = commands[cmd]
+ except KeyError:
+ util.err("no such command %r" % cmd)
+
+ spec = inspect.getargspec(cmd_fn)
+ if spec[3]:
+ positional = spec[0][1:-len(spec[3])]
+ kwarg = spec[0][-len(spec[3]):]
+ else:
+ positional = spec[0][1:]
+ kwarg = []
+
+ kw = dict(
+ (k, getattr(cmd_line_options, k))
+ for k in kwarg
+ )
+
+ if len(cmd_line_args) != len(positional):
+ util.err("Usage: %s %s [options] %s" % (
+ os.path.basename(argv[0]),
+ cmd,
+ " ".join(["<%s>" % p for p in positional])
+ ))
- opt = options.Options(parser, argv)
- cmd = opt.get_command().replace('-', '_')
- if cmd not in dir(command):
- parser.error("no such command %r" % cmd)
- getattr(command, cmd)(opt)
+ cfg = config.Config(cmd_line_options.config)
+ cmd_fn(cfg, *cmd_line_args, **kw)