def write_missing_housenumbers( self) -> Tuple[int, int, int, str, List[List[yattag.doc.Doc]]]: """ Calculate a write stat for the house number coverage of a relation. Returns a tuple of: todo street count, todo count, done count, percent and table. """ ongoing_streets, done_streets = self.get_missing_housenumbers() todo_count = 0 table = [] table.append([ util.html_escape(_("Street name")), util.html_escape(_("Missing count")), util.html_escape(_("House numbers")) ]) rows = [] for result in ongoing_streets: # street, only_in_ref row = [] row.append(result[0].to_html()) number_ranges = util.get_housenumber_ranges(result[1]) row.append(util.html_escape(str(len(number_ranges)))) doc = yattag.doc.Doc() if not self.get_config().get_street_is_even_odd( result[0].get_osm_name()): for index, item in enumerate( sorted(number_ranges, key=util.split_house_number_range)): if index: doc.text(", ") doc.asis(util.color_house_number(item).getvalue()) else: util.format_even_odd(number_ranges, doc) row.append(doc) todo_count += len(number_ranges) rows.append(row) # It's possible that get_housenumber_ranges() reduces the # of house numbers, e.g. 2, 4 and # 6 may be turned into 2-6, which is just 1 item. Sort by the 2nd col, which is the new # number of items. table += sorted(rows, reverse=True, key=lambda cells: int(cells[1].getvalue())) done_count = 0 for result in done_streets: number_ranges = util.get_housenumber_ranges(result[1]) done_count += len(number_ranges) if done_count > 0 or todo_count > 0: percent = "%.2f" % (done_count / (done_count + todo_count) * 100) else: percent = "100.00" # Write the bottom line to a file, so the index page show it fast. with self.get_files().get_housenumbers_percent_stream("w") as stream: stream.write(percent) return len(ongoing_streets), todo_count, done_count, percent, table
def missing_relations_view_result(relations: helpers.Relations, request_uri: str) -> yattag.Doc: """Expected request_uri: e.g. /osm/missing-streets/budapest_11/view-result.""" tokens = request_uri.split("/") relation_name = tokens[-2] relation = relations.get_relation(relation_name) doc = yattag.Doc() if not os.path.exists(relation.get_files().get_osm_streets_path()): doc.text(_("No existing streets: ")) with doc.tag("a", href="/osm/streets/" + relation_name + "/update-result"): doc.text(_("Call Overpass to create")) elif not os.path.exists(relation.get_files().get_ref_streets_path()): doc.text(_("No street list: ")) with doc.tag("a", href="/osm/missing-streets/" + relation_name + "/update-result"): doc.text(_("Create from reference")) else: ret = relation.write_missing_streets() todo_count, done_count, percent, streets = ret streets.sort(key=locale.strxfrm) table = [[util.html_escape(_("Street name"))]] for street in streets: table.append([util.html_escape(street)]) with doc.tag("p"): doc.text(_("OpenStreetMap is possibly missing the below {0} streets.").format(str(todo_count))) doc.text(_(" (existing: {0}, ready: {1}%).").format(str(done_count), str(percent))) doc.asis(util.html_table_from_list(table).getvalue()) return doc
def missing_streets_view_result(relations: areas.Relations, request_uri: str) -> yattag.doc.Doc: """Expected request_uri: e.g. /osm/missing-streets/budapest_11/view-result.""" tokens = request_uri.split("/") relation_name = tokens[-2] relation = relations.get_relation(relation_name) doc = yattag.doc.Doc() prefix = config.Config.get_uri_prefix() if not os.path.exists(relation.get_files().get_osm_streets_path()): doc.asis( webframe.handle_no_osm_streets(prefix, relation_name).getvalue()) return doc if not os.path.exists(relation.get_files().get_ref_streets_path()): doc.asis( webframe.handle_no_ref_streets(prefix, relation_name).getvalue()) return doc ret = relation.write_missing_streets() todo_count, done_count, percent, streets = ret streets.sort(key=locale.strxfrm) table = [[util.html_escape(_("Street name"))]] for street in streets: table.append([util.html_escape(street)]) with doc.tag("p"): doc.text( _("OpenStreetMap is possibly missing the below {0} streets."). format(str(todo_count))) doc.text( _(" (existing: {0}, ready: {1}).").format( str(done_count), util.format_percent(str(percent)))) doc.stag("br") with doc.tag("a", href=prefix + "/missing-streets/{}/view-turbo".format(relation_name)): doc.text( _("Overpass turbo query for streets with questionable names")) doc.stag("br") with doc.tag("a", href=prefix + "/missing-streets/" + relation_name + "/view-result.txt"): doc.text(_("Plain text format")) doc.stag("br") with doc.tag("a", href=prefix + "/missing-streets/" + relation_name + "/view-result.chkl"): doc.text(_("Checklist format")) doc.asis(util.html_table_from_list(table).getvalue()) doc.asis( util.invalid_refstreets_to_html( areas.get_invalid_refstreets(relation)).getvalue()) doc.asis( util.invalid_filter_keys_to_html( areas.get_invalid_filter_keys(relation)).getvalue()) return doc
def test_happy(self) -> None: """Tests the happy path.""" fro = [[util.html_escape("A1"), util.html_escape("B1")], [util.html_escape("A2"), util.html_escape("B2")]] expected = '<table class="sortable">' expected += '<tr><th><a href="#">A1</a></th>' expected += '<th><a href="#">B1</a></th></tr>' expected += '<tr><td>A2</td><td>B2</td></tr></table>' ret = util.html_table_from_list(fro).getvalue() self.assertEqual(ret, expected)
def write_missing_housenumbers( self) -> Tuple[int, int, int, str, List[List[yattag.doc.Doc]]]: """ Calculate a write stat for the house number coverage of a relation. Returns a tuple of: todo street count, todo count, done count, percent and table. """ ongoing_streets, done_streets = self.get_missing_housenumbers() todo_count = 0 table = [] table.append([ util.html_escape(_("Street name")), util.html_escape(_("Missing count")), util.html_escape(_("House numbers")) ]) for result in ongoing_streets: # street_name, only_in_ref row = [] row.append(util.html_escape(result[0])) number_ranges = util.get_housenumber_ranges(result[1]) row.append(util.html_escape(str(len(number_ranges)))) number_range_strings = [i.get_number() for i in number_ranges] doc = yattag.doc.Doc() if not self.get_config().get_street_is_even_odd(result[0]): for index, item in enumerate( sorted(number_range_strings, key=util.split_house_number)): if index: doc.text(", ") doc.asis(util.color_house_number(item).getvalue()) else: util.format_even_odd(number_ranges, doc) row.append(doc) todo_count += len(number_ranges) table.append(row) done_count = 0 for result in done_streets: number_ranges = util.get_housenumber_ranges(result[1]) done_count += len(number_ranges) if done_count > 0 or todo_count > 0: percent = "%.2f" % (done_count / (done_count + todo_count) * 100) else: percent = "100.00" # Write the bottom line to a file, so the index page show it fast. with self.get_files().get_housenumbers_percent_stream("w") as stream: stream.write(percent) return len(ongoing_streets), todo_count, done_count, percent, table
def __handle_error(self, exception, raw_env_proc): env, start_response = raw_env_proc err_msg = '<h1>Error occured while processing request: %s</h1>' % ( util.html_escape(env.get('PATH_INFO', '/'))) if self.debug_enabled: (e_type, e_value, e_tb) = sys.exc_info() err_msg += '<h2>Error:</h2><pre>%s</pre><h2>Traceback:</h2><pre>%s</pre>\n' % ( exception, ''.join(traceback.format_exception(e_type, e_value, e_tb)) ) #print exception env['wsgi.errors'].write(err_msg) headers = [('Content-Type', 'text/html; charset=UTF-8')] if isinstance(exception, HttpRequestException): status = exception.status status_str = "%d %s" % (status, HTTP_STATUS_MAP.get(status)) else: status = 500 status_str = "%d %s" % (status, HTTP_STATUS_MAP.get(status)) start_response(status_str, headers) return [ err_msg ]
def game_list(self, games): if len(games) == 0: return "<p>No games</p>" games = sorted(games, key=lambda g: g.created_at, reverse=True) rtn = "<ul>" for g in games: l = "<a href=\"/tournaments/%s/%s\">%s v. %s</a>" % ( util.url_escape(self.tournament.name), g.id, util.html_escape( g.players[0].name), util.html_escape(g.players[1].name)) if g.status != "*": l += " %s-%s %s" % (g.outcomes[0], g.outcomes[1], g.status) rtn += "<li>%s</li>" % (l, ) rtn += "</ul>" return rtn
def handle_main_relation(relations: areas.Relations, filter_for: Callable[[bool, areas.Relation], bool], relation_name: str) -> List[yattag.doc.Doc]: """Handles one relation (one table row) on the main page.""" relation = relations.get_relation(relation_name) # If checking both streets and house numbers, then "is complete" refers to the street coverage # for "hide complete" purposes. complete = True streets = relation.get_config().should_check_missing_streets() row = [] # List[yattag.doc.Doc] row.append(util.html_escape(relation_name)) prefix = config.Config.get_uri_prefix() if streets != "only": cell, percent = handle_main_housenr_percent(relation) doc = yattag.doc.Doc() doc.asis(cell.getvalue()) row.append(doc) complete = float(percent) >= 100.0 date = get_housenumbers_last_modified(relation) doc = yattag.doc.Doc() href = prefix + "/street-housenumbers/" + relation_name + "/view-result" with doc.tag("a", href=href, title=_("updated") + " " + date): doc.text(_("existing house numbers")) row.append(doc) else: row.append(yattag.doc.Doc()) row.append(yattag.doc.Doc()) if streets != "no": cell, percent = handle_main_street_percent(relation) row.append(cell) complete = float(percent) >= 100.0 else: row.append(yattag.doc.Doc()) date = get_streets_last_modified(relation) doc = yattag.doc.Doc() with doc.tag("a", href=prefix + "/streets/" + relation_name + "/view-result", title=_("updated") + " " + date): doc.text(_("existing streets")) row.append(doc) doc = yattag.doc.Doc() with doc.tag("a", href="https://www.openstreetmap.org/relation/" + str(relation.get_config().get_osmrelation())): doc.text(_("area boundary")) row.append(doc) if not filter_for(complete, relation): row.clear() return row
def handle_main(request_uri: str, relations: areas.Relations) -> yattag.doc.Doc: """Handles the main wsgi page. Also handles /osm/filter-for/* which filters for a condition.""" filter_for, refcounty = setup_main_filter_for(request_uri) doc = yattag.doc.Doc() doc.asis(webframe.get_toolbar(relations).getvalue()) doc.asis(handle_main_filters(relations, refcounty).getvalue()) table = [] table.append([ util.html_escape(_("Area")), util.html_escape(_("House number coverage")), util.html_escape(_("Existing house numbers")), util.html_escape(_("Street coverage")), util.html_escape(_("Existing streets")), util.html_escape(_("Additional streets")), util.html_escape(_("Area boundary")) ]) for relation_name in relations.get_names(): row = handle_main_relation(relations, filter_for, relation_name) if row: table.append(row) doc.asis(util.html_table_from_list(table).getvalue()) with doc.tag("p"): with doc.tag( "a", href="https://github.com/vmiklos/osm-gimmisn/tree/master/doc"): doc.text(_("Add new area")) doc.asis(webframe.get_footer().getvalue()) return doc
def get_footer(last_updated: str = "") -> yattag.Doc: """Produces the end of the page.""" items = [] # type: List[yattag.Doc] doc = yattag.Doc() doc.text(_("Version: ")) doc.asis(util.git_link(version.VERSION, "https://github.com/vmiklos/osm-gimmisn/commit/").getvalue()) items.append(doc) items.append(util.html_escape(_("OSM data © OpenStreetMap contributors."))) if last_updated: items.append(util.html_escape(_("Last update: ") + last_updated)) doc = yattag.Doc() doc.stag("hr") with doc.tag("div"): for index, item in enumerate(items): if index: doc.text(" ¦ ") doc.asis(item.getvalue()) return doc
def handle_main_relation( relations: helpers.Relations, filter_for: Callable[[bool, helpers.Relation], bool], relation_name: str ) -> List[yattag.Doc]: """Handles one relation (one table row) on the main page.""" relation = relations.get_relation(relation_name) complete = True streets = relation.get_config().should_check_missing_streets() row = [] # List[yattag.Doc] row.append(util.html_escape(relation_name)) if streets != "only": cell, percent = handle_main_housenr_percent(relation) doc = yattag.Doc() doc.asis(cell.getvalue()) row.append(doc) if float(percent) < 100.0: complete = False date = get_housenumbers_last_modified(relation) doc = yattag.Doc() href = "/osm/street-housenumbers/" + relation_name + "/view-result" with doc.tag("a", href=href, title=_("updated") + " " + date): doc.text(_("existing house numbers")) row.append(doc) else: row.append(yattag.Doc()) row.append(yattag.Doc()) if streets != "no": cell, percent = handle_main_street_percent(relation) row.append(cell) if float(percent) < 100.0: complete = False else: row.append(yattag.Doc()) date = get_streets_last_modified(relation) doc = yattag.Doc() with doc.tag("a", href="/osm/streets/" + relation_name + "/view-result", title=_("updated") + " " + date): doc.text(_("existing streets")) row.append(doc) doc = yattag.Doc() with doc.tag("a", href="https://www.openstreetmap.org/relation/" + str(relation.get_config().get_osmrelation())): doc.text(_("area boundary")) row.append(doc) if not filter_for(complete, relation): row.clear() return row
def test_happy(self) -> None: """Tests the happy path.""" relations = get_relations() relation = relations.get_relation("gazdagret") fro = [[util.html_escape("A1"), util.html_escape("B1")], [util.html_escape("A2"), util.html_escape("B2")]] ret = helpers.make_turbo_query_for_streets(relation, fro) expected = """[out:json][timeout:425]; rel(2713748)->.searchRelation; area(3602713748)->.searchArea; (way["name"="A2"](r.searchRelation); way["name"="A2"](area.searchArea); ); out body; >; out skel qt;""" self.assertEqual(ret, expected)
def handle_github_webhook(environ: Dict[str, Any]) -> yattag.Doc: """Handles a GitHub style webhook.""" body = urllib.parse.parse_qs(environ["wsgi.input"].read().decode('utf-8')) payload = body["payload"][0] root = json.loads(payload) if root["ref"] == "refs/heads/master": subprocess.run(["make", "-C", util.get_abspath(""), "deploy-pythonanywhere"], check=True) return util.html_escape("")
def numbered_streets_to_table( self, numbered_streets: util.NumberedStreets ) -> Tuple[List[List[yattag.doc.Doc]], int]: """Turns a list of numbered streets into a HTML table.""" todo_count = 0 table = [] table.append([ util.html_escape(tr("Street name")), util.html_escape(tr("Missing count")), util.html_escape(tr("House numbers")) ]) rows = [] for result in numbered_streets: # street, only_in_ref row = [] row.append(result[0].to_html()) number_ranges = util.get_housenumber_ranges(result[1]) row.append(util.html_escape(str(len(number_ranges)))) doc = yattag.doc.Doc() if not self.get_config().get_street_is_even_odd( result[0].get_osm_name()): for index, item in enumerate( sorted(number_ranges, key=util.split_house_number_range)): if index: doc.text(", ") doc.asis(util.color_house_number(item).getvalue()) else: util.format_even_odd(number_ranges, doc) row.append(doc) todo_count += len(number_ranges) rows.append(row) # It's possible that get_housenumber_ranges() reduces the # of house numbers, e.g. 2, 4 and # 6 may be turned into 2-6, which is just 1 item. Sort by the 2nd col, which is the new # number of items. table += sorted(rows, reverse=True, key=lambda cells: int(cells[1].getvalue())) return table, todo_count
def standings_table(self): standings = self.tournament.get_standings() if len(standings) == 0: return "<p>No players</p>" rtn = "<table border=\"1\"><tr><td><b>Player</b></td><td><b>Games Played</b></td><td><b>Points</b></td></tr>" for pname in standings: rtn += "<tr><td>%s</td><td>%s</td><td>%s</td></tr>" % ( util.html_escape(pname), standings[pname]['played'], standings[pname]['score']) rtn += "</table>" return rtn
def handle_github_webhook(environ: Dict[str, Any]) -> yattag.doc.Doc: """Handles a GitHub style webhook.""" body = urllib.parse.parse_qs(environ["wsgi.input"].read().decode('utf-8')) payload = body["payload"][0] root = json.loads(payload) if root["ref"] == "refs/heads/master": my_env = os.environ my_env["PATH"] = "osm-gimmisn-env/bin:" + my_env["PATH"] subprocess.run(["make", "-C", config.get_abspath(""), "deploy"], check=True, env=my_env) return util.html_escape("")
def handle_main_relation(ctx: context.Context, relations: areas.Relations, filter_for: Callable[[bool, areas.Relation], bool], relation_name: str) -> List[yattag.doc.Doc]: """Handles one relation (one table row) on the main page.""" relation = relations.get_relation(relation_name) # If checking both streets and house numbers, then "is complete" refers to both street and # housenr coverage for "hide complete" purposes. complete = True streets = relation.get_config().should_check_missing_streets() row = [] # List[yattag.doc.Doc] row.append(util.html_escape(relation_name)) if streets != "only": cell, percent = handle_main_housenr_percent(ctx, relation) doc = yattag.doc.Doc() doc.asis(cell.getvalue()) row.append(doc) complete &= float(percent) >= 100.0 row.append(handle_main_housenr_additional_count(ctx, relation)) else: row.append(yattag.doc.Doc()) row.append(yattag.doc.Doc()) if streets != "no": cell, percent = handle_main_street_percent(ctx, relation) row.append(cell) complete &= float(percent) >= 100.0 else: row.append(yattag.doc.Doc()) if streets != "no": row.append(handle_main_street_additional_count(ctx, relation)) else: row.append(yattag.doc.Doc()) doc = yattag.doc.Doc() with doc.tag("a", href="https://www.openstreetmap.org/relation/" + str(relation.get_config().get_osmrelation())): doc.text(tr("area boundary")) row.append(doc) if not filter_for(complete, relation): row.clear() return row
def render_GET(self, request): request.setHeader("Content-Type", "text/html; charset=utf-8") html = """ <html> <head> <base href="/static/"> <title>Play in tournament {tournament_name}</title> <link rel="stylesheet" href="/static/css/chessboard-0.3.0.css" /> </head> <body> <h2>Play in tournament {tournament_name}</h2> <div id="join_div"> Player name: <input type="text" id="player_name"><br> <button type="button" onclick="join_tournament();">Join tournament</button><br><br> </div> <div id="white_name"></div> <div id="black_name"></div> <div id="board" style="width: 400px"></div> <h3><div id="status_div"></div></h3> <script src="js/jquery-3.2.1.js"></script> <script src="js/chessboard-0.3.0.js"></script> <script src="js/game_lib.js"></script> <script> function join_tournament() {{ var status_div = document.getElementById("status_div"); var white_name_div = document.getElementById("white_name"); var black_name_div = document.getElementById("black_name"); var join_div = document.getElementById("join_div"); var player_name = document.getElementById("player_name").value; play_games({tournament_name_json}, player_name, join_div, status_div, white_name_div, black_name_div); }} </script> </body> </html> """.format(tournament_name=util.html_escape(self.tournament.name), tournament_name_json=json.dumps(self.tournament.name)) # var status_div = document.getElementById("status_div"); # var white_name_div = document.getElementById("white_name"); # var black_name_div = document.getElementById("black_name"); # var pgn_div = document.getElementById("game_pgn"); # observe_game(board, "{game_id}", status_div, white_name_div, black_name_div, pgn_div); return html
def render_GET(self, request): request.setHeader("Content-Type", "text/html; charset=utf-8") html = "<html><head><h1>Tournament %s</h1></head><body>" % ( util.html_escape(self.tournament.name), ) html += "<p>Details</p>" html += self.details_table() html += "<h3><a href=\"/tournaments/%s/play\">Join Tournament</a></32>" % ( util.url_escape(self.tournament.name), ) html += "<h2>Standings</h2>" html += self.standings_table() html += "<h2>Active Games</h2>" html += self.game_list(self.tournament.active_games()) html += "<h2>Completed Games</h2>" html += self.game_list(self.tournament.compleated_games()) html += "</body></html>" return html
def tag(tag, *args, **kw): if kw.has_key("c"): if args: raise TypeError( "The special 'c' keyword argument cannot be used in " "conjunction with non-keyword arguments" ) args = kw["c"] del kw["c"] attrargs = [] for attr, value in kw.items(): if value is None: continue if attr.endswith("_"): attr = attr[:-1] attrargs.append(' %s="%s"' % (attr, html_escape(value))) if not args and tag in empty_tags: return "<%s%s />" % (tag, "".join(attrargs)) else: return "<%s%s>%s</%s>" % (tag, "".join(attrargs), "".join(map(strify, args)), tag)
def tag(tag, *args, **kw): if kw.has_key("c"): if args: raise TypeError( "The special 'c' keyword argument cannot be used in " "conjunction with non-keyword arguments") args = kw["c"] del kw["c"] attrargs = [] for attr, value in kw.items(): if value is None: continue if attr.endswith('_'): attr = attr[:-1] attrargs.append(' %s="%s"' % (attr, html_escape(value))) if not args and tag in empty_tags: return '<%s%s />' % (tag, ''.join(attrargs)) else: return '<%s%s>%s</%s>' % (tag, ''.join(attrargs), ''.join( map(strify, args)), tag)
def render_GET(self, request): request.setHeader("Content-Type", "text/html; charset=utf-8") title = "%s (white) vs. (black) %s, game in %s+%s" % ( self.game.players[0].name, self.game.players[1].name, self.game.time_limit, self.game.increment) title = util.html_escape(title) html = """ <html> <head> <base href="/static/"> <title>{title}</title> <link rel="stylesheet" href="/static/css/chessboard-0.3.0.css" /> </head> <body> <h2>{title}</h2> <div id="white_name"></div> <div id="black_name"></div> <div id="board" style="width: 400px"></div> <h3><div id="status_div">{game_status}</div></h3> <pre><div id="game_pgn" style="width: 400px">{game_pgn}</div></pre> <script src="js/jquery-3.2.1.js"></script> <script src="js/chessboard-0.3.0.js"></script> <script src="js/game_lib.js"></script> <script> window.onload = function() {{ var status_div = document.getElementById("status_div"); var white_name_div = document.getElementById("white_name"); var black_name_div = document.getElementById("black_name"); observe_game("{game_id}", status_div, white_name_div, black_name_div); }} </script> </body> </html> """.format(title=title, game_id=self.game.id, game_pgn=cgi.escape(str(self.game.pgn)), game_status=self.game.outcome_str()) return html
def render_GET(self, request): request.setHeader("Content-Type", "text/html; charset=utf-8") tournament_html = "" if len(self.manager.tournaments): tournaments = sorted(self.manager.tournaments.values(), key=lambda t:t.created_at, reverse=True) tournament_html = "<ul>" for t in tournaments: tournament_html += "<li><a href='/tournaments/%s'>%s</a> (%s games, %s active players)</li>" % (util.url_escape(t.name), util.html_escape(t.name), len(t.all_games()), len(t.players)) tournament_html += "</ul>" else: tournament_html = "<p>No Tournaments :(</p>" form_html = """ <form action="/tournaments/new" method="post"> Tournament name: <input type="text" name="name"><br> Time limit (seconds): <input type="text" name="time_limit"><br> Increment (seconds): <input type="text" name="increment"><br> Games per pairing: <input type="text" name="games_per_pair"><br> <input type="submit" value="Submit"> </form>""" html = "<html><head><h1>Chess Server</h1></head><body><h2>Tournaments</h2>%s<h2>Create new tournament</h2>%s</body></html>" % (tournament_html, form_html) return html
def additional_streets_view_result(ctx: context.Context, relations: areas.Relations, request_uri: str) -> yattag.doc.Doc: """Expected request_uri: e.g. /osm/additional-streets/budapest_11/view-result.""" tokens = request_uri.split("/") relation_name = tokens[-2] relation = relations.get_relation(relation_name) doc = yattag.doc.Doc() prefix = ctx.get_ini().get_uri_prefix() if not ctx.get_file_system().path_exists( relation.get_files().get_osm_streets_path()): doc.asis( webframe.handle_no_osm_streets(prefix, relation_name).getvalue()) elif not ctx.get_file_system().path_exists( relation.get_files().get_ref_streets_path()): doc.asis( webframe.handle_no_ref_streets(prefix, relation_name).getvalue()) else: # Get "only in OSM" streets. streets = relation.write_additional_streets() count = len(streets) lexical_sort_key = util.get_lexical_sort_key() streets.sort( key=lambda street: lexical_sort_key(street.get_osm_name())) table = [[ util.html_escape(tr("Identifier")), util.html_escape(tr("Type")), util.html_escape(tr("Source")), util.html_escape(tr("Street name")) ]] for street in streets: cell = yattag.doc.Doc() href = "https://www.openstreetmap.org/{}/{}".format( street.get_osm_type(), street.get_osm_id()) with cell.tag("a", href=href, target="_blank"): cell.text(str(street.get_osm_id())) cells = [ cell, util.html_escape(street.get_osm_type()), util.html_escape(street.get_source()), util.html_escape(street.get_osm_name()), ] table.append(cells) with doc.tag("p"): doc.text( tr("OpenStreetMap additionally has the below {0} streets."). format(str(count))) doc.stag("br") with doc.tag("a", href=prefix + "/additional-streets/" + relation_name + "/view-result.txt"): doc.text(tr("Plain text format")) doc.stag("br") with doc.tag("a", href=prefix + "/additional-streets/" + relation_name + "/view-result.chkl"): doc.text(tr("Checklist format")) doc.stag("br") with doc.tag( "a", href=prefix + "/additional-streets/{}/view-turbo".format(relation_name)): doc.text(tr("Overpass turbo query for the below streets")) doc.asis(util.html_table_from_list(table).getvalue()) doc.asis( util.invalid_refstreets_to_html( relation.get_invalid_refstreets()).getvalue()) return doc
def mock_handler(_environ: Dict[str, BinaryIO]) -> yattag.doc.Doc: nonlocal mock_called mock_called = True return util.html_escape("")
def handle_stats_cityprogress(relations: areas.Relations) -> yattag.doc.Doc: """Expected request_uri: e.g. /osm/housenumber-stats/hungary/cityprogress.""" doc = yattag.doc.Doc() doc.asis(get_toolbar(relations).getvalue()) ref_citycounts: Dict[str, int] = {} with open(config.Config.get_reference_citycounts_path(), "r") as stream: first = True for line in stream.readlines(): if first: first = False continue cells = line.strip().split('\t') if len(cells) < 2: continue city = cells[0] count = int(cells[1]) ref_citycounts[city] = count today = time.strftime("%Y-%m-%d") osm_citycounts: Dict[str, int] = {} with open(config.Config.get_workdir() + "/stats/" + today + ".citycount", "r") as stream: for line in stream.readlines(): cells = line.strip().split('\t') if len(cells) < 2: continue city = cells[0] count = int(cells[1]) osm_citycounts[city] = count cities = util.get_in_both(list(ref_citycounts.keys()), list(osm_citycounts.keys())) cities.sort(key=locale.strxfrm) table = [] table.append([ util.html_escape(_("City name")), util.html_escape(_("House number coverage")), util.html_escape(_("OSM count")), util.html_escape(_("Reference count")) ]) for city in cities: percent = "100.00" if ref_citycounts[city] > 0 and osm_citycounts[city] < ref_citycounts[ city]: percent = "%.2f" % (osm_citycounts[city] / ref_citycounts[city] * 100) table.append([ util.html_escape(city), util.html_escape(util.format_percent(percent)), util.html_escape(str(osm_citycounts[city])), util.html_escape(str(ref_citycounts[city])) ]) doc.asis(util.html_table_from_list(table).getvalue()) with doc.tag("h2"): doc.text(_("Note")) with doc.tag("div"): doc.text( _("""These statistics are estimates, not taking house number filters into account. Only cities with house numbers in OSM are considered.""")) doc.asis(get_footer().getvalue()) return doc
pd['source'] = 'http://soe.stanford.edu/research/%s' % prof[0] pd['name'] = prof[1] #extract the primary deptmartment from within the <b> tags if '<b>' in prof[2]: pd['department'] = re.findall('<b>(.*?)</b>', prof[2])[0] else: pd['department'] = util.prep_department(util.remove_tags(prof[2])) research = prof[3].replace(' ', '').strip() if len(research) > 0: pd['keywords'] = util.split_and_clean(research, ',') pd['school'] = 'Stanford University' personal_page = util.dl_and_prep(pd['lab_website']) summary = re.findall('<h3>Research Statement</h3><p>(.*?)</p><h3>Degrees</h3>', personal_page) if summary: pd['research_summary'] = util.html_escape(summary[0].strip()) try: pd['image'] = 'http://soe.stanford.edu/research/%s' % re.findall('\'(images/photos_faculty_staff/.*?)\'', personal_page)[0] except Exception: import pdb; pdb.set_trace() pd['title'] = re.findall("Title:</td><td class=\"data\">(.*?)</td>", personal_page)[0] personal_website = re.findall("URL:</TD><TD class=\"data\"><a href='(.*?)'", personal_page) if personal_website: pd['personal_website'] = personal_website[0] print pd['name'], pd['department'] util.validate_professor(pd) output.append(pd) pickle.dump(output, file('prof_dicts/stanford.dat', 'w')) print 'Done!'
def escape(text): if isinstance(text,NoEscape): return str(text.raw_text) else: text=str(text) return html_escape(text)
def missing_streets_update(relations: helpers.Relations, relation_name: str) -> yattag.Doc: """Expected request_uri: e.g. /osm/missing-streets/ujbuda/update-result.""" reference = util.get_abspath(get_config().get('wsgi', 'reference_street').strip()) relation = relations.get_relation(relation_name) relation.write_ref_streets(reference) return util.html_escape(_("Update successful."))