Skip to content

Commit

Permalink
validate output file extension for output-format
Browse files Browse the repository at this point in the history
  • Loading branch information
mkangia committed Apr 3, 2024
1 parent 4cd33eb commit 4141e9d
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions commcare_export/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ def main(argv):

args = parser.parse_args(argv)

if args.output_format and args.output:
errors = []
errors.extend(_validate_output_filename(args.output_format, args.output))
if errors:
raise Exception(f"Could not proceed. Following issues were found: {', '.join(errors)}.")

if not args.no_logfile:
exe_dir = os.path.dirname(sys.executable)
log_file = os.path.join(exe_dir, "commcare_export.log")
Expand Down Expand Up @@ -254,6 +260,23 @@ def main(argv):
stats.print_stats(100)


def _validate_output_filename(output_format, output_filename):
"""
Validate file extensions for csv, xls and xlsx output formats.
Ensure extension unless using sql output_format.
"""
errors = []
if output_format == 'csv' and not output_filename.endswith('.csv'):
errors.append("For output format as csv, output file name should have extension csv")
elif output_format == 'xls' and not output_filename.endswith('.xls'):
errors.append("For output format as xls, output file name should have extension xls")
elif output_format == 'xlsx' and not output_filename.endswith('.xlsx'):
errors.append("For output format as xlsx, output file name should have extension xlsx")
elif output_format != 'sql' and "." not in output_filename:
errors.append("Missing extension for filename")
return errors


def _get_query(args, writer, column_enforcer=None):
return _get_query_from_file(
args.query,
Expand Down

0 comments on commit 4141e9d

Please sign in to comment.