reading.py (1895B) - raw
1 2 # vim:fileencoding=utf-8 3 4 import os 5 import genshi.template 6 import yaml 7 import lxml.etree 8 9 import constance 10 import viewutils 11 12 template_loader = genshi.template.TemplateLoader( 13 os.path.join(os.path.realpath(os.path.dirname(__file__)), 'templates', 'reading'), 14 variable_lookup='strict') 15 16 class ReadingLogEntry(object): 17 18 def __init__(self, yaml_dict): 19 self.title = viewutils.mini_markdown(yaml_dict['Title']) 20 self.author = yaml_dict['Author'] 21 self.publication_date = self.modified_date = self.date = yaml_dict['Date'] 22 self.url = yaml_dict.get('URL', None) 23 self.isbn = yaml_dict.get('ISBN', None) 24 self.rating = yaml_dict.get('Rating', None) 25 self.tags = frozenset() 26 self.guid = yaml_dict['GUID'] 27 28 def generate_atom(self, config): 29 return template_loader.load('entry.atom').generate(item=self, 30 config=config) 31 32 class ReadingLogEntrySet(object): 33 34 def __init__(self, filename): 35 self.filename = filename 36 self.entries = [] 37 for d in yaml.load_all(open(self.filename, 'r')): 38 self.entries.append(ReadingLogEntry(d)) 39 40 def __iter__(self): 41 return iter(self.entries) 42 43 def __len__(self): 44 return len(self.entries) 45 46 def generate(filename, xslt, config): 47 entries = ReadingLogEntrySet(filename) 48 49 rendered = template_loader.load('reading.html').generate(items=entries, 50 config=config).render('xhtml', encoding='utf8') 51 transformed = str(xslt(lxml.etree.fromstring(rendered))) 52 constance.output(os.path.join(os.path.dirname(filename), 'reading.html'), transformed) 53 54 # feed 55 rendered = template_loader.load('reading.atom').generate(items=entries, 56 config=config).render('xml', encoding='utf8') 57 constance.output(os.path.join(os.path.dirname(filename), 'reading.atom'), rendered) 58 59 return entries