Merge pull request #8264 from lrineau/linter-for-generate_how_to_cite.py

fix pylint warnings of generate_how_to_cite.py
This commit is contained in:
Laurent Rineau 2024-06-12 10:06:10 +02:00
commit e7d27c13d7
1 changed files with 202 additions and 99 deletions

View File

@ -9,7 +9,7 @@ import os
### Constants ### ### Constants ###
result_bib=r"""@book{ cgal:eb-${CGAL_RELEASE_YEAR_ID} RESULT_BIB = r"""@book{ cgal:eb-${CGAL_RELEASE_YEAR_ID}
, title = "{CGAL} User and Reference Manual" , title = "{CGAL} User and Reference Manual"
, author = "{The CGAL Project}" , author = "{The CGAL Project}"
, publisher = "{CGAL Editorial Board}" , publisher = "{CGAL Editorial Board}"
@ -18,7 +18,7 @@ result_bib=r"""@book{ cgal:eb-${CGAL_RELEASE_YEAR_ID}
, url = "https://doc.cgal.org/${CGAL_CREATED_VERSION_NUM}/Manual/packages.html" , url = "https://doc.cgal.org/${CGAL_CREATED_VERSION_NUM}/Manual/packages.html"
}""" }"""
result_txt=r"""// This file was generated by generate_how_to_cite.py. You shouldn't modify it directly. RESULT_TXT = r"""// This file was generated by generate_how_to_cite.py. You shouldn't modify it directly.
/*! /*!
\page how_to_cite_cgal Acknowledging %CGAL \page how_to_cite_cgal Acknowledging %CGAL
@ -73,13 +73,13 @@ The \cgal Project.
""" """
result_txt_footer=r"""</td> RESULT_TXT_FOOTER = r"""</td>
</tr> </tr>
</table><hr> </table><hr>
*/ */
""" """
pre_html=r"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> PRE_HTML = r"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <html xmlns="http://www.w3.org/1999/xhtml">
<head> <head>
<link rel="icon" type="image/png" href="../Manual/g-196x196-doc.png"/> <link rel="icon" type="image/png" href="../Manual/g-196x196-doc.png"/>
@ -90,11 +90,11 @@ pre_html=r"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt
</head> </head>
<body> <body>
""" """
post_html=r"""</body> POST_HTML = r"""</body>
</html> </html>
""" """
result_html=r"""<h1>how_to_cite_cgal.bib</h1><a name="cgal:eb-${CGAL_RELEASE_YEAR_ID}"></a><pre> RESULT_HTML = r"""<h1>how_to_cite_cgal.bib</h1><a name="cgal:eb-${CGAL_RELEASE_YEAR_ID}"></a><pre>
@book{<a href="how_to_cite_cgal.html#cgal:eb-${CGAL_RELEASE_YEAR_ID}">cgal:eb-${CGAL_RELEASE_YEAR_ID}</a>, @book{<a href="how_to_cite_cgal.html#cgal:eb-${CGAL_RELEASE_YEAR_ID}">cgal:eb-${CGAL_RELEASE_YEAR_ID}</a>,
title = {{CGAL} User and Reference Manual}, title = {{CGAL} User and Reference Manual},
author = {{The CGAL Project}}, author = {{The CGAL Project}},
@ -109,110 +109,209 @@ result_html=r"""<h1>how_to_cite_cgal.bib</h1><a name="cgal:eb-${CGAL_RELEASE_YEA
### Functions ### ### Functions ###
def gen_bib_entry(title, authors, bib, anchor): def gen_bib_entry(title, authors, bib, anchor):
res="\n\ res = (
@incollection{"+bib+"-${CGAL_RELEASE_YEAR_ID}\n\ "\n\
, author = \""+authors+"\"\n\ @incollection{"
, title = \""+title+"\"\n\ + bib
, publisher = \"{CGAL Editorial Board}\"\n\ + '-${CGAL_RELEASE_YEAR_ID}\n\
, edition = \"{${CGAL_CREATED_VERSION_NUM}}\"\n\ , author = "'
, booktitle = \"{CGAL} User and Reference Manual\"\n\ + authors
, url = \"https://doc.cgal.org/${CGAL_CREATED_VERSION_NUM}/Manual/packages.html#"+anchor+"\"\n\ + '"\n\
, title = "'
+ title
+ '"\n\
, publisher = "{CGAL Editorial Board}"\n\
, edition = "{${CGAL_CREATED_VERSION_NUM}}"\n\
, booktitle = "{CGAL} User and Reference Manual"\n\
, url = "https://doc.cgal.org/${CGAL_CREATED_VERSION_NUM}/Manual/packages.html#'
+ anchor
+ '"\n\
, year = ${CGAL_BUILD_YEAR4}\n\ , year = ${CGAL_BUILD_YEAR4}\n\
}\n" }\n'
)
return res return res
def gen_txt_entry(title, authors, bib, anchor, k): def gen_txt_entry(title, authors, bib, anchor, k):
title_r=title.replace("Kernel","%Kernel").replace("Interval","%Interval").replace("Matrix","%Matrix").replace("Kinetic","%Kinetic").replace("CGAL","\cgal").replace("Range","%Range") title_r = (
authors=authors.replace("CGAL","\cgal") title.replace("Kernel", "%Kernel")
res="<tr valign=\"top\">\n\ .replace("Interval", "%Interval")
<td align=\"right\" class=\"bibtexnumber\">\n\ .replace("Matrix", "%Matrix")
[<a name=\""+bib+"-${CGAL_RELEASE_YEAR_ID}\">"+str(k)+"</a>]\n\ .replace("Kinetic", "%Kinetic")
.replace("CGAL", r"\cgal")
.replace("Range", "%Range")
)
authors = authors.replace("CGAL", r"\cgal")
res = (
'<tr valign="top">\n\
<td align="right" class="bibtexnumber">\n\
[<a name="'
+ bib
+ '-${CGAL_RELEASE_YEAR_ID}">'
+ str(k)
+ '</a>]\n\
</td>\n\ </td>\n\
<td class=\"bibtexitem\">\n "+authors+".\n "+title_r+".\n\ <td class="bibtexitem">\n '
In <em>\cgal User and Reference Manual</em>. \cgal Editorial Board,\n\ + authors
+ ".\n "
+ title_r
+ '.\n\
In <em>\\cgal User and Reference Manual</em>. \\cgal Editorial Board,\n\
${CGAL_CREATED_VERSION_NUM} edition, ${CGAL_BUILD_YEAR4}.\n\ ${CGAL_CREATED_VERSION_NUM} edition, ${CGAL_BUILD_YEAR4}.\n\
[&nbsp;<a href=\"how_to_cite.html#"+bib+"-${CGAL_RELEASE_YEAR_ID}\">bib</a>&nbsp;| \n\ [&nbsp;<a href="how_to_cite.html#'
<a href=\"packages.html#"+anchor+"\">http</a>&nbsp;]\n\ + bib
+ '-${CGAL_RELEASE_YEAR_ID}">bib</a>&nbsp;| \n\
<a href="packages.html#'
+ anchor
+ '">http</a>&nbsp;]\n\
\n\ \n\
</td>\n\ </td>\n\
</tr>\n\n\n" </tr>\n\n\n'
)
return res return res
def gen_html_entry(title, authors, bib, anchor): def gen_html_entry(title, authors, bib, anchor):
res="<a name=\""+bib+"-${CGAL_RELEASE_YEAR_ID}\"></a><pre>\n\ res = (
@incollection{<a href=\"how_to_cite_cgal.html#"+bib+"-${CGAL_RELEASE_YEAR_ID}\">"+bib+"-${CGAL_RELEASE_YEAR_ID}</a>,\n\ '<a name="'
author = {"+authors+"},\n\ + bib
title = {"+title+"},\n\ + '-${CGAL_RELEASE_YEAR_ID}"></a><pre>\n\
@incollection{<a href="how_to_cite_cgal.html#'
+ bib
+ '-${CGAL_RELEASE_YEAR_ID}">'
+ bib
+ "-${CGAL_RELEASE_YEAR_ID}</a>,\n\
author = {"
+ authors
+ "},\n\
title = {"
+ title
+ '},\n\
publisher = {{CGAL Editorial Board}},\n\ publisher = {{CGAL Editorial Board}},\n\
edition = {{${CGAL_CREATED_VERSION_NUM}}},\n\ edition = {{${CGAL_CREATED_VERSION_NUM}}},\n\
booktitle = {{CGAL} User and Reference Manual},\n\ booktitle = {{CGAL} User and Reference Manual},\n\
url = {<a href=\"https://doc.cgal.org/${CGAL_CREATED_VERSION_NUM}/Manual/packages.html#"+anchor+"\">https://doc.cgal.org/${CGAL_CREATED_VERSION_NUM}/Manual/packages.html#"+anchor+"</a>},\n\ url = {<a href="https://doc.cgal.org/${CGAL_CREATED_VERSION_NUM}/Manual/packages.html#'
+ anchor
+ '">https://doc.cgal.org/${CGAL_CREATED_VERSION_NUM}/Manual/packages.html#'
+ anchor
+ "</a>},\n\
year = ${CGAL_BUILD_YEAR4}\n\ year = ${CGAL_BUILD_YEAR4}\n\
}\n\ }\n\
</pre>\n\n" </pre>\n\n"
)
return res return res
def protect_upper_case(title): def protect_upper_case(title):
return title.replace("dD","{dD}").replace("2D","{2D}").replace("3D","{3D}").replace("CGAL","{CGAL}").replace("Qt","{Qt}").replace("Boost","{Boost}") return (
title.replace("dD", "{dD}")
.replace("2D", "{2D}")
.replace("3D", "{3D}")
.replace("CGAL", "{CGAL}")
.replace("Qt", "{Qt}")
.replace("Boost", "{Boost}")
)
def protect_accentuated_letters(authors): def protect_accentuated_letters(authors):
res=authors.replace("é",r"{\'e}").replace("è",r"{\`e}").replace("É",r"{\'E}").replace("ä",r"{\"a}").replace("ö",r"{\"o}").replace("ñ",r"{\~n}").replace("ã",r"{\~a}").replace("ë",r"{\"e}").replace("ı",r"{\i}").replace("Ş",r"{\c{S}}").replace("ş",r"{\c{s}}").replace("%","").replace("đ",r"{\-d}") res = (
authors.replace("é", r"{\'e}")
.replace("è", r"{\`e}")
.replace("É", r"{\'E}")
.replace("ä", r"{\"a}")
.replace("ö", r"{\"o}")
.replace("ñ", r"{\~n}")
.replace("ã", r"{\~a}")
.replace("ë", r"{\"e}")
.replace("ı", r"{\i}")
.replace("Ş", r"{\c{S}}")
.replace("ş", r"{\c{s}}")
.replace("%", "")
.replace("đ", r"{\-d}")
)
try: try:
res.encode('ascii') res.encode("ascii")
except UnicodeEncodeError: except UnicodeEncodeError:
stderr.write("WARNING: a non-ASCII character has been found in author string for bibtex (probably a non-handled accentuated letter)." stderr.write(
"Check the new package added and update the function protect_accentuated_letters in Documentation/scripts/generate_how_to_cite.py\n\n") "WARNING: a non-ASCII character has been found in author string for bibtex (probably a non-handled accentuated letter)."
"Check the new package added and update the function protect_accentuated_letters in Documentation/scripts/generate_how_to_cite.py\n\n"
)
return res return res
def make_doc_path(pkg, arg):
if BRANCH_BUILD: def make_doc_path(pkg, arg, source_dir, branch_build):
return os.path.join(SOURCE_DIR, pkg, "doc", pkg, arg) if branch_build:
return os.path.join(source_dir, pkg, "doc", pkg, arg)
else: else:
return os.path.join(SOURCE_DIR, "doc", pkg, arg) return os.path.join(source_dir, "doc", pkg, arg)
### Start of the main function ### ### Start of the main function ###
assert len(argv) == 4, "require exactly three arguments: source_dir, build_dir, branch_build"
SOURCE_DIR = argv[1] def main():
BUILD_DIR = argv[2] assert (
BRANCH_BUILD = argv[3] len(argv) == 4
), "require exactly three arguments: source_dir, build_dir, branch_build"
source_dir = argv[1]
build_dir = argv[2]
branch_build = argv[3]
result_txt = RESULT_TXT
result_bib = RESULT_BIB
result_html = RESULT_HTML
pattern = re.compile(r"\\package_listing{([^}]*)}") pattern = re.compile(r"\\package_listing{([^}]*)}")
pattern_title_and_anchor = re.compile(r"\\cgalPkgDescriptionBegin{([^}]*),\s?([^}]*)}") pattern_title_and_anchor = re.compile(
r"\\cgalPkgDescriptionBegin{([^}]*),\s?([^}]*)}"
)
pattern_author = re.compile(r"\\cgalPkgAuthors?{([^}]*)}") pattern_author = re.compile(r"\\cgalPkgAuthors?{([^}]*)}")
pattern_bib = re.compile(r"\\cgalPkgBib{([^}]*)}") pattern_bib = re.compile(r"\\cgalPkgBib{([^}]*)}")
f = codecs.open(make_doc_path("Documentation", "packages.txt"), 'r', encoding='utf-8') f = codecs.open(
make_doc_path("Documentation", "packages.txt", source_dir, branch_build),
"r",
encoding="utf-8",
)
k = 2 k = 2
for line in f: for line in f:
match = pattern.match(line) match = pattern.match(line)
if(match): if match:
pkg = match.group(1) pkg = match.group(1)
filename = make_doc_path(pkg, "PackageDescription.txt") filename = make_doc_path(
pkgdesc = codecs.open(filename, 'r', encoding='utf-8') pkg, "PackageDescription.txt", source_dir, branch_build
)
pkgdesc = codecs.open(filename, "r", encoding="utf-8")
authors = "" authors = ""
bib = "" bib = ""
anchor = "" anchor = ""
for pkg_line in pkgdesc: for pkg_line in pkgdesc:
match = pattern_title_and_anchor.match(pkg_line) match = pattern_title_and_anchor.match(pkg_line)
if match: if match:
title=match.group(1).replace("\,",",") title = match.group(1).replace(r"\,", ",")
anchor = match.group(2) anchor = match.group(2)
continue continue
match = pattern_author.match(pkg_line) match = pattern_author.match(pkg_line)
if match: if match:
authors=match.group(1).replace(", and"," and").replace(","," and") authors = (
match.group(1).replace(", and", " and").replace(",", " and")
)
continue continue
match = pattern_bib.match(pkg_line) match = pattern_bib.match(pkg_line)
if match: if match:
bib = match.group(1) bib = match.group(1)
continue continue
assert len(bib)>0, "Did you forget a \cgalPkgBib{} in %r?" % filename assert len(bib) > 0, "Did you forget a \\cgalPkgBib{} in %r?" % filename
assert len(authors)>0, "Did you forget a \cgalPkgAuthors{} in %r?" % filename assert len(authors) > 0, (
assert len(anchor)>0, "Did you forget the anchor in \cgalPkgDescriptionBegin{} in %r?" % filename "Did you forget a \\cgalPkgAuthors{} in %r?" % filename
)
assert len(anchor) > 0, (
"Did you forget the anchor in \\cgalPkgDescriptionBegin{} in %r?"
% filename
)
result_txt += gen_txt_entry(title, authors, bib, anchor, k) result_txt += gen_txt_entry(title, authors, bib, anchor, k)
# convert title and author to bibtex format # convert title and author to bibtex format
title = protect_upper_case(title) title = protect_upper_case(title)
@ -221,10 +320,14 @@ for line in f:
result_html += gen_html_entry(title, authors, bib, anchor) result_html += gen_html_entry(title, authors, bib, anchor)
k += 1 k += 1
f = codecs.open(BUILD_DIR+"/how_to_cite_cgal.bib.in", 'w', encoding='utf-8') f = codecs.open(build_dir + "/how_to_cite_cgal.bib.in", "w", encoding="utf-8")
f.write(result_bib) f.write(result_bib)
result_txt+=result_txt_footer result_txt += RESULT_TXT_FOOTER
f = codecs.open(BUILD_DIR+"/how_to_cite_cgal.txt.in", 'w', encoding='utf-8') f = codecs.open(build_dir + "/how_to_cite_cgal.txt.in", "w", encoding="utf-8")
f.write(result_txt) f.write(result_txt)
f = codecs.open(BUILD_DIR+"/how_to_cite.html.in", 'w', encoding='utf-8') f = codecs.open(build_dir + "/how_to_cite.html.in", "w", encoding="utf-8")
f.write(pre_html + result_html + post_html) f.write(PRE_HTML + result_html + POST_HTML)
if __name__ == "__main__":
main()