From: Mike Bayer Date: Mon, 20 Oct 2014 18:24:41 +0000 (-0400) Subject: - amending d81619b50b9df7ff4458: X-Git-Tag: rel_0_7_0~59 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fd2a172df623650145d99bbe96b3f35bb3dd3ba3;p=thirdparty%2Fsqlalchemy%2Falembic.git - amending d81619b50b9df7ff4458: Revision files are now written out using the ``'wb'`` modifier to ``open()``, since Mako reads the templates with ``'rb'``, thus preventing CRs from being doubled up as has been observed on windows. The encoding of the output now defaults to 'utf-8', which can be configured using a newly added config file parameter ``output_encoding``. fixes #234 --- diff --git a/alembic/script.py b/alembic/script.py index 69509794..9d336716 100644 --- a/alembic/script.py +++ b/alembic/script.py @@ -36,12 +36,13 @@ class ScriptDirectory(object): def __init__(self, dir, file_template=_default_file_template, truncate_slug_length=40, - sourceless=False): + sourceless=False, output_encoding="utf-8"): self.dir = dir self.versions = os.path.join(self.dir, 'versions') self.file_template = file_template self.truncate_slug_length = truncate_slug_length or 40 self.sourceless = sourceless + self.output_encoding = output_encoding if not os.access(dir, os.F_OK): raise util.CommandError("Path doesn't exist: %r. Please use " @@ -70,7 +71,8 @@ class ScriptDirectory(object): 'file_template', _default_file_template), truncate_slug_length=truncate_slug_length, - sourceless=config.get_main_option("sourceless") == "true" + sourceless=config.get_main_option("sourceless") == "true", + output_encoding=config.get_main_option("output_encoding", "utf-8") ) def walk_revisions(self, base="base", head="head"): @@ -319,6 +321,7 @@ class ScriptDirectory(object): util.template_to_file, src, dest, + self.output_encoding, **kw ) diff --git a/alembic/templates/generic/alembic.ini.mako b/alembic/templates/generic/alembic.ini.mako index a738a24a..90037d77 100644 --- a/alembic/templates/generic/alembic.ini.mako +++ b/alembic/templates/generic/alembic.ini.mako @@ -20,6 +20,10 @@ script_location = ${script_location} # versions/ directory # sourceless = false +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + sqlalchemy.url = driver://user:pass@localhost/dbname diff --git a/alembic/templates/multidb/alembic.ini.mako b/alembic/templates/multidb/alembic.ini.mako index 132b2462..ced3558d 100644 --- a/alembic/templates/multidb/alembic.ini.mako +++ b/alembic/templates/multidb/alembic.ini.mako @@ -20,6 +20,10 @@ script_location = ${script_location} # versions/ directory # sourceless = false +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + databases = engine1, engine2 [engine1] diff --git a/alembic/templates/pylons/alembic.ini.mako b/alembic/templates/pylons/alembic.ini.mako index 771c0273..6a5f206e 100644 --- a/alembic/templates/pylons/alembic.ini.mako +++ b/alembic/templates/pylons/alembic.ini.mako @@ -20,6 +20,10 @@ script_location = ${script_location} # versions/ directory # sourceless = false +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + pylons_config_file = ./development.ini # that's it ! \ No newline at end of file diff --git a/alembic/util.py b/alembic/util.py index 9d48ffb9..d261d09d 100644 --- a/alembic/util.py +++ b/alembic/util.py @@ -57,10 +57,11 @@ except (ImportError, IOError): TERMWIDTH = None -def template_to_file(template_file, dest, **kw): +def template_to_file(template_file, dest, output_encoding, **kw): with open(dest, 'wb') as f: + template = Template(filename=template_file) f.write( - Template(filename=template_file).render(**kw) + template.render_unicode(**kw).encode(output_encoding) ) diff --git a/docs/build/changelog.rst b/docs/build/changelog.rst index 5b135687..dc87462e 100644 --- a/docs/build/changelog.rst +++ b/docs/build/changelog.rst @@ -11,7 +11,9 @@ Changelog Revision files are now written out using the ``'wb'`` modifier to ``open()``, since Mako reads the templates with ``'rb'``, thus preventing - CRs from being doubled up as has been observed on windows. + CRs from being doubled up as has been observed on windows. The encoding + of the output now defaults to 'utf-8', which can be configured using + a newly added config file parameter ``output_encoding``. .. change:: :tags: bug, operations diff --git a/docs/build/tutorial.rst b/docs/build/tutorial.rst index a0fd0ec2..82e7284b 100644 --- a/docs/build/tutorial.rst +++ b/docs/build/tutorial.rst @@ -132,6 +132,10 @@ The file generated with the "generic" configuration looks like:: # versions/ directory # sourceless = false + # the output encoding used when revision files + # are written from script.py.mako + # output_encoding = utf-8 + sqlalchemy.url = driver://user:pass@localhost/dbname # Logging configuration @@ -228,6 +232,11 @@ This file contains the following features: .. versionadded:: 0.6.4 +* ``output_encoding`` - the encoding to use when Alembic writes the + ``script.py.mako`` file into a new migration file. Defaults to ``'utf-8'``. + + .. versionadded:: 0.7.0 + * ``[loggers]``, ``[handlers]``, ``[formatters]``, ``[logger_*]``, ``[handler_*]``, ``[formatter_*]`` - these sections are all part of Python's standard logging configuration, the mechanics of which are documented at `Configuration File Format `_. diff --git a/tests/test_config.py b/tests/test_config.py index 1d03b18f..941504c0 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -9,6 +9,8 @@ from alembic.testing.mock import Mock, call from alembic.testing import eq_, assert_raises_message from alembic.testing.fixtures import capture_db +from alembic.testing.env import _no_sql_testing_config, clear_staging_env,\ + staging_env class ConfigTest(TestBase): @@ -46,7 +48,7 @@ class ConfigTest(TestBase): ) -class OutputEncodingTest(TestBase): +class StdoutOutputEncodingTest(TestBase): def test_plain(self): stdout = Mock(encoding='latin-1') @@ -74,3 +76,21 @@ class OutputEncodingTest(TestBase): stdout.mock_calls, [call.write('m?il x y'), call.write('\n')] ) + + +class TemplateOutputEncodingTest(TestBase): + def setUp(self): + staging_env() + self.cfg = _no_sql_testing_config() + + def tearDown(self): + clear_staging_env() + + def test_default(self): + script = ScriptDirectory.from_config(self.cfg) + eq_(script.output_encoding, 'utf-8') + + def test_setting(self): + self.cfg.set_main_option('output_encoding', 'latin-1') + script = ScriptDirectory.from_config(self.cfg) + eq_(script.output_encoding, 'latin-1')