forked from agronholm/sqlacodegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (51 loc) · 2.7 KB
/
Copy pathmain.py
File metadata and controls
61 lines (51 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import argparse
import io
import sys
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import MetaData
from sqlacodegen.codegen import CodeGenerator
if sys.version_info < (3, 8):
from importlib_metadata import version
else:
from importlib.metadata import version
def main() -> None:
parser = argparse.ArgumentParser(
description='Generates SQLAlchemy model code from an existing database.')
parser.add_argument('url', nargs='?', help='SQLAlchemy url to the database')
parser.add_argument('--version', action='store_true', help="print the version number and exit")
parser.add_argument('--schema', help='load tables from an alternate schema')
parser.add_argument('--tables', help='tables to process (comma-separated, default: all)')
parser.add_argument("--table-prefix", help="table's name prefix", default="")
parser.add_argument('--noviews', action='store_true', help="ignore views")
parser.add_argument('--noindexes', action='store_true', help='ignore indexes')
parser.add_argument('--noconstraints', action='store_true', help='ignore constraints')
parser.add_argument('--nojoined', action='store_true',
help="don't autodetect joined table inheritance")
parser.add_argument('--noinflect', action='store_true',
help="don't try to convert tables names to singular form")
parser.add_argument('--noclasses', action='store_true',
help="don't generate classes, only tables")
parser.add_argument('--nocomments', action='store_true', help="don't render column comments")
parser.add_argument('--outfile', help='file to write output to (default: stdout)')
parser.add_argument("--lfun", type=bool, help="use lfun framework")
args = parser.parse_args()
if args.version:
print(version('sqlacodegen'))
return
if not args.url:
print('You must supply a url\n', file=sys.stderr)
parser.print_help()
return
# Use reflection to fill in the metadata
engine = create_engine(args.url)
metadata = MetaData(engine)
tables = args.tables.split(',') if args.tables else None
metadata.reflect(engine, args.schema, not args.noviews, tables)
# Write the generated model code to the specified file or standard output
outfile = io.open(args.outfile, 'w', encoding='utf-8') if args.outfile else sys.stdout
generator = CodeGenerator(metadata, args.noindexes, args.noconstraints, args.nojoined,
args.noinflect, args.noclasses, table_name_prefix=args.table_prefix,
nocomments=args.nocomments, lfun=args.lfun)
generator.render(outfile)
if __name__ == '__main__':
main()