aboutsummaryrefslogtreecommitdiff
path: root/MetaTools/buildTableList.py
blob: c0a6453eabe03df3975eddcd08334ea77ee51a6d (plain)
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
#! /usr/bin/env python3

import sys
import os
import glob
from fontTools.ttLib import identifierToTag
import textwrap


fontToolsDir = os.path.dirname(os.path.dirname(os.path.join(os.getcwd(), sys.argv[0])))
fontToolsDir = os.path.normpath(fontToolsDir)
tablesDir = os.path.join(fontToolsDir, "Lib", "fontTools", "ttLib", "tables")
docFile = os.path.join(fontToolsDir, "Doc/source/ttx.rst")

names = glob.glob1(tablesDir, "*.py")

modules = []
tables = []
for name in names:
    try:
        tag = identifierToTag(name[:-3])
    except:
        pass
    else:
        modules.append(name[:-3])
        tables.append(tag.strip())

modules.sort()
tables.sort()


with open(os.path.join(tablesDir, "__init__.py"), "w") as file:
    file.write(
        '''
# DON'T EDIT! This file is generated by MetaTools/buildTableList.py.
def _moduleFinderHint():
	"""Dummy function to let modulefinder know what tables may be
	dynamically imported. Generated by MetaTools/buildTableList.py.

		>>> _moduleFinderHint()
	"""
'''
    )

    for module in modules:
        file.write("\tfrom . import %s\n" % module)

    file.write(
        """
if __name__ == "__main__":
	import doctest, sys
	sys.exit(doctest.testmod().failed)
"""
    )


begin = ".. begin table list\n"
end = ".. end table list"
with open(docFile) as f:
    doc = f.read()
beginPos = doc.find(begin)
assert beginPos > 0
beginPos = beginPos + len(begin) + 1
endPos = doc.find(end)

lines = textwrap.wrap(", ".join(tables[:-1]) + " and " + tables[-1], 66)
intro = "The following tables are currently supported::\n\n"
blockquote = "\n".join(" " * 4 + line for line in lines) + "\n"

doc = doc[:beginPos] + intro + blockquote + "\n" + doc[endPos:]

with open(docFile, "w") as f:
    f.write(doc)