Skip to content

Commit

Permalink
Start preparing final bash script
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaRenauld committed Apr 22, 2024
1 parent 35726e2 commit 46909ee
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 40 deletions.
8 changes: 4 additions & 4 deletions dwi_ml/GUI/args_management/argparse_equivalent.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def __init__(self, description=None):
logging.debug(" GUI.args_management.argparse_equivalent: "
"Initializing ArgparserForGui")
self.description = description
self.required_args_list = {}
self.optional_args_list = {}
self.required_args_dict = {}
self.optional_args_dict = {}
self.exclusive_groups = [] # type: List[MutuallyExclusiveGroup]

# Should never be changed if self is created with add_argument_group.
Expand All @@ -52,9 +52,9 @@ def __init__(self, description=None):
def add_argument(self, argname, **kwargs):
optional = True if argname[0] == '-' else False
if optional:
self.optional_args_list[argname] = format_arg(argname, **kwargs)
self.optional_args_dict[argname] = format_arg(argname, **kwargs)
else:
self.required_args_list[argname] = format_arg(argname, **kwargs)
self.required_args_dict[argname] = format_arg(argname, **kwargs)

def add_argument_group(self, desc):
g = ArgparserForGui(desc)
Expand Down
6 changes: 3 additions & 3 deletions dwi_ml/GUI/args_management/args_to_gui_all_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ def add_args_to_gui(gui_parser: ArgparserForGui, known_file_dialogs=None):
# 1. Required args:
title = dpg.add_text('\n Required arguments:')
dpg.bind_item_font(title, my_fonts['group_title'])
for argname, argoptions in gui_parser.required_args_list.items():
for argname, argoptions in gui_parser.required_args_dict.items():
add_one_arg_and_help_to_gui(argname, argoptions, known_file_dialogs)

# 2. Grouped options
# At first, we had the idea to add them into a closable section.
# We didn't like it that much. Removed.
for sub_parser in gui_parser.groups_of_args:
# There should not be required args in groups.
assert len(sub_parser.required_args_list.keys()) == 0
assert len(sub_parser.required_args_dict.keys()) == 0

