def run_indices(threads, args): "builds the index files" single_template = load_template(args.templates + "index.html.jst") grouped_template = load_template(args.templates + "grouped-index.html.jst") messages = [ NS([("from", simple_from(m["headers"]["from"]))] + list(m.items())) for _, messages in threads for m in messages ] # build simple indices for index in INDICES: data = dict(title=index.name(args), **index.partitioner(messages)) if "groups" in data: html = grouped_template.expand(data) else: html = single_template.expand(data) with open(path.join(args.output, index.filename(args)), mode="w", encoding="utf-8") as out: out.write(html) partials = {} if args.partials.isdir(): partials = PartialManager(args.partials) index_template = load_template(args.templates + "thread-index.html.jst", more_formatters=partials) partial_template = load_template(args.partials + "thread.html.jst", more_formatters=partials) # flatten the threads messages = [message for _, messages in threads for message in messages] def preprocess(message): from_ = simple_from(message.headers.get("from", "")) or None return dict(list(message.items()) + [("from", from_)]) # recursively render the threads here so the expanding doesn't # hit the recursion limit def render_thread(thread): return partial_template.expand( message=preprocess(thread.message), children=[render_thread(child) for child in thread.children] ) with open(path.join(args.output, args.thread_index), mode="w", encoding="utf-8") as out: out.write( index_template.expand( title=args.thread_title, threads=[ dict(message=preprocess(t.message), children=[render_thread(c) for c in t.children]) for t in normalize_threads(build_threads(messages)) ], ) )
def convert(url, gzip=False): "converts a mbox url to a JSON encodable structure" parser = parse_gz_mbox if gzip else parse_mbox # stdin if url == "-": messages = parser(fileobj=BytesIO(sys.stdin.read())) # local file elif not urlsplit(url).scheme: messages = parser(filename=url) else: messages = fetch_messages(url, parser=parser) threads = list(normalize_threads(build_threads(list(messages)))) tie_threads(threads) return list(flatten_threads(threads))