Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
	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
Ejemplo n.º 3
0
def output_write_files(rawdog, config, articles, article_dates):
	config.log("paged-output starting")

	outputfile = config["outputfile"]
	i = outputfile.rfind('.')
	if i != -1:
		prefix = outputfile[:i]
		suffix = outputfile[i:]
	else:
		prefix = outputfile
		suffix = ""

	chunks = []
	while articles != []:
		chunks.append(articles[:articles_per_page])
		articles = articles[articles_per_page:]

	fns = []
	for i in range(len(chunks)):
		if i == 0:
			fn = prefix + suffix
		else:
			fn = "%s%d%s" % (prefix, i, suffix)
		fns.append(fn)

	for i in range(len(chunks)):
		fn = fns[i]
		date = format_time(article_dates[chunks[i][0]], config)
		config.log("paged-output writing ", fn, " (", date, ")")

		f = StringIO()
		dw = DayWriter(f, config)
		for article in chunks[i]:
			dw.time(article_dates[article])
			rawdog.write_article(f, article, config)
		dw.close()

		bits = rawdog.get_main_template_bits(config)
		bits["items"] = f.getvalue()
		bits["num_items"] = str(len(rawdog.articles.values()))

		f = StringIO()
		f.write('<ul class="paged_output_pages">\n')
		for j in range(len(chunks)):
			latest_date = max([article_dates[a] for a in chunks[j]])
			f.write('<li>')
			if i != j:
				f.write('<a href="' + os.path.basename(fns[j]) + '">')
			f.write(format_time(latest_date, config))
			if i != j:
				f.write('</a>')
			f.write('</li>\n')
		f.write('</ul>\n')
		bits["paged_output_pages"] = f.getvalue()

		f = StringIO()
		def make_link(rel, page):
			if page >= 0 and page < len(chunks):
				f.write('<link rel="%s" href="%s">\n'
				        % (rel, os.path.basename(fns[page])))
		make_link("next", i - 1)
		make_link("prev", i + 1)
		bits["paged_output_head"] = f.getvalue()

		s = fill_template(rawdog.get_template(config), bits)
		f = open(fn + ".new", "w")
		write_ascii(f, s, config)
		f.close()
		os.rename(fn + ".new", fn)

	config.log("paged-output done")
	return False
