Advertisement
Guest User

Untitled

a guest
Aug 28th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 7.62 KB | None | 0 0
  1. diff -r 83c89768016d alembic/config.py
  2. --- a/alembic/config.py Wed Aug 22 21:43:45 2012 -0400
  3. +++ b/alembic/config.py Tue Aug 28 10:00:27 2012 -0400
  4. @@ -148,85 +148,99 @@
  5.          return self.get_section_option(self.config_ini_section, name, default)
  6.  
  7.  
  8. +class CommandLine(object):
  9. +    def __init__(self, prog=None, config_file=True):
  10. +        self._generate_args(prog, config_file)
  11. +
  12. +
  13. +    def _generate_args(self, prog, config_file):
  14. +        def add_options(parser, positional, kwargs):
  15. +            if 'template' in kwargs:
  16. +                parser.add_argument("-t", "--template",
  17. +                                default='generic',
  18. +                                type=str,
  19. +                                help="Setup template for use with 'init'")
  20. +            if 'message' in kwargs:
  21. +                parser.add_argument("-m", "--message",
  22. +                                type=str,
  23. +                                help="Message string to use with 'revision'")
  24. +            if 'sql' in kwargs:
  25. +                parser.add_argument("--sql",
  26. +                                action="store_true",
  27. +                                help="Don't emit SQL to database - dump to "
  28. +                                        "standard output/file instead")
  29. +            if 'tag' in kwargs:
  30. +                parser.add_argument("--tag",
  31. +                                type=str,
  32. +                                help="Arbitrary 'tag' name - can be used by "
  33. +                                "custom env.py scripts.")
  34. +            if 'autogenerate' in kwargs:
  35. +                parser.add_argument("--autogenerate",
  36. +                                action="store_true",
  37. +                                help="Populate revision script with candidate "
  38. +                                "migration operations, based on comparison of database to model.")
  39. +
  40. +
  41. +            # TODO:
  42. +            # --dialect - name of dialect when --sql mode is set - *no DB connections
  43. +            # should occur, add this to env.py templates as a conditional*
  44. +            positional_help = {
  45. +                'directory': "location of scripts directory",
  46. +                'revision': "revision identifier"
  47. +            }
  48. +            for arg in positional:
  49. +                subparser.add_argument(arg, help=positional_help.get(arg))
  50. +
  51. +        parser = ArgumentParser(prog=prog)
  52. +        parser.add_argument("-c", "--config",
  53. +                            type=str,
  54. +                            default="alembic.ini",
  55. +                            help="Alternate config file")
  56. +        parser.add_argument("-n", "--name",
  57. +                            type=str,
  58. +                            default="alembic",
  59. +                            help="Name of section in .ini file to use for Alembic config")
  60. +        subparsers = parser.add_subparsers()
  61. +
  62. +        for fn in [getattr(command, n) for n in dir(command)]:
  63. +            if inspect.isfunction(fn) and \
  64. +                fn.__name__[0] != '_' and \
  65. +                fn.__module__ == 'alembic.command':
  66. +
  67. +                spec = inspect.getargspec(fn)
  68. +                if spec[3]:
  69. +                    positional = spec[0][1:-len(spec[3])]
  70. +                    kwarg = spec[0][-len(spec[3]):]
  71. +                else:
  72. +                    positional = spec[0][1:]
  73. +                    kwarg = []
  74. +
  75. +                subparser = subparsers.add_parser(
  76. +                                    fn.__name__,
  77. +                                    help=fn.__doc__)
  78. +                add_options(subparser, positional, kwarg)
  79. +                subparser.set_defaults(cmd=(fn, positional, kwarg))
  80. +        self.parser = parser
  81. +
  82. +    def run_cmd(self, config, options):
  83. +        fn, positional, kwarg = options.cmd
  84. +
  85. +        try:
  86. +            fn(config,
  87. +                        *[getattr(options, k) for k in positional],
  88. +                        **dict((k, getattr(options, k)) for k in kwarg)
  89. +                    )
  90. +        except util.CommandError, e:
  91. +            util.err(str(e))
  92. +
  93. +    def main(self, argv=None):
  94. +        options = self.parser.parse_args(argv)
  95. +
  96. +        cfg = Config(options.config, options.name)
  97. +        self.run_cmd(cfg, options)
  98. +
  99.  def main(argv=None, prog=None, **kwargs):
  100.      """The console runner function for Alembic."""
  101.  
  102. -    def add_options(parser, positional, kwargs):
  103. -        if 'template' in kwargs:
  104. -            parser.add_argument("-t", "--template",
  105. -                            default='generic',
  106. -                            type=str,
  107. -                            help="Setup template for use with 'init'")
  108. -        if 'message' in kwargs:
  109. -            parser.add_argument("-m", "--message",
  110. -                            type=str,
  111. -                            help="Message string to use with 'revision'")
  112. -        if 'sql' in kwargs:
  113. -            parser.add_argument("--sql",
  114. -                            action="store_true",
  115. -                            help="Don't emit SQL to database - dump to "
  116. -                                    "standard output/file instead")
  117. -        if 'tag' in kwargs:
  118. -            parser.add_argument("--tag",
  119. -                            type=str,
  120. -                            help="Arbitrary 'tag' name - can be used by "
  121. -                            "custom env.py scripts.")
  122. -        if 'autogenerate' in kwargs:
  123. -            parser.add_argument("--autogenerate",
  124. -                            action="store_true",
  125. -                            help="Populate revision script with candidate "
  126. -                            "migration operations, based on comparison of database to model.")
  127. +    CommandLine(prog=prog).main(argv=argv)
  128.  
  129. -
  130. -        # TODO:
  131. -        # --dialect - name of dialect when --sql mode is set - *no DB connections
  132. -        # should occur, add this to env.py templates as a conditional*
  133. -        positional_help = {
  134. -            'directory':"location of scripts directory",
  135. -            'revision':"revision identifier"
  136. -        }
  137. -        for arg in positional:
  138. -            subparser.add_argument(arg, help=positional_help.get(arg))
  139. -
  140. -    parser = ArgumentParser(prog=prog)
  141. -    parser.add_argument("-c", "--config",
  142. -                        type=str,
  143. -                        default="alembic.ini",
  144. -                        help="Alternate config file")
  145. -    parser.add_argument("-n", "--name",
  146. -                        type=str,
  147. -                        default="alembic",
  148. -                        help="Name of section in .ini file to use for Alembic config")
  149. -    subparsers = parser.add_subparsers()
  150. -
  151. -    for fn in [getattr(command, n) for n in dir(command)]:
  152. -        if inspect.isfunction(fn) and \
  153. -            fn.__name__[0] != '_' and \
  154. -            fn.__module__ == 'alembic.command':
  155. -
  156. -            spec = inspect.getargspec(fn)
  157. -            if spec[3]:
  158. -                positional = spec[0][1:-len(spec[3])]
  159. -                kwarg = spec[0][-len(spec[3]):]
  160. -            else:
  161. -                positional = spec[0][1:]
  162. -                kwarg = []
  163. -
  164. -            subparser =  subparsers.add_parser(
  165. -                                fn.__name__,
  166. -                                help=fn.__doc__)
  167. -            add_options(subparser, positional, kwarg)
  168. -            subparser.set_defaults(cmd=(fn, positional, kwarg))
  169. -
  170. -    options = parser.parse_args(argv)
  171. -
  172. -    fn, positional, kwarg = options.cmd
  173. -
  174. -    cfg = Config(options.config, options.name)
  175. -    try:
  176. -        fn(cfg,
  177. -                    *[getattr(options, k) for k in positional],
  178. -                    **dict((k, getattr(options, k)) for k in kwarg)
  179. -                )
  180. -    except util.CommandError, e:
  181. -        util.err(str(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement