Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored arguments.py to utilize argparse for command-line argument parsing. #141

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 34 additions & 41 deletions pilot/utils/arguments.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import argparse
import hashlib
import os
import re
Expand All @@ -9,67 +10,59 @@


def get_arguments():
# The first element in sys.argv is the name of the script itself.
# Any additional elements are the arguments passed from the command line.
args = sys.argv[1:]

# Create an empty dictionary to store the key-value pairs.
arguments = {}

# Loop through the arguments and parse them as key-value pairs.
for arg in args:
if '=' in arg:
key, value = arg.split('=', 1)
arguments[key] = value
else:
arguments[arg] = True

if 'user_id' not in arguments:
arguments['user_id'] = username_to_uuid(getuser())


# Create an ArgumentParser object
parser = argparse.ArgumentParser()

# Add command-line arguments with their types and default values
parser.add_argument('--user_id', type=str, default=username_to_uuid(getuser()))
parser.add_argument('--workspace', type=str, default=None)
parser.add_argument('--app_id', type=str, default=str(uuid.uuid4()))
parser.add_argument('--email', type=str, default=get_email())
parser.add_argument('--password', type=str, default='password')
parser.add_argument('--step', type=str, default=None)

# Parse the command-line arguments
arguments = parser.parse_args()

# Initialize app as None
app = None
if 'workspace' in arguments:
app = get_app_by_user_workspace(arguments['user_id'], arguments['workspace'])

# If workspace is provided, get the corresponding app
if arguments.workspace:
app = get_app_by_user_workspace(arguments.user_id, arguments.workspace)
if app is not None:
arguments['app_id'] = app.id
arguments.app_id = app.id
else:
arguments['workspace'] = None
arguments.workspace = None

if 'app_id' in arguments:
# If app_id is provided, get the app details and print them
if arguments.app_id:
try:
if app is None:
app = get_app(arguments['app_id'])
app = get_app(arguments.app_id)

arguments['app_type'] = app.app_type
arguments['name'] = app.name
arguments['step'] = app.status
arguments.app_type = app.app_type
arguments.name = app.name
arguments.step = app.status
# Add any other fields from the App model you wish to include

print(green_bold('\n------------------ LOADING PROJECT ----------------------'))
print(green_bold(f'{app.name} (app_id={arguments["app_id"]})'))
print(green_bold(f'{app.name} (app_id={arguments.app_id})'))
print(green_bold('--------------------------------------------------------------\n'))
except ValueError as e:
print(e)
# Handle the error as needed, possibly exiting the script
else:
arguments['app_id'] = str(uuid.uuid4())
# If app_id is not provided, print details for starting a new project
print(green_bold('\n------------------ STARTING NEW PROJECT ----------------------'))
print("If you wish to continue with this project in future run:")
print(green_bold(f'python {sys.argv[0]} app_id={arguments["app_id"]}'))
print(green_bold(f'python {sys.argv[0]} app_id={arguments.app_id}'))
print(green_bold('--------------------------------------------------------------\n'))

if 'email' not in arguments:
arguments['email'] = get_email()

if 'password' not in arguments:
arguments['password'] = 'password'

if 'step' not in arguments:
arguments['step'] = None

# Return the parsed arguments
return arguments



def get_email():
# Attempt to get email from .gitconfig
gitconfig_path = os.path.expanduser('~/.gitconfig')
Expand Down
Loading