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