diff --git a/.gitignore b/.gitignore index f85251e..0bc5e59 100644 --- a/.gitignore +++ b/.gitignore @@ -37,7 +37,7 @@ MANIFEST pip-log.txt pip-delete-this-directory.txt -# Unit test / coverage reports +# Unit examples / coverage reports htmlcov/ .tox/ .nox/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/csv_to_sql_file.py b/csv_to_sql_file.py new file mode 100644 index 0000000..bc12ad6 --- /dev/null +++ b/csv_to_sql_file.py @@ -0,0 +1,71 @@ +import logging, argparse, sys, os +import csv +import re + + +# python csv_to_sql_file.py -file file.csv -table sql_table -output output.sql -headers -auto_type -date_format +# varchar integer float date bool + +DEFAULT_DATE_FORMAT = "DD/MM/YYYY" + + +def csv_to_file(csv_file, table, output_file, headers, auto_type, date_format): + print(csv_file, table, output_file, headers, auto_type, date_format) + + +def main(): + args = set_argparse() + + CSV_FILE = args.file + TABLE = args.table + OUTPUT_FILE = args.output + HEADERS = args.headers + AUTO_TYPE = args.auto_type + DATE_FORMAT = args.date_format + + logging.info('CSV file : %s', CSV_FILE) + logging.info('Table : %s', TABLE) + logging.info('Output file : %s', OUTPUT_FILE) + logging.info('Headers : %s', HEADERS) + logging.info('Auto type : %s', AUTO_TYPE) + logging.info('Date Format : %s', DATE_FORMAT) + + csv_to_file(CSV_FILE, TABLE, OUTPUT_FILE, HEADERS, AUTO_TYPE, DATE_FORMAT) + exit(0) + + +def set_argparse(): + parser = argparse.ArgumentParser(description='') + parser.action_groups.pop() + required = parser.add_argument_group('required arguments') + optional = parser.add_argument_group('optional arguments') + required.add_argument('-f', '--file', help=' (default: None)', default=None, required=True) + + optional.add_argument('-t', '--table', help=' (default: filename)', default=None) + optional.add_argument('-o', '--output', help=' (default: output.sql)', default='output.sql') + optional.add_argument('-h', '--headers', help=' (default: True)', default=True) + optional.add_argument('-at', '--auto_type', help=' (default: True)', default=True) + optional.add_argument('-df', '--date_format', help=f' (default: {DEFAULT_DATE_FORMAT})', + default=DEFAULT_DATE_FORMAT) + + optional.add_argument('-i', '--info', help='Info mode (default: True)', default=True, action='store_false') + optional.add_argument('-d', '--debug', help='Debug mode (default: False)', default=False, action='store_true') + + try: + args = parser.parse_args() + except argparse.ArgumentError as error: + logging.error('Catching an argumentError {}'.format(error)) + sys.exit('Catching an argumentError {}'.format(error)) + + if args.debug: + logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) + elif args.info: + logging.basicConfig(stream=sys.stderr, level=logging.INFO) + else: + logging.basicConfig(stream=sys.stderr, level=logging.ERROR) + + return args + + +if __name__ == '__main__': + main() diff --git a/main.py b/main.py deleted file mode 100644 index 20a033a..0000000 --- a/main.py +++ /dev/null @@ -1,16 +0,0 @@ -# This is a sample Python script. - -# Press Maj+F10 to execute it or replace it with your code. -# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. - - -def print_hi(name): - # Use a breakpoint in the code line below to debug your script. - print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. - - -# Press the green button in the gutter to run the script. -if __name__ == '__main__': - print_hi('PyCharm') - -# See PyCharm help at https://www.jetbrains.com/help/pycharm/ diff --git a/test_csv_to_sql_file.py b/test_csv_to_sql_file.py new file mode 100644 index 0000000..e69de29