-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·174 lines (144 loc) · 3.52 KB
/
build.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
"""
Author Kieran W 20200118
Build a README file using fragments from the following subdirectories
core
lang
extras
"""
# Imports
import argparse
import re
import os
# Constants
DIR_C = "core/"
DIR_L = "lang/"
DIR_E = "extras/"
FILE_EXT = ".md"
FILE_OUT = "output" + FILE_EXT
FRAG_DIV = "\n\n"
def fileToString(filepath):
"""Return the contents of a file as a string using a (relative) filepath
Args:
filepath (string): The file path
Returns:
string: The contents of the file
"""
string = ""
with open(filepath, "r") as file:
data = file.read()
for line in data:
string += line
return string + FRAG_DIV
def stringToFile(filepath, string):
"""Write a string to a file defined with a (relative) path
Args:
filepath (string): The file path
string (string): The string to write to the file
"""
file = open(filepath, "w")
file.write(string)
file.close()
"""
Command line arguments
"""
parser = argparse.ArgumentParser(
description="""Build a README file using fragments from the following
subdirectories
""")
parser.add_argument(
"-o",
"--output",
action="store",
help="""Enter the filename to output to with or without ".md". Leave blank for default
""")
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="""Print debug to the terminal
""")
# Define some variables
args = parser.parse_args()
debug = args.verbose
buildString = ""
if (debug):
print(args)
"""
Do building
"""
buildString += fileToString(DIR_C + "info.md")
if (debug):
print("added info")
buildString += fileToString(DIR_C + "proj-name.md")
if (debug):
print("added name")
"""
Set the Language
"""
options = []
for subdir, dirs, files in os.walk("lang"):
for fileName in files:
options += [subdir + os.sep + fileName]
print("-1/[enter]: [none]")
for index, option in enumerate(options):
print(str(index) + ":", option)
choiceString = input(
"Select the number that corresponds to an option above (between, and including, 0 and " +
str(
len(options) -
1) +
")\n>")
if len(choiceString) > 0:
choice = int(choiceString)
if choice >= 0 and choice < len(options):
buildString += fileToString(options[choice])
if (debug):
print("added language: " + options[choice])
buildString += fileToString(DIR_C + "proj-changelog.md")
if (debug):
print("added changelog")
buildString += fileToString(DIR_C + "proj-down.md")
if (debug):
print("added download")
buildString += fileToString(DIR_C + "proj-lice.md")
if (debug):
print("added license")
"""
Extras (can select multiple - order s-mobile, s-desktop, theme, browser-s)
"""
options = os.listdir("extras")
print("-1/[enter]: [none]")
for index, option in enumerate(options):
print(str(index) + ":", option)
choiceString = input(
"Select the number that corresponds to an option above (between, and including, 0 and " +
str(
len(options) -
1) +
" and separate with \",\")\n>")
if len(choiceString) > 0:
choices = choiceString.split(",")
for choice in choices:
choiceInt = int(choice)
if choiceInt >= 0 and choiceInt < len(options):
buildString += fileToString(DIR_E + options[choiceInt])
if (debug):
print("added extra: " + options[choiceInt])
"""
Get slugs to mangle
"""
print("Fill in the slugs:")
for tag in set(re.findall("{{(.*?)}}", buildString)):
tagReplace = input(tag + ":")
buildString = buildString.replace("{{" + tag + "}}", tagReplace)
"""
Output the file
"""
if args.output is not None:
outFileName = args.output
else:
outFileName = FILE_OUT
stringToFile(outFileName, buildString)
if (debug):
print("written file")