summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES4
-rw-r--r--alembic/command.py50
-rw-r--r--alembic/config.py18
3 files changed, 44 insertions, 28 deletions
diff --git a/CHANGES b/CHANGES
index 598c969..5e1265f 100644
--- 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
diff --git a/alembic/command.py b/alembic/command.py
index c6bc3dc..747744e 100644
--- a/alembic/command.py
+++ b/alembic/command.py
@@ -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):
diff --git a/alembic/config.py b/alembic/config.py
index d04cdbf..8c25aea 100644
--- a/alembic/config.py
+++ b/alembic/config.py
@@ -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: