#!/usr/bin/env python3 # docstring=lists all tools and their docstrings import argparse import os import os.path as path CFG = { "max_lines": 20, "offset": 20 } SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__)) def parse_docstring(file_path, max_lines=CFG["max_lines"]): """ in file `file_path` and from the top until `max_lines` is reached, checks if `docstring=` is present. """ with open(file_path, "r") as file_fd: count = 0 while count <= max_lines: count += 1 line = file_fd.readline() if "docstring=" in line: try: return line.split("=")[1].strip("\n").strip(" ") except IndexError: return "" return "" def offset(element, offset=CFG["offset"]): """ calculates the amount of spaces until `offset` is reached for `element` length """ element_length = len(element.strip("")) if element_length >= offset: return "" else: return " " * (offset - element_length) def list_tools(): """ lists all tools and their docstring from the same directory this sits in """ list_dir = sorted(os.listdir(SCRIPT_PATH)) for element in list_dir: element_path = path.join(SCRIPT_PATH, element) if path.isfile(element_path): print(element, end="") docstring = parse_docstring(element_path) if docstring: print(f":{offset(element)}{docstring}", end="") print() def main(): ap = argparse.ArgumentParser() ap.add_argument("-p", "--path", help="output path to tools directory", action="store_true") args = ap.parse_args() if args.path: print(SCRIPT_PATH) else: list_tools() if __name__ == "__main__": main()