Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff -r 83c89768016d alembic/config.py
- --- a/alembic/config.py Wed Aug 22 21:43:45 2012 -0400
- +++ b/alembic/config.py Tue Aug 28 10:00:27 2012 -0400
- @@ -148,85 +148,99 @@
- return self.get_section_option(self.config_ini_section, name, default)
- +class CommandLine(object):
- + def __init__(self, prog=None, config_file=True):
- + self._generate_args(prog, config_file)
- +
- +
- + def _generate_args(self, prog, config_file):
- + 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))
- + 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.")
- + CommandLine(prog=prog).main(argv=argv)
- -
- - # 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))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement