def get_formatted_repeat_desc(self): """Format the description (label and value) for parameters in a repeat.""" tool_inp = {} for inp in self.tool_inp_desc["inputs"]: tool_inp.setdefault(inp['name'], inp) repeat_paramlist = '' tmp_wf_param_values = self.wf_param_values cur_level = self.level for ind, param in enumerate(tmp_wf_param_values): self.wf_param_values = param self.level = cur_level + 1 paramlist_in_repeat = self.get_lower_param_desc() if paramlist_in_repeat != '': # add first click repeat_paramlist += templates.render(INPUT_ADD_REPEAT, **{ 'space': SPACE * (self.level), 'repeat_label': self.tool_inp_desc['title']}) repeat_paramlist += paramlist_in_repeat self.level = cur_level self.wf_param_values = tmp_wf_param_values repeat_desc = '' if repeat_paramlist != '': repeat_desc += templates.render(INPUT_SECTION, **{ 'space': SPACE * self.level, 'section_label': self.tool_inp_desc['title']}) + repeat_paramlist return repeat_desc
def get_formatted_repeat_desc(self): """Format the description (label and value) for parameters in a repeat.""" tool_inp = {} for inp in self.tool_inp_desc["inputs"]: tool_inp.setdefault(inp['name'], inp) repeat_paramlist = '' tmp_wf_param_values = self.wf_param_values cur_level = self.level for ind, param in enumerate(tmp_wf_param_values): self.wf_param_values = param self.level = cur_level + 1 paramlist_in_repeat = self.get_lower_param_desc() if paramlist_in_repeat != '': # add first click repeat_paramlist += templates.render( INPUT_ADD_REPEAT, **{ 'space': SPACE * (self.level), 'repeat_label': self.tool_inp_desc['title'] }) repeat_paramlist += paramlist_in_repeat self.level = cur_level self.wf_param_values = tmp_wf_param_values repeat_desc = '' if repeat_paramlist != '': repeat_desc += templates.render( INPUT_SECTION, **{ 'space': SPACE * self.level, 'section_label': self.tool_inp_desc['title'] }) + repeat_paramlist return repeat_desc
def create_topic_structure(self): """Create the skeleton of a new topic. 1. create the folder and its structure 2. update the index.md to match your topic's name 3. fill the metadata 4. add a symbolic link to the metadata.yaml from the metadata folder """ # create the folder and its structure os.makedirs(self.dir) self.img_folder = os.path.join(self.dir, "images") os.makedirs(self.img_folder) self.tuto_folder = os.path.join(self.dir, "tutorials") os.makedirs(self.tuto_folder) # create the index.md and add the topic name self.index_fp = os.path.join(self.dir, "index.md") with open(self.index_fp, 'w') as index_f: index_f.write( templates.render(INDEX_FILE_TEMPLATE, **{'topic': self.name})) # create the README file self.readme_fp = os.path.join(self.dir, "README.md") with open(self.readme_fp, 'w') as readme_f: readme_f.write( templates.render(README_FILE_TEMPLATE, **{'topic': self.title})) # create the metadata file self.metadata_fp = os.path.join(self.dir, "metadata.yaml") save_to_yaml(self.export_metadata_to_ordered_dict(), self.metadata_fp) # create Dockerfile self.docker_folder = os.path.join(self.dir, "docker") os.makedirs(self.docker_folder) self.dockerfile_fp = os.path.join(self.docker_folder, "Dockerfile") with open(self.dockerfile_fp, 'w') as dockerfile: dockerfile.write( templates.render( DOCKER_FILE_TEMPLATE, **{'topic_name': self.name, 'topic_title': self.title})) # create empty introduction slides self.slides_folder = os.path.join(self.dir, "slides") os.makedirs(self.slides_folder) self.intro_slide_fp = os.path.join(self.slides_folder, "introduction.html") with open(self.intro_slide_fp, 'w') as intro_slide_f: intro_slide_f.write( templates.render( INTRO_SLIDES_FILE_TEMPLATE, **{'title': "Introduction to %s" % self.title, 'type': "introduction"})) # add a symbolic link to the metadata.yaml metadata_dir = "metadata" if not os.path.isdir(metadata_dir): os.makedirs(metadata_dir) os.chdir(metadata_dir) os.symlink(os.path.join("..", self.metadata_fp), "%s.yaml" % self.name) os.chdir("..")
def write_hands_on_tutorial(self): """Write the content of the hands-on tutorial in the corresponding file.""" # add the zenodo links self.body = templates.render(TUTO_HAND_ON_BODY_TEMPLATE, **{ "z_file_links": "\n> ".join(self.zenodo_file_links), "body": self.body }) # write in the tutorial file with the metadata on the top metadata = self.get_tuto_metata() with open(self.tuto_fp, 'w') as md: md.write(templates.render(TUTO_HAND_ON_TEMPLATE, **{ "metadata": metadata, "body": self.body }))
def get_formatted_inputs(self): """Format the inputs of a step.""" inputlist = '' inps = [] if isinstance(self.wf_param_values, list): # multiple input (not collection) icon = 'param-files' for i in self.wf_param_values: inps.append('`%s` %s' % ( i['output_name'], get_input_tool_name(i['id'], self.wf_steps))) else: inp = self.wf_param_values if 'id' in inp: # sinle input or collection inp_type = self.wf_steps[str(inp['id'])]['type'] if 'collection' in inp_type: icon = 'param-collection' else: icon = 'param-file' inps = ['`%s` %s' % ( inp['output_name'], get_input_tool_name(inp['id'], self.wf_steps))] if len(inps) > 0: inputlist += templates.render(INPUT_FILE_TEMPLATE, **{ "icon": icon, "input_name": self.tool_inp_desc['label'], "input_value": ', '.join(inps), "space": SPACE * self.level }) return inputlist
def get_empty_param(): """Get the string for an empty param.""" return templates.render(INPUT_PARAM, **{ 'space': 1 * SPACE, 'param_label': 'Parameter', 'param_value': 'a value' })
def get_formatted_other_param_desc(self): """Get value of a 'simple' parameter if different from the default value, None otherwise.""" param_value = None if self.tool_inp_desc['value'] == self.wf_param_values and not self.force_default: param_value = None elif self.type == 'boolean': if bool(self.tool_inp_desc['value']) == self.wf_param_values: param_value = None else: param_value = 'Yes' if self.wf_param_values else 'No' elif self.type == 'select': param_values = [] for opt in self.tool_inp_desc['options']: if opt[1] == self.wf_param_values: param_values.append(opt[0]) param_value = ', '.join(param_values) elif self.type == 'data_column': param_value = "c%s" % self.wf_param_values else: param_value = self.wf_param_values param_desc = '' if param_value is not None: param_desc = templates.render(INPUT_PARAM, **{ 'space': SPACE * self.level, 'param_label': self.tool_inp_desc['label'], 'param_value': param_value}) return param_desc
def write_hands_on_tutorial(self): """Write the content of the hands-on tutorial in the corresponding file.""" # add the zenodo links self.body = templates.render( TUTO_HAND_ON_BODY_TEMPLATE, **{ "z_file_links": "\n> ".join(self.zenodo_file_links), "body": self.body }) # write in the tutorial file with the metadata on the top metadata = self.get_tuto_metata() with open(self.tuto_fp, 'w') as md: md.write( templates.render(TUTO_HAND_ON_TEMPLATE, **{ "metadata": metadata, "body": self.body }))
def get_formatted_other_param_desc(self): """Get value of a 'simple' parameter if different from the default value, None otherwise.""" param_value = None if self.tool_inp_desc[ 'value'] == self.wf_param_values and not self.force_default: param_value = None elif self.type == 'boolean': if bool(self.tool_inp_desc['value']) == self.wf_param_values: param_value = None else: param_value = 'Yes' if self.wf_param_values else 'No' elif self.type == 'select': param_values = [] for opt in self.tool_inp_desc['options']: if opt[1] == self.wf_param_values: param_values.append(opt[0]) param_value = ', '.join(param_values) elif self.type == 'data_column': param_value = "c%s" % self.wf_param_values else: param_value = self.wf_param_values param_desc = '' if param_value is not None: param_desc = templates.render( INPUT_PARAM, **{ 'space': SPACE * self.level, 'param_label': self.tool_inp_desc['label'], 'param_value': param_value }) return param_desc
def get_empty_param(): """Get the string for an empty param.""" return templates.render(INPUT_PARAM, **{ 'space': 1*SPACE, 'param_label': 'Parameter', 'param_value': 'a value' })
def format_wf_steps(wf, gi): """Get a string with the hands-on boxes describing the different steps of the worklow.""" body = '' steps = wf['steps'] for s in range(len(steps)): print('format_wf_steps') wf_step = steps[str(s)] pprint(wf_step) # get params in workflow wf_param_values = {} if wf_step['tool_state'] and wf_step['input_connections']: wf_param_values = get_wf_param_values(wf_step['tool_state'], get_wf_inputs(wf_step['input_connections'])) if not wf_param_values: continue # get tool description try: tool_desc = gi.tools.show_tool(wf_step['tool_id'], io_details=True) except Exception: tool_desc = {'inputs': []} # get formatted param description paramlist = '' pprint(tool_desc) pprint(wf_param_values) print(type(wf_param_values)) for inp in tool_desc["inputs"]: pprint(inp) tool_inp = ToolInput(inp, wf_param_values, steps, 1, should_be_there=True) paramlist += tool_inp.get_formatted_desc() # format the hands-on box body += templates.render(HANDS_ON_TOOL_BOX_TEMPLATE, **{ "tool_name": wf_step['name'], "paramlist": paramlist}) return body
def __init__(self, training, topic, name="new_tuto", title="The new tutorial", zenodo_link=""): """Init a tutorial instance.""" self.training = training self.topic = topic self.name = name self.title = title self.zenodo_link = zenodo_link self.zenodo_file_links = [] self.questions = [] self.objectives = [] self.time = "" self.key_points = [] self.contributors = [] self.body = "" self.init_wf_fp = None self.init_wf_id = None self.hands_on = True self.slides = False self.datatype_fp = "" self.set_dir_name() self.init_data_lib() self.body = templates.render(HANDS_ON_TOOL_BOX_TEMPLATE, **{ 'tool_name': "My Tool", 'inputlist': get_empty_input(), 'paramlist': get_empty_param() })
def get_formatted_inputs(self): """Format the inputs of a step.""" inputlist = '' inps = [] if isinstance(self.wf_param_values, list): # multiple input (not collection) icon = 'param-files' for i in self.wf_param_values: inps.append('`%s` %s' % (i['output_name'], get_input_tool_name(i['id'], self.wf_steps))) else: inp = self.wf_param_values if 'id' in inp: # sinle input or collection inp_type = self.wf_steps[str(inp['id'])]['type'] if 'collection' in inp_type: icon = 'param-collection' else: icon = 'param-file' inps = [ '`%s` %s' % (inp['output_name'], get_input_tool_name(inp['id'], self.wf_steps)) ] if len(inps) > 0: inputlist += templates.render( INPUT_FILE_TEMPLATE, **{ "icon": icon, "input_name": self.tool_inp_desc['label'], "input_value": ', '.join(inps), "space": SPACE * self.level }) return inputlist
def format_wf_steps(wf, gi): """Get a string with the hands-on boxes describing the different steps of the worklow.""" body = '' steps = wf['steps'] for s in range(len(steps)): wf_step = steps[str(s)] # get params in workflow wf_param_values = {} if wf_step['tool_state'] and wf_step['input_connections']: wf_param_values = get_wf_param_values(wf_step['tool_state'], get_wf_inputs(wf_step['input_connections'])) if not wf_param_values: continue # get tool description try: tool_desc = gi.tools.show_tool(wf_step['tool_id'], io_details=True) except Exception: tool_desc = {'inputs': []} # get formatted param description paramlist = '' for inp in tool_desc["inputs"]: tool_inp = ToolInput(inp, wf_param_values, steps, 1, should_be_there=True) paramlist += tool_inp.get_formatted_desc() # format the hands-on box body += templates.render(HANDS_ON_TOOL_BOX_TEMPLATE, **{ "tool_name": wf_step['name'], "tool_id": wf_step['tool_id'], "paramlist": paramlist}) return body
def __init__(self, training, topic, name="new_tuto", title="The new tutorial", zenodo_link=""): """Init a tutorial instance.""" self.training = training self.topic = topic self.name = name self.title = title self.zenodo_link = zenodo_link self.zenodo_file_links = [] self.questions = [] self.objectives = [] self.time = "" self.key_points = [] self.contributors = [] self.body = "" self.init_wf_fp = None self.init_wf_id = None self.hands_on = True self.slides = False self.datatype_fp = "" self.set_dir_name() self.init_data_lib() self.body = templates.render( HANDS_ON_TOOL_BOX_TEMPLATE, **{ 'tool_name': "My Tool", 'inputlist': get_empty_input(), 'paramlist': get_empty_param() })
def get_empty_input(): """Get the string for an empty input.""" return templates.render(INPUT_FILE_TEMPLATE, **{ 'space': 1 * SPACE, 'icon': 'param-file', 'input_name': 'Input file', 'input_value': 'File' })
def get_empty_input(): """Get the string for an empty input.""" return templates.render(INPUT_FILE_TEMPLATE, **{ 'space': 1*SPACE, 'icon': 'param-file', 'input_name': 'Input file', 'input_value': 'File' })
def get_formatted_section_desc(self): """Format the description (label and value) for parameters in a section.""" section_paramlist = '' sub_param_desc = self.get_lower_param_desc() if sub_param_desc != '': section_paramlist += templates.render(INPUT_SECTION, **{ 'space': SPACE * self.level, 'section_label': self.tool_inp_desc['title']}) section_paramlist += sub_param_desc return section_paramlist
def _build_repository(tool_path, tool_el): tool_id = tool_el.getroot().get("id") tool_name = tool_el.getroot().get("name") template_vars = dict( tool_id=tool_id, tool_name=tool_name, ) other_paths = paths[:] other_paths.remove(tool_path) tool_excludes = excludes + list(other_paths) repo_dict = { "include": default_include, "exclude": tool_excludes, } for key in ["name", "description", "long_description"]: template_key = "%s_template" % key template = auto_tool_repos.get(template_key, None) if template: value = templates.render(template, **template_vars) repo_dict[key] = value return repo_dict
def create_tutorial(self, ctx): """Create the skeleton of a new tutorial.""" # create tuto folder and empty files os.makedirs(self.dir) os.makedirs(self.tour_dir) os.makedirs(self.wf_dir) # extract the data library from Zenodo and the links for the tutorial if self.zenodo_link != '': info("Create the data library from Zenodo") self.prepare_data_library_from_zenodo() # create tutorial skeleton from workflow and copy workflow file if self.hands_on: info("Create tutorial skeleton from workflow (if it is provided)") self.create_hands_on_tutorial(ctx) self.export_workflow_file() # create slide skeleton if self.slides: with open(self.slide_fp, 'w') as slide_f: slide_f.write( templates.render(TUTO_SLIDES_TEMPLATE, **{"metadata": self.get_tuto_metata()}))
def _build_repository(tool_path, tool_source): tool_id = tool_source.parse_id().lower() tool_name = tool_source.parse_name() description = tool_source.parse_description() template_vars = dict( tool_id=tool_id, tool_name=tool_name, description=description, ) other_paths = paths[:] other_paths.remove(tool_path) tool_excludes = excludes + list(other_paths) repo_dict = { "include": default_include, "exclude": tool_excludes, } for key in ["name", "description", "long_description"]: template_key = "%s_template" % key template = auto_tool_repos.get(template_key) if template: value = templates.render(template, **template_vars) repo_dict[key] = value return repo_dict
def write_bibliography(self): """Write the content of the bibliography file for the tutorial.""" with open(self.bib_fp, 'w') as bib: bib.write( templates.render(TUTO_BIBLIOGRAPHY_TEMPLATE, **{"body": self.body}))
def _render(kwds, template_str=TOOL_TEMPLATE): """ Apply supplied template variables to TOOL_TEMPLATE to generate the final tool. """ return templates.render(template_str, **kwds)