Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
small script for filtering Wiki Monkey's logs
#! /usr/bin/env python
import sys
import json
class PackageTemplatesFilter:
def __init__(self):
self.pages = {}
def add_log(self, logfile):
lines = open(logfile, "r").readlines()
title = ""
for line in lines:
s = line.split(maxsplit=2)
if len(s) < 3:
print("Unexpected log format on line %d in file %s" % (lines.index(line), logfile), file=sys.stderr)
break
timestamp, level, msg = s
# detect warnings for packages not found in any repo
if level == "JSN":
data = json.loads(msg)["data"]
title = data["page"]
lang = data["pagelanguage"]
pkg = data["package"]
assert data["error"] == "notfound"
if lang not in self.pages:
self.pages[lang] = {}
if title in self.pages[lang]:
self.pages[lang][title].append(pkg)
else:
self.pages[lang][title] = [pkg]
def report(self):
report = ""
for lang in sorted(self.pages.keys()):
report += "== %s ==\n" % lang
pages = self.pages[lang]
for title in sorted(pages.keys()):
report += "* [[%s]]\n" % title
for pkg in pages[title]:
report += "** %s\n" % pkg
return report
if __name__ == "__main__":
f = PackageTemplatesFilter()
for log in sys.argv[1:]:
f.add_log(log)
print(f.report())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment