Skip to content

Commit

Permalink
man: Use a context manager for opening files (OSGeo#4291)
Browse files Browse the repository at this point in the history
* man/build_html: Use context manager for get_desc

* man/build_topics: Use context managers for opening files

* man/build_rest: Replace read_file and write_file with Path.read_text and Path.write_text

* man/build_rest: Use context manager for get_desc

* man/build_index_rest: Use a context manager for opening file

* man/build_keywords: Use a context manager for opening files

* man/build_class_graphical: Use a context manager for opening files

* man/build_full_index_rest: Use a context manager for opening files

* man/build_full_index: Use a context manager for opening files

* man/build_class_rest: Use a context manager for opening files

* man/build_class: Use a context manager for opening files

* style: Remove unneeded SIM115 exclusions in man/

* man/build_topics: Use context managers for opening keyfile

* man/build_html: Add missing Path import

* man/build_keywords: Ignore line too long (E501)

* Sort imports in build_rest

* man/build_topics: write_footer need to be inside keyfile context manager
  • Loading branch information
echoix authored Nov 23, 2024
1 parent ab67825 commit 6c45ad9
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 313 deletions.
60 changes: 29 additions & 31 deletions man/build_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,39 @@ def build_class(ext):
os.chdir(man_dir)

filename = modclass + f".{ext}"
f = open(filename + ".tmp", "w")

write_header(
f,
"{} modules - GRASS GIS {} Reference Manual".format(
modclass.capitalize(), grass_version
),
template=ext,
)
modclass_lower = modclass.lower()
modclass_visible = modclass
if modclass_lower not in no_intro_page_classes:
if modclass_visible == "raster3d":
# convert keyword to nice form
modclass_visible = "3D raster"
f.write(
modclass_intro_tmpl.substitute(
modclass=modclass_visible, modclass_lower=modclass_lower
)
with open(filename + ".tmp", "w") as f:
write_header(
f,
"{} modules - GRASS GIS {} Reference Manual".format(
modclass.capitalize(), grass_version
),
template=ext,
)
f.write(modclass_tmpl.substitute(modclass=to_title(modclass_visible)))
modclass_lower = modclass.lower()
modclass_visible = modclass
if modclass_lower not in no_intro_page_classes:
if modclass_visible == "raster3d":
# convert keyword to nice form
modclass_visible = "3D raster"
f.write(
modclass_intro_tmpl.substitute(
modclass=modclass_visible, modclass_lower=modclass_lower
)
)
f.write(modclass_tmpl.substitute(modclass=to_title(modclass_visible)))

# for all modules:
for cmd in get_files(man_dir, cls, extension=ext):
basename = os.path.splitext(cmd)[0]
desc = check_for_desc_override(basename)
if desc is None:
desc = get_desc(cmd)
f.write(desc2_tmpl.substitute(cmd=cmd, basename=basename, desc=desc))
if ext == "html":
f.write("</table>\n")
# for all modules:
for cmd in get_files(man_dir, cls, extension=ext):
basename = os.path.splitext(cmd)[0]
desc = check_for_desc_override(basename)
if desc is None:
desc = get_desc(cmd)
f.write(desc2_tmpl.substitute(cmd=cmd, basename=basename, desc=desc))
if ext == "html":
f.write("</table>\n")

write_footer(f, f"index.{ext}", year, template=ext)
write_footer(f, f"index.{ext}", year, template=ext)

f.close()
replace_file(filename)


Expand Down
101 changes: 49 additions & 52 deletions man/build_class_graphical.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,65 +131,62 @@ def generate_page_for_category(
short_family, module_family, imgs, year, skip_no_image=False
):
filename = module_family + "_graphical.html"

output = open(filename + ".tmp", "w")

output.write(
header1_tmpl.substitute(
title="GRASS GIS %s Reference Manual: Graphical index" % grass_version
)
)
output.write(header_graphical_index_tmpl)

if module_family.lower() not in {"general", "postscript"}:
if module_family == "raster3d":
# covert keyword to nice form
module_family = "3D raster"
with open(filename + ".tmp", "w") as output:
output.write(
modclass_intro_tmpl.substitute(
modclass=module_family, modclass_lower=module_family.lower()
header1_tmpl.substitute(
title="GRASS GIS %s Reference Manual: Graphical index" % grass_version
)
)
if module_family == "wxGUI":
output.write("<h3>wxGUI components:</h3>")
elif module_family == "guimodules":
output.write("<h3>g.gui.* modules:</h3>")
else:
output.write("<h3>{0} modules:</h3>".format(to_title(module_family)))
output.write('<ul class="img-list">')

# for all modules:
for cmd in get_files(man_dir, short_family, ignore_gui=False):
basename = os.path.splitext(cmd)[0]
desc = check_for_desc_override(basename)
if desc is None:
desc = get_desc(cmd)
img = get_module_image(basename, imgs)
img_class = "linkimg"
if skip_no_image and not img:
continue
if not img:
img = "grass_logo.png"
img_class = "default-img"
if basename.startswith("wxGUI"):
basename = basename.replace(".", " ")
output.write(
"<li>"
'<a href="{html}">'
'<img class="{img_class}" src="{img}">'
'<span class="name">{name}</span> '
'<span class="desc">{desc}</span>'
"</a>"
"</li>".format(
html=cmd, img=img, name=basename, desc=desc, img_class=img_class
output.write(header_graphical_index_tmpl)

if module_family.lower() not in {"general", "postscript"}:
if module_family == "raster3d":
# covert keyword to nice form
module_family = "3D raster"
output.write(
modclass_intro_tmpl.substitute(
modclass=module_family, modclass_lower=module_family.lower()
)
)
if module_family == "wxGUI":
output.write("<h3>wxGUI components:</h3>")
elif module_family == "guimodules":
output.write("<h3>g.gui.* modules:</h3>")
else:
output.write("<h3>{0} modules:</h3>".format(to_title(module_family)))
output.write('<ul class="img-list">')

# for all modules:
for cmd in get_files(man_dir, short_family, ignore_gui=False):
basename = os.path.splitext(cmd)[0]
desc = check_for_desc_override(basename)
if desc is None:
desc = get_desc(cmd)
img = get_module_image(basename, imgs)
img_class = "linkimg"
if skip_no_image and not img:
continue
if not img:
img = "grass_logo.png"
img_class = "default-img"
if basename.startswith("wxGUI"):
basename = basename.replace(".", " ")
output.write(
"<li>"
'<a href="{html}">'
'<img class="{img_class}" src="{img}">'
'<span class="name">{name}</span> '
'<span class="desc">{desc}</span>'
"</a>"
"</li>".format(
html=cmd, img=img, name=basename, desc=desc, img_class=img_class
)
)
)

output.write("</ul>")
output.write("</ul>")

write_footer(output, "index.html", year, template="html")
write_footer(output, "index.html", year, template="html")

output.close()
replace_file(filename)


Expand Down
37 changes: 18 additions & 19 deletions man/build_class_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,26 @@
modclass = sys.argv[2]

filename = modclass + ".txt"

f = open(filename + ".tmp", "wb")

write_rest_header(f, "GRASS GIS %s Reference Manual: %s" % (grass_version, modclass))
if modclass.lower() not in {"general", "miscellaneous", "postscript"}:
f.write(
modclass_intro_tmpl.substitute(
modclass=modclass, modclass_lower=modclass.lower()
)
with open(filename + ".tmp", "wb") as f:
write_rest_header(
f, "GRASS GIS %s Reference Manual: %s" % (grass_version, modclass)
)
f.write(modclass_tmpl.substitute(modclass=modclass))
if modclass.lower() not in {"general", "miscellaneous", "postscript"}:
f.write(
modclass_intro_tmpl.substitute(
modclass=modclass, modclass_lower=modclass.lower()
)
)
f.write(modclass_tmpl.substitute(modclass=modclass))

# for all modules:
for cmd in rest_files(cls):
basename = os.path.splitext(cmd)[0]
desc = check_for_desc_override(basename)
if desc is None:
desc = get_desc(cmd)
f.write(desc2_tmpl.substitute(basename=basename, desc=desc))
# for all modules:
for cmd in rest_files(cls):
basename = os.path.splitext(cmd)[0]
desc = check_for_desc_override(basename)
if desc is None:
desc = get_desc(cmd)
f.write(desc2_tmpl.substitute(basename=basename, desc=desc))

write_rest_footer(f, "index.txt")
write_rest_footer(f, "index.txt")

f.close()
replace_file(filename)
52 changes: 25 additions & 27 deletions man/build_full_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,35 @@ def build_full_index(ext):

# begin full index:
filename = f"full_index.{ext}"
f = open(filename + ".tmp", "w")

write_header(
f,
"GRASS GIS {} Reference Manual - Full index".format(grass_version),
body_width="80%",
template=ext,
)
with open(filename + ".tmp", "w") as f:
write_header(
f,
"GRASS GIS {} Reference Manual - Full index".format(grass_version),
body_width="80%",
template=ext,
)

# generate main index of all modules:
f.write(full_index_header)
# generate main index of all modules:
f.write(full_index_header)

if ext == "html":
f.write(toc)

# for all module groups:
for cls, cls_label in classes:
f.write(cmd2_tmpl.substitute(cmd_label=to_title(cls_label), cmd=cls))
# for all modules:
for cmd in get_files(man_dir, cls, extension=ext):
basename = os.path.splitext(cmd)[0]
desc = check_for_desc_override(basename)
if desc is None:
desc = get_desc(cmd)
f.write(desc1_tmpl.substitute(cmd=cmd, basename=basename, desc=desc))
if ext == "html":
f.write("</table>\n")

write_footer(f, f"index.{ext}", year, template=ext)
f.write(toc)

# for all module groups:
for cls, cls_label in classes:
f.write(cmd2_tmpl.substitute(cmd_label=to_title(cls_label), cmd=cls))
# for all modules:
for cmd in get_files(man_dir, cls, extension=ext):
basename = os.path.splitext(cmd)[0]
desc = check_for_desc_override(basename)
if desc is None:
desc = get_desc(cmd)
f.write(desc1_tmpl.substitute(cmd=cmd, basename=basename, desc=desc))
if ext == "html":
f.write("</table>\n")

write_footer(f, f"index.{ext}", year, template=ext)

f.close()
replace_file(filename)


Expand Down
46 changes: 22 additions & 24 deletions man/build_full_index_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,32 @@

# begin full index:
filename = "full_index.txt"
f = open(filename + ".tmp", "wb")
with open(filename + ".tmp", "wb") as f:
write_rest_header(f, "GRASS GIS %s Reference Manual: Full index" % grass_version)

write_rest_header(f, "GRASS GIS %s Reference Manual: Full index" % grass_version)
# generate main index of all modules:
f.write(full_index_header)
# "

# generate main index of all modules:
f.write(full_index_header)
# "
# for cls in classes:
# f.write(cmd1_tmpl.substitute(cmd = cls))
# if cls != classes[-1]:
# f.write(" | ")

# for cls in classes:
# f.write(cmd1_tmpl.substitute(cmd = cls))
# if cls != classes[-1]:
# f.write(" | ")
f.write(sections)

f.write(sections)
# for all module groups:
for cls in classes:
f.write(cmd2_tmpl.substitute(cmd=cls))
# for all modules:
for cmd in rest_files(cls):
basename = os.path.splitext(cmd)[0]
desc = check_for_desc_override(basename)
if desc is None:
desc = get_desc(cmd)
f.write(desc1_tmpl.substitute(basename=basename, desc=desc))
f.write("\n")

# for all module groups:
for cls in classes:
f.write(cmd2_tmpl.substitute(cmd=cls))
# for all modules:
for cmd in rest_files(cls):
basename = os.path.splitext(cmd)[0]
desc = check_for_desc_override(basename)
if desc is None:
desc = get_desc(cmd)
f.write(desc1_tmpl.substitute(basename=basename, desc=desc))
f.write("\n")
write_rest_footer(f, "index.txt")

write_rest_footer(f, "index.txt")

f.close()
replace_file(filename)
Loading

0 comments on commit 6c45ad9

Please sign in to comment.