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