Skip to content

Commit

Permalink
Fixed doc generation
Browse files Browse the repository at this point in the history
  • Loading branch information
pchakraborty committed Apr 5, 2024
1 parent 3c19f0e commit 8c9a8a9
Showing 1 changed file with 40 additions and 32 deletions.
72 changes: 40 additions & 32 deletions docs/make_md_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import io
import glob
from mdutils.mdutils import MdUtils
import subprocess as sp

Expand All @@ -13,69 +14,75 @@
doc_dir_path = os.path.dirname(os.path.realpath(__file__))
# Then we need to get to the mepo/mepo.d/command directory. First the "main" dir
main_dir_path = os.path.dirname(doc_dir_path)
# Now add 'src/mepo'
mepod_dir_path = os.path.join(main_dir_path, 'src', 'mepo')
# And then 'command'
command_dir_path = os.path.join(mepod_dir_path, 'command')
# Now add "src/mepo"
mepod_dir_path = os.path.join(main_dir_path, "src", "mepo")
# And then "command"
command_dir_path = os.path.join(mepod_dir_path, "command")

mepo_command_path = os.path.join(main_dir_path, 'bin', 'mepo')
mepo_command_path = os.path.join(main_dir_path, "bin", "mepo")

def get_command_list(directory):
# Walk the tree
roots = [x[0] for x in os.walk(directory)]
# # Walk the tree
# roots = [x[0] for x in os.walk(directory)]

# Now remove "." from the list
roots = roots[1:]
# # Now remove "." from the list
# roots = roots[1:]
# print(f"roots: {roots}")

# Just get the relative paths
rel_roots = [os.path.relpath(x,directory) for x in roots]
# # Just get the relative paths
# rel_roots = [os.path.relpath(x,directory) for x in roots]
# print(f"rel roots: {rel_roots}")

# Now exclude __pycache__
command_dirs = [x for x in rel_roots if '__pycache__' not in x]
# # Now exclude __pycache__
# command_dirs = [x for x in rel_roots if "__pycache__" not in x]
# print(f"command dirs: {command_dirs}")

# Convert slashes to spaces
all_commands = [x.replace('/',' ') for x in command_dirs]

# Now let's find the commands that have subcommands
## First we get commands with spaces
commands_with_spaces = [x for x in all_commands if ' ' in x]
## Now let's just get the first elements
temp = [x.split()[0] for x in commands_with_spaces]
## Get the uniques
commands_with_subcommands = list(set(temp))
all_commands_py = glob.glob(os.path.join(directory, "*.py"))
all_commands = [os.path.basename(x).replace(".py", "") for x in all_commands_py]
all_commands.remove("command") # manually remove

# Now let"s find the commands that have subcommands
## First we get commands with underscore
commands_with_underscore = [x for x in all_commands if "_" in x]
commands_with_subcommands = [x.replace("_", " ") for x in commands_with_underscore]

# Now remove those from our list
all_useful_commands = [x for x in all_commands if x not in commands_with_subcommands]
all_useful_commands = [x for x in all_commands if x not in commands_with_underscore]
all_useful_commands += commands_with_subcommands

return sorted(all_useful_commands)

def create_markdown_from_usage(command, mdFile):
cmd = [mepo_command_path, command, '--help']
cmd = [mepo_command_path, command, "--help"]

# Some commands have spaces, so we need to break it up again
cmd = ' '.join(cmd).split()
cmd = " ".join(cmd).split()
print(f"command: {cmd}")

result = sp.run(cmd, capture_output=True, universal_newlines=True, env={'COLUMNS':'256'})
result = sp.run(cmd, capture_output=True, universal_newlines=True, env={"COLUMNS":"256"})
output = result.stdout
print(f"output: {output}")

output_list = output.split("\n")
print(f"output list: {output_list}")

# Command summary
summary = output_list[2]
mdFile.write(summary)

# Usage
usage = output_list[0]
usage = usage.replace('usage: ','')
usage = usage.replace("usage: ","")
mdFile.new_header(level=3, title="Usage")
mdFile.insert_code(usage)

positional_arguments = output.partition('positional arguments:\n')[2].partition('\n\n')[0]
positional_arguments = output.partition("positional arguments:\n")[2].partition("\n\n")[0]
if positional_arguments:
mdFile.new_header(level=3, title="Positional Arguments")
mdFile.insert_code(positional_arguments)

optional_arguments = output.partition('optional arguments:\n')[2].partition('\n\n')[0]
optional_arguments = output.partition("optional arguments:\n")[2].partition("\n\n")[0]
# Remove extra blank lines
optional_arguments = os.linesep.join([s for s in optional_arguments.splitlines() if s])
if optional_arguments:
Expand All @@ -84,18 +91,19 @@ def create_markdown_from_usage(command, mdFile):

if __name__ == "__main__":

doc_file = 'Mepo-Commands.md'
doc_file = "Mepo-Commands.md"
mdFile = MdUtils(file_name=doc_file)

mdFile.new_header(level=1, title="Overview")
mdFile.new_paragraph(preamble)

mdFile.new_header(level=1, title="Commands")
command_list = get_command_list(command_dir_path)
print(f"command_list: {command_list}")
for command in command_list:
mdFile.new_header(level=2, title=command)
create_markdown_from_usage(command,mdFile)

mdFile.new_table_of_contents(table_title='Table of Contents', depth=2)
mdFile.new_table_of_contents(table_title="Table of Contents", depth=2)
mdFile.create_md_file()
print(f'Generated {doc_file}.')
print(f"Generated {doc_file}.")

0 comments on commit 8c9a8a9

Please sign in to comment.