# There should not be a subgroup in a group
assert len(sub_parser.groups_of_args) == 0
Expand All @@ -61,7 +61,7 @@ def _add_options_group_to_gui(gui_parser, known_file_dialogs):
Adds arguments under the group's title.
"""
# 2. "normal" optional args:
for argname, argoptions in gui_parser.optional_args_list.items():
for argname, argoptions in gui_parser.optional_args_dict.items():
add_one_arg_and_help_to_gui(argname, argoptions, known_file_dialogs)

# 2. Mutually exclusive args.
Expand Down
80 changes: 52 additions & 28 deletions dwi_ml/GUI/args_management/gui_to_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,25 @@

import dearpygui.dearpygui as dpg

from dwi_ml.GUI.args_management.argparse_equivalent import ArgparserForGui
from dwi_ml.GUI.args_management.args_to_gui_one_arg import OPTION_CHECKBOX
from dwi_ml.GUI.utils.gui_popup_message import show_infobox


def get_one_value_verify_ok(arg_name):
required = arg_name[0] != '-'
item_type = dpg.get_item_type(arg_name)

# 1. Checking it this is a file dialog
if item_type == 'mvAppItemType::mvFileDialog':
raise NotImplementedError(
"Implementation error. Please use input group from file dialog "
"rather than accessing file dialog value directly.")

# 2. Verifying associated checkbox.
# Will return None if it does not exist.
checked = dpg.get_value(arg_name + OPTION_CHECKBOX)
value = dpg.get_value(arg_name)
if value == '':
# GUI default value.
value = None
if checked:
# We use the default value.
value = None

return value, required


def get_all_values(groups: Dict[str, Dict], split_every=50):
def get_all_values_as_str(argparser: ArgparserForGui, split_every=50):

values = [' ']
for group_name, group_args in groups.items():
# Let's start with required values.
for arg_name, params in argparser.required_args_dict.items():
is_ok, value = get_one_value_if_set(arg_name)
if not is_ok:
# Showed a message box. Leaving now.
return
print("For required arg '{}': got value '{}'"
.format(arg_name, value))

return
for group_name, group_args in argparser.items():
for tmp_arg_name, params in group_args.items():
if split_every is not None and len(values[-1]) > split_every:
values[-1] += " \\"
Expand All @@ -45,7 +32,7 @@ def get_all_values(groups: Dict[str, Dict], split_every=50):
arg_value = None
required = False
for sub_arg_name, sub_params in params.items():
tmp_value, required = get_one_value_verify_ok(sub_arg_name)
tmp_value, required = get_one_value_if_set(sub_arg_name)
assert not required
if tmp_value is not None:
# Two mutually exclusive values should not be possible
Expand All @@ -56,7 +43,7 @@ def get_all_values(groups: Dict[str, Dict], split_every=50):
arg_value = tmp_value
else:
arg_name = tmp_arg_name
arg_value, required = get_one_value_verify_ok(tmp_arg_name)
arg_value, required = get_one_value_if_set(tmp_arg_name)

# Ok. Now format to bash script
if 'action' in params and \
Expand All @@ -78,3 +65,40 @@ def get_all_values(groups: Dict[str, Dict], split_every=50):
values[-1] += ' ' + arg_name + ' ' + str(arg_value)

return values


def get_one_value_if_set(arg_name):

# Verifying if it is optional (has an associated checkbox).
# Will return None if it does not exist.
if dpg.does_item_exist(arg_name + OPTION_CHECKBOX):
is_checked = dpg.get_value(arg_name + OPTION_CHECKBOX)
if not is_checked:
# No value defined! Not adding to bash script, will use the default
# value.
value = None
else:
value = get_value_from_narg_group(arg_name)
else:
# Required arg.
value = get_value_from_narg_group(arg_name)

if value is None or value == '':
show_infobox("Missing required value",
"Value for required argument '{}' is not set!"
.format(arg_name))
return False, None
return True, value


def get_value_from_narg_group(arg_name):
item_type = dpg.get_item_type(arg_name)

# 1. Checking it this is a file dialog
if item_type == 'mvAppItemType::mvFileDialog':
raise NotImplementedError(
"Implementation error. Please contact developpers with this "
"information: use input group from file dialog "
"rather than accessing file dialog value directly!")

return None
20 changes: 15 additions & 5 deletions dwi_ml/GUI/menu1_create_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from dwi_ml.GUI.utils.gui_popup_message import show_infobox
from dwi_ml.GUI.my_styles import STYLE_FIXED_WINDOW, \
get_my_fonts_dictionary, add_color_legend
from dwi_ml.GUI.args_management.gui_to_argparse import get_all_values
from dwi_ml.GUI.args_management.gui_to_argparse import get_all_values_as_str
from dwi_ml.GUI.utils.window import callback_change_window
from dwi_ml.data.hdf5.utils import prepare_parser_hdf5_creation

Expand Down Expand Up @@ -68,14 +68,15 @@ def open_menu_create_hdf5():

dpg.add_text("\n\n\n\n")
with dpg.group(horizontal=True) as group:
dpg.add_text('Choose where to save output script: ')
dpg.add_text('Choose where to save output command line '
'(bash .sh file): ')
add_file_dialog_input_group(
TAG_HDF_SCRIPT_PATH, ['unique_file', ['.sh']],
input_parent=group, button_parent=group)

dpg.add_text("\n")
dpg.add_button(
label='OK! Create my script!', indent=1005, width=200,
label='OK! Create my command line!', indent=1005, width=200,
height=100, callback=__create_hdf5_script_callback,
user_data=gui_parser)

Expand All @@ -91,14 +92,23 @@ def __create_hdf5_script_callback(_, __, user_data):

# 1. Verify that user defined where to save the script.
out_path = dpg.get_value(TAG_HDF_SCRIPT_PATH)
if out_path is None:
if out_path is None or out_path == '':
show_infobox("Missing value", "Please choose an output file for the "
"created bash script.")
return
elif out_path[-3:] != '.sh':
show_infobox("Wrong filename", "Your output filename should be .sh or "
".txt. Got '{}'".format(out_path))
return

logging.info("Will save the command line in {}.".format(out_path))

# 2. Get values. Verify that no warning message popped-up (if so, returns
# None)
all_values = get_all_values(user_data)
# Note. User_data = the ArgparserForGUI
all_values = get_all_values_as_str(user_data)
if all_values is None:
# Showed a message box. Leaving now.
return

logging.debug("WILL WRITE:")
Expand Down

0 comments on commit 46909ee

Please sign in to comment.