def set_filename(self, rawdog, config, fn): """Set the output filename. If it changes, switch to a new output file. If set to None, close the current output file.""" if fn == self.current_fn: return if self.current_fn is not None: self.dw.close() self.write_output(rawdog, config) if fn is not None: self.f = StringIO() self.dw = DayWriter(self.f, config) self.current_fn = fn
def output_write_files(rawdog, config, articles, article_dates): f_hdr = StringIO() rawdoglib.plugins.call_hook("output_items_begin", rawdog, config, f_hdr) remaining = articles[:] groups = {} other_count = 0 for url, feed in rawdog.feeds.items(): if (feed.args.has_key("define_group")): group = feed.args["define_group"] group = group.replace("_", " ") if (groups.has_key(group)): groups[group]["feeds"].append(feed) else: groups[group] = {"count" : 0, "feeds" : [feed]} f = StringIO() for (key, group) in groups.items(): dw = DayWriter(f, config) f.write("<hr>\n") f.write('<h1><a name="' + key + '">' + key + '</a></h1>\n') for article in remaining[:]: for feed in group["feeds"]: if article.feed == feed.url: if not rawdoglib.plugins.call_hook("output_items_heading", rawdog, config, f, article, article_dates[article]): dw.time(article_dates[article]) rawdog.write_article(f, article, config) remaining.remove(article) group["count"] += 1 dw.close() f.write("<hr>\n") if remaining: dw = DayWriter(f, config) f.write('<h1><a name="Other">Other</a></h1>\n') for article in remaining: if not rawdoglib.plugins.call_hook("output_items_heading", rawdog, config, f, article, article_dates[article]): dw.time(article_dates[article]) rawdog.write_article(f, article, config) other_count += 1 f_hdr.write("<ul>\n") for (key, group) in groups.items(): if group["count"]: f_hdr.write('<li><a href="#%(key)s">%(key)s (%(count)d items)</a></li>\n' % {'key' : key, 'count' : group["count"]}) if other_count: f_hdr.write('<li><a href="#%(key)s">%(key)s (%(count)d items)</a></li>\n' % {'key' : 'Other', 'count' : other_count}) f_hdr.write("</ul>\n") rawdoglib.plugins.call_hook("output_items_end", rawdog, config, f) bits = rawdog.get_main_template_bits(config) bits["items"] = f_hdr.getvalue() + f.getvalue() bits["num_items"] = str(len(rawdog.articles)) rawdoglib.plugins.call_hook("output_bits", rawdog, config, bits) s = fill_template(rawdog.get_template(config), bits) outputfile = config["outputfile"] if outputfile == "-": write_ascii(sys.stdout, s, config) else: config.log("Writing output file: ", outputfile) f = open(outputfile + ".new", "w") write_ascii(f, s, config) f.close() os.rename(outputfile + ".new", outputfile) return False