def link_values(model, subject, property): values = get_values(model, subject, property) for value in values: if value is None: yield None elif value.is_literal(): yield html_escape(value.literal_value['string']) elif value.is_resource(): yield """<a href="%s">%s</a>""" % html_escapes(value.uri, value.uri)
def image_values(model, subject, property): values = get_values(model, subject, property) for value in values: if value is None: yield None elif value.is_literal(): yield html_escape(value.literal_value['string']) elif value.is_resource(): yield """<img src="%s" />""" % html_escapes(value.uri)
def friend_values(datarooturi, model, subject, property): values = get_values(model, subject, property) nick2people = get_nick2people() for value in values: if value is None: yield None elif value.is_literal(): yield html_escape(value.literal_value['string']) elif value.is_resource(): for nick,person in nick2people.items(): if str(value.uri) == person: user = datarooturi + "users/%s#user" % nick yield """<a href="%s">%s</a>""" % html_escapes(user, nick)
def nick_values(datarooturi, model, subject, properties): values = get_values(model, subject, properties) for value in values: if value is None: yield None elif value.is_resource(): uri = str(value.uri) for pattern in [ r"http://(irc.sioc-project.org)/users/(.+)#user", r"(%s)users/(.+)#user" % re.escape(datarooturi), r"irc://(freenode|freenode.net|irc.freenode.net)/(.+),isnick"]: match = re.match(pattern, uri) if match and match.group(0) == uri: yield """ <a href="%susers/%s#user">%s</a> """ % html_escapes(datarooturi, match.groups()[1], match.groups()[1]) break
def multitarget(keya, keyb, api, **params): res = u"" max_results = 99 # geocode source: coords[keya] = call_geocode(keya, api)[0] rev_coords[best_geocode_kkj(coords[keya])] = format_loc(best_geocode(coords[keya])) # geocode n closest targets: poi_group = poi_groups[keyb] poi_group.load_all() pois = poi_group.n_closest(5, best_geocode_kkj(coords[keya])) addresses = [poi.address for poi in pois] for keybpart in addresses: coords[keybpart] = call_geocode(keybpart, api)[0] rev_coords[best_geocode_kkj(coords[keybpart])] = format_loc(best_geocode(coords[keybpart])) res += print_utf8(u"<h3>Lähtöpisteenä %s</h3>" % html_escapes(format_loc(best_geocode(coords[keya])))) # public transport: routes = [] for poi in pois: route_res = call_route(a=best_geocode_kkj(coords[keya]), b=best_geocode_kkj(coords[poi.address]), api=api) route = sorted(route_res.findall("ROUTE"), key=route_duration)[0] routes += [(route_duration(route), poi, route)] routes.sort() res += u"<table><tr><th colspan=3>Joukkoliikenteellä lähimmät</th></tr>" for (_duration, poi, route) in routes[:max_results]: # print_route(route) source = rev_coords.get(route[1].get("x")[:-2] + "," + route[1].get("y")[:-2], "") target = rev_coords[route[-1].get("x")[:-2] + "," + route[-1].get("y")[:-2]] info_url = "http://www.reittiopas.fi/fi/?from=%s&to=%s" % (source, target) lines = ( "-".join([format_code(part.get("code"), api).strip() for part in route.findall("LINE")]) or u"vain kävelyä" ) res += print_utf8( """<tr><td align=right>%s</td><td>%s</td><td align=right>(<a href="%s">%s</a>)</td></tr>""" % html_escapes(format_length(route.find("LENGTH"))[0], poi.address, info_url, lines) ) res += "\n" res += u"""<tr><td colspan=3 align=right class='attribution'>(Tietolähde <a href="http://www.reittiopas.fi/">HSL Reittiopas</a>)</td></tr></table>""" # biking: if api != "Matka.fi": res += u"<table><tr><th colspan=3>Pyörällä lähimmät</th></tr>" for dist, keybpart in sorted( [ (call_kevytliikenne(best_geocode_kkj(coords[keya]), best_geocode_kkj(coords[keybpart])), keybpart) for keybpart in addresses ] )[:max_results]: info_url = "http://kevytliikenne.ytv.fi/fi/#from(point*%s)to(point*%s)" % ( best_geocode_kkj(coords[keya]).replace(",", "*"), best_geocode_kkj(coords[keybpart]).replace(",", "*"), ) res += print_utf8( """<tr><td align=right>%s</td><td>%s</td><td align=right>(<a href="%s">%.1f km</a>)</td></tr>""" % ( format_duration(dist / 1000.0 / 17.0 * 60), html_escape(rev_coords[best_geocode_kkj(coords[keybpart])]), info_url, dist / 1000.0, ) ) res += u"""<tr><td colspan=3 align=right class='attribution'>(Tietolähde <a href="http://pk.hsl.fi/">Pyöräilyn ja kävelyn Reittiopas)</a></td></tr></table>""" # walking: routes = [] for keybpart in addresses: route_res = call_route( a=best_geocode_kkj(coords[keya]), b=best_geocode_kkj(coords[keybpart]), use_bus=0, use_train=0, use_ferry=0, use_metro=0, use_tram=0, show=1, api=api, ) routes += route_res.findall("ROUTE") routes = sorted(routes, key=route_duration) res += u"<table><tr><th colspan=3>Kävellen lähimmät</th></tr>" for route in routes[:max_results]: # print_route(route) source = rev_coords.get(route[1].get("x")[:-2] + "," + route[1].get("y")[:-2], "") target = rev_coords[route[-1].get("x")[:-2] + "," + route[-1].get("y")[:-2]] info_url = ( "http://www.reittiopas.fi/fi/?from=%s&to=%s&mc1=1&mc2=1&mc3=1&mc4=1&mc5=1&mc6=1&mc0=1&nroutes=1&searchformtype=advanced#ResultDetails" % (source, target) ) res += print_utf8( """<tr><td align=right>%s</td><td>%s</td><td align=right>(<a href="%s">%s</a>)</td></tr>""" % html_escapes( format_length(route.find("LENGTH"))[0], target, info_url, format_length(route.find("LENGTH"))[1] ) ) res += "\n" res += u"""<tr><td colspan=3 align=right class='attribution'>(Tietolähde <a href="http://www.reittiopas.fi/">HSL Reittiopas</a>)</td></tr></table>""" # as the crow flies: if False: # don't render this info: res += u"<table><tr><th colspan=3>Kartalla lähimmät</th></tr>" for dist, keybpart in sorted( [ (distance_map(best_geocode_kkj(coords[keya]), best_geocode_kkj(coords[keybpart])), keybpart) for keybpart in addresses ] )[:max_results]: res += print_utf8( """<tr><td>%s</td><td align=right>(%.1f km)</td></tr>""" % (html_escape(rev_coords[best_geocode_kkj(coords[keybpart])]), dist / 1000.0) ) res += u"""<tr><td colspan=3 align=right class='attribution'>(Tietolähde <a href="http://www.reittiopas.fi/">HSL Reittiopas</a>)</td></tr></table>""" return res