]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- call it 0.4.0
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Sep 2012 16:24:43 +0000 (12:24 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Sep 2012 16:24:43 +0000 (12:24 -0400)
- [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

CHANGES
alembic/__init__.py
alembic/config.py

diff --git a/CHANGES b/CHANGES
index 7f37ef89beda46459b479fb95f9b0224c5961462..84d5c9202ac3ce2e7a2df8360b88edd5c53ff564 100644 (file)
--- 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.
index 36db19fe6a3b404766af0cbee7b101acb8004f62..5a8fffada9dc6d52d16c42c1773eaec375e31567 100644 (file)
@@ -1,6 +1,6 @@
 from os import path
 
-__version__ = '0.3.7'
+__version__ = '0.4.0'
 
 package_dir = path.abspath(path.dirname(__file__))
 
index 061fdbab80cd02aa9dc1399c07e6adfa2458fbc3..d04cdbfe1603c23ef83208fd56e1ecf91595357e 100644 (file)
@@ -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)
+