Ejemplo n.º 4
0
class DatedOutput:
	def __init__(self):
		self.page_date_format = "%Y-%m-%d"
		self.calendar_month_format = "%B %Y"
		self.calendar_day_format = "%a"
		self.calendar_date_format = "%d"

		self.output_files = {}
		self.current_date = None
		self.current_fn = None
		self.f = None
		self.dw = None

	def config_option(self, config, name, value):
		if name == "pagedateformat":
			self.page_date_format = value
			return False
		elif name == "calendarmonthformat":
			self.calendar_month_format = value
			return False
		elif name == "calendardayformat":
			self.calendar_day_format = value
			return False
		elif name == "calendardateformat":
			self.calendar_date_format = value
			return False
		else:
			return True

	def generate_list(self):
		"""Generate the list of pages."""

		f = StringIO()

		# Sort and reverse the list of dates, so we have the newest
		# first.
		dates = self.output_files.keys()
		dates.sort()
		dates.reverse()

		f.write('<ul class="paged_output_pages">\n')
		for date in dates:
			fn = self.output_files[date]

			f.write('<li>')
			if fn != self.current_fn:
				f.write('<a href="' + os.path.basename(fn) + '">')
			f.write(date)
			if fn != self.current_fn:
				f.write('</a>')
			f.write('</li>\n')
		f.write('</ul>\n')

		return f.getvalue()

	def generate_calendar(self):
		"""Generate a calendar for the month containing the current
		date."""

		t = time.strptime(self.current_date, self.page_date_format)
		this_month = datetime.date(t.tm_year, t.tm_mon, 1)

		# Find links to the previous and next months, if they exist.
		prev_date = None
		next_date = None
		dates = self.output_files.keys()
		dates.sort()
		for date in dates:
			t = time.strptime(date, self.page_date_format)
			that_month = datetime.date(t.tm_year, t.tm_mon, 1)

			if that_month < this_month:
				prev_date = date
			if that_month > this_month:
				next_date = date
				break

		f = StringIO()
		self.generate_one_calendar(f, this_month, prev_date, next_date)
		return f.getvalue()

	def generate_calendars(self):
		"""Generate calendars for all months."""

		f = StringIO()

		last_month = None
		dates = self.output_files.keys()
		dates.sort()
		dates.reverse()
		for date in dates:
			t = time.strptime(date, self.page_date_format)
			month = datetime.date(t.tm_year, t.tm_mon, 1)

			if month == last_month:
				continue
			last_month = month

			self.generate_one_calendar(f, month, None, None)
			f.write('\n')

		return f.getvalue()

	def generate_one_calendar(self, f, this_month, prev_date, next_date):
		"""Generate a calendar for a given month."""

		cal = calendar.Calendar()

		f.write('<table class="calendar">\n')

		# Print the previous/month name/next bar.
		f.write('<tr class="cal-head">\n')
		f.write('<td class="cal-prev">')
		if prev_date is not None:
			f.write('<a href="%s">&lt;</a>' % os.path.basename(self.output_files[prev_date]))
		f.write('</td>\n')
		f.write('<td class="cal-month" colspan="5">%s</td>\n' % safe_strftime(this_month, self.calendar_month_format))
		f.write('<td class="cal-next">')
		if next_date is not None:
			f.write('<a href="%s">&gt;</a>' % os.path.basename(self.output_files[next_date]))
		f.write('</td>\n')
		f.write('</tr>\n')

		# Print the day-names bar.
		f.write('<tr class="cal-days">\n')
		for day in cal.iterweekdays():
			# Find a date that corresponds to the day number we
			# want to print. I don't see a better way to do this
			# in datetime...
			date = datetime.date(1981, 9, 25)
			while date.weekday() != day:
				date += datetime.timedelta(days = 1)

			f.write('<th>%s</th>' % safe_strftime(date, self.calendar_day_format))
		f.write('</tr>\n')

		# Print the weeks of the month.
		for week in cal.monthdatescalendar(this_month.year, this_month.month):
			f.write('<tr class="cal-week">\n')
			for day in week:
				date = safe_strftime(day, self.page_date_format)

				f.write('<td class="cal-day">')
				if day.month != this_month.month:
					f.write('<em class="cal-othermonth">')
					after = '</em>'
				elif date == self.current_date:
					f.write('<strong class="cal-current">')
					after = '</strong>'
				elif date in self.output_files:
					f.write('<a class="cal-link" href="' + os.path.basename(self.output_files[date]) + '">')
					after = '</a>'
				else:
					after = ''
				f.write(safe_strftime(day, self.calendar_date_format))
				f.write(after)
				f.write('</td>')
			f.write('</tr>\n')

		f.write('</table>\n')

	def write_output(self, rawdog, config):
		"""Write out the current output file."""

		bits = rawdog.get_main_template_bits(config)
		bits["items"] = self.f.getvalue()
		bits["num_items"] = str(len(rawdog.articles.values()))
		bits["dated_output_pages"] = self.generate_list()
		bits["dated_output_calendar"] = self.generate_calendar()
		bits["dated_output_calendars"] = self.generate_calendars()

		s = fill_template(rawdog.get_template(config), bits)
		fn = self.current_fn
		config.log("dated-output writing output file: ", fn)
		f = open(fn + ".new", "w")
		write_ascii(f, s, config)
		f.close()
		os.rename(fn + ".new", fn)

	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(self, rawdog, config, articles, article_dates):
		config.log("dated-output starting")

		# Extract the prefix and suffix from the configured outputfile.
		outputfile = config["outputfile"]
		i = outputfile.rfind('.')
		if i != -1:
			prefix = outputfile[:i]
			suffix = outputfile[i:]
		else:
			prefix = outputfile
			suffix = ""

		# Figure out the output filename date for each article.
		article_fn_dates = {}
		self.output_files = {}
		for article in articles:
			tm = time.localtime(article_dates[article])
			date = safe_ftime(self.page_date_format, tm)
			article_fn_dates[article] = date

			if date in self.output_files:
				pass
			elif self.output_files == {}:
				# First output file: use the configured name.
				self.output_files[date] = outputfile
			else:
				# Otherwise use a dated name.
				self.output_files[date] = "%s-%s%s" % (prefix, date, suffix)

		# Write out each article.
		for article in articles:
			date = article_fn_dates[article]
			self.set_filename(rawdog, config, self.output_files[date])
			self.current_date = date

			self.dw.time(article_dates[article])
			rawdog.write_article(self.f, article, config)

		self.set_filename(rawdog, config, None)

		config.log("dated-output done")
		return False
