From 658ab2bf9bfaa6df0c18f09147515779d5ad3186 Mon Sep 17 00:00:00 2001 From: Grigori Fursin Date: Sun, 18 Apr 2021 08:41:35 +0200 Subject: [PATCH] added --default flag to ck install package to select 0 when multiple packages and variations available. --- CHANGES.txt | 4 ++++ ck/kernel.py | 2 +- ck/repo/module/misc/module.py | 30 +++++++++++++++++++----------- ck/repo/module/package/module.py | 32 +++++++++++++++++++++----------- 4 files changed, 45 insertions(+), 23 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index d1497d9dd5..0553f21fd0 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,3 +1,7 @@ +* V1.55.7 + * added --default flag to ck install package to select 0 + when multiple packages and variations available. + * V1.55.6 * extended module:package to ask a user to select a variation for a given package when there are multiple versions available. diff --git a/ck/kernel.py b/ck/kernel.py index b10e305e94..00c6e7037c 100755 --- a/ck/kernel.py +++ b/ck/kernel.py @@ -27,7 +27,7 @@ # We use 3 digits for the main (released) version and 4th digit for development revision -__version__ = "1.55.6" +__version__ = "1.55.7" # Do not use characters (to detect outdated version)! # Import packages that are global for the whole kernel diff --git a/ck/repo/module/misc/module.py b/ck/repo/module/misc/module.py index d63193f48d..463442aff2 100644 --- a/ck/repo/module/misc/module.py +++ b/ck/repo/module/misc/module.py @@ -511,13 +511,14 @@ def prepare_entry_template(i): def select_string(i): """ Input: { - options - an ordered list of strings to select from - (question) - the question to ask - (default) - default selection - (no_autoselect) - if "yes", enforce the interactive choice even if there is only 1 option - (no_autoretry) - if "yes", bail out on any unsuitable input, do not offer to retry - (no_skip_line) - if "yes", do not skip a line after each option - (first_match) - if "yes", take the first match in case there are multiple + options - an ordered list of strings to select from + (question) - the question to ask + (default) - default selection + (no_autoselect) - if "yes", enforce the interactive choice even if there is only 1 option + (no_autoretry) - if "yes", bail out on any unsuitable input, do not offer to retry + (no_skip_line) - if "yes", do not skip a line after each option + (first_match) - if "yes", take the first match in case there are multiple + (select_default) - if "yes", select 0 } Output: { @@ -533,6 +534,8 @@ def select_string(i): import copy + select_default=i.get('select_default','').strip().lower() + question = i.get('question', 'Please select from the options above') options = copy.deepcopy( i.get('options') ) default = i.get('default', None) @@ -567,11 +570,16 @@ def select_string(i): while num_matches!=1: - r = ck.inp({'text': "{}{}: ".format(question, ' [ hit return for "{}" ]'.format(default) if default!=None and len(default) else '')}) - response = r['string'] + if select_default != 'yes': + r = ck.inp({'text': "{}{}: ".format(question, ' [ hit return for "{}" ]'.format(default) if default!=None and len(default) else '')}) + response = r['string'] + + if response=='' and default!=None: + response = default + else: + ck.out('Selected 0 (default)') - if response=='' and default!=None: - response = default + response = '0' try: # try to convert into int() and see if it works error_message = None diff --git a/ck/repo/module/package/module.py b/ck/repo/module/package/module.py index 3a728cf10c..cddcb20ee5 100644 --- a/ck/repo/module/package/module.py +++ b/ck/repo/module/package/module.py @@ -117,6 +117,8 @@ def install(i): (ask_version) - ask for the version of the package the user wants to install (debug) - if 'yes', open shell before installing but with all resolved deps + + (default) - if 'yes', install default package (answer 0 to selection questions) } Output: { @@ -141,6 +143,8 @@ def install(i): ask=i.get('ask','') + default=i.get('default','').strip().lower() + xtags=i.get('tags','') xor_tags=i.get('or_tags','') xno_tags=i.get('no_tags','') @@ -362,11 +366,12 @@ def install(i): ver_options.append( skip_display_line ) select_adict = ck.access({'action': 'select_string', - 'module_uoa': 'misc', - 'options': ver_options, - 'default': '0', - 'no_skip_line': 'yes', - 'question': 'Please select the package to install', + 'module_uoa': 'misc', + 'options': ver_options, + 'default': '0', + 'select_default': default, + 'no_skip_line': 'yes', + 'question': 'Please select the package to install', }) if select_adict['return']>0: return select_adict @@ -495,14 +500,19 @@ def install(i): j+=1 ck.out('') - x=input('Please select a variation or press Enter for the default one (0): ') - x=x.strip() - if x=='': x='0' + if default!='yes': + x=input('Please select a variation or press Enter for the default one (0): ') - ix=int(x) - if ix<0 or ix>=j: - return {'return':1, 'error':'variation number is not recognized'} + x=x.strip() + if x=='': x='0' + + ix=int(x) + if ix<0 or ix>=j: + return {'return':1, 'error':'variation number is not recognized'} + else: + ck.out('Selected 0 (default)') + ix=0 required_variations=[key_variation[ix]]