2020-05-30 09:04:58 +12:00
|
|
|
def get_boolean_choice(prompt, default=True):
|
2022-10-26 02:38:55 +13:00
|
|
|
yn = 'Y/n' if default else 'y/N'
|
2020-05-30 09:04:58 +12:00
|
|
|
|
|
|
|
choice = input(f'{prompt} [{yn}]: ')
|
|
|
|
if not choice:
|
|
|
|
return default
|
|
|
|
elif choice[0].lower() == 'y':
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2020-12-17 00:35:31 +13:00
|
|
|
|
|
|
|
|
2021-12-30 00:55:35 +13:00
|
|
|
def get_int_choice(prompt, default=None, min_choice=None, max_choice=None, return_on_invalid=False):
|
|
|
|
if default is not None:
|
|
|
|
prompt = f'{prompt} [{default}]: '
|
|
|
|
else:
|
|
|
|
prompt = f'{prompt}: '
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
2022-10-26 02:38:55 +13:00
|
|
|
if inp := input(prompt):
|
|
|
|
choice = int(inp)
|
|
|
|
else:
|
2021-12-30 00:55:35 +13:00
|
|
|
return default
|
2021-12-31 07:05:23 +13:00
|
|
|
except ValueError:
|
|
|
|
if return_on_invalid:
|
|
|
|
return None
|
|
|
|
return_on_invalid = True
|
|
|
|
continue
|
2021-12-30 00:55:35 +13:00
|
|
|
else:
|
|
|
|
if min_choice is not None and choice < min_choice:
|
|
|
|
print(f'Number must be greater than {min_choice}')
|
|
|
|
if return_on_invalid:
|
|
|
|
return None
|
|
|
|
return_on_invalid = True
|
|
|
|
continue
|
|
|
|
if max_choice is not None and choice > max_choice:
|
|
|
|
print(f'Number must be less than {max_choice}')
|
|
|
|
if return_on_invalid:
|
|
|
|
return None
|
|
|
|
return_on_invalid = True
|
|
|
|
continue
|
|
|
|
return choice
|
|
|
|
|
|
|
|
|
2021-09-05 19:41:54 +12:00
|
|
|
def sdl_prompt(sdl_data, title):
|
2020-12-17 00:35:31 +13:00
|
|
|
tags = ['']
|
2021-09-05 19:41:54 +12:00
|
|
|
if '__required' in sdl_data:
|
|
|
|
tags.extend(sdl_data['__required']['tags'])
|
2020-12-17 00:35:31 +13:00
|
|
|
|
2021-09-29 20:04:09 +13:00
|
|
|
print(f'You are about to install {title}, this application supports selective downloads.')
|
|
|
|
print('The following optional packs are available (tag - name):')
|
2021-09-05 19:41:54 +12:00
|
|
|
for tag, info in sdl_data.items():
|
2020-12-17 00:35:31 +13:00
|
|
|
if tag == '__required':
|
|
|
|
continue
|
|
|
|
print(' *', tag, '-', info['name'])
|
|
|
|
|
2021-09-29 20:04:09 +13:00
|
|
|
examples = ', '.join([g for g in sdl_data.keys() if g != '__required'][:2])
|
|
|
|
print(f'Please enter tags of pack(s) to install (space/comma-separated, e.g. "{examples}")')
|
|
|
|
print('Leave blank to use defaults (only required data will be downloaded).')
|
2022-10-26 02:38:55 +13:00
|
|
|
choices = input('Additional packs [Enter to confirm]: ')
|
2020-12-17 00:35:31 +13:00
|
|
|
if not choices:
|
|
|
|
return tags
|
|
|
|
|
2021-09-29 20:04:09 +13:00
|
|
|
for c in choices.strip('"').replace(',', ' ').split():
|
2020-12-17 00:35:31 +13:00
|
|
|
c = c.strip()
|
2021-09-05 19:41:54 +12:00
|
|
|
if c in sdl_data:
|
|
|
|
tags.extend(sdl_data[c]['tags'])
|
2020-12-17 00:35:31 +13:00
|
|
|
else:
|
|
|
|
print('Invalid tag:', c)
|
|
|
|
|
|
|
|
return tags
|
2021-12-29 05:37:26 +13:00
|
|
|
|
|
|
|
|
|
|
|
def strtobool(val):
|
|
|
|
"""Convert a string representation of truth to true (1) or false (0).
|
|
|
|
|
|
|
|
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
|
|
|
|
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
|
|
|
|
'val' is anything else.
|
|
|
|
|
|
|
|
Copied from python standard library as distutils.util.strtobool is deprecated.
|
|
|
|
"""
|
|
|
|
val = val.lower()
|
|
|
|
if val in ('y', 'yes', 't', 'true', 'on', '1'):
|
|
|
|
return 1
|
|
|
|
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
raise ValueError("invalid truth value %r" % (val,))
|
|
|
|
|