From 64f6a6a584eedb8f5d8227656e94b80a022e525e Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 8 Jul 2012 14:15:06 -0400 Subject: [PATCH] pep8 --- alembic/autogenerate.py | 146 +++++++++++++++++++++++----------------- 1 file changed, 83 insertions(+), 63 deletions(-) diff --git a/alembic/autogenerate.py b/alembic/autogenerate.py index e8fa0199..2e365cd1 100644 --- a/alembic/autogenerate.py +++ b/alembic/autogenerate.py @@ -13,8 +13,8 @@ ################################################### # public def compare_metadata(context, metadata): - """Compare a database schema to that given in a :class:`~sqlalchemy.schema.MetaData` - instance. + """Compare a database schema to that given in a + :class:`~sqlalchemy.schema.MetaData` instance. The database connection is presented in the context of a :class:`.MigrationContext` object, which @@ -67,9 +67,11 @@ def compare_metadata(context, metadata): Output:: [ ( 'add_table', - Table('bat', MetaData(bind=None), Column('info', String(), table=), schema=None)), + Table('bat', MetaData(bind=None), + Column('info', String(), table=), schema=None)), ( 'remove_table', - Table(u'bar', MetaData(bind=None), Column(u'data', VARCHAR(), table=), schema=None)), + Table(u'bar', MetaData(bind=None), + Column(u'data', VARCHAR(), table=), schema=None)), ( 'add_column', 'foo', Column('data', Integer(), table=)), @@ -99,7 +101,8 @@ def compare_metadata(context, metadata): ################################################### # top level -def _produce_migration_diffs(context, template_args, imports, _include_only=()): +def _produce_migration_diffs(context, template_args, + imports, _include_only=()): opts = context.opts metadata = opts['target_metadata'] if metadata is None: @@ -112,7 +115,8 @@ def _produce_migration_diffs(context, template_args, imports, _include_only=()): autogen_context, connection = _autogen_context(context, imports) diffs = [] - _produce_net_changes(connection, metadata, diffs, autogen_context, _include_only) + _produce_net_changes(connection, metadata, diffs, + autogen_context, _include_only) template_args[opts['upgrade_token']] = \ _indent(_produce_upgrade_commands(diffs, autogen_context)) template_args[opts['downgrade_token']] = \ @@ -123,15 +127,16 @@ def _autogen_context(context, imports): opts = context.opts connection = context.bind return { - 'imports':imports, - 'connection':connection, - 'dialect':connection.dialect, - 'context':context, - 'opts':opts + 'imports': imports, + 'connection': connection, + 'dialect': connection.dialect, + 'context': context, + 'opts': opts }, connection def _indent(text): - text = "### commands auto generated by Alembic - please adjust! ###\n" + text + text = "### commands auto generated by Alembic - "\ + "please adjust! ###\n" + text text += "\n### end Alembic commands ###" text = re.compile(r'^', re.M).sub(" ", text).strip() return text @@ -148,7 +153,8 @@ def _produce_net_changes(connection, metadata, diffs, autogen_context, if include_only: conn_table_names = conn_table_names.intersection(include_only) - metadata_table_names = OrderedSet([table.name for table in metadata.sorted_tables]) + metadata_table_names = OrderedSet([table.name + for table in metadata.sorted_tables]) _compare_tables(conn_table_names, metadata_table_names, inspector, metadata, diffs, autogen_context) @@ -194,7 +200,8 @@ def _compare_tables(conn_table_names, metadata_table_names, ################################################### # element comparison -def _compare_columns(tname, conn_table, metadata_table, diffs, autogen_context): +def _compare_columns(tname, conn_table, metadata_table, + diffs, autogen_context): metadata_cols_by_name = dict((c.name, c) for c in metadata_table.c) conn_col_names = set(conn_table) metadata_col_names = set(metadata_cols_by_name) @@ -246,8 +253,8 @@ def _compare_nullable(tname, cname, conn_col, diffs.append( ("modify_nullable", tname, cname, { - "existing_type":conn_col['type'], - "existing_server_default":conn_col['default'], + "existing_type": conn_col['type'], + "existing_server_default": conn_col['default'], }, conn_col_nullable, metadata_col_nullable), @@ -265,10 +272,12 @@ def _compare_type(tname, cname, conn_col, conn_type = conn_col['type'] metadata_type = metadata_col.type if conn_type._type_affinity is sqltypes.NullType: - log.info("Couldn't determine database type for column '%s.%s'" % (tname, cname)) + log.info("Couldn't determine database type " + "for column '%s.%s'" % (tname, cname)) return if metadata_type._type_affinity is sqltypes.NullType: - log.info("Column '%s.%s' has no type within the model; can't compare" % (tname, cname)) + log.info("Column '%s.%s' has no type within " + "the model; can't compare" % (tname, cname)) return isdiff = autogen_context['context']._compare_type(conn_col, metadata_col) @@ -278,8 +287,8 @@ def _compare_type(tname, cname, conn_col, diffs.append( ("modify_type", tname, cname, { - "existing_nullable":conn_col['nullable'], - "existing_server_default":conn_col['default'], + "existing_nullable": conn_col['nullable'], + "existing_server_default": conn_col['default'], }, conn_type, metadata_type), @@ -295,7 +304,8 @@ def _compare_server_default(tname, cname, conn_col, metadata_col, conn_col_default = conn_col['default'] if conn_col_default is None and metadata_default is None: return False - rendered_metadata_default = _render_server_default(metadata_default, autogen_context) + rendered_metadata_default = _render_server_default( + metadata_default, autogen_context) isdiff = autogen_context['context']._compare_server_default( conn_col, metadata_col, rendered_metadata_default @@ -305,8 +315,8 @@ def _compare_server_default(tname, cname, conn_col, metadata_col, diffs.append( ("modify_default", tname, cname, { - "existing_nullable":conn_col['nullable'], - "existing_type":conn_col['type'], + "existing_nullable": conn_col['nullable'], + "existing_type": conn_col['type'], }, conn_col_default, metadata_default), @@ -349,8 +359,8 @@ def _invoke_adddrop_command(updown, args, autogen_context): cmd_args = args[1:] + (autogen_context,) _commands = { - "table":(_drop_table, _add_table), - "column":(_drop_column, _add_column), + "table": (_drop_table, _add_table), + "column": (_drop_column, _add_column), } cmd_callables = _commands[cmd_type] @@ -369,9 +379,9 @@ def _invoke_modify_command(updown, args, autogen_context): kw = {} _arg_struct = { - "modify_type":("existing_type", "type_"), - "modify_nullable":("existing_nullable", "nullable"), - "modify_default":("existing_server_default", "server_default"), + "modify_type": ("existing_type", "type_"), + "modify_nullable": ("existing_nullable", "nullable"), + "modify_default": ("existing_server_default", "server_default"), } for diff in args: diff_kw = diff[3] @@ -399,9 +409,9 @@ def _invoke_modify_command(updown, args, autogen_context): def _add_table(table, autogen_context): return "%(prefix)screate_table(%(tablename)r,\n%(args)s\n)" % { - 'tablename':table.name, - 'prefix':_alembic_autogenerate_prefix(autogen_context), - 'args':',\n'.join( + 'tablename': table.name, + 'prefix': _alembic_autogenerate_prefix(autogen_context), + 'args': ',\n'.join( [_render_column(col, autogen_context) for col in table.c] + sorted([rcons for rcons in [_render_constraint(cons, autogen_context) for cons in @@ -413,22 +423,22 @@ def _add_table(table, autogen_context): def _drop_table(table, autogen_context): return "%(prefix)sdrop_table(%(tname)r)" % { - "prefix":_alembic_autogenerate_prefix(autogen_context), - "tname":table.name + "prefix": _alembic_autogenerate_prefix(autogen_context), + "tname": table.name } def _add_column(tname, column, autogen_context): return "%(prefix)sadd_column(%(tname)r, %(column)s)" % { - "prefix":_alembic_autogenerate_prefix(autogen_context), - "tname":tname, - "column":_render_column(column, autogen_context) + "prefix": _alembic_autogenerate_prefix(autogen_context), + "tname": tname, + "column": _render_column(column, autogen_context) } def _drop_column(tname, column, autogen_context): return "%(prefix)sdrop_column(%(tname)r, %(cname)r)" % { - "prefix":_alembic_autogenerate_prefix(autogen_context), - "tname":tname, - "cname":column.name + "prefix": _alembic_autogenerate_prefix(autogen_context), + "tname": tname, + "cname": column.name } def _modify_col(tname, cname, @@ -442,16 +452,19 @@ def _modify_col(tname, cname, sqla_prefix = _sqlalchemy_autogenerate_prefix(autogen_context) indent = " " * 11 text = "%(prefix)salter_column(%(tname)r, %(cname)r" % { - 'prefix':_alembic_autogenerate_prefix(autogen_context), - 'tname':tname, - 'cname':cname} + 'prefix': _alembic_autogenerate_prefix( + autogen_context), + 'tname': tname, + 'cname': cname} text += ",\n%sexisting_type=%s" % (indent, _repr_type(sqla_prefix, existing_type, autogen_context)) if server_default is not False: text += ",\n%sserver_default=%s" % (indent, - _render_server_default(server_default, autogen_context),) + _render_server_default( + server_default, autogen_context),) if type_ is not None: - text += ",\n%stype_=%s" % (indent, _repr_type(sqla_prefix, type_, autogen_context)) + text += ",\n%stype_=%s" % (indent, + _repr_type(sqla_prefix, type_, autogen_context)) if nullable is not None: text += ",\n%snullable=%r" % ( indent, nullable,) @@ -478,16 +491,19 @@ def _render_column(column, autogen_context): opts = [] if column.server_default: opts.append(("server_default", - _render_server_default(column.server_default, autogen_context))) + _render_server_default( + column.server_default, autogen_context + ))) if column.nullable is not None: opts.append(("nullable", column.nullable)) # TODO: for non-ascii colname, assign a "key" return "%(prefix)sColumn(%(name)r, %(type)s, %(kw)s)" % { - 'prefix':_sqlalchemy_autogenerate_prefix(autogen_context), - 'name':column.name, - 'type':_repr_type(_sqlalchemy_autogenerate_prefix(autogen_context), column.type, autogen_context), - 'kw':", ".join(["%s=%s" % (kwname, val) for kwname, val in opts]) + 'prefix': _sqlalchemy_autogenerate_prefix(autogen_context), + 'name': column.name, + 'type': _repr_type(_sqlalchemy_autogenerate_prefix(autogen_context), + column.type, autogen_context), + 'kw': ", ".join(["%s=%s" % (kwname, val) for kwname, val in opts]) } def _render_server_default(default, autogen_context): @@ -495,7 +511,8 @@ def _render_server_default(default, autogen_context): if isinstance(default.arg, basestring): default = default.arg else: - default = str(default.arg.compile(dialect=autogen_context['dialect'])) + default = str(default.arg.compile( + dialect=autogen_context['dialect'])) if isinstance(default, basestring): # TODO: this is just a hack to get # tests to pass until we figure out @@ -528,8 +545,8 @@ def _render_primary_key(constraint, autogen_context): if constraint.name: opts.append(("name", repr(constraint.name))) return "%(prefix)sPrimaryKeyConstraint(%(args)s)" % { - "prefix":_sqlalchemy_autogenerate_prefix(autogen_context), - "args":", ".join( + "prefix": _sqlalchemy_autogenerate_prefix(autogen_context), + "args": ", ".join( [repr(c.key) for c in constraint.columns] + ["%s=%s" % (kwname, val) for kwname, val in opts] ), @@ -540,11 +557,13 @@ def _render_foreign_key(constraint, autogen_context): if constraint.name: opts.append(("name", repr(constraint.name))) # TODO: deferrable, initially, etc. - return "%(prefix)sForeignKeyConstraint([%(cols)s], [%(refcols)s], %(args)s)" % { - "prefix":_sqlalchemy_autogenerate_prefix(autogen_context), - "cols":", ".join("'%s'" % f.parent.key for f in constraint.elements), - "refcols":", ".join(repr(f._get_colspec()) for f in constraint.elements), - "args":", ".join( + return "%(prefix)sForeignKeyConstraint([%(cols)s], "\ + "[%(refcols)s], %(args)s)" % { + "prefix": _sqlalchemy_autogenerate_prefix(autogen_context), + "cols": ", ".join("'%s'" % f.parent.key for f in constraint.elements), + "refcols": ", ".join(repr(f._get_colspec()) + for f in constraint.elements), + "args": ", ".join( ["%s=%s" % (kwname, val) for kwname, val in opts] ), } @@ -576,13 +595,14 @@ def _render_unique_constraint(constraint, autogen_context): if constraint.name: opts.append(("name", "'%s'" % constraint.name)) return "%(prefix)sUniqueConstraint(%(cols)s%(opts)s)" % { - 'opts':", " + (", ".join("%s=%s" % (k, v) for k, v in opts)) if opts else "", + 'opts': ", " + (", ".join("%s=%s" % (k, v) + for k, v in opts)) if opts else "", 'cols': ",".join(["'%s'" % c.name for c in constraint.columns]), - "prefix":_sqlalchemy_autogenerate_prefix(autogen_context) + "prefix": _sqlalchemy_autogenerate_prefix(autogen_context) } _constraint_renderers = { - schema.PrimaryKeyConstraint:_render_primary_key, - schema.ForeignKeyConstraint:_render_foreign_key, - schema.UniqueConstraint:_render_unique_constraint, - schema.CheckConstraint:_render_check_constraint + schema.PrimaryKeyConstraint: _render_primary_key, + schema.ForeignKeyConstraint: _render_foreign_key, + schema.UniqueConstraint: _render_unique_constraint, + schema.CheckConstraint: _render_check_constraint }