Skip to content

Commit

Permalink
update_settings_from_args refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mrT23 committed Jul 30, 2023
1 parent 13389c7 commit 28bd2d8
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions pr_agent/algo/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations
from typing import List

import difflib
from datetime import datetime
Expand Down Expand Up @@ -213,25 +214,42 @@ def load_large_diff(file, new_file_content_str: str, original_file_content_str:
return patch


def update_settings_from_args(args):
if args and len(args) >= 1:
def update_settings_from_args(args: List[str]) -> None:
"""
Update the settings of the Dynaconf object based on the arguments passed to the function.
Args:
args: A list of arguments passed to the function.
Returns:
None
Raises:
ValueError: If the argument is not in the correct format.
"""
if args:
for arg in args:
try:
arg = arg.strip('-').strip()
vals = arg.replace('=', '.').replace('=', '.').split('.')
vals = arg.split('=')
if len(vals) != 2:
raise ValueError(f'Invalid argument format: {arg}')
key, value = vals
keys = key.split('.')
d = settings
for i, v in enumerate(vals[:-1]):
if i == len(vals) - 2:
if v in d:
if type(d[v]) == bool:
d[v] = vals[-1].lower() in ("yes", "true", "t", "1")
else:
d[v] = type(d[v])(vals[-1])
logging.info(f'Updated setting {vals[:-1]} to: "{vals[-1]}"')
break
else:
logging.error(f'Invalid setting {vals[:-1]}')
else:
d = d[v]
for i, k in enumerate(keys[:-1]):
if k not in d:
raise ValueError(f'Invalid setting: {key}')
d = d[k]
if keys[-1] not in d:
raise ValueError(f'Invalid setting: {key}')
if isinstance(d[keys[-1]], bool):
d[keys[-1]] = value.lower() in ("yes", "true", "t", "1")
else:
d[keys[-1]] = type(d[keys[-1]])(value)
logging.info(f'Updated setting {key} to: "{value}"')
except ValueError as e:
logging.error(str(e))
except Exception as e:
logging.error(f'Failed to parse argument {arg}: {e}')

0 comments on commit 28bd2d8

Please sign in to comment.