Skip to content

Commit

Permalink
Make beeflow config new non-interactive by default
Browse files Browse the repository at this point in the history
  • Loading branch information
arhall0 committed Dec 24, 2024
1 parent 4296dac commit ba6c990
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions beeflow/common/config_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def init(cls, userconfig=None, **_kwargs):
break
except FileNotFoundError:
print("Configuration file is missing! Generating new config file.")
new(USERCONFIG_FILE)
new(USERCONFIG_FILE, interactive=False)
# remove default keys from the other sections
cls.CONFIG = config_utils.filter_and_validate(config, VALIDATOR)

Expand Down Expand Up @@ -256,7 +256,7 @@ def filepath_completion_input(*pargs, **kwargs):
default=7777, validator=int)

VALIDATOR.option('DEFAULT', 'workload_scheduler', choices=('Slurm', 'LSF', 'Flux', 'Simple'),
info='backend workload scheduler to interact with ')
default='Slurm', info='backend workload scheduler to interact with ')

VALIDATOR.option('DEFAULT', 'delete_completed_workflow_dirs', validator=validation.bool_,
default=True, info='delete workflow directory for completed jobs')
Expand Down Expand Up @@ -295,11 +295,11 @@ def filepath_completion_input(*pargs, **kwargs):
# General job requirements
VALIDATOR.section('job', info='General job requirements.')
VALIDATOR.option('job', 'default_account', validator=lambda val: val.strip(),
info='default account to launch jobs with (leave blank if none)')
default='', info='default account to launch jobs with (leave blank if none)')
VALIDATOR.option('job', 'default_time_limit', validator=validation.time_limit,
info='default account time limit (leave blank if none)')
default='', info='default account time limit (leave blank if none)')
VALIDATOR.option('job', 'default_partition', validator=lambda val: val.strip(),
info='default partition to run jobs on (leave blank if none)')
default='', info='default partition to run jobs on (leave blank if none)')


def validate_chrun_opts(opts):
Expand Down Expand Up @@ -373,7 +373,7 @@ def __init__(self, fname, validator):
self.validator = validator
self.sections = {}

def choose_values(self):
def choose_values(self, interactive=False, flux=False):
"""Choose configuration values based on user input."""
dirname = os.path.dirname(self.fname)
if dirname:
Expand Down Expand Up @@ -401,10 +401,12 @@ def choose_values(self):
print()
printed = True

this_default = option.default
if flux is True and opt_name == 'workload_scheduler':
this_default = "Flux"
# Check for a default value
if option.default is not None:
default = option.default
value = option.validate(default)
if not interactive and this_default is not None:
value = option.validate(this_default)
print(f'Setting option "{opt_name}" to default value "{value}".')
print()
self.sections[sec_name][opt_name] = value
Expand All @@ -427,7 +429,8 @@ def _input_loop(self, opt_name, option):
print_wrap(f'{opt_name} - {option.info}')
if option.choices is not None:
print(f'(allowed values: {",".join(option.choices)})')
value = input_fn(f'{opt_name}: ')
value = input_fn(f'Enter selection for {opt_name} or\n'
+ f'leave blank for default ({option.default}): ') or option.default
# Validate the input
try:
option.validate(value)
Expand All @@ -436,7 +439,7 @@ def _input_loop(self, opt_name, option):
value = None
return value

def save(self):
def save(self, interactive=False):
"""Save the config to a file."""
print()
print('The following configuration options were chosen:')
Expand All @@ -446,10 +449,11 @@ def save(self):
for opt_name in section:
print(f'{opt_name} = {section[opt_name]}')
print()
ans = input('Would you like to save this config? [y/n] ')
if ans.lower() != 'y':
print('Quitting without saving')
return
if interactive:
ans = input('Would you like to save this config? [y/n] ')
if ans.lower() != 'y':
print('Quitting without saving')
return
config_utils.write_config(self.fname, self.sections)
print(70 * '#')
print('Before running BEE, check defaults in the configuration file:',
Expand Down Expand Up @@ -568,12 +572,21 @@ def info():

@app.command()
def new(path: str = typer.Argument(default=USERCONFIG_FILE,
help='Path to new config file')):
help='Path to new config file'),
interactive: bool = typer.Option(False, '--interactive', '-i',
help='Whether or not to be prompted'
+ ' during config generation'),
flux: bool = typer.Option(False, '--flux', '-f',
help='Changes default scheduler to Flux')):
"""Create a new config file."""
if os.path.exists(path):
if check_yes(f'Path "{path}" already exists.\nWould you like to save a copy of it?'):
if not interactive or check_yes(f'Path "{path}" already exists.\n'
+ 'Would you like to save a copy of it?'):
config_utils.backup(path)
ConfigGenerator(path, VALIDATOR).choose_values().save()
ConfigGenerator(path, VALIDATOR).choose_values(
flux=flux,
interactive=interactive
).save(interactive=interactive)


@app.command()
Expand Down

0 comments on commit ba6c990

Please sign in to comment.