From 192ffac140b8865f2527069a0eccf47145151cae Mon Sep 17 00:00:00 2001 From: Inus Scheepers Date: Thu, 16 May 2024 18:46:28 +0200 Subject: [PATCH] Resolve arg passing if module call --- .vscode/launch.json | 2 +- src/args.py | 17 +++++++++-------- src/fb_export.py | 14 ++++++-------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 61b36f0..e7afb64 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,7 @@ "program": "src/fb_export.py", //"${file}", "console": "integratedTerminal", "justMyCode": true, - "args": [ //"test/employee.fdb", // "-e", // "-c", // "-l", //"-m 10", // "-u", "SYSDBA", "-p", "masterkey", + "args": [ "test/employee.fdb", // "-e", // "-c", // "-l", //"-m 10", // "-u", "SYSDBA", "-p", "masterkey", // "-o" , "OutDir", // "-F", "json" ] , },], diff --git a/src/args.py b/src/args.py index 2fbcc20..2617c65 100644 --- a/src/args.py +++ b/src/args.py @@ -3,9 +3,8 @@ import argparse -def get_args(*args): - if len(args) != 0: - return args +def get_args(*fbe_args): + parser = argparse.ArgumentParser(description="Firebird export") parser.add_argument( 'path_to_db', type=str, @@ -18,14 +17,12 @@ def get_args(*args): parser.add_argument('-l', '--limit', action='store_true', default=False, help="Limit tables and fields to those in limit_sql.py") parser.add_argument('-m', '--maxrows', help="Max number of rows to export") - - #fixme - numsamples to be conditional on sampledata parser.add_argument('-s', '--sampledata', action='store_true', default=False, help="Also show sample data records") parser.add_argument('-n', '--numsamples', default=3, help="number of sample rows") - parser.add_argument('-c', '--combine', action='store_true', default=False, - help="Combine output into a single file") + parser.add_argument('-j', '--join', action='store_true', default=False, + help="Join output files") format = [ 'csv', 'json',] # 'excel', 'sql', 'hdf', 'pickle', 'html' ....]? parser.add_argument("-F", "--format", choices=format, default='csv', help="Export output format, default .CSV") parser.add_argument("-o", "--outdir", type=str, dest='outdir', default='Export', help="Output directory") @@ -33,5 +30,9 @@ def get_args(*args): parser.add_argument("-p", "--password", type=str, default='masterkey', help="Firebird DB password") - args = parser.parse_args() + if len(fbe_args) == 0: # Command line + args = parser.parse_args() + else: # From module + args = parser.parse_args( fbe_args[0]) + return args diff --git a/src/fb_export.py b/src/fb_export.py index f163d5e..ca7abc3 100755 --- a/src/fb_export.py +++ b/src/fb_export.py @@ -1,7 +1,5 @@ #!/usr/bin/env python3 -# Dump firebase data - - +# Export Firebird database contents to CSV import fdb from shutil import rmtree @@ -28,13 +26,13 @@ unzip_testdb() -def main(*arg): - args = get_args() +def main(*fbe_arg): + args = get_args(fbe_arg) if os.getenv('GITHUB_ACTIONS'): con = fdb.connect(args.path_to_db, user=args.user, password=args.password, fb_library_name='/opt/firebird/lib/libfbembed.so') else: - con = fdb.connect(args.path_to_db, user=args.user, password=args.password) + con = fdb.connect(args.path_to_db) print("Connected to ", con.database_name, ' via ', con.firebird_version, file=sys.stderr) @@ -129,7 +127,7 @@ def main(*arg): except: print("Error converting DF to json: " + table, file=sys.stderr) - if args.combine: + if args.join: filename = dbf_name.rstrip('.fdb') + '.' + args.format fmode = 'a' else: @@ -147,4 +145,4 @@ def main(*arg): rmtree('/tmp/firebird') if __name__ == '__main__': - main(sys.argv) + main()