#NEWLINE# @ click . command ( ) #NEWLINE# @ click . option ( '--database_url' , prompt = True , help = 'Database connection URL' ) #NEWLINE# @ click . option ( '--table_schema' , default = 'public' , help = 'Postgres table_schema, default is: public' ) #NEWLINE# @ click . option ( '--output_file' , prompt = True , help = 'Path for generated markdown file' ) #NEWLINE# def cli( database_url , table_schema , output_file ) : #NEWLINE# #INDENT# '\n    Export Postgres table documentation to a markdown file\n    ' #NEWLINE# try : #NEWLINE# #INDENT# db = database_connection ( database_url = database_url ) #NEWLINE# #UNINDENT# except UnableToConnectToDatabase : #NEWLINE# #INDENT# click . echo ( "Unable to connect to database using 'database_url': {0}" . format ( table_schema ) ) #NEWLINE# sys . exit ( 1 ) #NEWLINE# #UNINDENT# cursor = db . cursor ( ) #NEWLINE# cursor . execute ( build_schema_query ( table_schema = table_schema ) ) #NEWLINE# results = cursor . fetchall ( ) #NEWLINE# cursor . close ( ) #NEWLINE# parsed = parse_schema_data ( schema_data = results ) #NEWLINE# with open ( output_file , 'w' ) as f : #NEWLINE# #INDENT# for table_name in sorted ( parsed . keys ( ) ) : #NEWLINE# #INDENT# f . write ( SECTION_HEADING . format ( table_name ) ) #NEWLINE# f . write ( TABLE_HEADER ) #NEWLINE# f . write ( TABLE_DIVIDER ) #NEWLINE# for column in parsed [ table_name ] : #NEWLINE# #INDENT# f . write ( TABLE_ROW . format ( ** column ) ) #NEWLINE# #UNINDENT# f . write ( '\n' )