1919
2020import argparse
2121import sys
22+ import warnings
2223
23- from . import generator , models
24+ import gel
25+
26+ from gel .codegen import generator , models
27+ from gel .compatibility .introspection import get_schema_json , GelORMWarning
28+ from gel .compatibility .clihelper import _get_conn_args
29+ from gel .orm .sqla import ModelGenerator as SQLAModGen
30+ from gel .orm .sqlmodel import ModelGenerator as SQLModGen
31+ from gel .orm .django .generator import ModelGenerator as DjangoModGen
2432
2533
2634class ColoredArgumentParser (argparse .ArgumentParser ):
@@ -34,7 +42,15 @@ def error(self, message):
3442
3543
3644parser = ColoredArgumentParser (
37- description = "Generate Python code for .edgeql files."
45+ description = "Generate Python code for various Gel interfaces."
46+ )
47+ parser .add_argument (
48+ "mode" ,
49+ choices = ['edgeql' , 'pydantic' , 'sqlalchemy' , 'sqlmodel' , 'django' ],
50+ help = "Pick which mode to generate code for: "
51+ "`edgeql` makes Python code for .edgeql files; "
52+ "`pydantic` makes Pydantic models based on the database schema; "
53+ "`sqlalchemy`, `sqlmodel`, `django` makes Python ORM code for a database" ,
3854)
3955parser .add_argument ("--dsn" )
4056parser .add_argument ("--credentials-file" , metavar = "PATH" )
@@ -69,11 +85,13 @@ def error(self, message):
6985 help = "Choose one or more targets to generate code (default is async)."
7086)
7187parser .add_argument (
72- "--models" ,
73- action = "store_true" ,
74- default = False ,
75- help = "Using the schema generate Pydantic models that can be used for "
76- "bulk inserts." ,
88+ "--out" ,
89+ help = "The output directory for the generated files." ,
90+ )
91+ parser .add_argument (
92+ "--mod" ,
93+ help = "The fullname of the Python module corresponding to the output "
94+ "directory." ,
7795)
7896if sys .version_info [:2 ] >= (3 , 9 ):
7997 parser .add_argument (
@@ -101,10 +119,59 @@ def error(self, message):
101119
102120def main ():
103121 args = parser .parse_args ()
104- if args .models :
105- models .Generator (args ).run ()
106- return
107122
108- if not hasattr (args , "skip_pydantic_validation" ):
109- args .skip_pydantic_validation = True
110- generator .Generator (args ).run ()
123+ match args .mode :
124+ case 'edgeql' :
125+ if not hasattr (args , "skip_pydantic_validation" ):
126+ args .skip_pydantic_validation = True
127+ generator .Generator (args ).run ()
128+
129+ case 'pydantic' :
130+ models .Generator (args ).run ()
131+
132+ case 'sqlalchemy' :
133+ if args .mod is None :
134+ parser .error ('sqlalchemy requires to specify --mod' )
135+
136+ with warnings .catch_warnings (record = True ) as wlist :
137+ warnings .simplefilter ("always" , GelORMWarning )
138+ spec = get_schema_json (
139+ gel .create_client (** generator ._get_conn_args (args )))
140+ gen = SQLAModGen (
141+ outdir = args .out ,
142+ basemodule = args .mod ,
143+ )
144+ gen .render_models (spec )
145+
146+ for w in wlist :
147+ print (w .message )
148+
149+ case 'sqlmodel' :
150+ if args .mod is None :
151+ parser .error ('sqlmodel requires to specify --mod' )
152+
153+ with warnings .catch_warnings (record = True ) as wlist :
154+ warnings .simplefilter ("always" , GelORMWarning )
155+ spec = get_schema_json (
156+ gel .create_client (** _get_conn_args (args )))
157+ gen = SQLModGen (
158+ outdir = args .out ,
159+ basemodule = args .mod ,
160+ )
161+ gen .render_models (spec )
162+
163+ for w in wlist :
164+ print (w .message )
165+
166+ case 'django' :
167+ with warnings .catch_warnings (record = True ) as wlist :
168+ warnings .simplefilter ("always" , GelORMWarning )
169+ spec = get_schema_json (
170+ gel .create_client (** _get_conn_args (args )))
171+ gen = DjangoModGen (
172+ out = args .out ,
173+ )
174+ gen .render_models (spec )
175+
176+ for w in wlist :
177+ print (w .message )
0 commit comments