Scripts: clean up gen-man.py

This commit is contained in:
李通洲
2024-11-13 10:09:36 +08:00
parent 5a1be2535b
commit fe57b28581
+95 -115
View File
@@ -1,7 +1,8 @@
#!/usr/bin/env python3
"""
Python script to generate a man page for the command `fastfetch`.
The man content will be printed to stdout so you will need to
The man content will be printed to stdout so you will need to
pipe it to a file if you want to save it.
The command options will be generated using a JSON file.
For the format of the JSON file, see https://github.com/fastfetch-cli/fastfetch/blob/dev/src/data/help.json
@@ -10,106 +11,105 @@ For the format of the JSON file, see https://github.com/fastfetch-cli/fastfetch/
from json import load
from datetime import date
from re import search
from os import path
###### Text Decorations Tags ######
startUnderline = "\\fI" # start underline text tag
endUnderline = "\\fR" # end underline text tag
startUnderline = r"\fI" # start underline text tag
endUnderline = r"\fR" # end underline text tag
startBold = "\\fB" # start bold text tag
endBold = "\\fR" # end bold text tag
startBold = r"\fB" # start bold text tag
endBold = r"\fR" # end bold text tag
###### Parameters ######
# path to the current directory
pathToCurrentDir = path.dirname(__file__)
# path to the JSON option file
pathToHelpFile = "../src/data/help.json"
pathToHelpFile = path.join(pathToCurrentDir, "../src/data/help.json")
# man page section
manSection = 1
# title (center header)
titlePage = "Fastfetch man page"
titlePage = "Fastfetch man page"
# date (center footer)
todayDate = date.today().strftime("%b %d %Y") # format : "Month (abbreviation) Day Year"
# file to fastfetch version (left footer)
pathToVersionFile = "../CMakeLists.txt"
pathToVersionFile = path.join(pathToCurrentDir, "../CMakeLists.txt")
###### Sections Text ######
# text displayed in the "NAME" section
nameSection = "\
fastfetch - a neofetch-like tool for fetching system \
information and displaying them in a pretty way"
# text displayed in the "DESCRIPTION" section
descriptionSection = "\
A maintained, feature-rich and performance \
oriented, neofetch like system information tool."
nameSection = r"fastfetch \- A maintained, feature\-rich and performance oriented, neofetch like system information tool"
# text displayed at the beginning of the "OPTIONS" section
optionSection = "\
Parsing is not case sensitive. E.g. \\fB--lib-PCI\\fR \
is equal to \\fB--Lib-Pci\\fR. \
\n\n\
If a value is between square brakets, it is optional. \
An optional boolean value defaults to true if not \
specified. \
\n\n\
More detailed help messages for each options can be \
printed with \\fB-h <option_without_dash_prefix>\\fR. \
\n\n\
All options can be made permanent with command \
\\fBfastfetch <options> --gen-config\\fR. \
"
optionSection = r"""
Parsing is not case sensitive. E.g. \fB--logo-type\fR is
equal to \fB--LOGO-TYPE\fR.
If a value is between square brakets, it is optional.
An optional boolean value defaults to true if not specified.
More detailed help messages for each options can be printed
with \fB-h <option_without_dash_prefix>\fR.
All options can be made permanent with command
\fBfastfetch <options> --gen-config\fR.
"""
# text displayed in the "CONFIGURATION"
configurationSection = f"\
.SS Fetch Structure \n\
The structure of a fetch describes the modules that should \
be included in the output. It consists of a string of modules, \
separated by a colon (:). To list all available modules, \
use --list-modules \
\n\n\
.SS Config Files \n\
Fastfetch uses JSONC based format for configuration. \
Fastfetch doesn't generate config file automatically; \
it should be generated manually by {startBold}--gen-config.{endBold} \
The config file will be saved in \
{startBold}~/.config/fastfetch/config.jsonc{endBold} by default. \
\n\n\
A JSONC config file is a JSON file that also supports comments \
with (// and /* */). Those files must have the extension '.jsonc'. \
\n\n\
The specified configuration/preset files are searched in the following order: \
\n\n\
{startBold}1.{endBold} relative to the current working directory \
\n\n\
{startBold}2.{endBold} relative to ~/.local/share/fastfetch/presets/ \
\n\n\
{startBold}3.{endBold} relative to /usr/share/fastfetch/presets/ \
\n\n\
Fastfetch provides some default presets. List them with --list-presets. \
"
configurationSection = f"""
.SS Fetch Structure
The structure of a fetch describes the modules that should
be included in the output. It consists of a string of modules,
separated by a colon (:). To list all available modules,
use --list-modules.
.SS Config Files
Fastfetch uses JSONC based format for configuration.
Fastfetch doesn't generate config file automatically;
it should be generated manually by {startBold}--gen-config{endBold}.
The config file will be saved in
{startBold}~/.config/fastfetch/config.jsonc{endBold} by default.
A JSONC config file is a JSON file that also supports comments
with (// and /* */). Those files must have the extension '.jsonc'.
The specified configuration/preset files are searched in the following order:
{startBold}1.{endBold} relative to the current working directory
{startBold}2.{endBold} relative to ~/.local/share/fastfetch/presets/
{startBold}3.{endBold} relative to /usr/share/fastfetch/presets/
Fastfetch provides some default presets. List them with --list-presets.
"""
# text displayed in the "EXAMPLE" section
exampleSection = "\
.SS Config files:\n\
.nf \
// ~/.config/fastfetch/config.jsonc \n\
{\n\
\"$schema\": \"https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json\",\n\
\"modules\": [ \n\
\"title\", \n\
\"separator\", \n\
\"module1\", \n\
{ \n\
\"type\": \"module2\", \n\
\"module2-option\": \"value\" \n\
} \n\
]\n\
} \n\
.fi"
exampleSection = """
.SS Config files:
.nf
// ~/.config/fastfetch/config.jsonc
{
"$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json",
"modules": [
"title",
"separator",
"module1",
{
"type": "module2",
"module2-option": "value"
}
]
}
.fi
"""
# text displayed in the "BUGS" section
bugSection = "Please report bugs to : \
@@ -128,50 +128,37 @@ wikiSection = "Fastfetch github wiki : https://github.com/fastfetch-cli/fastfetc
startOptionalArgument = f"[{startUnderline}?"
# if an optional argument is displayed as [?optArg] (with "optArg underlined")
# this value should be f"{endUnderline}]"
endOptionalArgument = f"{endUnderline}]"
endOptionalArgument = f"{endUnderline}]"
### mandatory arguments tags ###
startMandatoryArgument = f"{startUnderline}"
startMandatoryArgument = f"{startUnderline}"
endMandatoryArgument = f"{endUnderline}"
def generateManPage():
def main():
# importing the JSON file
try:
with open(pathToHelpFile, 'r') as jsonFile:
helpFileData = load(jsonFile) # json.load
jsonFile.close()
except IOError as error:
print("Error with file", pathToHelpFile, ":", error)
return
with open(pathToHelpFile, 'r') as jsonFile:
helpFileData = load(jsonFile) # json.load
######## Start printing the generated .1 file ########
###### header, footer & config #####
print(f".TH man {manSection}", end=" ")
print(f".TH FASTFETCH {manSection} ", end=" ")
print(f"\"{todayDate}\"", end=" ")
# version number
try:
with open(pathToVersionFile, 'r') as versionFile:
with open(pathToVersionFile, 'r') as versionFile:
# research version number in file with regex
for line in versionFile:
researchVersion = search("^\s*VERSION (\d+\.\d+\.\d+)$", line) # re.search()
if (researchVersion != None):
versionNumber = "".join(line[researchVersion.start():researchVersion.end()].split(" "))
versionNumber = versionNumber[:7] + " " + versionNumber[7:]
break
# research version number in file with regex
for line in versionFile:
researchVersion = search(r"^\s*VERSION (\d+\.\d+\.\d+)$", line)
if (researchVersion):
print(f"\"{researchVersion.group(1)}\"", end=" ")
break
versionFile.close()
except IOError as error:
print("Error with file", pathToHelpFile, ":", error)
return
print(f"\"{versionNumber}\"", end=" ")
print(f"\"{titlePage}\"")
@@ -185,13 +172,7 @@ def generateManPage():
print(".SH SYNOPSIS")
print(".B fastfetch")
print(f"[{startUnderline}OPTIONS{endUnderline}]")
##### Description #####
print(".SH DESCRIPTION")
print(descriptionSection)
print(f"[{startUnderline}OPTIONS{endUnderline}]")\
###### Wiki ######
@@ -229,17 +210,17 @@ def generateManPage():
# short option (-opt)
if "short" in keyList:
print(f"\\-{ option['short'] }", end="")
print(fr"\-{ option['short'] }", end="")
# if also have a long option, print a comma
if "long" in keyList:
print(", ", end="")
# long option (--option)
if "long" in keyList:
print(f"\\-\\-{ option['long'] }", end="")
print(fr"\-\-{ option['long'] }", end="")
print(endBold, end=" ")
# arguments
if "arg" in keyList:
# if argument is optional, print "[arg]"
@@ -249,10 +230,10 @@ def generateManPage():
# if argument is mandatory, print "arg"
else:
print(startMandatoryArgument + option['arg']['type'] + endMandatoryArgument, end="")
# description
print(f"\n {option['desc']} \n")
###### Examples ######
@@ -268,6 +249,5 @@ def generateManPage():
if __name__ == "__main__":
generateManPage()
main()