]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- [feature] Added "stdout" option to Config, provides
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Sep 2012 17:09:30 +0000 (13:09 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Sep 2012 17:09:30 +0000 (13:09 -0400)
  control over where the "print" output of commands like
  "history", "init", "current" etc. are sent.  #43

CHANGES
alembic/command.py
alembic/config.py

diff --git a/CHANGES b/CHANGES
index 598c9695d71c20e232fd669b2a9f47e52b2a3be0..5e1265f0fbcf337d76e56d5740f45673db90f86a 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -5,6 +5,10 @@
   front-ends can re-use the argument parsing built
   in.  #70
 
+- [feature] Added "stdout" option to Config, provides
+  control over where the "print" output of commands like
+  "history", "init", "current" etc. are sent.  #43
+
 - [bug] Fixed the "multidb" template which was badly out
   of date.   It now generates revision files using
   the configuration to determine the different
index c6bc3dc4ba9b7a2467d9aa9320ed9062d2f797d4..747744ec6520ffe9ca8ed8ce92027101ed1f718a 100644 (file)
@@ -6,17 +6,17 @@ import os
 def list_templates(config):
     """List available templates"""
 
-    print "Available templates:\n"
+    config.print_stdout("Available templates:\n")
     for tempname in os.listdir(config.get_template_directory()):
         readme = os.path.join(
                         config.get_template_directory(),
                         tempname,
                         'README')
         synopsis = open(readme).next()
-        print "%s - %s" % (tempname, synopsis)
+        config.print_stdout("%s - %s", tempname, synopsis)
 
-    print "\nTemplates are used via the 'init' command, e.g.:"
-    print "\n  alembic init --template pylons ./scripts"
+    config.print_stdout("\nTemplates are used via the 'init' command, e.g.:")
+    config.print_stdout("\n  alembic init --template pylons ./scripts")
 
 def init(config, directory, template='generic'):
     """Initialize a new scripts directory."""
@@ -115,11 +115,11 @@ def upgrade(config, revision, sql=False, tag=None):
     with EnvironmentContext(
         config,
         script,
-        fn = upgrade,
-        as_sql = sql,
-        starting_rev = starting_rev,
-        destination_rev = revision,
-        tag = tag
+        fn=upgrade,
+        as_sql=sql,
+        starting_rev=starting_rev,
+        destination_rev=revision,
+        tag=tag
     ):
         script.run_env()
 
@@ -139,11 +139,11 @@ def downgrade(config, revision, sql=False, tag=None):
     with EnvironmentContext(
         config,
         script,
-        fn = downgrade,
-        as_sql = sql,
-        starting_rev = starting_rev,
-        destination_rev = revision,
-        tag = tag
+        fn=downgrade,
+        as_sql=sql,
+        starting_rev=starting_rev,
+        destination_rev=revision,
+        tag=tag
     ):
         script.run_env()
 
@@ -153,17 +153,17 @@ def history(config):
     script = ScriptDirectory.from_config(config)
     for sc in script.walk_revisions():
         if sc.is_head:
-            print
-        print sc
+            config.print_stdout("")
+        config.print_stdout(sc)
 
 def branches(config):
     """Show current un-spliced branch points"""
     script = ScriptDirectory.from_config(config)
     for sc in script.walk_revisions():
         if sc.is_branch_point:
-            print sc
+            config.print_stdout(sc)
             for rev in sc.nextrev:
-                print "%s -> %s" % (
+                config.print_stdout("%s -> %s",
                     " " * len(str(sc.down_revision)),
                     script.get_revision(rev)
                 )
@@ -173,7 +173,7 @@ def current(config):
 
     script = ScriptDirectory.from_config(config)
     def display_version(rev, context):
-        print "Current revision for %s: %s" % (
+        config.print_stdout("Current revision for %s: %s",
                             util.obfuscate_url_pw(
                                 context.connection.engine.url),
                             script.get_revision(rev))
@@ -182,7 +182,7 @@ def current(config):
     with EnvironmentContext(
         config,
         script,
-        fn = display_version
+        fn=display_version
     ):
         script.run_env()
 
@@ -204,11 +204,11 @@ def stamp(config, revision, sql=False, tag=None):
     with EnvironmentContext(
         config,
         script,
-        fn = do_stamp,
-        as_sql = sql,
-        destination_rev = revision,
-        tag = tag
-    ) as env:
+        fn=do_stamp,
+        as_sql=sql,
+        destination_rev=revision,
+        tag=tag
+    ):
         script.run_env()
 
 def splice(config, parent, child):
index d04cdbfe1603c23ef83208fd56e1ecf91595357e..8c25aeaa5910848569aa07074d3f1ee449a2edba 100644 (file)
@@ -3,6 +3,7 @@ from argparse import ArgumentParser
 import ConfigParser
 import inspect
 import os
+import sys
 
 class Config(object):
     """Represent an Alembic configuration.
@@ -41,16 +42,22 @@ class Config(object):
      .ini file
     :param output_buffer: optional file-like input buffer which
      will be passed to the :class:`.MigrationContext` - used to redirect
-     access when using Alembic programmatically.
+     the output of "offline generation" when using Alembic programmatically.
+    :param stdout: buffer where the "print" output of commands will be sent.
+     Defaults to ``sys.stdout``.
+
+     ..versionadded:: 0.4
 
     """
-    def __init__(self, file_=None, ini_section='alembic', output_buffer=None):
+    def __init__(self, file_=None, ini_section='alembic', output_buffer=None,
+                        stdout=sys.stdout):
         """Construct a new :class:`.Config`
 
         """
         self.config_file_name = file_
         self.config_ini_section = ini_section
         self.output_buffer = output_buffer
+        self.stdout = stdout
 
     config_file_name = None
     """Filesystem path to the .ini file in use."""
@@ -63,6 +70,11 @@ class Config(object):
 
     """
 
+    def print_stdout(self, text, *arg):
+        """Render a message to standard out."""
+
+        self.stdout.write((str(text) % arg) + "\n")
+
     @util.memoized_property
     def file_config(self):
         """Return the underlying :class:`ConfigParser` object.
@@ -78,7 +90,7 @@ class Config(object):
             here = os.path.abspath(os.path.dirname(self.config_file_name))
         else:
             here = ""
-        file_config = ConfigParser.SafeConfigParser({'here':here})
+        file_config = ConfigParser.SafeConfigParser({'here': here})
         if self.config_file_name:
             file_config.read([self.config_file_name])
         else: