<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals , division , print_function , absolute_import <EOL> from collections import defaultdict <EOL> from inspect import ArgSpec <EOL> from keyword import iskeyword <EOL> import inspect <EOL> import sys <EOL> import re <EOL> from sqlalchemy import ( Enum , ForeignKeyConstraint , PrimaryKeyConstraint , CheckConstraint , UniqueConstraint , Table , <EOL> Column ) <EOL> from sqlalchemy . schema import ForeignKey <EOL> from sqlalchemy . util import OrderedDict <EOL> from sqlalchemy . types import Boolean , String <EOL> import sqlalchemy <EOL> try : <EOL> from sqlalchemy . sql . expression import text , TextClause <EOL> except ImportError : <EOL> from sqlalchemy . sql . expression import text , _TextClause as TextClause <EOL> _re_boolean_check_constraint = re . compile ( r"<STR_LIT>" ) <EOL> _re_column_name = re . compile ( r'<STR_LIT>' ) <EOL> _re_enum_check_constraint = re . compile ( r"<STR_LIT>" ) <EOL> _re_enum_item = re . compile ( r"<STR_LIT>" ) <EOL> _re_invalid_identifier = re . compile ( r'<STR_LIT>' if sys . version_info [ <NUM_LIT:0> ] < <NUM_LIT:3> else r'<STR_LIT>' ) <EOL> _re_first_cap = re . compile ( '<STR_LIT>' ) <EOL> _re_all_cap = re . compile ( '<STR_LIT>' ) <EOL> _flask_prepend = '<STR_LIT>' <EOL> class _DummyInflectEngine ( object ) : <EOL> def singular_noun ( self , noun ) : <EOL> return noun <EOL> def plural_noun ( self , noun ) : <EOL> import inflect <EOL> inflect_engine = inflect . engine ( ) <EOL> return inflect_engine . plural_noun ( noun ) <EOL> def _convert_to_valid_identifier ( name ) : <EOL> assert name , '<STR_LIT>' <EOL> if name [ <NUM_LIT:0> ] . isdigit ( ) or iskeyword ( name ) : <EOL> name = '<STR_LIT:_>' + name <EOL> return _re_invalid_identifier . sub ( '<STR_LIT:_>' , name ) <EOL> def _get_compiled_expression ( statement ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( statement , TextClause ) : <EOL> return statement . text <EOL> dialect = statement . _from_objects [ <NUM_LIT:0> ] . bind . dialect <EOL> compiler = statement . _compiler ( dialect ) <EOL> class LiteralCompiler ( compiler . __class__ ) : <EOL> def visit_bindparam ( self , bindparam , within_columns_clause = False , literal_binds = False , ** kwargs ) : <EOL> return super ( LiteralCompiler , self ) . render_literal_bindparam ( <EOL> bindparam , within_columns_clause = within_columns_clause , <EOL> literal_binds = literal_binds , ** kwargs <EOL> ) <EOL> compiler = LiteralCompiler ( dialect , statement ) <EOL> return compiler . process ( statement ) <EOL> def _get_constraint_sort_key ( constraint ) : <EOL> if isinstance ( constraint , CheckConstraint ) : <EOL> return '<STR_LIT>' . format ( constraint . sqltext ) <EOL> return constraint . __class__ . __name__ [ <NUM_LIT:0> ] + repr ( constraint . columns ) <EOL> def _get_common_fk_constraints ( table1 , table2 ) : <EOL> """<STR_LIT>""" <EOL> c1 = set ( c for c in table1 . constraints if isinstance ( c , ForeignKeyConstraint ) and <EOL> c . elements [ <NUM_LIT:0> ] . column . table == table2 ) <EOL> c2 = set ( c for c in table2 . constraints if isinstance ( c , ForeignKeyConstraint ) and <EOL> c . elements [ <NUM_LIT:0> ] . column . table == table1 ) <EOL> return c1 . union ( c2 ) <EOL> def _getargspec_init ( method ) : <EOL> try : <EOL> return inspect . getargspec ( method ) <EOL> except TypeError : <EOL> if method is object . __init__ : <EOL> return ArgSpec ( [ '<STR_LIT>' ] , None , None , None ) <EOL> else : <EOL> return ArgSpec ( [ '<STR_LIT>' ] , '<STR_LIT:args>' , '<STR_LIT>' , None ) <EOL> def _render_column_type ( coltype ) : <EOL> args = [ ] <EOL> if isinstance ( coltype , Enum ) : <EOL> args . extend ( repr ( arg ) for arg in coltype . enums ) <EOL> if coltype . name is not None : <EOL> args . append ( '<STR_LIT>' . format ( coltype . name ) ) <EOL> else : <EOL> argspec = _getargspec_init ( coltype . __class__ . __init__ ) <EOL> defaults = dict ( zip ( argspec . args [ - len ( argspec . defaults or ( ) ) : ] , argspec . defaults or ( ) ) ) <EOL> missing = object ( ) <EOL> use_kwargs = False <EOL> for attr in argspec . args [ <NUM_LIT:1> : ] : <EOL> if attr . startswith ( '<STR_LIT:_>' ) : <EOL> continue <EOL> value = getattr ( coltype , attr , missing ) <EOL> default = defaults . get ( attr , missing ) <EOL> if value is missing or value == default : <EOL> use_kwargs = True <EOL> elif use_kwargs : <EOL> args . append ( '<STR_LIT>' . format ( attr , repr ( value ) ) ) <EOL> else : <EOL> args . append ( repr ( value ) ) <EOL> text = _flask_prepend + coltype . __class__ . __name__ <EOL> if args : <EOL> text += '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( args ) ) <EOL> return text <EOL> def _render_column ( column , show_name ) : <EOL> kwarg = [ ] <EOL> is_sole_pk = column . primary_key and len ( column . table . primary_key ) == <NUM_LIT:1> <EOL> dedicated_fks = [ c for c in column . foreign_keys if len ( c . constraint . columns ) == <NUM_LIT:1> ] <EOL> is_unique = any ( isinstance ( c , UniqueConstraint ) and set ( c . columns ) == set ( [ column ] ) <EOL> for c in column . table . constraints ) <EOL> is_unique = is_unique or any ( i . unique and set ( i . columns ) == set ( [ column ] ) for i in column . table . indexes ) <EOL> has_index = any ( set ( i . columns ) == set ( [ column ] ) for i in column . table . indexes ) <EOL> render_coltype = not dedicated_fks or any ( fk . column is column for fk in dedicated_fks ) <EOL> if column . key != column . name : <EOL> kwarg . append ( '<STR_LIT:key>' ) <EOL> if column . primary_key : <EOL> kwarg . append ( '<STR_LIT:primary_key>' ) <EOL> if not column . nullable and not is_sole_pk : <EOL> kwarg . append ( '<STR_LIT>' ) <EOL> if is_unique : <EOL> column . unique = True <EOL> kwarg . append ( '<STR_LIT>' ) <EOL> elif has_index : <EOL> column . index = True <EOL> kwarg . append ( '<STR_LIT:index>' ) <EOL> if column . server_default : <EOL> server_default = '<STR_LIT>' + _flask_prepend + '<STR_LIT>' <EOL> return _flask_prepend + '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( <EOL> ( [ repr ( column . name ) ] if show_name else [ ] ) + <EOL> ( [ _render_column_type ( column . type ) ] if render_coltype else [ ] ) + <EOL> [ _render_constraint ( x ) for x in dedicated_fks ] + <EOL> [ repr ( x ) for x in column . constraints ] + <EOL> [ '<STR_LIT>' . format ( k , repr ( getattr ( column , k ) ) ) for k in kwarg ] + <EOL> ( [ server_default ] if column . server_default else [ ] ) <EOL> ) ) <EOL> def _render_constraint ( constraint ) : <EOL> def render_fk_options ( * opts ) : <EOL> opts = [ repr ( opt ) for opt in opts ] <EOL> for attr in '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' : <EOL> value = getattr ( constraint , attr , None ) <EOL> if value : <EOL> opts . append ( '<STR_LIT>' . format ( attr , value ) ) <EOL> return '<STR_LIT:U+002CU+0020>' . join ( opts ) <EOL> if isinstance ( constraint , ForeignKey ) : <EOL> remote_column = '<STR_LIT>' . format ( constraint . column . table . fullname , constraint . column . name ) <EOL> return _flask_prepend + '<STR_LIT>' . format ( render_fk_options ( remote_column ) ) <EOL> elif isinstance ( constraint , ForeignKeyConstraint ) : <EOL> local_columns = constraint . columns <EOL> remote_columns = [ '<STR_LIT>' . format ( fk . column . table . fullname , fk . column . name ) <EOL> for fk in constraint . elements ] <EOL> return _flask_prepend + '<STR_LIT>' . format ( render_fk_options ( local_columns , remote_columns ) ) <EOL> elif isinstance ( constraint , CheckConstraint ) : <EOL> return _flask_prepend + '<STR_LIT>' . format ( _get_compiled_expression ( constraint . sqltext ) ) <EOL> elif isinstance ( constraint , UniqueConstraint ) : <EOL> columns = [ repr ( col . name ) for col in constraint . columns ] <EOL> return _flask_prepend + '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( columns ) ) <EOL> def _underscore ( name ) : <EOL> """<STR_LIT>""" <EOL> s1 = _re_first_cap . sub ( r'<STR_LIT>' , name ) <EOL> return _re_all_cap . sub ( r'<STR_LIT>' , s1 ) . lower ( ) <EOL> def _is_model_descendant ( model_a , model_b ) : <EOL> """<STR_LIT>""" <EOL> if model_a . name == model_b . name : <EOL> return True <EOL> if not model_b . children : <EOL> return False <EOL> return any ( _is_model_descendant ( model_a , b ) for b in model_b . children ) <EOL> def _render_index ( index ) : <EOL> columns = [ repr ( col . name ) for col in index . columns ] <EOL> return _flask_prepend + '<STR_LIT>' . format ( index . name , '<STR_LIT:U+002CU+0020>' . join ( columns ) ) <EOL> class ImportCollector ( OrderedDict ) : <EOL> def add_import ( self , obj ) : <EOL> type_ = type ( obj ) if not isinstance ( obj , type ) else obj <EOL> pkgname = '<STR_LIT>' if type_ . __name__ in sqlalchemy . __all__ else type_ . __module__ <EOL> self . add_literal_import ( pkgname , type_ . __name__ ) <EOL> def add_literal_import ( self , pkgname , name ) : <EOL> names = self . setdefault ( pkgname , set ( ) ) <EOL> names . add ( name ) <EOL> def render ( self ) : <EOL> return '<STR_LIT:\n>' . join ( '<STR_LIT>' . format ( package , '<STR_LIT:U+002CU+0020>' . join ( sorted ( names ) ) ) <EOL> for package , names in self . items ( ) ) <EOL> class Model ( object ) : <EOL> def __init__ ( self , table ) : <EOL> super ( Model , self ) . __init__ ( ) <EOL> self . table = table <EOL> self . schema = table . schema <EOL> for column in table . columns : <EOL> cls = column . type . __class__ <EOL> for supercls in cls . __mro__ : <EOL> if hasattr ( supercls , '<STR_LIT>' ) : <EOL> cls = supercls <EOL> if supercls . __name__ != supercls . __name__ . upper ( ) and not supercls . __name__ . startswith ( '<STR_LIT:_>' ) : <EOL> break <EOL> column . type = column . type . adapt ( cls ) <EOL> def add_imports ( self , collector ) : <EOL> if self . table . columns : <EOL> collector . add_import ( Column ) <EOL> for column in self . table . columns : <EOL> collector . add_import ( column . type ) <EOL> if column . server_default : <EOL> collector . add_literal_import ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for constraint in sorted ( self . table . constraints , key = _get_constraint_sort_key ) : <EOL> if isinstance ( constraint , ForeignKeyConstraint ) : <EOL> if len ( constraint . columns ) > <NUM_LIT:1> : <EOL> collector . add_literal_import ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> collector . add_literal_import ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif isinstance ( constraint , UniqueConstraint ) : <EOL> if len ( constraint . columns ) > <NUM_LIT:1> : <EOL> collector . add_literal_import ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif not isinstance ( constraint , PrimaryKeyConstraint ) : <EOL> collector . add_import ( constraint ) <EOL> for index in self . table . indexes : <EOL> if len ( index . columns ) > <NUM_LIT:1> : <EOL> collector . add_import ( index ) <EOL> class ModelTable ( Model ) : <EOL> def add_imports ( self , collector ) : <EOL> super ( ModelTable , self ) . add_imports ( collector ) <EOL> collector . add_import ( Table ) <EOL> def render ( self ) : <EOL> met = '<STR_LIT>' if _flask_prepend == '<STR_LIT>' else '<STR_LIT>' <EOL> text = '<STR_LIT>' . format ( self . table . name , _flask_prepend , met ) <EOL> for column in self . table . columns : <EOL> text += '<STR_LIT>' . format ( _render_column ( column , True ) ) <EOL> for constraint in sorted ( self . table . constraints , key = _get_constraint_sort_key ) : <EOL> if isinstance ( constraint , PrimaryKeyConstraint ) : <EOL> continue <EOL> if isinstance ( constraint , ( ForeignKeyConstraint , UniqueConstraint ) ) and len ( constraint . columns ) == <NUM_LIT:1> : <EOL> continue <EOL> text += '<STR_LIT>' . format ( _render_constraint ( constraint ) ) <EOL> for index in self . table . indexes : <EOL> if len ( index . columns ) > <NUM_LIT:1> : <EOL> text += '<STR_LIT>' . format ( _render_index ( index ) ) <EOL> if self . schema : <EOL> text += "<STR_LIT>" . format ( self . schema ) <EOL> return text . rstrip ( '<STR_LIT>' ) + '<STR_LIT>' <EOL> class ModelClass ( Model ) : <EOL> parent_name = '<STR_LIT>' <EOL> def __init__ ( self , table , association_tables , inflect_engine , detect_joined ) : <EOL> super ( ModelClass , self ) . __init__ ( table ) <EOL> self . name = self . _tablename_to_classname ( table . name , inflect_engine ) <EOL> self . children = [ ] <EOL> self . attributes = OrderedDict ( ) <EOL> for column in table . columns : <EOL> self . _add_attribute ( column . name , column ) <EOL> pk_column_names = set ( col . name for col in table . primary_key . columns ) <EOL> for constraint in sorted ( table . constraints , key = _get_constraint_sort_key ) : <EOL> if isinstance ( constraint , ForeignKeyConstraint ) : <EOL> target_cls = self . _tablename_to_classname ( constraint . elements [ <NUM_LIT:0> ] . column . table . name , inflect_engine ) <EOL> if detect_joined and self . parent_name == '<STR_LIT>' and set ( constraint . columns ) == pk_column_names : <EOL> self . parent_name = target_cls <EOL> else : <EOL> relationship_ = ManyToOneRelationship ( self . name , target_cls , constraint , inflect_engine ) <EOL> self . _add_attribute ( relationship_ . preferred_name , relationship_ ) <EOL> for association_table in association_tables : <EOL> fk_constraints = [ c for c in association_table . constraints if isinstance ( c , ForeignKeyConstraint ) ] <EOL> fk_constraints . sort ( key = _get_constraint_sort_key ) <EOL> target_cls = self . _tablename_to_classname ( fk_constraints [ <NUM_LIT:1> ] . elements [ <NUM_LIT:0> ] . column . table . name , inflect_engine ) <EOL> relationship_ = ManyToManyRelationship ( self . name , target_cls , association_table , inflect_engine ) <EOL> self . _add_attribute ( relationship_ . preferred_name , relationship_ ) <EOL> @ staticmethod <EOL> def _tablename_to_classname ( tablename , inflect_engine ) : <EOL> camel_case_name = '<STR_LIT>' . join ( part [ : <NUM_LIT:1> ] . upper ( ) + part [ <NUM_LIT:1> : ] for part in tablename . split ( '<STR_LIT:_>' ) ) <EOL> return inflect_engine . singular_noun ( camel_case_name ) or camel_case_name <EOL> def _add_attribute ( self , attrname , value ) : <EOL> attrname = tempname = _convert_to_valid_identifier ( attrname ) <EOL> counter = <NUM_LIT:1> <EOL> while tempname in self . attributes : <EOL> tempname = attrname + str ( counter ) <EOL> counter += <NUM_LIT:1> <EOL> self . attributes [ tempname ] = value <EOL> return tempname <EOL> def add_imports ( self , collector ) : <EOL> super ( ModelClass , self ) . add_imports ( collector ) <EOL> if any ( isinstance ( value , Relationship ) for value in self . attributes . values ( ) ) : <EOL> collector . add_literal_import ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for child in self . children : <EOL> child . add_imports ( collector ) <EOL> def render ( self ) : <EOL> text = '<STR_LIT>' . format ( self . name , self . parent_name ) <EOL> text += '<STR_LIT>' . format ( self . table . name ) <EOL> table_args = [ ] <EOL> for constraint in sorted ( self . table . constraints , key = _get_constraint_sort_key ) : <EOL> if isinstance ( constraint , PrimaryKeyConstraint ) : <EOL> continue <EOL> if isinstance ( constraint , ( ForeignKeyConstraint , UniqueConstraint ) ) and len ( constraint . columns ) == <NUM_LIT:1> : <EOL> continue <EOL> table_args . append ( _render_constraint ( constraint ) ) <EOL> for index in self . table . indexes : <EOL> if len ( index . columns ) > <NUM_LIT:1> : <EOL> table_args . append ( _render_index ( index ) ) <EOL> table_kwargs = { } <EOL> if self . schema : <EOL> table_kwargs [ '<STR_LIT>' ] = self . schema <EOL> kwargs_items = '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' . format ( key , table_kwargs [ key ] ) for key in table_kwargs ) <EOL> kwargs_items = '<STR_LIT>' . format ( kwargs_items ) if kwargs_items else None <EOL> if table_kwargs and not table_args : <EOL> text += '<STR_LIT>' . format ( kwargs_items ) <EOL> elif table_args : <EOL> if kwargs_items : <EOL> table_args . append ( kwargs_items ) <EOL> if len ( table_args ) == <NUM_LIT:1> : <EOL> table_args [ <NUM_LIT:0> ] += '<STR_LIT:U+002C>' <EOL> text += '<STR_LIT>' . format ( '<STR_LIT>' . join ( table_args ) ) <EOL> text += '<STR_LIT:\n>' <EOL> for attr , column in self . attributes . items ( ) : <EOL> if isinstance ( column , Column ) : <EOL> show_name = attr != column . name <EOL> text += '<STR_LIT>' . format ( attr , _render_column ( column , show_name ) ) <EOL> if any ( isinstance ( value , Relationship ) for value in self . attributes . values ( ) ) : <EOL> text += '<STR_LIT:\n>' <EOL> for attr , relationship in self . attributes . items ( ) : <EOL> if isinstance ( relationship , Relationship ) : <EOL> text += '<STR_LIT>' . format ( attr , relationship . render ( ) ) <EOL> for child_class in self . children : <EOL> text += '<STR_LIT>' + child_class . render ( ) <EOL> return text <EOL> class Relationship ( object ) : <EOL> def __init__ ( self , source_cls , target_cls ) : <EOL> super ( Relationship , self ) . __init__ ( ) <EOL> self . source_cls = source_cls <EOL> self . target_cls = target_cls <EOL> self . kwargs = OrderedDict ( ) <EOL> self . backref_name = _underscore ( self . source_cls ) <EOL> def render ( self ) : <EOL> text = _flask_prepend + '<STR_LIT>' <EOL> args = [ repr ( self . target_cls ) ] <EOL> if '<STR_LIT>' in self . kwargs : <EOL> text += '<STR_LIT>' <EOL> delimiter , end = '<STR_LIT>' , '<STR_LIT>' <EOL> else : <EOL> delimiter , end = '<STR_LIT:U+002CU+0020>' , '<STR_LIT:)>' <EOL> args . extend ( [ key + '<STR_LIT:=>' + value for key , value in self . kwargs . items ( ) ] ) <EOL> return text + delimiter . join ( args ) + end <EOL> def make_backref ( self , relationships , classes ) : <EOL> backref = self . backref_name <EOL> original_backref = backref <EOL> suffix = <NUM_LIT:0> <EOL> while ( self . target_cls , backref ) in [ ( x . target_cls , x . backref_name ) for x in relationships ] : <EOL> backref = original_backref + str ( '<STR_LIT>' . format ( suffix ) ) <EOL> suffix += <NUM_LIT:1> <EOL> self . kwargs [ '<STR_LIT>' ] = repr ( backref ) <EOL> for rel in [ x for x in relationships if '<STR_LIT>' in x . kwargs ] : <EOL> if self . target_cls in classes and rel . target_cls in classes : <EOL> if _is_model_descendant ( classes [ self . target_cls ] , classes [ rel . target_cls ] ) : <EOL> self . backref_name = self . target_cls . lower ( ) + '<STR_LIT:_>' + backref <EOL> self . kwargs [ '<STR_LIT>' ] = repr ( self . backref_name ) <EOL> if _is_model_descendant ( classes [ rel . target_cls ] , classes [ self . target_cls ] ) : <EOL> backref = rel . backref_name <EOL> rel . backref_name = rel . target_cls . lower ( ) + '<STR_LIT:_>' + backref <EOL> rel . kwargs [ '<STR_LIT>' ] = repr ( rel . backref_name ) <EOL> class ManyToOneRelationship ( Relationship ) : <EOL> def __init__ ( self , source_cls , target_cls , constraint , inflect_engine ) : <EOL> super ( ManyToOneRelationship , self ) . __init__ ( source_cls , target_cls ) <EOL> colname = constraint . columns [ <NUM_LIT:0> ] <EOL> tablename = constraint . elements [ <NUM_LIT:0> ] . column . table . name <EOL> if not colname . endswith ( '<STR_LIT>' ) : <EOL> self . preferred_name = inflect_engine . singular_noun ( tablename ) or tablename <EOL> else : <EOL> self . preferred_name = colname [ : - <NUM_LIT:3> ] <EOL> self . backref_name = inflect_engine . plural_noun ( self . backref_name ) <EOL> if any ( isinstance ( c , ( PrimaryKeyConstraint , UniqueConstraint ) ) and <EOL> set ( col . name for col in c . columns ) == set ( constraint . columns ) <EOL> for c in constraint . table . constraints ) : <EOL> self . kwargs [ '<STR_LIT>' ] = '<STR_LIT:False>' <EOL> if source_cls == target_cls : <EOL> self . preferred_name = '<STR_LIT>' if not colname . endswith ( '<STR_LIT>' ) else colname [ : - <NUM_LIT:3> ] <EOL> pk_col_names = [ col . name for col in constraint . table . primary_key ] <EOL> self . kwargs [ '<STR_LIT>' ] = '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( pk_col_names ) ) <EOL> if len ( constraint . elements ) > <NUM_LIT:1> : <EOL> self . kwargs [ '<STR_LIT>' ] = "<STR_LIT>" . format ( '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' . format ( source_cls , k . parent . name , target_cls , k . column . name ) <EOL> for k in constraint . elements ] ) ) <EOL> else : <EOL> self . kwargs [ '<STR_LIT>' ] = "<STR_LIT>" . format ( source_cls , constraint . columns [ <NUM_LIT:0> ] , target_cls , <EOL> constraint . elements [ <NUM_LIT:0> ] . column . name ) <EOL> class ManyToManyRelationship ( Relationship ) : <EOL> def __init__ ( self , source_cls , target_cls , assocation_table , inflect_engine ) : <EOL> super ( ManyToManyRelationship , self ) . __init__ ( source_cls , target_cls ) <EOL> self . kwargs [ '<STR_LIT>' ] = repr ( assocation_table . schema + '<STR_LIT:.>' + assocation_table . name ) <EOL> constraints = [ c for c in assocation_table . constraints if isinstance ( c , ForeignKeyConstraint ) ] <EOL> constraints . sort ( key = _get_constraint_sort_key ) <EOL> colname = constraints [ <NUM_LIT:1> ] . columns [ <NUM_LIT:0> ] <EOL> tablename = constraints [ <NUM_LIT:1> ] . elements [ <NUM_LIT:0> ] . column . table . name <EOL> self . preferred_name = tablename if not colname . endswith ( '<STR_LIT>' ) else colname [ : - <NUM_LIT:3> ] + '<STR_LIT:s>' <EOL> self . backref_name = inflect_engine . plural_noun ( self . backref_name ) <EOL> if source_cls == target_cls : <EOL> self . preferred_name = '<STR_LIT>' if not colname . endswith ( '<STR_LIT>' ) else colname [ : - <NUM_LIT:3> ] + '<STR_LIT:s>' <EOL> pri_pairs = zip ( constraints [ <NUM_LIT:0> ] . columns , constraints [ <NUM_LIT:0> ] . elements ) <EOL> sec_pairs = zip ( constraints [ <NUM_LIT:1> ] . columns , constraints [ <NUM_LIT:1> ] . elements ) <EOL> pri_joins = [ '<STR_LIT>' . format ( source_cls , elem . column . name , assocation_table . name , col ) <EOL> for col , elem in pri_pairs ] <EOL> sec_joins = [ '<STR_LIT>' . format ( target_cls , elem . column . name , assocation_table . name , col ) <EOL> for col , elem in sec_pairs ] <EOL> self . kwargs [ '<STR_LIT>' ] = ( <EOL> repr ( '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( pri_joins ) ) ) if len ( pri_joins ) > <NUM_LIT:1> else repr ( pri_joins [ <NUM_LIT:0> ] ) ) <EOL> self . kwargs [ '<STR_LIT>' ] = ( <EOL> repr ( '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( sec_joins ) ) ) if len ( sec_joins ) > <NUM_LIT:1> else repr ( sec_joins [ <NUM_LIT:0> ] ) ) <EOL> class CodeGenerator ( object ) : <EOL> header = '<STR_LIT>' <EOL> footer = '<STR_LIT>' <EOL> def __init__ ( self , metadata , noindexes = False , noconstraints = False , <EOL> nojoined = False , noinflect = False , nobackrefs = False , <EOL> flask = False , fkcols = None ) : <EOL> super ( CodeGenerator , self ) . __init__ ( ) <EOL> if noinflect : <EOL> inflect_engine = _DummyInflectEngine ( ) <EOL> else : <EOL> import inflect <EOL> inflect_engine = inflect . engine ( ) <EOL> _special_columns = fkcols or [ ] <EOL> self . flask = flask <EOL> if not self . flask : <EOL> global _flask_prepend <EOL> _flask_prepend = '<STR_LIT>' <EOL> links = defaultdict ( lambda : [ ] ) <EOL> association_tables = set ( ) <EOL> for table in metadata . tables . values ( ) : <EOL> fk_constraints = [ constr for constr in table . constraints if isinstance ( constr , ForeignKeyConstraint ) ] <EOL> if len ( fk_constraints ) == <NUM_LIT:2> and all ( col . foreign_keys for col in table . columns if col . name not in _special_columns ) : <EOL> association_tables . add ( table . name ) <EOL> tablename = sorted ( fk_constraints , key = _get_constraint_sort_key ) [ <NUM_LIT:0> ] . elements [ <NUM_LIT:0> ] . column . table . name <EOL> links [ tablename ] . append ( table ) <EOL> self . models = [ ] <EOL> self . collector = ImportCollector ( ) <EOL> classes = { } <EOL> for table in sorted ( metadata . tables . values ( ) , key = lambda t : ( t . schema or '<STR_LIT>' , t . name ) ) : <EOL> if table . name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> if noindexes : <EOL> table . indexes . clear ( ) <EOL> if noconstraints : <EOL> table . constraints = set ( [ table . primary_key ] ) <EOL> table . foreign_keys . clear ( ) <EOL> for col in table . columns : <EOL> col . foreign_keys . clear ( ) <EOL> else : <EOL> for constraint in table . constraints . copy ( ) : <EOL> if isinstance ( constraint , CheckConstraint ) : <EOL> sqltext = _get_compiled_expression ( constraint . sqltext ) <EOL> match = _re_boolean_check_constraint . match ( sqltext ) <EOL> if match : <EOL> colname = _re_column_name . match ( match . group ( <NUM_LIT:1> ) ) . group ( <NUM_LIT:3> ) <EOL> table . constraints . remove ( constraint ) <EOL> table . c [ colname ] . type = Boolean ( ) <EOL> continue <EOL> match = _re_enum_check_constraint . match ( sqltext ) <EOL> if match : <EOL> colname = _re_column_name . match ( match . group ( <NUM_LIT:1> ) ) . group ( <NUM_LIT:3> ) <EOL> items = match . group ( <NUM_LIT:2> ) <EOL> if isinstance ( table . c [ colname ] . type , String ) : <EOL> table . constraints . remove ( constraint ) <EOL> if not isinstance ( table . c [ colname ] . type , Enum ) : <EOL> options = _re_enum_item . findall ( items ) <EOL> table . c [ colname ] . type = Enum ( * options , native_enum = False ) <EOL> continue <EOL> if not table . primary_key or table . name in association_tables : <EOL> model = ModelTable ( table ) <EOL> else : <EOL> model = ModelClass ( table , links [ table . name ] , inflect_engine , not nojoined ) <EOL> classes [ model . name ] = model <EOL> self . models . append ( model ) <EOL> model . add_imports ( self . collector ) <EOL> for model in classes . values ( ) : <EOL> if model . parent_name != '<STR_LIT>' : <EOL> classes [ model . parent_name ] . children . append ( model ) <EOL> self . models . remove ( model ) <EOL> if not nobackrefs : <EOL> for model in classes . values ( ) : <EOL> visited = [ ] <EOL> for relationship in model . attributes . values ( ) : <EOL> if isinstance ( relationship , Relationship ) : <EOL> relationship . make_backref ( visited , classes ) <EOL> visited . append ( relationship ) <EOL> if self . flask : <EOL> self . collector . add_literal_import ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> parent_name = '<STR_LIT>' <EOL> for model in classes . values ( ) : <EOL> if model . parent_name == '<STR_LIT>' : <EOL> model . parent_name = parent_name <EOL> else : <EOL> self . collector . add_literal_import ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def render ( self , outfile = sys . stdout ) : <EOL> print ( self . header , file = outfile ) <EOL> print ( self . collector . render ( ) + '<STR_LIT>' , file = outfile ) <EOL> if self . flask : <EOL> print ( '<STR_LIT>' , file = outfile ) <EOL> else : <EOL> if any ( isinstance ( model , ModelClass ) for model in self . models ) : <EOL> print ( '<STR_LIT>' , file = outfile ) <EOL> else : <EOL> print ( '<STR_LIT>' , file = outfile ) <EOL> for model in self . models : <EOL> print ( '<STR_LIT>' + model . render ( ) . rstrip ( '<STR_LIT:\n>' ) , file = outfile ) <EOL> if self . footer : <EOL> print ( self . footer , file = outfile ) </s>
