2023-09-04 19:08:23 +02:00
import argparse
import json
2023-09-12 16:38:44 +02:00
from api . baserow_api import BaserowAPI
from repository . table_repository import TableRepository
2023-09-05 00:57:29 +02:00
from matrix_builder import MatrixBuilder
2023-09-04 19:08:23 +02:00
import view
2023-09-05 00:57:29 +02:00
def handle_output ( quiet , data ) :
2023-09-04 19:44:43 +02:00
if not quiet :
view . print_json_output ( data )
2023-09-04 19:08:23 +02:00
def main ( ) :
args = parse_arguments ( )
api = BaserowAPI ( args . base_url , args . api_key , args . verbose )
2023-09-12 16:38:44 +02:00
data_processor = TableRepository ( api , args . verbose )
2023-09-04 19:44:43 +02:00
2023-09-04 19:08:23 +02:00
if args . table_ids :
2023-09-05 00:57:29 +02:00
tables_data = fetch_table_data ( data_processor , args . table_ids )
2023-09-04 19:44:43 +02:00
if " linked_fields " in args . output :
2023-09-05 00:57:29 +02:00
linked_fields_data = data_processor . get_link_fields_for_all_tables ( tables_data )
2023-09-04 19:44:43 +02:00
handle_output ( args . quiet , linked_fields_data )
if " tables " in args . output :
handle_output ( args . quiet , tables_data )
2023-09-04 23:00:45 +02:00
if " matrix " in args . output :
2023-09-05 02:36:14 +02:00
matrix_builder = MatrixBuilder ( data_processor , tables_data , args . verbose )
matrix_data = matrix_builder . build_multitable_matrix ( )
2023-09-04 23:00:45 +02:00
handle_output ( args . quiet , matrix_data )
2023-09-04 19:44:43 +02:00
if args . database_id :
2023-09-05 00:57:29 +02:00
all_data = data_processor . get_all_data_from_database ( args . database_id )
handle_output ( args . quiet , all_data )
2023-09-04 19:44:43 +02:00
2023-09-04 19:08:23 +02:00
def parse_arguments ( ) :
parser = argparse . ArgumentParser ( description = " Fetch all data from a Baserow database. " )
parser . add_argument ( " base_url " , help = " Base URL of your Baserow instance, e.g., https://YOUR_BASEROW_INSTANCE_URL/api/ " )
parser . add_argument ( " api_key " , help = " Your Baserow API key. " )
parser . add_argument ( " --database_id " , help = " ID of the Baserow database you want to fetch data from. " , default = None )
parser . add_argument ( " --table_ids " , help = " IDs of the Baserow tables you want to fetch data from, separated by commas. " , default = None )
parser . add_argument ( " -v " , " --verbose " , action = " store_true " , help = " Enable verbose mode for debugging. " )
2023-09-04 19:44:43 +02:00
parser . add_argument ( " --output " , choices = [ " linked_fields " , " tables " , " matrix " ] , default = [ ] , nargs = ' + ' , help = " Specify the type(s) of output: linked_fields, matrix, content or both " )
2023-09-04 19:08:23 +02:00
parser . add_argument ( " --quiet " , action = " store_true " , help = " Suppress output of json " )
return parser . parse_args ( )
2023-09-05 00:57:29 +02:00
def fetch_table_data ( data_processor , table_ids_str ) :
2023-09-04 19:08:23 +02:00
table_ids = table_ids_str . split ( ' , ' )
2023-09-05 00:57:29 +02:00
return data_processor . get_tables ( table_ids )
2023-09-04 19:08:23 +02:00
if __name__ == " __main__ " :
main ( )