Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # Copyright 2011, The Board of Regents of Leland Stanford, Jr. University
- # All rights reserved. See LICENSE.
- # Author: Christine Williams <christine.bennett.williams@gmail.com>
- # Description: Pulls events from database and writes them to disc.
- from Cheetah.Template import Template
- from lxml import etree
- from pprint import pprint
- from optparse import OptionParser
- from StringIO import StringIO
- import bisect
- import datetime
- import re
- import sys
- import datastore
- import config
- import file_manager
- class PostFlipBook:
- """Sets up the flipbooks for posts- yearmonths, tags, all posts."""
- def __init__(self, uri="", pretty_name=""):
- self.uri = uri
- self.pretty_name = pretty_name
- self.posts = []
- def render(self, fm, options):
- groups = group(self.posts, 10)
- pagenums = range(len(groups))
- pages = zip([None] + pagenums[:-1], pagenums, pagenums[1:] + [None], groups)
- for next_pg_num, current_pg_num, back_pg_num, posts in pages:
- fm.save(options.output_dir + self.page_uri(current_pg_num), str(Template(file="news-template.tmpl",
- searchList=[{"posts" : posts,
- "pretty_name": self.pretty_name,
- "forward_url": next_pg_num is not None and self.page_uri(next_pg_num),
- "forward_text": "Newer Posts»",
- "back_url": back_pg_num is not None and self.page_uri(back_pg_num),
- "back_text": "Older Posts"}])))
- def page_uri(self, current_pg_num):
- if current_pg_num == 0:
- return self.uri
- else:
- path, extension = self.uri.rsplit(".", 1)
- return path + "-" + str(current_pg_num) + "." + extension
- class FlipbookIndex:
- def __init__(self, yearmonth, categories):
- self.yearmonth = yearmonth
- self.categories = categories
- def group(lst, n):
- return zip(*[lst[i::n] for i in range(n)])
- def parse_args(argv):
- """Sets up the option parser and parses command line arguments.
- Returns:
- options, args: a pair whose first element is an Options object with settings
- taken from the command line, and whose second element is the remaining
- unparsed arguments on the command line.
- """
- op = OptionParser()
- op.add_option("-o", "--output-dir", dest="output_dir",
- help="Output generated files under DIR",
- metavar="DIR",
- default="/Library/Server/Web/Data/Sites/Default/")
- options, args = op.parse_args(argv[1:])
- if not options.output_dir.endswith("/"):
- options.output_dir += "/"
- return options, args
- def main(argv):
- options, args = parse_args(argv)
- fm = file_manager.FileManager()
- ds = datastore.DataStore("database.db")
- start_date = datetime.datetime.now()
- end_date = start_date + datetime.timedelta(31)
- # NOTE(scottrw): the list() function is necessary because GetEventsInRange
- # returns a generator, which is exhausted by the first template, leaving no
- # events left for the second template!
- events = list(ds.GetEventsInRange(start_date, end_date))
- all_events = list(ds.GetAllEvents())
- all_posts = list(ds.GetAllPosts())
- cal_months = {}
- all_workshop_months = {}
- all_event_months = {}
- for event in all_events:
- cal_months.setdefault(event.calendar_title, {}).setdefault(event.yearmonth(), []).append(event)
- all_event_months.setdefault(event.yearmonth(), []).append(event)
- all_posts_fb = PostFlipBook("news-videos/news/index.html", "All Posts")
- for post in all_posts:
- all_posts_fb.posts.append(post)
- all_posts_fb.render(fm, options)
- calendar_urls = [(name, uri(name)) for name in config.calendar_ids.keys()]
- CalendarFlipBook(calendar_title="Upcoming Events", output_base=options.output_dir + "events/calendar/", landing_page_template="calendar-landing-page.tmpl").WriteUpcomingEvents(options, fm, events)
- CalendarFlipBook(calendar_title="Workshop Calendar", output_base="workshops/calendar/", landing_page_template="workshops-landing-page.tmpl").WriteUpcomingEvents(fm, events, calendar_urls)
- WriteEventPages(options, fm, all_events, calendar_urls)
- WriteIndividualCalendars(options, fm, events, cal_months, calendar_urls, start_date, end_date)
- WritePerMonthCalendars(options, fm, all_event_months, calendar_urls)
- WritePerMonthWorkshopCalendars(options, fm, all_event_months, calendar_urls)
- WritePerCalPerMonthCalendars(options, fm, cal_months, calendar_urls)
- WritePostPages(options, fm, all_posts)
- #pprint(sorted(fm.files.keys()))
- assert fm.HasFile(options.output_dir + "events/calendar/test-shc-calendar.html")
- assert fm.HasFile(options.output_dir + "workshops/calendar/test-workshop-calendar.html")
- assert fm.HasFile(options.output_dir + "events/calendar/2012-01-test-shc-calendar.html")
- assert fm.HasFile(options.output_dir + "events/calendar/2012-03-test-shc-calendar.html")
- assert fm.HasFile(options.output_dir + "events/calendar/2012-01-test-workshop-calendar.html")
- assert fm.HasFile(options.output_dir + "events/calendar/2012-03-test-workshop-calendar.html")
- assert fm.HasFile(options.output_dir + "events/calendar/2012-1-9-all-day-event.html")
- assert fm.HasFile(options.output_dir + "events/calendar/2012-1-9-all-day-workshop-event.html")
- assert fm.HasFile(options.output_dir + "events/calendar/2012-1-10-location-only-workshop-event.html")
- assert fm.HasFile(options.output_dir + "events/calendar/2012-1-10-location-only-event.html")
- assert fm.HasFile(options.output_dir + "events/calendar/2012-1-11-multi-day-workshop-event.html")
- assert fm.HasFile(options.output_dir + "events/calendar/2012-1-11-multi-day-event.html")
- #assert fm.HasFile(options.output_dir + "events/calendar/2012-1-14-event-to-be-changed.html")
- assert fm.HasFile(options.output_dir + "events/calendar/2012-1-14-workshop-event-to-be-changed.html")
- assert fm.HasFile(options.output_dir + "events/calendar/2012-3-29-far-away-shc-event.html")
- assert fm.HasFile(options.output_dir + "events/calendar/2012-3-30-far-away-workshop-event.html")
- shc_test_text = fm.GetFile(options.output_dir + "events/calendar/test-shc-calendar.html")
- dom = etree.HTML(shc_test_text)
- MyAssert(dom.xpath('//title')[0].text,'Test SHC Calendar | Stanford Humanities Center')
- assert dom.xpath('//div[@id = "topnext"]')
- MyAssert(dom.xpath('//div[@id = "topnext"]/a')[0].get('href'), "../../events/calendar/2012-03-test-shc-calendar.html")
- MyAssert(dom.xpath('//div[@id = "topnext"]/a')[0].text, u"Mar 2012\xbb")
- assert dom.xpath('//div[@id = "topback"]')
- MyAssert(dom.xpath('//div[@id = "topback"]/a')[0].get('href'), "../../events/calendar/2012-01-test-shc-calendar.html")
- MyAssert(dom.xpath('//div[@id = "topback"]/a')[0].text, u"\xabJan 2012")
- assert dom.xpath('//div[@id = "bottomnext"]')
- MyAssert(dom.xpath('//div[@id = "bottomnext"]/a')[0].get('href'), "../../events/calendar/2012-03-test-shc-calendar.html")
- MyAssert(dom.xpath('//div[@id = "bottomnext"]/a')[0].text, u"Mar 2012\xbb")
- assert dom.xpath('//div[@id = "bottomback"]')
- MyAssert(dom.xpath('//div[@id = "bottomback"]/a')[0].get('href'), "../../events/calendar/2012-01-test-shc-calendar.html")
- MyAssert(dom.xpath('//div[@id = "bottomback"]/a')[0].text, u"\xabJan 2012")
- workshop_test_text = fm.GetFile(options.output_dir + "workshops/calendar/test-workshop-calendar.html")
- dom = etree.HTML(workshop_test_text)
- MyAssert(dom.xpath('//title')[0].text, 'Test Workshop Calendar | Stanford Humanities Center')
- assert dom.xpath('//div[@id = "topnext"]')
- MyAssert(dom.xpath('//div[@id = "topnext"]/a')[0].get('href'), "../../events/calendar/2012-03-test-workshop-calendar.html")
- MyAssert(dom.xpath('//div[@id = "topnext"]/a')[0].text, u"Mar 2012\xbb")
- assert dom.xpath('//div[@id = "topback"]')
- MyAssert(dom.xpath('//div[@id = "topback"]/a')[0].get('href'), "../../events/calendar/2012-01-test-workshop-calendar.html")
- MyAssert(dom.xpath('//div[@id = "topback"]/a')[0].text, u"\xabJan 2012")
- assert dom.xpath('//div[@id = "bottomnext"]')
- MyAssert(dom.xpath('//div[@id = "bottomnext"]/a')[0].get('href'), "../../events/calendar/2012-03-test-workshop-calendar.html")
- MyAssert(dom.xpath('//div[@id = "bottomnext"]/a')[0].text, u"Mar 2012\xbb")
- assert dom.xpath('//div[@id = "bottomback"]')
- MyAssert(dom.xpath('//div[@id = "bottomback"]/a')[0].get('href'), "../../events/calendar/2012-01-test-workshop-calendar.html")
- MyAssert(dom.xpath('//div[@id = "bottomback"]/a')[0].text, u"\xabJan 2012")
- jan_shc_text = fm.GetFile(options.output_dir + "events/calendar/2012-01-test-shc-calendar.html")
- dom = etree.HTML(jan_shc_text)
- MyAssert(dom.xpath('//title')[0].text,'Test SHC Calendar Events For January 2012 | Stanford Humanities Center')
- assert dom.xpath('//div[@id = "topnext"]')
- MyAssert(dom.xpath('//div[@id = "topnext"]/a')[0].get('href'), "../../events/calendar/2012-03-test-shc-calendar.html")
- MyAssert(dom.xpath('//div[@id = "topnext"]/a')[0].text, u"Mar 2012\xbb")
- assert not dom.xpath('//div[@id = "topback"]'), "Expected None, found %r" % dom.xpath('//div[@id = "topback"]')
- MyAssert(dom.xpath('//div[@id = "bottomnext"]/a')[0].get('href'), "../../events/calendar/2012-03-test-shc-calendar.html")
- MyAssert(dom.xpath('//div[@id = "bottomnext"]/a')[0].text, u"Mar 2012\xbb")
- assert not dom.xpath('//div[@id = "bottomback"]')
- mar_shc_text = fm.GetFile(options.output_dir + "events/calendar/2012-03-test-shc-calendar.html")
- dom = etree.HTML(mar_shc_text)
- MyAssert(dom.xpath('//title')[0].text,'Test SHC Calendar Events For March 2012 | Stanford Humanities Center')
- assert not dom.xpath('//div[@id = "topnext"]')
- assert dom.xpath('//div[@id = "topback"]')
- MyAssert(dom.xpath('//div[@id = "topback"]/a')[0].get('href'), "../../events/calendar/2012-01-test-shc-calendar.html")
- MyAssert(dom.xpath('//div[@id = "topback"]/a')[0].text, u"\xabJan 2012")
- assert not dom.xpath('//div[@id = "bottomnext"]')
- assert dom.xpath('//div[@id = "bottomback"]')
- MyAssert(dom.xpath('//div[@id = "bottomback"]/a')[0].get('href'), "../../events/calendar/2012-01-test-shc-calendar.html")
- MyAssert(dom.xpath('//div[@id = "bottomback"]/a')[0].text, u"\xabJan 2012")
- jan_workshop_text = fm.GetFile(options.output_dir + "events/calendar/2012-01-test-workshop-calendar.html")
- dom = etree.HTML(jan_workshop_text)
- MyAssert(dom.xpath('//title')[0].text,'Test Workshop Calendar Events For January 2012 | Stanford Humanities Center')
- assert dom.xpath('//div[@id = "topnext"]')
- MyAssert(dom.xpath('//div[@id = "topnext"]/a')[0].get('href'), "../../events/calendar/2012-03-test-workshop-calendar.html")
- MyAssert(dom.xpath('//div[@id = "topnext"]/a')[0].text, u"Mar 2012\xbb")
- assert not dom.xpath('//div[@id = "topback"]')
- MyAssert(dom.xpath('//div[@id = "bottomnext"]/a')[0].get('href'), "../../events/calendar/2012-03-test-workshop-calendar.html")
- MyAssert(dom.xpath('//div[@id = "bottomnext"]/a')[0].text, u"Mar 2012\xbb")
- assert not dom.xpath('//div[@id = "bottomback"]')
- mar_workshop_text = fm.GetFile(options.output_dir + "events/calendar/2012-03-test-workshop-calendar.html")
- dom = etree.HTML(mar_workshop_text)
- MyAssert(dom.xpath('//title')[0].text,'Test Workshop Calendar Events For March 2012 | Stanford Humanities Center')
- assert not dom.xpath('//div[@id = "topnext"]')
- assert dom.xpath('//div[@id = "topback"]')
- MyAssert(dom.xpath('//div[@id = "topback"]/a')[0].get('href'), "../../events/calendar/2012-01-test-workshop-calendar.html")
- MyAssert(dom.xpath('//div[@id = "topback"]/a')[0].text, u"\xabJan 2012")
- assert not dom.xpath('//div[@id = "bottomnext"]')
- assert dom.xpath('//div[@id = "bottomback"]')
- MyAssert(dom.xpath('//div[@id = "bottomback"]/a')[0].get('href'), "../../events/calendar/2012-01-test-workshop-calendar.html")
- MyAssert(dom.xpath('//div[@id = "bottomback"]/a')[0].text, u"\xabJan 2012")
- #print fm.show_diff()
- fm.commit()
- def MyAssert(actual, expected):
- assert actual == expected, "Got %r" % actual
- class CalendarFlipBook:
- def __init__(self, calendar_title="", output_base="", landing_page_template=""):
- self.calendar_title = calendar_title
- self.output_base = output_base
- self.landing_page_template = landing_page_template
- def WriteUpcomingEvents(self, options, fm, events, calendar_urls=None):
- fm.save(self.output_base + "index.html",
- str(Template(file=self.landing_page_template,
- searchList=[{"events": events,
- "calendar_title": self.calendar_title,
- "calendar_urls": calendar_urls,
- "forward_url": events and events[-1].start_time.strftime('%Y-%m.html'),
- "forward_text": events and "All events for %s»" % events[-1].start_time.strftime('%b %Y'),
- "back_url": events and events[0].start_time.strftime('%Y-%m.html'),
- "back_text": events and "All events for %s" % events[0].start_time.strftime('%b %Y')}])))
- def WriteUpcomingWorkshops(options, fm, events, calendar_urls):
- fm.save(options.output_dir + "workshops/calendar/index.html",
- str(Template(file="workshop-landing-page.tmpl",
- searchList=[{"events": events,
- "calendar_title": "Workshop Calendar",
- "calendar_urls": calendar_urls,
- "forward_url": events and events[-1].start_time.strftime('%Y-%m.html'),
- "forward_text": events and "All events for %s»" % events[-1].start_time.strftime('%b %Y'),
- "back_url": events and events[0].start_time.strftime('%Y-%m.html'),
- "back_text": events and "All events for %s" % events[0].start_time.strftime('%b %Y')}])))
- def WriteEventPages(options, fm, all_events, calendar_urls):
- for event in all_events:
- if event.calendar_title in ("Stanford Humanities Center Events", "Co-sponsored Events Held at the Humanities Center", "Test SHC Calendar"):
- tmpl = "shc_event.tmpl"
- else:
- tmpl = "workshop_event.tmpl"
- fm.save(options.output_dir + event.uri(),
- str(Template(file=tmpl,
- searchList=[{"event": event,
- "calendar_title": event.calendar_title,
- "calendar_urls": calendar_urls,
- "forward_url": None,
- "forward_text": None,
- "back_url": None,
- "back_text": None}])))
- def WriteIndividualCalendars(options, fm, events, cal_months, calendar_urls, start_date, end_date):
- events_by_calendar = {}
- for cal_id in config.calendar_ids.iterkeys():
- events_by_calendar[cal_id] = []
- for e in events:
- events_by_calendar[e.calendar_title].append(e)
- for calendar_name, calendar_events in events_by_calendar.iteritems():
- if calendar_name in ("Stanford Humanities Center Events", "Co-sponsored Events Held at the Humanities Center", "Test SHC Calendar"):
- tmpl = "calendar-landing-page.tmpl"
- else:
- tmpl = "workshop-landing-page.tmpl"
- if not calendar_events:
- all_events = cal_months.get(calendar_name, None)
- if not all_events:
- forward_url = None
- forward_text = None
- back_url = None
- back_text = None
- else:
- months = all_events.keys()
- months.sort()
- back_index = max(0, min(bisect.bisect_left(months, start_date), len(months) - 1) - 1)
- next_index = min(bisect.bisect_left(months, start_date), len(months) - 1)
- back_date = months[back_index]
- next_date = months[next_index]
- forward_url = "../../" + month_uri(next_date, calendar_name)
- forward_text = next_date.strftime('%b %Y') + "»"
- back_url = "../../" + month_uri(back_date, calendar_name)
- back_text = back_date.strftime('%b %Y')
- else:
- forward_url = "../../" + month_uri(calendar_events[-1].start_time, calendar_name)
- forward_text = calendar_events[-1].start_time.strftime('%b %Y') + "»"
- back_url = "../../" + month_uri(calendar_events[0].start_time, calendar_name)
- back_text = calendar_events[0].start_time.strftime('%b %Y')
- fm.save(options.output_dir + uri(calendar_name),
- str(Template(file=tmpl,
- searchList=[{"events": calendar_events,
- "calendar_title": calendar_name,
- "calendar_urls": calendar_urls,
- "back_url": back_url,
- "back_text": back_text,
- "forward_url": forward_url,
- "forward_text": forward_text}])))
- def WritePerMonthCalendars(options, fm, all_event_months, calendar_urls):
- month_events = sorted(all_event_months.items())
- months = [month for month, events in month_events]
- for (yearmonth, events), back, forward in zip(month_events,
- [None] + months[:-1],
- months[1:] + [None]):
- calendar_title = yearmonth.strftime("Events Calendar for %B %Y")
- #Render a calendar template into the right file
- fm.save(options.output_dir + yearmonth.strftime("events/calendar/%Y-%m.html"),
- str(Template(file="calendar-landing-page.tmpl",
- searchList=[{"events": events,
- "calendar_title": calendar_title,
- "back_url": back and back.strftime('%Y-%m.html'),
- "back_text": back and back.strftime ('%b %Y'),
- "forward_url": forward and forward.strftime('%Y-%m.html'),
- "forward_text": forward and forward.strftime('%b %Y') + "»",
- "calendar": calendar_title}])))
- def WritePerMonthWorkshopCalendars(options, fm, all_event_months, calendar_urls):
- month_events = sorted(all_event_months.items())
- months = [month for month, events in month_events]
- for (yearmonth, events), back, forward in zip(month_events,
- [None] + months[:-1],
- months[1:] + [None]):
- calendar_title = yearmonth.strftime("Workshop Calendar for %B %Y")
- #Render a calendar template into the right file
- fm.save(options.output_dir + yearmonth.strftime("workshops/calendar/%Y-%m.html"),
- str(Template(file="workshop-landing-page.tmpl",
- searchList=[{"events": events,
- "calendar_title": calendar_title,
- "calendar_urls": calendar_urls,
- "back_url": back and back.strftime('%Y-%m.html'),
- "back_text": back and back.strftime ('%b %Y'),
- "forward_url": forward and forward.strftime('%Y-%m.html'),
- "forward_text": forward and forward.strftime('%b %Y') + "»",
- "calendar": calendar_title}])))
- def WritePerCalPerMonthCalendars(options, fm, cal_months, calendar_urls):
- for calendar, all_event_months in cal_months.iteritems():
- month_events = sorted(all_event_months.items())
- months = [month for month, events in month_events]
- for (yearmonth, events), back, forward in zip(month_events,
- [None] + months[:-1],
- months[1:] + [None]):
- calendar_title = calendar + " Events For " + yearmonth.strftime("%B %Y")
- #Render a calendar template into the right file
- if calendar in ("Stanford Humanities Center Events", "Co-sponsored Events Held at the Humanities Center", "Test SHC Calendar"):
- tmpl = "calendar-landing-page.tmpl"
- else:
- tmpl = "workshop-landing-page.tmpl"
- fm.save(options.output_dir + month_uri(yearmonth, calendar),
- str(Template(file=tmpl,
- searchList=[{"events": events,
- "calendar_title": calendar_title,
- "calendar_urls": calendar_urls,
- "back_url": back and "../../" + month_uri(back, calendar),
- "back_text": back and back.strftime('%b %Y'),
- "forward_url": forward and "../../" + month_uri(forward, calendar),
- "forward_text": forward and forward.strftime('%b %Y') + "»",
- "calendar": calendar}])))
- def month_uri(yearmonth, calendar):
- return yearmonth.strftime("events/calendar/%%Y-%%m-%s.html" % friendly_title(calendar))
- def uri(calendar_title):
- if calendar_title == "Stanford Humanities Center Events":
- return "events/calendar/%s.html" % (friendly_title(calendar_title))
- elif calendar_title == "Co-sponsored Events Held at the Humanities Center":
- return "events/calendar/%s.html" % (friendly_title(calendar_title))
- elif calendar_title == "Test SHC Calendar":
- return "events/calendar/%s.html" % (friendly_title(calendar_title))
- elif calendar_title == "Test Workshop Calendar":
- return "workshops/calendar/%s.html" % (friendly_title(calendar_title))
- else:
- return "workshops/calendar/%s.html" % (friendly_title(calendar_title))
- def friendly_title(calendar_title):
- title = re.sub(" +", "-", calendar_title.lower())
- title = re.sub("[^-a-z0-9]", "", title)
- return title
- def WritePostPages(options, fm, all_posts):
- for post in all_posts:
- tmpl = "post-template.tmpl"
- fm.save(options.output_dir + post.uri(),
- str(Template(file=tmpl,
- searchList=[{"post" : post,
- "title": post.title,
- "published" : post.published,
- "content" : post.content,
- "categories" : post.categories}])))
- if __name__ == "__main__":
- main(sys.argv)
Add Comment
Please, Sign In to add comment