-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
315 lines (265 loc) · 9.64 KB
/
generate.py
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
import argparse
import re
import sys
from functools import partial
import logging
import shutil
import sqlite3
from pathlib import Path
from bs4 import BeautifulSoup
from bs4.formatter import HTMLFormatter
PLIST = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>%(name)s</string>
<key>CFBundleName</key>
<string>%(name)s</string>
<key>DocSetPlatformFamily</key>
<string>%(name)s</string>
<key>isDashDocset</key>
<true/>
<key>dashIndexFilePath</key>
<string>index.html</string>
</dict>
</plist>
"""
def get_logger():
logger = logging.getLogger("nuke-docset")
if not logger.handlers:
hdlr = logging.StreamHandler()
fmt = logging.Formatter("[%(name)s] %(levelname)8s: %(funcName)s: %(message)s")
hdlr.setFormatter(fmt)
logger.addHandler(hdlr)
logger.propagate = False
# add verbose: the debug messages are only visible if Nuke was launched
# with the -V 2 flag.
logging.addLevelName(15, "VERBOSE")
setattr(logging, "VERBOSE", 15)
setattr(logger, "verbose", partial(logger.log, logging.VERBOSE))
logger.setLevel(logging.INFO)
logger.verbose(
"Log level: %r", logging.getLevelName(logger.getEffectiveLevel())
)
return logger
class ProgressBar:
def __init__(self, vmax, width=100):
self.vmax = int(vmax)
self.width = int(width)
self.inc = 0
sys.stdout.write("\33[?25l") # hide shell cursor
def increment(self):
self.inc = min(self.vmax, self.inc + 1)
ratio = float(self.inc) / float(self.vmax)
done = int(ratio * self.width)
remaining = self.width - done
percent = int(ratio * 100.0)
sys.stdout.write("\r[%s%s] %d%%" % ("=" * done, " " * remaining, percent))
sys.stdout.flush()
# new line when done
if self.inc == self.vmax:
sys.stdout.write("\33[?25h") # show shell cursor
sys.stdout.write("\n")
def mk_structure(name):
cwd_path = Path.cwd()
root_path = Path(cwd_path, "%s.docset" % name)
if root_path.exists():
shutil.rmtree(root_path)
root_path.mkdir()
res_path = Path(root_path, "Contents", "Resources")
res_path.mkdir(parents=True)
doc_path = Path(root_path, "Contents", "Resources", "Documents")
shutil.copytree(args.directory, doc_path)
plistp = Path(root_path, "Contents", "Info.plist")
plistp.write_text(PLIST % {"name": name}, encoding="utf-8")
db_path = Path(res_path, "docSet.dsidx")
shutil.copy2(Path(cwd_path, "icon.png"), Path(root_path, "icon.png"))
return db_path, doc_path
def init_db(db_path):
log = get_logger()
log.debug("Connecting to db...")
conn = sqlite3.connect(db_path)
cur = conn.cursor()
log.debug("Cleaning db...")
try:
cur.execute("DROP TABLE searchIndex;")
except sqlite3.OperationalError as err:
log.debug(err)
# raise RuntimeError('Failed to clear db ! Aborting !')
finally:
cur.execute(
"CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);"
)
cur.execute("CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);")
log.info("Created tables")
return conn
def found(*args):
if not getattr(found, "set", None):
setattr(found, "set", set())
if args:
found.set.add(args[0])
else:
return found.set
def get_parent_by_type(elmt, htype):
it = elmt
while it.name != htype:
it = it.parent
return it
def memItemRightList(h2, category, html):
table = get_parent_by_type(h2, "table")
items = [it for it in table.find_all("td")]
result = {}
for i, item in enumerate(items):
if item.a:
for ia in item.find_all("a"):
if "el" not in ia.get("class", []):
continue
if "inherit" in item.parent.get("class", []):
continue
name = ia.string
url = ia.get("href")
result[name] = url
elif item.b and html:
left = item.parent.find("td", {"class": "memItemLeft"})
if not (left and left.a):
continue
if "anchor" in left.a.get("class"):
name = item.b.string
url = "%s#%s" % (html, left.a.get("id"))
result[name] = url
return result
def write_entries_by_cat(data, name, category, html=None):
cur, h2, class_name = data
if h2.a.get("name") == name:
for t_name, t_url in memItemRightList(h2, category, html).items():
if t_name and t_url:
cur.execute(
"INSERT OR IGNORE INTO searchIndex(name, type, path) "
"VALUES ('{class_name}::{type_name}', '{category}', '{path}')".format(
class_name=class_name,
type_name=t_name,
path=t_url,
category=category,
)
)
return True
return False
def write_class_entries(conn, html):
log = get_logger()
log.debug(" > %s", html)
class_name = re.search("\\w*_\d*(\\w+)\\.html", html.name).group(1)
cur = conn.cursor()
cur.execute(
"INSERT OR IGNORE INTO searchIndex(name, type, path) "
"VALUES ('{name}', 'Class', '{path}')".format(name=class_name, path=html.name)
)
html_data = html.read_text()
soup = BeautifulSoup(html_data, "html.parser")
hn = html.name
for h2 in soup.find_all("h2", {"class": "groupheader"}):
# if h2.a:
# found(h2.a.get("name"))
data = (cur, h2, class_name)
if not h2.a:
continue
elif write_entries_by_cat(data, "typedef-members", "Type", html=hn):
continue
elif write_entries_by_cat(data, "pub-types", "Type", html=hn):
continue
elif write_entries_by_cat(data, "pro-types", "Type", html=hn):
continue
elif write_entries_by_cat(data, "pub-methods", "Method", html=hn):
continue
elif write_entries_by_cat(data, "pub-static-methods", "Function", html=hn):
continue
elif write_entries_by_cat(data, "pro-methods", "Method", html=hn):
continue
elif write_entries_by_cat(data, "pro-static-methods", "Function", html=hn):
continue
elif write_entries_by_cat(data, "pub-attribs", "Attribute", html=hn):
continue
elif write_entries_by_cat(data, "pro-attribs", "Attribute", html=hn):
continue
elif write_entries_by_cat(data, "pub-static-attribs", "Attribute", html=hn):
continue
elif write_entries_by_cat(data, "pro-static-attribs", "Attribute", html=hn):
continue
def write_header_entries(conn, html):
log = get_logger()
log.debug(" > %s", html)
namespace = re.search("(\\w*)_8h\\.html", html.name).group(1)
cur = conn.cursor()
cur.execute(
"INSERT OR IGNORE INTO searchIndex(name, type, path) "
"VALUES ('{name}', 'Namespace', '{path}')".format(
name=namespace, path=html.name
)
)
html_data = html.read_text()
soup = BeautifulSoup(html_data, "html.parser")
hn = html.name
for h2 in soup.find_all("h2", {"class": "groupheader"}):
# if h2.a:
# found(h2.a.get("name"))
data = (cur, h2, namespace)
if not h2.a:
continue
elif write_entries_by_cat(data, "define-members", "Macro", html=hn):
continue
elif write_entries_by_cat(data, "enum-members", "Enum", html=hn):
continue
elif write_entries_by_cat(data, "func-members", "Function", html=hn):
continue
elif write_entries_by_cat(data, "typedef-members", "Type", html=hn):
continue
elif write_entries_by_cat(data, "var-members", "Variable", html=hn):
continue
def write_db_entries(conn, html):
if html.name.startswith("class"):
write_class_entries(conn, html)
else:
write_header_entries(conn, html)
def mk_database(db_path, doc_path):
log = get_logger()
html_files = [f for f in doc_path.glob("*.html")]
pbar = ProgressBar(len(html_files), width=55)
conn = init_db(db_path)
rex = re.compile("((classDD_\\w*)|(\\w*_8h))\\.html")
for html in html_files:
if not re.match(rex, html.name) or "-members" in html.name:
pbar.increment()
continue
write_db_entries(conn, html)
pbar.increment()
# print found header categories
for f in sorted(found()):
log.info("Found: %r", f)
conn.commit()
log.info("commit done")
conn.close()
log.info("connection closed")
def mk_docset(args):
log = get_logger()
log.info("Generating docset...")
db_path, doc_path = mk_structure(args.name)
mk_database(db_path, doc_path)
if __name__ == "__main__":
log = get_logger()
log.info("Starting up...")
parser = argparse.ArgumentParser(
description="Builds a docset from a directory full of html files."
)
parser.add_argument("directory")
parser.add_argument("-n", "--name", action="store", required=True)
parser.add_argument("-v", "--verbose", action="store_true")
try:
args = parser.parse_args()
except BaseException as err:
log.error(err)
parser.print_help()
else:
if args.verbose:
log.setLevel(logging.DEBUG)
mk_docset(args)
log.info("done !")