Skip to content

Commit

Permalink
Adding vaildation checks for --batch-id option
Browse files Browse the repository at this point in the history
  • Loading branch information
skchronicles committed Apr 4, 2024
1 parent 868189f commit 009c749
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
5 changes: 3 additions & 2 deletions mpox-seek
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ from src.utils import (
exists,
fatal,
permissions,
require
require,
valid_directory
)


Expand Down Expand Up @@ -641,7 +642,7 @@ def parsed_arguments(name, description):
# Batch Identifer
subparser_run.add_argument(
'--batch-id',
type = lambda option: option.replace(' ', '_'),
type = lambda option: valid_directory(parser, option.replace(' ', '_')),
required = False,
default = '',
help = argparse.SUPPRESS
Expand Down
38 changes: 37 additions & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Python standard library
from __future__ import print_function
from shutil import copytree, copyfileobj
import os, sys, hashlib
import os, sys, hashlib, re
import subprocess, json


Expand Down Expand Up @@ -58,6 +58,42 @@ def permissions(parser, path, *args, **kwargs):
return os.path.abspath(path)



def valid_directory(parser, batch_id, *args, **kwargs):
"""Checks if the value provided to '--batch-id' option is valid.
Check if the string consists of only alphanumeric characters,
hypens, and underscores.
@param parser <argparse.ArgumentParser() object>:
Argparse parser object
@param batch_id <str>:
Value provided to '--batch-id' option
@return path <str>:
Returns batch_id if it is valid, else error is raised
"""
# Check for default argparser value, if so return
# default --batch-id value is set to ''.
if batch_id == '':
# --batch-id is not provided,
# return default value back
return batch_id

# Check if the batch id is a single hypen or underscore
# i.e --batch-id '-' or --batch-id '_' was given
if batch_id == '-' or batch_id == '_':
parser.error(
"Error: --batch-id '{}' is invalid! A batch identifer cannot be a single hypen or underscore.".format(batch_id)
)
# Check if the batch id is valid, enforce only alphanumeric
# characters, hypens, and underscores
if not re.match(r'^[A-Za-z0-9\-_]+$', batch_id):
parser.error(
"Error: --batch-id '{}' is invalid! A batch identifer must consist of only alphanumeric characters, hypens, and underscores.".format(batch_id)
)

return batch_id



def standard_input(parser, path, *args, **kwargs):
"""Checks for standard input when provided or permissions using permissions().
@param parser <argparse.ArgumentParser() object>:
Expand Down

0 comments on commit 009c749

Please sign in to comment.