#!\Python\python
################################################################################
# Convert any file type to any other file type
# If database, select and/or insert data into table
################################################################################
import read_write_csv, read_write_json, read_write_xml, read_write_db
import re

source_file = input("Input source file_name or database_name...........: ")
target_file = input("Input target file_name or database_name.table_name: ")
sql_select  = input("Input SQL select statement (if any)...............: ")


### Read #######################################################################

if  re.search("\.csv", source_file, re.IGNORECASE):             #if file_name contains csv
    list = read_write_csv.readCSV(source_file)
elif re.search("\.json", source_file, re.IGNORECASE):           #if file_name contains json
    list = read_write_json.readJSON(source_file)
elif re.search("\.xml", source_file, re.IGNORECASE):            #if file_name contains xml
    list = read_write_xml.readXML(source_file)
elif re.search("\s*select ", sql_select, re.IGNORECASE):        #if SQL contains select
    db_name = source_file
    list = read_write_db.select(db_name, sql_select)


### Write ######################################################################

if  re.search("\.csv", target_file, re.IGNORECASE):             #if file_name contains csv
    ok = read_write_csv.writeCSV(list, target_file)
elif re.search("\.json", target_file, re.IGNORECASE):           #if file_name contains json
    ok = read_write_json.writeJSON(list, target_file)
elif re.search("\.xml", target_file, re.IGNORECASE):            #if file_name contains xml
    ok = read_write_xml.writeXML(list, target_file)
else:
    try:
        (db_name, table_name) = target_file.split('.',2)
    except:
        print("Error: target database_name.table_name should only have 1 dot")

    ok = read_write_db.insert(list, db_name, table_name)

print(F"Done - {ok}")