From: Mike Bayer Date: Sun, 30 Sep 2012 16:24:43 +0000 (-0400) Subject: - call it 0.4.0 X-Git-Tag: rel_0_4_0~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=35ef4896bf2db3bf754e6fc4839cc3ea880ba1b7;p=thirdparty%2Fsqlalchemy%2Falembic.git - call it 0.4.0 - [feature] The command line runner has been organized into a reusable CommandLine object, so that other front-ends can re-use the argument parsing built in. #70 --- diff --git a/CHANGES b/CHANGES index 7f37ef89..84d5c920 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,10 @@ -0.3.7 +0.4.0 ===== +- [feature] The command line runner has been organized + into a reusable CommandLine object, so that other + front-ends can re-use the argument parsing built + in. #70 + - [bug] Fixed MySQL rendering for server_default which didn't work if the server_default was a generated SQL expression. Courtesy Moriyoshi Koizumi. diff --git a/alembic/__init__.py b/alembic/__init__.py index 36db19fe..5a8fffad 100644 --- a/alembic/__init__.py +++ b/alembic/__init__.py @@ -1,6 +1,6 @@ from os import path -__version__ = '0.3.7' +__version__ = '0.4.0' package_dir = path.abspath(path.dirname(__file__)) diff --git a/alembic/config.py b/alembic/config.py index 061fdbab..d04cdbfe 100644 --- a/alembic/config.py +++ b/alembic/config.py @@ -148,85 +148,97 @@ class Config(object): return self.get_section_option(self.config_ini_section, name, default) +class CommandLine(object): + def __init__(self, prog=None): + self._generate_args(prog) + + + def _generate_args(self, prog): + def add_options(parser, positional, kwargs): + if 'template' in kwargs: + parser.add_argument("-t", "--template", + default='generic', + type=str, + help="Setup template for use with 'init'") + if 'message' in kwargs: + parser.add_argument("-m", "--message", + type=str, + help="Message string to use with 'revision'") + if 'sql' in kwargs: + parser.add_argument("--sql", + action="store_true", + help="Don't emit SQL to database - dump to " + "standard output/file instead") + if 'tag' in kwargs: + parser.add_argument("--tag", + type=str, + help="Arbitrary 'tag' name - can be used by " + "custom env.py scripts.") + if 'autogenerate' in kwargs: + parser.add_argument("--autogenerate", + action="store_true", + help="Populate revision script with candidate " + "migration operations, based on comparison " + "of database to model.") + + positional_help = { + 'directory': "location of scripts directory", + 'revision': "revision identifier" + } + for arg in positional: + subparser.add_argument(arg, help=positional_help.get(arg)) + + parser = ArgumentParser(prog=prog) + parser.add_argument("-c", "--config", + type=str, + default="alembic.ini", + help="Alternate config file") + parser.add_argument("-n", "--name", + type=str, + default="alembic", + help="Name of section in .ini file to " + "use for Alembic config") + subparsers = parser.add_subparsers() + + for fn in [getattr(command, n) for n in dir(command)]: + if inspect.isfunction(fn) and \ + fn.__name__[0] != '_' and \ + fn.__module__ == 'alembic.command': + + spec = inspect.getargspec(fn) + if spec[3]: + positional = spec[0][1:-len(spec[3])] + kwarg = spec[0][-len(spec[3]):] + else: + positional = spec[0][1:] + kwarg = [] + + subparser = subparsers.add_parser( + fn.__name__, + help=fn.__doc__) + add_options(subparser, positional, kwarg) + subparser.set_defaults(cmd=(fn, positional, kwarg)) + self.parser = parser + + def run_cmd(self, config, options): + fn, positional, kwarg = options.cmd + + try: + fn(config, + *[getattr(options, k) for k in positional], + **dict((k, getattr(options, k)) for k in kwarg) + ) + except util.CommandError, e: + util.err(str(e)) + + def main(self, argv=None): + options = self.parser.parse_args(argv) + + cfg = Config(options.config, options.name) + self.run_cmd(cfg, options) + def main(argv=None, prog=None, **kwargs): """The console runner function for Alembic.""" - def add_options(parser, positional, kwargs): - if 'template' in kwargs: - parser.add_argument("-t", "--template", - default='generic', - type=str, - help="Setup template for use with 'init'") - if 'message' in kwargs: - parser.add_argument("-m", "--message", - type=str, - help="Message string to use with 'revision'") - if 'sql' in kwargs: - parser.add_argument("--sql", - action="store_true", - help="Don't emit SQL to database - dump to " - "standard output/file instead") - if 'tag' in kwargs: - parser.add_argument("--tag", - type=str, - help="Arbitrary 'tag' name - can be used by " - "custom env.py scripts.") - if 'autogenerate' in kwargs: - parser.add_argument("--autogenerate", - action="store_true", - help="Populate revision script with candidate " - "migration operations, based on comparison of database to model.") - - - # TODO: - # --dialect - name of dialect when --sql mode is set - *no DB connections - # should occur, add this to env.py templates as a conditional* - positional_help = { - 'directory':"location of scripts directory", - 'revision':"revision identifier" - } - for arg in positional: - subparser.add_argument(arg, help=positional_help.get(arg)) - - parser = ArgumentParser(prog=prog) - parser.add_argument("-c", "--config", - type=str, - default="alembic.ini", - help="Alternate config file") - parser.add_argument("-n", "--name", - type=str, - default="alembic", - help="Name of section in .ini file to use for Alembic config") - subparsers = parser.add_subparsers() - - for fn in [getattr(command, n) for n in dir(command)]: - if inspect.isfunction(fn) and \ - fn.__name__[0] != '_' and \ - fn.__module__ == 'alembic.command': - - spec = inspect.getargspec(fn) - if spec[3]: - positional = spec[0][1:-len(spec[3])] - kwarg = spec[0][-len(spec[3]):] - else: - positional = spec[0][1:] - kwarg = [] - - subparser = subparsers.add_parser( - fn.__name__, - help=fn.__doc__) - add_options(subparser, positional, kwarg) - subparser.set_defaults(cmd=(fn, positional, kwarg)) - - options = parser.parse_args(argv) - - fn, positional, kwarg = options.cmd - - cfg = Config(options.config, options.name) - try: - fn(cfg, - *[getattr(options, k) for k in positional], - **dict((k, getattr(options, k)) for k in kwarg) - ) - except util.CommandError, e: - util.err(str(e)) + CommandLine(prog=prog).main(argv=argv) +