from CSAir import *
"""CSAir Server helper functions"""
class ServerLog:
"""Class containing the HTML log of user actions"""
def __init__(self):
self.log_string = ""
def get_log(self):
return self.log_string
def append(self, entry):
self.log_string = Markup(entry) + self.log_string
def log_yellow(self, message):
self.log_string = (Markup("<div class=\"alert alert-warning\" role=\"alert\">" + str(message) + "</div>")) \
+ self.log_string
def log_green(self, message):
#return_string += "<div class=\"alert alert-success\" role=\"alert\">" + message + "</div>")
self.log_string = (Markup('<div class="alert alert-success" role="alert">' + str(message) + '</div>')) \
+ self.log_string
def clear(self):
self.log_string = ""
def get_stats(graph):
"""
Get overall statistics.
:param graph: The graph
:return: HTML string containing statistics.
"""
stats = print_longest(graph) + "\n"
stats += print_shortest(graph) + "\n"
stats += print_average(graph) + "\n"
stats += print_smallest_city(graph) + "\n"
stats += print_largest_city(graph) + "\n"
stats += print_average_city(graph)
s_text = Markup("<div class=\"row\"><div class=\"col-xs-6 col-md-4\">")
i = 0
for lin in stats.split('\n'):
s_text += Markup.escape(lin) + Markup('<br />')
i += 1
if i % 2 == 0: # for every two lines create a new column
s_text += Markup("<br /></div><div class=\"col-xs-6 col-md-4\">")
s_text += Markup("</div></div>")
return s_text
def generate_city_menu(graph):
"""
Generates the city menu for the web GUI.
:param graph: the graph
:return: HTML string containing the menu.
"""
city_menu = ""
for city in sorted(graph.get_nodes()):
city_menu += Markup("<li><a href=\"/?code=" + city + "\" class=\"list-group-item\">" + city + " "
+ "(" + city.get_object().name + ")</a></li>")
return city_menu
def n_to_br(string):
"""
Converts all newline characters in a string to HTML line breaks.
:param string: String to convert
:return: converted string
"""
out_string = ""
for lin in string.split('\n'):
out_string += Markup.escape(lin) + Markup('<br />')
return out_string
def generate_shortest_route(graph, source, dest, rm, add, log):
"""
Helper function for display_route
Calls dijkstra to find the shortest route between source and dest
:return: The shortest route if found.
"""
route = None
#Make sure we are not removing or adding a route.
if (source is not None and dest is not None) and (add is None and rm is None):
route = graph.dijkstra(source, dest)
if route is not None:
log.log_green("Found shortest route between " + source + " and " + dest)
else:
log.log_yellow("Did not find shortest route between " + source + " and " + dest)
return route
def generate_map_url_from_list(graph, route_list):
"""
Generates the map URL from a route list.
:param graph: The graph
:param route_list: The route
:return: the map URL, if the list is empty returns the overall map.
"""
if route_list is not None:
url_string = "http://www.gcmap.com/map?P="
for i, _ in enumerate(route_list[0:-1]):
url_string += (route_list[i] + "-" + route_list[i + 1] + "%0D%0A")
return url_string + "&MS=wls&MR=1800&MX=720x360&PM=*"
else:
return generate_map_url(graph)
def set_city_params_to_none():
"""Helper function for display_main"""
c_info = None
wx_url = None
name = None
country = None
continent = None
timezone = None
latitude = None
longitude = None
pop = None
region = None
return c_info, continent, country, latitude, longitude, name, pop, region, timezone, wx_url
def get_route_args(request):
"""Helper function for display_route"""
try:
source = request.args.get('source').upper()
except AttributeError:
source = None
try:
dest = request.args.get('dest').upper()
except AttributeError:
dest = None
try:
route_str = request.args.get('route').upper()
except AttributeError:
route_str = None
noquery = request.args.get('noquery')
clear = request.args.get('clear')
add = request.args.get('add')
direction = request.args.get('direction')
distance = request.args.get('distance')
rm = request.args.get('rm')
if direction == "true":
direction = "y"
else:
direction = "n"
return add, clear, dest, direction, distance, noquery, rm, route_str, source
def get_add_city_args(request):
"""Helper function for add_city"""
name = request.args.get('cityName')
country = request.args.get('cityCountry')
continent = request.args.get('cityContinent')
timezone = request.args.get('cityTimezone')
latitude = request.args.get('cityLatitude')
longitude = request.args.get('cityLongitude')
population = request.args.get('cityPop')
region = request.args.get('cityRegion')
return continent, country, latitude, longitude, name, population, region, timezone