diff options
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/extract.py | 65 |
1 files changed, 41 insertions, 24 deletions
diff --git a/tools/extract.py b/tools/extract.py index 8035b20..7f76fe9 100755 --- a/tools/extract.py +++ b/tools/extract.py @@ -6,6 +6,7 @@ import json import os import shutil import sqlite3 +import sys import time import urllib.parse @@ -51,38 +52,54 @@ def attachment_link(row): os.path.join("attachments", "wiki", h1[:3], h1, h2 + fn2), \ os.path.join(urllib.parse.quote(row.id, ""), urllib.parse.quote(row.filename, "")) -with open("filter.json") as f: - filter = json.load(f) +class Filter: -def keep(name): - for k, v in filter: - assert k in "+-" - if fnmatch.fnmatch(name, v): - return k == "+" - return True + def __init__(self, filename = "filter.json"): + with open(filename) as f: + filter = json.load(f) + if not all(action in "-+" for action, pattern in filter): + sys.exit("Bad action \"{}\" in filter".format(action)) + self.filter = tuple((action == "+", pattern) for action, pattern in filter) + + def __call__(self, name): + for action, pattern in self.filter: + if fnmatch.fnmatch(name, pattern): + return action + return True class Row(sqlite3.Row): def __getattr__(self, name): return self[name] -for dn in ("wiki", "pelican/content"): - if not os.path.exists(dn): +def main(): + + for dn in ("wiki", "pelican"): + shutil.rmtree(dn) + + for dn in ("wiki", "pelican/content/images", "pelican/content/pages"): os.makedirs(dn) -db = sqlite3.connect("trac.db") -db.row_factory = Row + #os.link("pelican.conf", "pelican/pelican.conf") + + keep = Filter() + + db = sqlite3.connect("trac.db") + db.row_factory = Row + + for row in db.execute(wiki_query): + if keep(row.name): + slug = urllib.parse.quote(row.name, "") + print(slug, row.version) + with open("wiki/{}.trac".format(slug), "w") as f: + f.write(row.text) + md = trac2md.WikiToMD(row.text) + with open("pelican/content/{}.md".format(slug), "w") as f: + f.write(md) -for row in db.execute(wiki_query): - if keep(row.name): - slug = urllib.parse.quote(row.name, "") - print(slug, row.version) - with open("wiki/{}.trac".format(slug), "w") as f: - f.write(row.text) - md = trac2md.WikiToMD(row.text) - with open("pelican/content/{}.md".format(slug), "w") as f: - f.write(md) + for row in db.execute(attachment_query): + print("{} => {}".format(*attachment_link(row))) -for row in db.execute(attachment_query): - print("{} => {}".format(*attachment_link(row))) + db.close() -db.close() +if __name__ == "__main__": + main() |