CSAir

.idea
    .name 13 B
    Assignment2.0.iml 295 B
    dictionaries
       lantay77.xml 357 B
    encodings.xml 171 B
    misc.xml 348 B
    modules.xml 289 B
    runConfigurations
       Server.xml 1008 B
       Unittests_in_Tests.xml 1 KB
    scopes
       scope_settings.xml 143 B
    vcs.xml 189 B
    workspace.xml 59 KB
CSAir.py 11 KB
CSAir_helpers.py 9 KB
CSAir_json.py 7 KB
CSAir_server.py 5 KB
Graph
    DiGraph.py 9 KB
    Edge.py 518 B
    Node.py 360 B
Location.py 1 KB
ManualTestPlan.odt 441 KB
ManualTestPlan.pdf 1029 KB
Query.py 2 KB
README 499 B
Server.py 8 KB
Tests
    expected
       brief_print_expected.txt 13 B
       continents_expected.txt 58 B
       expected.json 1 KB
       hubs_expected.txt 70 B
       list_expected.txt 28 B
       longest_expected.txt 36 B
       other_expected.txt 205 B
       print_expected.txt 130 B
       shortest_expected.txt 37 B
    invalid.json 15 KB
    out
       brief_print_test.txt 13 B
       continents_test.txt 58 B
       hubs_test.txt 70 B
       list_test.txt 28 B
       longest_test.txt 36 B
       other_test.txt 205 B
       out.json 1 KB
       print_test.txt 130 B
       shortest_test.txt 37 B
    testCSAir.py 3 KB
    testFunctionsForServer.py 1 KB
    testGraph.py 1 KB
    testLocation.py 938 B
    testServer.py 6 KB
    test_2.1.py 8 KB
    valid.json 871 B
    valid2.json 582 B
add_city_post.py 800 B
changes 633 B
cmi_hub.json 1 KB
map_data.json 15 KB
map_data_r2.json 15 KB
static
    bootstrap.js 59 KB
    css
       bootstrap-theme.css 20 KB
       bootstrap-theme.css.map 22 KB
       bootstrap-theme.min.css 18 KB
       bootstrap.css 129 KB
       bootstrap.css.map 215 KB
       bootstrap.min.css 106 KB
       starter-template.css 186 B
    fonts
       glyphicons-halflings-regular.eot 19 KB
       glyphicons-halflings-regular.svg 61 KB
       glyphicons-halflings-regular.ttf 40 KB
       glyphicons-halflings-regular.woff 22 KB
    js
       bootstrap.js 59 KB
       bootstrap.min.js 31 KB
templates
    city.html 13 KB
    routes.html 10 KB
uploads
CSAir_server.py
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