/usr/share/pyshared/planet/shell/tmpl.py is in planet-venus 0~bzr116-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | from xml.sax.saxutils import escape
import sgmllib, time, os, sys, new, urlparse, re
from planet import config, feedparser
import htmltmpl
voids=feedparser._BaseHTMLProcessor.elements_no_end_tag
empty=re.compile(r"<((%s)[^>]*)></\2>" % '|'.join(voids))
class stripHtml(sgmllib.SGMLParser):
"remove all tags from the data"
def __init__(self, data):
sgmllib.SGMLParser.__init__(self)
self.result=''
if isinstance(data, str):
try:
self.feed(data.decode('utf-8'))
except:
self.feed(data)
else:
self.feed(data)
self.close()
def __str__(self):
if isinstance(self.result, unicode):
return self.result.encode('utf-8')
return self.result
def handle_entityref(self, ref):
import htmlentitydefs
if ref in htmlentitydefs.entitydefs:
ref=htmlentitydefs.entitydefs[ref]
if len(ref)==1:
self.result+=unichr(ord(ref))
elif ref.startswith('&#') and ref.endswith(';'):
self.handle_charref(ref[2:-1])
else:
self.result+='&%s;' % ref
else:
self.result+='&%s;' % ref
def handle_charref(self, ref):
try:
if ref.startswith('x'):
self.result+=unichr(int(ref[1:],16))
else:
self.result+=unichr(int(ref))
except:
self.result+='&#%s;' % ref
def handle_data(self, data):
if data: self.result+=data
# Data format mappers
def String(value):
if isinstance(value, unicode): return value.encode('utf-8')
return value
def Plain(value):
return str(stripHtml(value))
def PlanetDate(value):
return time.strftime(config.date_format(), value)
def NewDate(value):
return time.strftime(config.new_date_format(), value)
def Rfc822(value):
return time.strftime("%a, %d %b %Y %H:%M:%S +0000", value)
def Rfc3399(value):
return time.strftime("%Y-%m-%dT%H:%M:%S+00:00", value)
# Map from FeedParser path to Planet tmpl names
Base = [
['author', String, 'author'],
['author_name', String, 'author_detail', 'name'],
['generator', String, 'generator'],
['id', String, 'id'],
['icon', String, 'icon'],
['last_updated_822', Rfc822, 'updated_parsed'],
['last_updated_iso', Rfc3399, 'updated_parsed'],
['last_updated', PlanetDate, 'updated_parsed'],
['link', String, 'link'],
['logo', String, 'logo'],
['rights', String, 'rights_detail', 'value'],
['subtitle', String, 'subtitle_detail', 'value'],
['title', String, 'title_detail', 'value'],
['title_plain', Plain, 'title_detail', 'value'],
['url', String, 'links', {'rel':'self'}, 'href'],
['url', String, 'planet_http_location'],
]
Items = [
['author', String, 'author'],
['author_email', String, 'author_detail', 'email'],
['author_name', String, 'author_detail', 'name'],
['author_uri', String, 'author_detail', 'href'],
['content_language', String, 'content', 0, 'language'],
['content', String, 'summary_detail', 'value'],
['content', String, 'content', 0, 'value'],
['date', PlanetDate, 'published_parsed'],
['date', PlanetDate, 'updated_parsed'],
['date_822', Rfc822, 'published_parsed'],
['date_822', Rfc822, 'updated_parsed'],
['date_iso', Rfc3399, 'published_parsed'],
['date_iso', Rfc3399, 'updated_parsed'],
['enclosure_href', String, 'links', {'rel': 'enclosure'}, 'href'],
['enclosure_length', String, 'links', {'rel': 'enclosure'}, 'length'],
['enclosure_type', String, 'links', {'rel': 'enclosure'}, 'type'],
['id', String, 'id'],
['link', String, 'links', {'rel': 'alternate'}, 'href'],
['new_channel', String, 'source', 'id'],
['new_date', NewDate, 'published_parsed'],
['new_date', NewDate, 'updated_parsed'],
['rights', String, 'rights_detail', 'value'],
['title_language', String, 'title_detail', 'language'],
['title_plain', Plain, 'title_detail', 'value'],
['title', String, 'title_detail', 'value'],
['summary_language', String, 'summary_detail', 'language'],
['updated', PlanetDate, 'updated_parsed'],
['updated_822', Rfc822, 'updated_parsed'],
['updated_iso', Rfc3399, 'updated_parsed'],
['published', PlanetDate, 'published_parsed'],
['published_822', Rfc822, 'published_parsed'],
['published_iso', Rfc3399, 'published_parsed'],
]
# Add additional rules for source information
for rule in Base:
Items.append(['channel_'+rule[0], rule[1], 'source'] + rule[2:])
def tmpl_mapper(source, rules):
"Apply specified rules to the source, and return a template dictionary"
output = {}
for rule in rules:
node = source
for path in rule[2:]:
if isinstance(path, str) and path in node:
if path == 'value':
if node.get('type','')=='text/plain':
node['value'] = escape(node['value'])
node['type'] = 'text/html'
elif node.get('type','')=='application/xhtml+xml':
node['value'] = empty.sub(r"<\1 />", node['value'])
node = node[path]
elif isinstance(path, int):
node = node[path]
elif isinstance(path, dict):
for test in node:
for key, value in path.items():
if test.get(key,None) != value: break
else:
node = test
break
else:
break
else:
break
else:
if node: output[rule[0]] = rule[1](node)
# copy over all planet namespaced elements from parent source
for name,value in source.items():
if name.startswith('planet_'):
output[name[7:]] = String(value)
if not output.get('name') and source.has_key('title_detail'):
output['name'] = Plain(source.title_detail.value)
# copy over all planet namespaced elements from child source element
if 'source' in source:
for name,value in source.source.items():
if name.startswith('planet_'):
output['channel_' + name[7:]] = String(value)
if not output.get('channel_name') and \
source.source.has_key('title_detail'):
output['channel_name'] = Plain(source.source.title_detail.value)
return output
def _end_planet_source(self):
self._end_source()
context = self._getContext()
if not context.has_key('sources'): context['sources'] = []
context.sources.append(context.source)
del context['source']
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 = []
for feed in data.feed.get('sources',[]):
source = tmpl_mapper(feed, Base)
sources.append([source.get('name'), source])
sources.sort()
output['Channels'] = [source for name,source in sources]
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 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:
reluri = os.path.splitext(os.path.basename(output_file))[0]
tp.set('url', urlparse.urljoin(config.link(),reluri))
output = open(output_file, "w")
output.write(tp.process(template))
output.close()
else:
return tp.process(template)
if __name__ == '__main__':
sys.path.insert(0, os.path.split(sys.path[0])[0])
for test in sys.argv[1:]:
from pprint import pprint
pprint(template_info('/home/rubys/bzr/venus/tests/data/filter/tmpl/'+test))
|