Skip to content

Commit fe57b28

Browse files
committed
Scripts: clean up gen-man.py
1 parent 5a1be25 commit fe57b28

File tree

1 file changed

+97
-117
lines changed

1 file changed

+97
-117
lines changed

scripts/gen-man.py

Lines changed: 97 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env python3
2+
23
"""
34
Python script to generate a man page for the command `fastfetch`.
4-
The man content will be printed to stdout so you will need to
5+
The man content will be printed to stdout so you will need to
56
pipe it to a file if you want to save it.
67
The command options will be generated using a JSON file.
78
For the format of the JSON file, see https://github.com/fastfetch-cli/fastfetch/blob/dev/src/data/help.json
@@ -10,106 +11,105 @@
1011
from json import load
1112
from datetime import date
1213
from re import search
14+
from os import path
1315

1416

1517
###### Text Decorations Tags ######
1618

17-
startUnderline = "\\fI" # start underline text tag
18-
endUnderline = "\\fR" # end underline text tag
19+
startUnderline = r"\fI" # start underline text tag
20+
endUnderline = r"\fR" # end underline text tag
1921

20-
startBold = "\\fB" # start bold text tag
21-
endBold = "\\fR" # end bold text tag
22+
startBold = r"\fB" # start bold text tag
23+
endBold = r"\fR" # end bold text tag
2224

2325

2426
###### Parameters ######
2527

28+
# path to the current directory
29+
pathToCurrentDir = path.dirname(__file__)
2630
# path to the JSON option file
27-
pathToHelpFile = "../src/data/help.json"
31+
pathToHelpFile = path.join(pathToCurrentDir, "../src/data/help.json")
2832
# man page section
2933
manSection = 1
3034
# title (center header)
31-
titlePage = "Fastfetch man page"
35+
titlePage = "Fastfetch man page"
3236
# date (center footer)
3337
todayDate = date.today().strftime("%b %d %Y") # format : "Month (abbreviation) Day Year"
3438
# file to fastfetch version (left footer)
35-
pathToVersionFile = "../CMakeLists.txt"
39+
pathToVersionFile = path.join(pathToCurrentDir, "../CMakeLists.txt")
3640

3741

3842
###### Sections Text ######
3943

4044
# text displayed in the "NAME" section
41-
nameSection = "\
42-
fastfetch - a neofetch-like tool for fetching system \
43-
information and displaying them in a pretty way"
44-
45-
# text displayed in the "DESCRIPTION" section
46-
descriptionSection = "\
47-
A maintained, feature-rich and performance \
48-
oriented, neofetch like system information tool."
45+
nameSection = r"fastfetch \- A maintained, feature\-rich and performance oriented, neofetch like system information tool"
4946

5047
# text displayed at the beginning of the "OPTIONS" section
51-
optionSection = "\
52-
Parsing is not case sensitive. E.g. \\fB--lib-PCI\\fR \
53-
is equal to \\fB--Lib-Pci\\fR. \
54-
\n\n\
55-
If a value is between square brakets, it is optional. \
56-
An optional boolean value defaults to true if not \
57-
specified. \
58-
\n\n\
59-
More detailed help messages for each options can be \
60-
printed with \\fB-h <option_without_dash_prefix>\\fR. \
61-
\n\n\
62-
All options can be made permanent with command \
63-
\\fBfastfetch <options> --gen-config\\fR. \
64-
"
48+
optionSection = r"""
49+
Parsing is not case sensitive. E.g. \fB--logo-type\fR is
50+
equal to \fB--LOGO-TYPE\fR.
51+
52+
If a value is between square brakets, it is optional.
53+
An optional boolean value defaults to true if not specified.
54+
55+
More detailed help messages for each options can be printed
56+
with \fB-h <option_without_dash_prefix>\fR.
57+
58+
All options can be made permanent with command
59+
\fBfastfetch <options> --gen-config\fR.
60+
"""
6561

6662
# text displayed in the "CONFIGURATION"
67-
configurationSection = f"\
68-
.SS Fetch Structure \n\
69-
The structure of a fetch describes the modules that should \
70-
be included in the output. It consists of a string of modules, \
71-
separated by a colon (:). To list all available modules, \
72-
use --list-modules \
73-
\n\n\
74-
.SS Config Files \n\
75-
Fastfetch uses JSONC based format for configuration. \
76-
Fastfetch doesn't generate config file automatically; \
77-
it should be generated manually by {startBold}--gen-config.{endBold} \
78-
The config file will be saved in \
79-
{startBold}~/.config/fastfetch/config.jsonc{endBold} by default. \
80-
\n\n\
81-
A JSONC config file is a JSON file that also supports comments \
82-
with (// and /* */). Those files must have the extension '.jsonc'. \
83-
\n\n\
84-
The specified configuration/preset files are searched in the following order: \
85-
\n\n\
86-
{startBold}1.{endBold} relative to the current working directory \
87-
\n\n\
88-
{startBold}2.{endBold} relative to ~/.local/share/fastfetch/presets/ \
89-
\n\n\
90-
{startBold}3.{endBold} relative to /usr/share/fastfetch/presets/ \
91-
\n\n\
92-
Fastfetch provides some default presets. List them with --list-presets. \
93-
"
63+
configurationSection = f"""
64+
.SS Fetch Structure
65+
66+
The structure of a fetch describes the modules that should
67+
be included in the output. It consists of a string of modules,
68+
separated by a colon (:). To list all available modules,
69+
use --list-modules.
70+
71+
72+
.SS Config Files
73+
74+
Fastfetch uses JSONC based format for configuration.
75+
Fastfetch doesn't generate config file automatically;
76+
it should be generated manually by {startBold}--gen-config{endBold}.
77+
The config file will be saved in
78+
{startBold}~/.config/fastfetch/config.jsonc{endBold} by default.
79+
80+
A JSONC config file is a JSON file that also supports comments
81+
with (// and /* */). Those files must have the extension '.jsonc'.
82+
83+
The specified configuration/preset files are searched in the following order:
84+
85+
{startBold}1.{endBold} relative to the current working directory
86+
87+
{startBold}2.{endBold} relative to ~/.local/share/fastfetch/presets/
88+
89+
{startBold}3.{endBold} relative to /usr/share/fastfetch/presets/
90+
91+
Fastfetch provides some default presets. List them with --list-presets.
92+
"""
9493

9594
# text displayed in the "EXAMPLE" section
96-
exampleSection = "\
97-
.SS Config files:\n\
98-
.nf \
99-
// ~/.config/fastfetch/config.jsonc \n\
100-
{\n\
101-
\"$schema\": \"https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json\",\n\
102-
\"modules\": [ \n\
103-
\"title\", \n\
104-
\"separator\", \n\
105-
\"module1\", \n\
106-
{ \n\
107-
\"type\": \"module2\", \n\
108-
\"module2-option\": \"value\" \n\
109-
} \n\
110-
]\n\
111-
} \n\
112-
.fi"
95+
exampleSection = """
96+
.SS Config files:
97+
.nf
98+
// ~/.config/fastfetch/config.jsonc
99+
{
100+
"$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json",
101+
"modules": [
102+
"title",
103+
"separator",
104+
"module1",
105+
{
106+
"type": "module2",
107+
"module2-option": "value"
108+
}
109+
]
110+
}
111+
.fi
112+
"""
113113

114114
# text displayed in the "BUGS" section
115115
bugSection = "Please report bugs to : \
@@ -128,50 +128,37 @@
128128
startOptionalArgument = f"[{startUnderline}?"
129129
# if an optional argument is displayed as [?optArg] (with "optArg underlined")
130130
# this value should be f"{endUnderline}]"
131-
endOptionalArgument = f"{endUnderline}]"
131+
endOptionalArgument = f"{endUnderline}]"
132132

133133
### mandatory arguments tags ###
134-
startMandatoryArgument = f"{startUnderline}"
134+
startMandatoryArgument = f"{startUnderline}"
135135
endMandatoryArgument = f"{endUnderline}"
136136

137-
def generateManPage():
137+
def main():
138138

139139
# importing the JSON file
140-
try:
141-
with open(pathToHelpFile, 'r') as jsonFile:
142-
helpFileData = load(jsonFile) # json.load
143-
jsonFile.close()
144-
except IOError as error:
145-
print("Error with file", pathToHelpFile, ":", error)
146-
return
147-
140+
with open(pathToHelpFile, 'r') as jsonFile:
141+
helpFileData = load(jsonFile) # json.load
142+
148143

149144
######## Start printing the generated .1 file ########
150145

151146

152147
###### header, footer & config #####
153148

154-
print(f".TH man {manSection}", end=" ")
149+
print(f".TH FASTFETCH {manSection} ", end=" ")
155150
print(f"\"{todayDate}\"", end=" ")
156151

157152
# version number
158-
try:
159-
with open(pathToVersionFile, 'r') as versionFile:
160-
161-
# research version number in file with regex
162-
for line in versionFile:
163-
researchVersion = search("^\s*VERSION (\d+\.\d+\.\d+)$", line) # re.search()
164-
if (researchVersion != None):
165-
versionNumber = "".join(line[researchVersion.start():researchVersion.end()].split(" "))
166-
versionNumber = versionNumber[:7] + " " + versionNumber[7:]
167-
break
168-
169-
versionFile.close()
170-
except IOError as error:
171-
print("Error with file", pathToHelpFile, ":", error)
172-
return
173-
174-
print(f"\"{versionNumber}\"", end=" ")
153+
with open(pathToVersionFile, 'r') as versionFile:
154+
155+
# research version number in file with regex
156+
for line in versionFile:
157+
researchVersion = search(r"^\s*VERSION (\d+\.\d+\.\d+)$", line)
158+
if (researchVersion):
159+
print(f"\"{researchVersion.group(1)}\"", end=" ")
160+
break
161+
175162
print(f"\"{titlePage}\"")
176163

177164

@@ -185,13 +172,7 @@ def generateManPage():
185172

186173
print(".SH SYNOPSIS")
187174
print(".B fastfetch")
188-
print(f"[{startUnderline}OPTIONS{endUnderline}]")
189-
190-
191-
##### Description #####
192-
193-
print(".SH DESCRIPTION")
194-
print(descriptionSection)
175+
print(f"[{startUnderline}OPTIONS{endUnderline}]")\
195176

196177

197178
###### Wiki ######
@@ -229,17 +210,17 @@ def generateManPage():
229210

230211
# short option (-opt)
231212
if "short" in keyList:
232-
print(f"\\-{ option['short'] }", end="")
213+
print(fr"\-{ option['short'] }", end="")
233214
# if also have a long option, print a comma
234215
if "long" in keyList:
235216
print(", ", end="")
236217

237218
# long option (--option)
238219
if "long" in keyList:
239-
print(f"\\-\\-{ option['long'] }", end="")
220+
print(fr"\-\-{ option['long'] }", end="")
240221

241222
print(endBold, end=" ")
242-
223+
243224
# arguments
244225
if "arg" in keyList:
245226
# if argument is optional, print "[arg]"
@@ -249,10 +230,10 @@ def generateManPage():
249230
# if argument is mandatory, print "arg"
250231
else:
251232
print(startMandatoryArgument + option['arg']['type'] + endMandatoryArgument, end="")
252-
233+
253234
# description
254235
print(f"\n {option['desc']} \n")
255-
236+
256237

257238
###### Examples ######
258239

@@ -268,6 +249,5 @@ def generateManPage():
268249

269250

270251

271-
272252
if __name__ == "__main__":
273-
generateManPage()
253+
main()

0 commit comments

Comments
 (0)