-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtk-readme-generator.py
109 lines (87 loc) · 2.65 KB
/
tk-readme-generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
Readme generator for ShotGrid configurations, apps, frameworks and engines.
"""
import argparse
from enum import Enum
from pathlib import Path
import yaml
from generator_config import generate_config_readme
from generator_general import generate_general_readme
class Mode(Enum):
general = "general"
config = "config"
def __str__(self):
return self.value
parser = argparse.ArgumentParser(
prog="tk-readme-generator",
description="Generate a README file from a info.yml file",
)
parser.add_argument("file", help="The info file to use.")
parser.add_argument(
"-o", "--override", action="store_true", help="Override the existing readme."
)
parser.add_argument(
"-p",
"--prepend",
metavar="FILEPATH",
help="Prepend an existing readme file after the name and description.",
)
parser.add_argument(
"-a",
"--append",
metavar="FILEPATH",
help="Append an existing readme file to the end.",
)
parser.add_argument(
"-m",
"--mode",
type=Mode,
choices=list(Mode),
help="Make a readme for a general info file (framework/engine/app) or a config info file.",
)
args = parser.parse_args()
def generate_readme(
file: str,
mode: Mode = None,
override: bool = False,
prepend: str = None,
append: str = None,
):
"""
Generate a readme from an info.yml file.
Args:
file: File path to info.yml
mode: Readme type to create
override: Override existing README.md (Default is `False`)
prepend: File path to Markdown file to prepend in Readme
append: File path to Markdown file to append in Readme
"""
filepath = Path(file)
if not filepath.is_file():
msg = "The file couldn't be found."
raise Exception(msg)
with open(file, "r") as file:
info = yaml.safe_load(file)
if info is None:
msg = "The file couldn't be parsed."
raise Exception(msg)
# Decide mode if not provided
if mode is None:
includes_filepath = filepath.parent / "env" / "includes"
if includes_filepath.is_dir():
mode = Mode.config
else:
mode = Mode.general
# Generate readme
if mode == Mode.general:
readme = generate_general_readme(filepath, prepend, append)
else:
readme = generate_config_readme(
filepath,
)
output_file = Path(filepath.parent) / "README.md"
if output_file.is_file() and not override:
output_file = Path(filepath.parent) / "README_GENERATED.md"
with open(output_file, "w") as text_file:
text_file.write(readme)
generate_readme(args.file, args.mode, args.override, args.prepend, args.append)