# Path: lib/layer.py
# def layer(self, node):
#     for l in self.node_to_layer:
#         if int(node) < l[0]:
#             return l[1]
#     print "Unable to find the right layer for node {0}".format(node)
#     print self.node_to_layer
# 
# Path: lib/bikestations.py
# def get_text(node):
# def get_int(node):
# def get_float(node):
#     def __init__(self, url, name, metadata):
#     def import_data(self):
#     def update_from_db(self):
#     def to_string(self):
# class BikeStationImporter():
# 
# Path: web/shorturl.py
# class shortURL:
#     def __init__(self,metadata):
#     def addRouteToDatabase(self,lonMap,latMap,zoom,lonStart,latStart,lonDest,latDest,addressStart,addressDest, time):
#     def getDataFromHash(self,value):


from lib.core import mumoro
from lib.core.mumoro import Bike, Car, Foot, PublicTransport, cost, co2, dist, elevation, line_change, mode_change, Costs
from lib import layer
from lib import bikestations as bikestations
from web import shorturl
from sqlalchemy import *
from sqlalchemy.orm import mapper, sessionmaker, clear_mappers
from cherrypy import request
from genshi.template import TemplateLoader
import cherrypy
import sys
import simplejson as json
import os
import time
import urllib
import httplib
import hashlib
import datetime
    return {'nodes': str(nd), 'edges' : str(ed), 'services': str(services) }

def import_kalkati_data(filename, network_name = "Public Transport"):
    return import_gtfs_data(filename, network_name)

def import_freq(self, line_name, nodesf, linesf):
    return import_gtfs_data(line_name)

#Loads a bike service API ( from already formatted URL ). Insert bike stations in database and enables schedulded re-check.
def import_bike_service( url, name ):
    engine = create_engine(db_type + ":///" + db_params)
    metadata = MetaData(bind = engine)
    mumoro_metadata = Table('metadata', metadata, autoload = True)
    s = mumoro_metadata.select((mumoro_metadata.c.origin == url) & (mumoro_metadata.c.node_or_edge == 'bike_stations'))
    rs = s.execute()
    for row in rs:
         bt = row[0]
    bike_stations_array.append( {'url_api': url,'table': str(bt)} )
    return {'url_api': url,'table': str(bt)}

#Loads data from previous inserted data and creates a layer used in multi-modal graph
def street_layer( data, name, color, mode ):
    if not data or not name:
        raise NameError('One or more parameters are missing')
    if not is_color_valid( color ):
        raise NameError('Color for the layer is invalid')
    if mode != mumoro.Foot and mode != mumoro.Bike and mode != mumoro.Car and mode != None:
        raise NameError('Wrong layer mode paramater')
    engine = create_engine(db_type + ":///" + db_params)
    metadata = MetaData(bind = engine)

# Path: lib/layer.py
# def layer(self, node):
#     for l in self.node_to_layer:
#         if int(node) < l[0]:
#             return l[1]
#     print "Unable to find the right layer for node {0}".format(node)
#     print self.node_to_layer
# 
# Path: lib/bikestations.py
# def get_text(node):
# def get_int(node):
# def get_float(node):
#     def __init__(self, url, name, metadata):
#     def import_data(self):
#     def update_from_db(self):
#     def to_string(self):
# class BikeStationImporter():
# 
# Path: web/shorturl.py
# class shortURL:
#     def __init__(self,metadata):
#     def addRouteToDatabase(self,lonMap,latMap,zoom,lonStart,latStart,lonDest,latDest,addressStart,addressDest, time):
#     def getDataFromHash(self,value):


# Path: server.py
from lib.core import mumoro
from lib.core.mumoro import Bike, Car, Foot, PublicTransport, cost, co2, dist, elevation, line_change, mode_change, Costs
from lib import layer
from lib import bikestations as bikestations
from web import shorturl
from sqlalchemy import *
from sqlalchemy.orm import mapper, sessionmaker, clear_mappers
from cherrypy import request
from genshi.template import TemplateLoader
import cherrypy
import sys
import simplejson as json
import os
import time
import urllib
import httplib
import hashlib
import datetime

    return {'nodes': str(nd), 'edges' : str(ed), 'services': str(services) }

def import_kalkati_data(filename, network_name = "Public Transport"):
    return import_gtfs_data(filename, network_name)

def import_freq(self, line_name, nodesf, linesf):
    return import_gtfs_data(line_name)

#Loads a bike service API ( from already formatted URL ). Insert bike stations in database and enables schedulded re-check.
def import_bike_service( url, name ):
    engine = create_engine(db_type + ":///" + db_params)
    metadata = MetaData(bind = engine)
    mumoro_metadata = Table('metadata', metadata, autoload = True)
    s = mumoro_metadata.select((mumoro_metadata.c.origin == url) & (mumoro_metadata.c.node_or_edge == 'bike_stations'))
    rs = s.execute()
    for row in rs:
         bt = row[0]
    bike_stations_array.append( {'url_api': url,'table': str(bt)} )
    return {'url_api': url,'table': str(bt)}

#Loads data from previous inserted data and creates a layer used in multi-modal graph
def street_layer( data, name, color, mode ):
    if not data or not name:
        raise NameError('One or more parameters are missing')
    if not is_color_valid( color ):
        raise NameError('Color for the layer is invalid')
    if mode != mumoro.Foot and mode != mumoro.Bike and mode != mumoro.Car and mode != None:
        raise NameError('Wrong layer mode paramater')
    engine = create_engine(db_type + ":///" + db_params)
    metadata = MetaData(bind = engine)

    res = layer.Layer(name, mode, data, metadata)