xref: /aosp_15_r20/external/fonttools/MetaTools/buildTableList.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1#! /usr/bin/env python3
2
3import sys
4import os
5import glob
6from fontTools.ttLib import identifierToTag
7import textwrap
8
9
10fontToolsDir = os.path.dirname(os.path.dirname(os.path.join(os.getcwd(), sys.argv[0])))
11fontToolsDir = os.path.normpath(fontToolsDir)
12tablesDir = os.path.join(fontToolsDir, "Lib", "fontTools", "ttLib", "tables")
13docFile = os.path.join(fontToolsDir, "Doc/source/ttx.rst")
14
15names = glob.glob1(tablesDir, "*.py")
16
17modules = []
18tables = []
19for name in names:
20    try:
21        tag = identifierToTag(name[:-3])
22    except:
23        pass
24    else:
25        modules.append(name[:-3])
26        tables.append(tag.strip())
27
28modules.sort()
29tables.sort()
30
31
32with open(os.path.join(tablesDir, "__init__.py"), "w") as file:
33    file.write(
34        '''
35# DON'T EDIT! This file is generated by MetaTools/buildTableList.py.
36def _moduleFinderHint():
37	"""Dummy function to let modulefinder know what tables may be
38	dynamically imported. Generated by MetaTools/buildTableList.py.
39
40		>>> _moduleFinderHint()
41	"""
42'''
43    )
44
45    for module in modules:
46        file.write("\tfrom . import %s\n" % module)
47
48    file.write(
49        """
50if __name__ == "__main__":
51	import doctest, sys
52	sys.exit(doctest.testmod().failed)
53"""
54    )
55
56
57begin = ".. begin table list\n"
58end = ".. end table list"
59with open(docFile) as f:
60    doc = f.read()
61beginPos = doc.find(begin)
62assert beginPos > 0
63beginPos = beginPos + len(begin) + 1
64endPos = doc.find(end)
65
66lines = textwrap.wrap(", ".join(tables[:-1]) + " and " + tables[-1], 66)
67intro = "The following tables are currently supported::\n\n"
68blockquote = "\n".join(" " * 4 + line for line in lines) + "\n"
69
70doc = doc[:beginPos] + intro + blockquote + "\n" + doc[endPos:]
71
72with open(docFile, "w") as f:
73    f.write(doc)
74