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

Fix/argparse for cli #521

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions pilot/helpers/Project.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(self, args, name=None, project_description=None, clarifications=Non
self.skip_steps = None
self.main_prompt = None
self.files = []
self.continuing_project = args.get('continuing_project', False)
self.continuing_project = args.continuing_project #if continuing_project is not present in args, it will default to False.

self.ipc_client_instance = ipc_client_instance

Expand Down Expand Up @@ -103,16 +103,16 @@ def start(self):
self.tech_lead.create_development_plan()

# TODO move to constructor eventually
if self.args['step'] is not None and STEPS.index(self.args['step']) < STEPS.index('coding'):
if self.args.step is not None and STEPS.index(self.args.step) < STEPS.index('coding'):
clear_directory(self.root_path)
delete_all_app_development_data(self.args['app_id'])
delete_all_app_development_data(self.args.app_id)
self.finish_loading()

if 'skip_until_dev_step' in self.args:
self.skip_until_dev_step = self.args['skip_until_dev_step']
if self.args['skip_until_dev_step'] == '0':
self.skip_until_dev_step = self.args.skip_until_dev_step
if self.args.skip_until_dev_step == '0':
clear_directory(self.root_path)
delete_all_app_development_data(self.args['app_id'])
delete_all_app_development_data(self.args.app_id)
self.finish_loading()
elif self.skip_until_dev_step is not None:
should_overwrite_files = None
Expand Down Expand Up @@ -168,7 +168,7 @@ def get_directory_tree(self, with_descriptions=False):
"""
# files = {}
# if with_descriptions and False:
# files = File.select().where(File.app_id == self.args['app_id'])
# files = File.select().where(File.app_id == self.args.app_id)
# files = {snapshot.name: snapshot for snapshot in files}
# return build_directory_tree_with_descriptions(self.root_path, ignore=IGNORE_FOLDERS, files=files, add_descriptions=False)
return build_directory_tree(self.root_path, ignore=IGNORE_FOLDERS)
Expand All @@ -190,7 +190,7 @@ def get_all_coded_files(self):
Returns:
list: A list of coded files.
"""
files = File.select().where(File.app_id == self.args['app_id'])
files = File.select().where(File.app_id == self.args.app_id)

# TODO temoprary fix to eliminate files that are not in the project
files = [file for file in files if len(FileSnapshot.select().where(FileSnapshot.file_id == file.id)) > 0]
Expand Down
14 changes: 8 additions & 6 deletions pilot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ def init():

builtins.print, ipc_client_instance = get_custom_print(args)

if '--api-key' in args:
os.environ["OPENAI_API_KEY"] = args['--api-key']
if '--get-created-apps-with-steps' in args:

if args.api_key:
os.environ["OPENAI_API_KEY"] = args.api_key
if args.get_created_apps_with_steps:

run_exit_fn = False

if ipc_client_instance is not None:
Expand All @@ -61,16 +63,16 @@ def init():
f"{'' if len(app['development_steps']) == 0 else app['development_steps'][-1]['id']:3}"
f" {app['name']}" for app in get_created_apps_with_steps()))

elif '--ux-test' in args:
elif args.ux_test:
from test.ux_tests import run_test
run_test(args['--ux-test'], args)
run_test(args.ux_test, args)
run_exit_fn = False
else:
if settings.telemetry is None:
telemetry.setup()
loader.save("telemetry")

if args.get("app_id"):
if args.app_id:
telemetry.set("is_continuation", True)

# TODO get checkpoint from database and fill the project with it
Expand Down
111 changes: 55 additions & 56 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,82 +10,80 @@
from utils.utils import should_execute_step
from const.common import STEPS


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 = {
'continuing_project': False
}

# 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

theme_mapping = {'light': style_config.theme.LIGHT, 'dark': style_config.theme.DARK}
theme_value = arguments.get('theme', 'dark')
style_config.set_theme(theme=theme_mapping.get(theme_value, style_config.theme.DARK))

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

