def run(script, doc, output_file=None, options={}): """process a Django template file""" # this is needed to use the Django template system as standalone # I need to re-import the settings at every call because I have to # set the TEMPLATE_DIRS variable programmatically from django.conf import settings try: settings.configure(DEBUG=True, TEMPLATE_DEBUG=True, TEMPLATE_DIRS=(os.path.dirname(script), )) except RuntimeError: pass from django.template import Context from django.template.loader import get_template # set up the Django context by using the default htmltmpl # datatype converters context = Context(autoescape=(config.django_autoescape() == 'on')) context.update(tmpl.template_info(doc)) context['Config'] = config.planet_options() t = get_template(script) if output_file: reluri = os.path.splitext(os.path.basename(output_file))[0] context['url'] = urlparse.urljoin(config.link(), reluri) f = open(output_file, 'w') ss = t.render(context) if isinstance(ss, unicode): ss = ss.encode('utf-8') f.write(ss) f.close() else: # @@this is useful for testing purposes, but does it # belong here? return t.render(context)
def run(script, doc, output_file=None, options={}): """process a Django template file""" # this is needed to use the Django template system as standalone # I need to re-import the settings at every call because I have to # set the TEMPLATE_DIRS variable programmatically from django.conf import settings try: settings.configure(DEBUG=True, TEMPLATE_DEBUG=True, TEMPLATE_DIRS=(os.path.dirname(script),)) except EnvironmentError: pass from django.template import Context from django.template.loader import get_template # set up the Django context by using the default htmltmpl # datatype converters context = Context() context.update(tmpl.template_info(doc)) context["Config"] = config.planet_options() t = get_template(script) if output_file: reluri = os.path.splitext(os.path.basename(output_file))[0] context["url"] = urlparse.urljoin(config.link(), reluri) f = open(output_file, "w") ss = t.render(context) if isinstance(ss, unicode): ss = ss.encode("utf-8") f.write(ss) f.close() else: # @@this is useful for testing purposes, but does it # belong here? return t.render(context)
def run(script, doc, output_file=None, options={}): """ process an HTMLTMPL file """ manager = htmltmpl.TemplateManager() template = manager.prepare(script) tp = htmltmpl.TemplateProcessor(html_escape=0) for key,value in template_info(doc).items(): tp.set(key, value) if output_file: basename = os.path.basename(output_file) reluri = os.path.splitext(os.path.basename(output_file))[0] tp.set('url', urlparse.urljoin(config.link(),reluri)) tp.set('fullurl', urlparse.urljoin(config.link(),basename)) output = open(output_file, "w") output.write(tp.process(template)) output.close() else: return tp.process(template)
def run(script, doc, output_file=None, options={}): """ process an HTMLTMPL file """ manager = htmltmpl.TemplateManager() template = manager.prepare(script) tp = htmltmpl.TemplateProcessor(html_escape=0) for key, value in template_info(doc).items(): tp.set(key, value) if output_file: basename = os.path.basename(output_file) reluri = os.path.splitext(os.path.basename(output_file))[0] tp.set('url', urlparse.urljoin(config.link(), reluri)) tp.set('fullurl', urlparse.urljoin(config.link(), basename)) output = open(output_file, "w") output.write(tp.process(template)) output.close() else: return tp.process(template)
def test_link(self): self.assertEqual('', config.link())
def template_info(source): """ get template information from a feedparser output """ # wire in support for planet:source, call feedparser, unplug planet:source mixin=feedparser._FeedParserMixin mixin._start_planet_source = mixin._start_source mixin._end_planet_source = \ new.instancemethod(_end_planet_source, None, mixin) data=feedparser.parse(source) del mixin._start_planet_source del mixin._end_planet_source # apply rules to convert feed parser output to htmltmpl input output = {'Channels': [], 'Items': []} output.update(tmpl_mapper(data.feed, Base)) sources = [(source.get('planet_name',None),source) for source in data.feed.get('sources',[])] sources.sort() for name, feed in sources: output['Channels'].append(tmpl_mapper(feed, Base)) for entry in data.entries: output['Items'].append(tmpl_mapper(entry, Items)) # synthesize isPermaLink attribute for item in output['Items']: if item.get('id') == item.get('link'): item['guid_isPermaLink']='true' else: item['guid_isPermaLink']='false' # feed level information output['generator'] = config.generator_uri() output['name'] = config.name() output['link'] = config.link() output['owner_name'] = config.owner_name() output['owner_email'] = config.owner_email() if config.feed(): output['feed'] = config.feed() output['feedtype'] = config.feed().find('rss')>=0 and 'rss' or 'atom' # date/time information date = time.gmtime() output['date'] = PlanetDate(date) output['date_iso'] = Rfc3399(date) output['date_822'] = Rfc822(date) # remove new_dates and new_channels that aren't "new" date = channel = None for item in output['Items']: if item.has_key('new_date'): if item['new_date'] == date: del item['new_date'] else: date = item['new_date'] if item.has_key('new_channel'): if item['new_channel'] == channel and not item.has_key('new_date'): del item['new_channel'] else: channel = item['new_channel'] return output
def test_link(self): self.assertEqual("http://example.com/", config.link())
def test_link(self): self.assertEqual('http://example.com/', config.link())