示例#1
0
def prepare_all_tools():
    JanisShed.hydrate(modules=[janis_unix, janis_bioinformatics])

    data_types = JanisShed.get_all_datatypes()
    tools = {
        ts[0].id(): {t.version(): t
                     for t in ts}
        for ts in JanisShed.get_all_tools()
    }

    Logger.info(f"Preparing documentation for {len(tools)} tools")
    Logger.info(f"Preparing documentation for {len(data_types)} data_types")

    tool_module_index = {}
    dt_module_index = {}
    ROOT_KEY = "root"

    for toolname, toolsbyversion in tools.items():
        # tool = tool_vs[0][0]()
        tool_versions = sort_tool_versions(list(toolsbyversion.keys()))
        default_version = tool_versions[0]
        Logger.log(
            f"Preparing {toolname}, found {len(tool_versions)} version[s] ({','.join(tool_versions)})"
        )

        defaulttool = toolsbyversion[default_version]
        if isclass(defaulttool):
            defaulttool = defaulttool()
        try:
            tool_path_components = list(
                filter(
                    lambda a: bool(a),
                    [defaulttool.tool_module(),
                     defaulttool.tool_provider()],
                ))
        except Exception as e:
            Logger.critical(f"Failed to generate docs for {toolname}: {e}")
            continue

        # (toolURL, tool, isPrimary)
        toolurl_to_tool = [(toolname.lower(), defaulttool, True)] + [
            (get_tool_url(toolname, v), toolsbyversion[v], False)
            for v in tool_versions
        ]

        path_components = "/".join(tool_path_components)
        output_dir = f"{tools_dir}/{path_components}/".lower()
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        for (toolurl, tool, isprimary) in toolurl_to_tool:
            output_str = prepare_tool(tool, tool_versions, not isprimary)
            output_filename = output_dir + toolurl + ".rst"
            with open(output_filename, "w+") as tool_file:
                tool_file.write(output_str)

        nested_keys_append_with_root(tool_module_index,
                                     tool_path_components,
                                     toolname,
                                     root_key=ROOT_KEY)

        Logger.log("Prepared " + toolname)

    for d in data_types:
        # tool = tool_vs[0][0]()
        if issubclass(d, Array):
            Logger.info("Skipping Array DataType")
            continue
        try:
            dt = d()
        except:
            print(d.__name__ + " failed to instantiate")
            continue
        did = dt.name().lower()
        Logger.log("Preparing " + dt.name())
        output_str = prepare_data_type(dt)

        dt_path_components = []
        # dt_path_components = list(filter(
        #     lambda a: bool(a),
        #     [, tool.tool_provider()]
        # ))

        path_components = "/".join(dt_path_components)
        output_dir = f"{dt_dir}{path_components}/"
        output_filename = output_dir + did + ".rst"

        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        nested_keys_append_with_root(dt_module_index,
                                     dt_path_components,
                                     did,
                                     root_key=ROOT_KEY)

        with open(output_filename, "w+") as dt_file:
            dt_file.write(output_str)

        Logger.log("Prepared " + did)

    def prepare_modules_in_index(contents, title, dir, max_depth=1):
        module_filename = dir + "/index.rst"
        module_tools = sorted(
            set(contents[ROOT_KEY] if ROOT_KEY in contents else []))
        submodule_keys = sorted(m for m in contents.keys() if m != ROOT_KEY)
        indexed_submodules_tools = [m.lower() for m in submodule_keys]

        with open(module_filename, "w+") as module_file:
            module_file.write(
                get_tool_toc(
                    alltoolsmap=tools,
                    title=title,
                    intro_text=
                    f"Automatically generated index page for {title}:",
                    subpages=indexed_submodules_tools,
                    tools=module_tools,
                    max_depth=max_depth,
                ))

        for submodule in submodule_keys:
            prepare_modules_in_index(contents=contents[submodule],
                                     title=submodule,
                                     dir=f"{dir}/{submodule}/")

    def prepare_dtmodules_in_index(contents, title, dir, max_depth=1):
        module_filename = dir + "/index.rst"
        module_tools = sorted(
            set(contents[ROOT_KEY] if ROOT_KEY in contents else []))
        submodule_keys = sorted(m for m in contents.keys() if m != ROOT_KEY)
        indexed_submodules_tools = [
            m.lower() + "/index" for m in submodule_keys
        ]

        with open(module_filename, "w+") as module_file:
            module_file.write(
                get_toc(
                    title=title,
                    intro_text=
                    f"Automatically generated index page for {title}:",
                    subpages=indexed_submodules_tools + module_tools,
                    max_depth=max_depth,
                ))

        for submodule in submodule_keys:
            prepare_modules_in_index(contents=contents[submodule],
                                     title=submodule,
                                     dir=f"{dir}/{submodule}/")

    prepare_modules_in_index(tool_module_index, title="Tools", dir=tools_dir)
    prepare_dtmodules_in_index(dt_module_index,
                               title="Data Types",
                               dir=dt_dir,
                               max_depth=1)
