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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
#!/usr/bin/python3
import sys
from os.path import abspath, dirname, join
from pathlib import Path
import tomllib as toml
from mako.template import Template
rootDir = dirname(dirname(abspath(__file__)))
sys.path.insert(0, rootDir)
from pyglossary.core import userPluginsDir
from pyglossary.glossary import Glossary
from pyglossary.sort_keys import defaultSortKeyName
Glossary.init(
# usePluginsJson=False,
)
"""
Mako template engine:
https://docs.makotemplates.org/en/latest/
https://github.com/sqlalchemy/mako
https://pypi.org/project/Mako/
Package python3-mako in Debian repos
"""
template = Template(
"""${"##"} ${description}
${topTables}
% if readDependsLinks and readDependsLinks == writeDependsLinks:
${"### Dependencies for reading and writing"}
PyPI Links: ${readDependsLinks}
To install, run:
```sh
${readDependsCmd}
```
% else:
% if readDependsLinks:
${"### Dependencies for reading"}
PyPI Links: ${readDependsLinks}
To install, run:
```sh
${readDependsCmd}
```
% endif
% if writeDependsLinks:
${"### Dependencies for writing"}
PyPI Links: ${writeDependsLinks}
To install, run
```sh
${writeDependsCmd}
```
% endif
% endif
% if extraDocs:
% for title, text in extraDocs:
${f"### {title}"}
${text.replace('(./doc/', '(../')}
% endfor
% endif
${toolsTable}
""",
)
def codeValue(x):
s = str(x)
if s:
return "`" + s + "`"
return ""
def yesNo(x):
if x is True:
return "Yes"
if x is False:
return "No"
return ""
def kindEmoji(kind):
if not kind:
return ""
return {
"text": "📝",
"binary": "🔢",
"directory": "📁",
"package": "📦",
}[kind]
def renderLink(title, url):
if "(" in title or ")" in title:
url = f"<{url}>"
title = title.replace("|", "-")
return f"[{title}]({url})"
def pypiLink(pypiName):
urlPath = pypiName.replace("==", "/")
urlPath = urlPath.replace(">", "%3E")
return renderLink(
pypiName.replace("==", " "),
f"https://pypi.org/project/{urlPath}",
)
def makeDependsDoc(cls):
if not (cls and getattr(cls, "depends", None)):
return "", ""
links = ", ".join([pypiLink(pypiName) for pypiName in cls.depends.values()])
cmd = "pip3 install " + " ".join(
cls.depends.values(),
)
return links, cmd
def sortKeyName(p):
value = p.sortKeyName
if value:
return codeValue(value)
return "(" + codeValue(defaultSortKeyName) + ")"
def renderCell(value):
return str(value).replace("\n", "\\n").replace("\t", "\\t")
def renderTable(rows):
"""rows[0] must be headers."""
rows = [[renderCell(cell) for cell in row] for row in rows]
width = [max(len(row[i]) for row in rows) for i in range(len(rows[0]))]
rows = [
[cell.ljust(width[i], " ") for i, cell in enumerate(row)]
for rowI, row in enumerate(rows)
]
rows.insert(1, ["-" * colWidth for colWidth in width])
return "\n".join(["| " + " | ".join(row) + " |" for row in rows])
def renderTableNoPadding(rows):
"""rows[0] must be headers."""
rows = [[renderCell(cell) for cell in row] for row in rows]
width = [len(x) for x in rows[0]]
rows.insert(1, ["-" * colWidth for colWidth in width])
return "\n".join(["| " + " | ".join(row) + " |" for row in rows])
def renderRWOptions(options):
return renderTable(
[("Name", "Default", "Type", "Comment")]
+ [
(
optName,
codeValue(default),
optionsType[optName],
optionsComment[optName],
)
for optName, default in options.items()
],
)
def pluginIsActive(p):
if not p.enable:
return False
if not (p.canRead or p.canWrite):
return False
return userPluginsDirPath not in p.path.parents
def getToolSourceLink(tool):
url = tool.get("source")
if not url:
return "―"
_, title = url.split("://")
if title.startswith("github.com/"):
title = "@" + title[len("github.com/") :]
return renderLink(title, url)
userPluginsDirPath = Path(userPluginsDir)
plugins = [p for p in Glossary.plugins.values() if pluginIsActive(p)]
pluginsDir = join(rootDir, "pyglossary", "plugins")
for p in plugins:
module = p.module
optionsProp = p.optionsProp
wiki = module.wiki
wiki_md = "―"
if wiki:
if wiki.startswith("https://github.com/"):
wiki_title = "@" + wiki[len("https://github.com/") :]
else:
wiki_title = wiki.split("/")[-1].replace("_", " ")
wiki_md = renderLink(wiki_title, wiki)
website_md = "―"
website = module.website
if website:
if isinstance(website, str):
website_md = website
else:
try:
url, title = website
except ValueError:
raise ValueError(f"{website = }") from None
website_md = renderLink(title, url)
(
readDependsLinks,
readDependsCmd,
) = makeDependsDoc(getattr(module, "Reader", None))
(
writeDependsLinks,
writeDependsCmd,
) = makeDependsDoc(getattr(module, "Writer", None))
extraDocs = getattr(module, "extraDocs", [])
toolsFile = join(pluginsDir, p.moduleName, "tools.toml")
try:
with open(toolsFile, "rb") as _file:
tools_toml = toml.load(_file)
except FileNotFoundError:
tools = []
except Exception as e:
print(f"\nFile: {toolsFile}")
raise e
else:
for toolName, tool in tools_toml.items():
tool.update({"name": toolName})
tools = tools_toml.values()
table = [
("Attribute", "Value"),
("Name", p.name),
("snake_case_name", p.lname),
("Description", p.description),
("Extensions", ", ".join([codeValue(ext) for ext in p.extensions])),
("Read support", yesNo(p.canRead)),
("Write support", yesNo(p.canWrite)),
("Single-file", yesNo(p.singleFile)),
("Kind", f"{kindEmoji(module.kind)} {module.kind}"),
]
if p.canWrite:
table += [
("Sort-on-write", p.sortOnWrite.desc),
("Sort key", sortKeyName(p)),
]
table += [
("Wiki", wiki_md),
("Website", website_md),
]
generalInfoTable = "### General Information\n\n" + renderTable(table)
topTables = generalInfoTable
try:
optionsType = {optName: opt.typ for optName, opt in optionsProp.items()}
except Exception:
print(f"{optionsProp = }")
raise
optionsComment = {
optName: opt.comment.replace("\n", "<br />")
for optName, opt in optionsProp.items()
}
readOptions = p.getReadOptions()
if readOptions:
topTables += "\n\n### Read options\n\n" + renderRWOptions(readOptions)
writeOptions = p.getWriteOptions()
if writeOptions:
topTables += "\n\n### Write options\n\n" + renderRWOptions(writeOptions)
toolsTable = ""
if tools:
toolsTable = "### Dictionary Applications/Tools\n\n" + renderTable(
[
(
"Name & Website",
"Source code",
"License",
"Platforms",
"Language",
),
]
+ [
(
f"[{tool['name']}]({tool['web']})",
getToolSourceLink(tool),
tool["license"],
", ".join(tool["platforms"]),
tool.get("plang", ""),
)
for tool in tools
],
)
text = template.render(
description=p.description,
codeValue=codeValue,
yesNo=yesNo,
topTables=topTables,
optionsProp=optionsProp,
readOptions=readOptions,
writeOptions=writeOptions,
optionsComment=optionsComment,
optionsType=optionsType,
readDependsLinks=readDependsLinks,
readDependsCmd=readDependsCmd,
writeDependsLinks=writeDependsLinks,
writeDependsCmd=writeDependsCmd,
extraDocs=extraDocs,
toolsTable=toolsTable,
)
for _i in range(3):
text = text.replace("\n\n\n", "\n\n")
if text.endswith("\n\n"):
text = text[:-1]
with open(
join(rootDir, "doc", "p", f"{p.lname}.md"),
mode="w",
encoding="utf-8",
newline="\n",
) as _file:
_file.write(text)
indexText = renderTableNoPadding(
[("Description", "Name", "Doc Link")]
+ [
(
p.description,
p.name,
renderLink(f"{p.lname}.md", f"./{p.lname}.md"),
)
for p in plugins
],
)
with open(
join(rootDir, "doc", "p", "__index__.md"),
mode="w",
encoding="utf-8",
newline="\n",
) as _file:
_file.write(indexText + "\n")
|