app = None
if 'workspace' in arguments:
arguments['workspace'] = os.path.abspath(arguments['workspace'])
app = get_app_by_user_workspace(arguments['user_id'], arguments['workspace'])
if app is not None:
arguments['app_id'] = str(app.id)
arguments['continuing_project'] = True
else:
arguments['workspace'] = None
# Create an ArgumentParser object
parser = argparse.ArgumentParser()

if 'app_id' in arguments:
if app is None:
app = get_app(arguments['app_id'])
# 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)

arguments['app_type'] = app.app_type
arguments['name'] = app.name
arguments['status'] = app.status
arguments['continuing_project'] = True
if 'step' not in arguments or ('step' in arguments and not should_execute_step(arguments['step'], app.status)):
arguments['step'] = 'finished' if app.status == 'finished' else STEPS[STEPS.index(app.status) + 1]
# Parse the command-line arguments
arguments = parser.parse_args()

print(color_green_bold('\n------------------ LOADING PROJECT ----------------------'))
print(color_green_bold(f'{app.name} (app_id={arguments["app_id"]})'))
print(color_green_bold('--------------------------------------------------------------\n'))
# Initialize app as None
app = None

elif '--get-created-apps-with-steps' not in args:
# 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.continuing_project = True
else:
arguments.workspace = None

# 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)

arguments.app_type = app.app_type
arguments.name = app.name
arguments.status = app.status
arguments.continuing_project = True
# Add any other fields from the App model you wish to include
if arguments.steps == None or (arguments.step and not should_execute_step(arguments.step, app.status)):
arguments.step = 'finished' if app.status == 'finished' else STEPS[STEPS.index(app.status) + 1]
print(color_green_bold('\n------------------ LOADING PROJECT ----------------------'))
print(color_green_bold(f'{app.name} (app_id={arguments.app_id})'))
print(color_green_bold('--------------------------------------------------------------\n'))
except ValueError as e:
print(e)
# Handle the error as needed, possibly exiting the script
elif not arguments.get_created_apps_with_steps:
arguments['app_id'] = str(uuid.uuid4())
print(color_green_bold('\n------------------ STARTING NEW PROJECT ----------------------'))
print("If you wish to continue with this project in future run:")
print(color_green_bold(f'python {sys.argv[0]} app_id={arguments["app_id"]}'))
print(color_green_bold('--------------------------------------------------------------\n'))
# else:
# # If app_id is not provided, print details for starting a new project
# print(color_green_bold('\n------------------ STARTING NEW PROJECT ----------------------'))
# print("If you wish to continue with this project in future run:")
# print(color_green_bold(f'python {sys.argv[0]} app_id={arguments.app_id}'))
# print(color_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():
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there are two get_emails()

# Attempt to get email from .gitconfig
gitconfig_path = os.path.expanduser('~/.gitconfig')

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

if os.path.exists(gitconfig_path):
with open(gitconfig_path, 'r', encoding="utf-8") as file:
with open(gitconfig_path, 'r') as file:
content = file.read()

# Use regex to search for email address
Expand All @@ -108,4 +107,4 @@ def username_to_uuid(username):
"""
sha1 = hashlib.sha1(username.encode()).hexdigest()
uuid_str = "{}-{}-{}-{}-{}".format(sha1[:8], sha1[8:12], sha1[12:16], sha1[16:20], sha1[20:32])
return str(uuid.UUID(uuid_str))
return str(uuid.UUID(uuid_str))
4 changes: 2 additions & 2 deletions pilot/utils/custom_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def local_print(*args, **kwargs):
built_in_print(message, **kwargs)

ipc_client_instance = None
if '--external-log-process-port' in args:
ipc_client_instance = IPCClient(args['--external-log-process-port'])
if args.external_log_process_port:
ipc_client_instance = IPCClient(args.external_log_process_port)
return print_to_external_process, ipc_client_instance
else:
return local_print, ipc_client_instance
3 changes: 2 additions & 1 deletion pilot/utils/llm_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ def return_result(result_data, lines_printed):
endpoint_url,
headers=headers,
json=data,
stream=True
stream=True,
timeout=120
)

if response.status_code != 200:
Expand Down