示例#2
0
def prepare_code_tool_page(tool: CodeTool, versions: List[str]):
    if not tool:
        return None

        # tool_modules = tool.__module__.split(".") # janis._bioinformatics_.tools.$toolproducer.$toolname.$version

    metadata: ToolMetadata = tool.bind_metadata() or tool.metadata

    if not tool.friendly_name():
        raise Exception(
            f"Tool '{type(tool).__name__}' ({tool.id()}) did not provide the required 'friendly_name' for the docs"
        )

    fn = tool.friendly_name() if tool.friendly_name() else tool.id()
    en = f" ({tool.id()})" if fn != tool.id() else ""
    tn = fn + en

    onelinedescription = prepare_byline(tool.id(),
                                        metadata.short_documentation,
                                        metadata.contributors, versions)

    formatted_url = (format_rst_link(metadata.documentationUrl,
                                     metadata.documentationUrl)
                     if metadata.documentationUrl else
                     "*No URL to the documentation was provided*")

    toolmetadata = [
        t for t in [
            ("ID", f"``{tool.id()}``"),
            ("URL", formatted_url),
            ("Versions", ", ".join(versions)),
            ("Container", tool.container()),
            ("Authors", ", ".join(metadata.contributors)),
            ("Citations", metadata.citation),
            ("DOI", metadata.doi) if metadata.doi else None,
            ("Created", str(metadata.dateCreated)),
            ("Updated", str(metadata.dateUpdated)),
        ] if t and len(t) == 2
    ]

    input_headers = ["name", "type", "documentation"]

    required_input_tuples = [[
        i.id(), i.intype.id(), i.doc.doc if i.doc else ""
    ] for i in tool.tool_inputs() if not i.intype.optional]
    optional_input_tuples = [[
        i.id(), i.intype.id(), i.doc.doc if i.doc else ""
    ] for i in tool.tool_inputs() if i.intype.optional]

    formatted_inputs = tabulate(required_input_tuples + optional_input_tuples,
                                input_headers,
                                tablefmt="rst")

    formatted_toolversions_array = []
    formatted_toolincludes_array = []
    for v in versions:
        link = get_tool_url(tool.id(), v)
        formatted_toolincludes_array.append(".. include:: " + link)
        if v == tool.version():
            formatted_toolversions_array.append(
                f"- {v} (current)"
            )  # + format_rst_link(v + " (current)", link))
        else:
            formatted_toolversions_array.append(
                "- " + format_rst_link(v, link + ".html"))

    output_headers = ["name", "type", "documentation"]
    output_tuples = [[o.id(), o.outtype.id(), o.doc]
                     for o in tool.tool_outputs()]
    formatted_outputs = tabulate(output_tuples, output_headers, tablefmt="rst")

    tool_prov = ""
    if tool.tool_provider() is None:
        print("Tool :" + tool.id() + " has no company")
    else:
        tool_prov = "." + tool.tool_provider().lower()

    nl = "\n"

    return f"""\