Ejemplo n.º 5
0
class DatedOutput:
    def __init__(self):
        self.page_date_format = "%Y-%m-%d"
        self.calendar_month_format = "%B %Y"
        self.calendar_day_format = "%a"
        self.calendar_date_format = "%d"

        self.output_files = {}
        self.current_date = None
        self.current_fn = None
        self.f = None
        self.dw = None

    def config_option(self, config, name, value):
        if name == "pagedateformat":
            self.page_date_format = value
            return False
        elif name == "calendarmonthformat":
            self.calendar_month_format = value
            return False
        elif name == "calendardayformat":
            self.calendar_day_format = value
            return False
        elif name == "calendardateformat":
            self.calendar_date_format = value
            return False
        else:
            return True

    def generate_list(self):
        """Generate the list of pages."""

        f = StringIO()

        # Sort and reverse the list of dates, so we have the newest
        # first.
        dates = self.output_files.keys()
        dates.sort()
        dates.reverse()

        f.write('<ul class="paged_output_pages">\n')
        for date in dates:
            fn = self.output_files[date]

            f.write('<li>')
            if fn != self.current_fn:
                f.write('<a href="' + os.path.basename(fn) + '">')
            f.write(date)
            if fn != self.current_fn:
                f.write('</a>')
            f.write('</li>\n')
        f.write('</ul>\n')

        return f.getvalue()

    def generate_calendar(self):
        """Generate a calendar for the month containing the current
        date."""

        t = time.strptime(self.current_date, self.page_date_format)
        this_month = datetime.date(t.tm_year, t.tm_mon, 1)

        # Find links to the previous and next months, if they exist.
        prev_date = None
        next_date = None
        dates = self.output_files.keys()
        dates.sort()
        for date in dates:
            t = time.strptime(date, self.page_date_format)
            that_month = datetime.date(t.tm_year, t.tm_mon, 1)

            if that_month < this_month:
                prev_date = date
            if that_month > this_month:
                next_date = date
                break

        f = StringIO()
        self.generate_one_calendar(f, this_month, prev_date, next_date)
        return f.getvalue()

    def generate_calendars(self):
        """Generate calendars for all months."""

        f = StringIO()

        last_month = None
        dates = self.output_files.keys()
        dates.sort()
        dates.reverse()
        for date in dates:
            t = time.strptime(date, self.page_date_format)
            month = datetime.date(t.tm_year, t.tm_mon, 1)

            if month == last_month:
                continue
            last_month = month

            self.generate_one_calendar(f, month, None, None)
            f.write('\n')

        return f.getvalue()

    def generate_one_calendar(self, f, this_month, prev_date, next_date):
        """Generate a calendar for a given month."""

        cal = calendar.Calendar()

        f.write('<table class="calendar">\n')

        # Print the previous/month name/next bar.
        f.write('<tr class="cal-head">\n')
        f.write('<td class="cal-prev">')
        if prev_date is not None:
            f.write('<a href="%s">&lt;</a>' %
                    os.path.basename(self.output_files[prev_date]))
        f.write('</td>\n')
        f.write('<td class="cal-month" colspan="5">%s</td>\n' %
                safe_strftime(this_month, self.calendar_month_format))
        f.write('<td class="cal-next">')
        if next_date is not None:
            f.write('<a href="%s">&gt;</a>' %
                    os.path.basename(self.output_files[next_date]))
        f.write('</td>\n')
        f.write('</tr>\n')

        # Print the day-names bar.
        f.write('<tr class="cal-days">\n')
        for day in cal.iterweekdays():
            # Find a date that corresponds to the day number we
            # want to print. I don't see a better way to do this
            # in datetime...
            date = datetime.date(1981, 9, 25)
            while date.weekday() != day:
                date += datetime.timedelta(days=1)

            f.write('<th>%s</th>' %
                    safe_strftime(date, self.calendar_day_format))
        f.write('</tr>\n')

        # Print the weeks of the month.
        for week in cal.monthdatescalendar(this_month.year, this_month.month):
            f.write('<tr class="cal-week">\n')
            for day in week:
                date = safe_strftime(day, self.page_date_format)

                f.write('<td class="cal-day">')
                if day.month != this_month.month:
                    f.write('<em class="cal-othermonth">')
                    after = '</em>'
                elif date == self.current_date:
                    f.write('<strong class="cal-current">')
                    after = '</strong>'
                elif date in self.output_files:
                    f.write('<a class="cal-link" href="' +
                            os.path.basename(self.output_files[date]) + '">')
                    after = '</a>'
                else:
                    after = ''
                f.write(safe_strftime(day, self.calendar_date_format))
                f.write(after)
                f.write('</td>')
            f.write('</tr>\n')

        f.write('</table>\n')

    def write_output(self, rawdog, config):
        """Write out the current output file."""

        bits = rawdog.get_main_template_bits(config)
        bits["items"] = self.f.getvalue()
        bits["num_items"] = str(len(rawdog.articles.values()))
        bits["dated_output_pages"] = self.generate_list()
        bits["dated_output_calendar"] = self.generate_calendar()
        bits["dated_output_calendars"] = self.generate_calendars()

        s = fill_template(rawdog.get_template(config), bits)
        fn = self.current_fn
        config.log("dated-output writing output file: ", fn)
        f = open(fn + ".new", "w")
        write_ascii(f, s, config)
        f.close()
        os.rename(fn + ".new", fn)

    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(self, rawdog, config, articles, article_dates):
        config.log("dated-output starting")

        # Extract the prefix and suffix from the configured outputfile.
        outputfile = config["outputfile"]
        i = outputfile.rfind('.')
        if i != -1:
            prefix = outputfile[:i]
            suffix = outputfile[i:]
        else:
            prefix = outputfile
            suffix = ""

        # Figure out the output filename date for each article.
        article_fn_dates = {}
        self.output_files = {}
        for article in articles:
            tm = time.localtime(article_dates[article])
            date = safe_ftime(self.page_date_format, tm)
            article_fn_dates[article] = date

            if date in self.output_files:
                pass
            elif self.output_files == {}:
                # First output file: use the configured name.
                self.output_files[date] = outputfile
            else:
                # Otherwise use a dated name.
                self.output_files[date] = "%s-%s%s" % (prefix, date, suffix)

        # Write out each article.
        for article in articles:
            date = article_fn_dates[article]
            self.set_filename(rawdog, config, self.output_files[date])
            self.current_date = date

            self.dw.time(article_dates[article])
            rawdog.write_article(self.f, article, config)

        self.set_filename(rawdog, config, None)

        config.log("dated-output done")
        return False
Ejemplo n.º 6
0
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