def load_info_from_docstring(docstring, *, delimiter="---"): """Load summary and description from docstring :param str delimiter: Summary and description information delimiter. If a line starts with this string, this line and the lines after are ignored. Defaults to "---". """ split_lines = trim_docstring(docstring).split('\n') if delimiter is not None: # Info is separated from rest of docstring by a `delimiter` line for index, line in enumerate(split_lines): if line.lstrip().startswith(delimiter): cut_at = index break else: cut_at = index + 1 split_lines = split_lines[:cut_at] # Description is separated from summary by an empty line for index, line in enumerate(split_lines): if line.strip() == '': summary_lines = split_lines[:index] description_lines = split_lines[index + 1:] break else: summary_lines = split_lines description_lines = [] info = {} if summary_lines: info['summary'] = dedent('\n'.join(summary_lines)) if description_lines: info['description'] = dedent('\n'.join(description_lines)) return info
def load_info_from_docstring(docstring): """Load summary and description from docstring""" split_lines = trim_docstring(docstring).split('\n') # Info is separated from rest of docstring by a '---' line for index, line in enumerate(split_lines): if line.lstrip().startswith('---'): cut_at = index break else: cut_at = index + 1 split_info_lines = split_lines[:cut_at] # Description is separated from summary by an empty line for index, line in enumerate(split_info_lines): if line.strip() == '': summary_lines = split_info_lines[:index] description_lines = split_info_lines[index + 1:] break else: summary_lines = split_info_lines description_lines = [] info = {} if summary_lines: info['summary'] = dedent('\n'.join(summary_lines)) if description_lines: info['description'] = dedent('\n'.join(description_lines)) return info
def load_yaml_from_docstring(docstring): """Loads YAML from docstring.""" split_lines = trim_docstring(docstring).split('\n') # Cut YAML from rest of docstring for index, line in enumerate(split_lines): line = line.strip() if line.startswith('---'): cut_from = index break else: return {} yaml_string = '\n'.join(split_lines[cut_from:]) yaml_string = dedent(yaml_string) return yaml.safe_load(yaml_string) or {}
def load_yaml_from_docstring(self, docstring): """Loads YAML from docstring.""" split_lines = trim_docstring(docstring).split("\n") # Cut YAML from rest of docstring for index, line in enumerate(split_lines): line = line.strip() if line.startswith("---"): cut_from = index break else: return {} split_lines = split_lines[cut_from:] if len(split_lines) > 1 and split_lines[cut_from + 1].startswith('doc_template'): _, template_file = split_lines[cut_from + 1].split(':') template_file = template_file.strip() template = loader.get_template(template_file) yaml_string = template.render({}) else: yaml_string = "\n".join(split_lines) yaml_string = dedent(yaml_string) return yaml.safe_load(yaml_string) or {}