# Path: helpers/config.py
# def load_config():
#     """ Load configuration and set debug flag for this environment """
#     
#     # Load global configuration
#     config = yaml.load(open(os.path.abspath('./conf/global.yaml'), 'r').read())
# 
#     # Detect development or production environment and configure accordingly
#     if os.environ['SERVER_SOFTWARE'].startswith('Dev'):
#         conf_f = open(os.path.abspath('./conf/development.yaml'), 'r')
#         config = dict(config.items() + yaml.load(conf_f.read()).items())
#     else:
#         conf_f = open(os.path.abspath('./conf/production.yaml'), 'r')
#         config = dict(config.items() + yaml.load(conf_f.read()).items())
# 
#     return config
# 
# Path: controllers/root.py
# class HomeRoute(SessionHandler):
#     def get(self):
# def error_404(request, response, exception):
# def error_500(request, response, exception):
# 
# Path: controllers/public.py
# class ProfileRoute(SessionHandler):
# class DataSourceRoute(SessionHandler):
# class DataViewRoute(SessionHandler):
#     def get(self, profile_slug):
#     def get(self, profile_slug, data_source_slug):
#     def get(self, profile_slug, data_source_slug, data_view_ext):
# 
# Path: controllers/session.py
# class LoginRoute(SessionHandler):
# class LogoutRoute(SessionHandler):
# class OAuth2CallbackRoute(SessionHandler):
#     def get(self):
#     def get(self):
#     def get(self):
# 
# Path: controllers/dashboard.py
# class MainRoute(SessionHandler):
#     def get(self):
# 
# Path: controllers/api.py
# def log_api_error(obj,error):
#     def dispatch(self):
#     def get(self):
#     def post(self):
#     def get(self):
#     def post(self):
#     def get(self, data_source_id):
#     def post(self, data_source_id):
#     def delete(self, data_source_id):
#     def get(self, data_source_id):
#     def post(self, data_source_id):
#     def get(self, data_source_id, data_view_id):
#     def post(self, data_source_id, data_view_id):
#     def delete(self, data_source_id, data_view_id):
#     def get(self):
#     def get(self, google_sheets_id):
#     def get(self, google_sheets_id, worksheet_key):
#     def post(self):
#     def get(self):
# class APIHandler(SessionHandler):
# class UserRoute(APIHandler):
# class DataSourceListRoute(APIHandler):
# class DataSourceItemRoute(APIHandler):
# class DataViewListRoute(APIHandler):
# class DataViewItemRoute(APIHandler):
# class GoogleSheetsListRoute(APIHandler):
# class GoogleSheetsItemRoute(APIHandler):
# class GoogleSheetsWorksheetRoute(APIHandler):
# class TemplatePreviewRoute(APIHandler):
# class Error404Route(RequestHandler):


import sys
from webapp2 import WSGIApplication
from helpers.config import load_config
from controllers import root
from controllers import public
from controllers import session
from controllers import dashboard
from controllers import api
    (r'/api/0/google/sheets/([\w\-]+)/?',       api.GoogleSheetsItemRoute),
    (r'/api/0/google/sheets/([\w\-]+)/(\w+)/?', api.GoogleSheetsWorksheetRoute),
    (r'/api/0/user/?',                          api.UserRoute),
    (r'/api/0/data_source/?',                   api.DataSourceListRoute),
    (r'/api/0/data_source/(\d+)/?',             api.DataSourceItemRoute),
    (r'/api/0/data_source/(\d+)/view/?',        api.DataViewListRoute),
    (r'/api/0/data_source/(\d+)/view/(\d+)/?',  api.DataViewItemRoute),
    (r'/api/0/template/preview/?',              api.TemplatePreviewRoute),
    (r'/api/.*',                                api.Error404Route),
    (r'/api/?',                                 api.Error404Route),

    # Public Site
    # Last in list for profile pattern matching
    (r'/([\w\-]+)/?',                           public.ProfileRoute),
    (r'/([\w\-]+)/([\w\-]+)/?',                 public.DataSourceRoute),
    (r'/([\w\-]+)/([\w\-]+)\.([A-Za-z]+)?',     public.DataViewRoute),
]


# Load config
config = load_config()


# Create the application
app = WSGIApplication(routes=routes, debug=config['debug'], config=config)


# Define error page handlers for production only
# We want to see full error traces during development
if not config['debug']:

# Path: helpers/config.py
# def load_config():
#     """ Load configuration and set debug flag for this environment """
#     
#     # Load global configuration
#     config = yaml.load(open(os.path.abspath('./conf/global.yaml'), 'r').read())
# 
#     # Detect development or production environment and configure accordingly
#     if os.environ['SERVER_SOFTWARE'].startswith('Dev'):
#         conf_f = open(os.path.abspath('./conf/development.yaml'), 'r')
#         config = dict(config.items() + yaml.load(conf_f.read()).items())
#     else:
#         conf_f = open(os.path.abspath('./conf/production.yaml'), 'r')
#         config = dict(config.items() + yaml.load(conf_f.read()).items())
# 
#     return config
# 
# Path: controllers/root.py
# class HomeRoute(SessionHandler):
#     def get(self):
# def error_404(request, response, exception):
# def error_500(request, response, exception):
# 
# Path: controllers/public.py
# class ProfileRoute(SessionHandler):
# class DataSourceRoute(SessionHandler):
# class DataViewRoute(SessionHandler):
#     def get(self, profile_slug):
#     def get(self, profile_slug, data_source_slug):
#     def get(self, profile_slug, data_source_slug, data_view_ext):
# 
# Path: controllers/session.py
# class LoginRoute(SessionHandler):
# class LogoutRoute(SessionHandler):
# class OAuth2CallbackRoute(SessionHandler):
#     def get(self):
#     def get(self):
#     def get(self):
# 
# Path: controllers/dashboard.py
# class MainRoute(SessionHandler):
#     def get(self):
# 
# Path: controllers/api.py
# def log_api_error(obj,error):
#     def dispatch(self):
#     def get(self):
#     def post(self):
#     def get(self):
#     def post(self):
#     def get(self, data_source_id):
#     def post(self, data_source_id):
#     def delete(self, data_source_id):
#     def get(self, data_source_id):
#     def post(self, data_source_id):
#     def get(self, data_source_id, data_view_id):
#     def post(self, data_source_id, data_view_id):
#     def delete(self, data_source_id, data_view_id):
#     def get(self):
#     def get(self, google_sheets_id):
#     def get(self, google_sheets_id, worksheet_key):
#     def post(self):
#     def get(self):
# class APIHandler(SessionHandler):
# class UserRoute(APIHandler):
# class DataSourceListRoute(APIHandler):
# class DataSourceItemRoute(APIHandler):
# class DataViewListRoute(APIHandler):
# class DataViewItemRoute(APIHandler):
# class GoogleSheetsListRoute(APIHandler):
# class GoogleSheetsItemRoute(APIHandler):
# class GoogleSheetsWorksheetRoute(APIHandler):
# class TemplatePreviewRoute(APIHandler):
# class Error404Route(RequestHandler):


# Path: main.py
import sys
from webapp2 import WSGIApplication
from helpers.config import load_config
from controllers import root
from controllers import public
from controllers import session
from controllers import dashboard
from controllers import api

    (r'/api/0/google/sheets/([\w\-]+)/?',       api.GoogleSheetsItemRoute),
    (r'/api/0/google/sheets/([\w\-]+)/(\w+)/?', api.GoogleSheetsWorksheetRoute),
    (r'/api/0/user/?',                          api.UserRoute),
    (r'/api/0/data_source/?',                   api.DataSourceListRoute),
    (r'/api/0/data_source/(\d+)/?',             api.DataSourceItemRoute),
    (r'/api/0/data_source/(\d+)/view/?',        api.DataViewListRoute),
    (r'/api/0/data_source/(\d+)/view/(\d+)/?',  api.DataViewItemRoute),
    (r'/api/0/template/preview/?',              api.TemplatePreviewRoute),
    (r'/api/.*',                                api.Error404Route),
    (r'/api/?',                                 api.Error404Route),

    # Public Site
    # Last in list for profile pattern matching
    (r'/([\w\-]+)/?',                           public.ProfileRoute),
    (r'/([\w\-]+)/([\w\-]+)/?',                 public.DataSourceRoute),
    (r'/([\w\-]+)/([\w\-]+)\.([A-Za-z]+)?',     public.DataViewRoute),
]


# Load config
config = load_config()


# Create the application
app = WSGIApplication(routes=routes, debug=config['debug'], config=config)


# Define error page handlers for production only
# We want to see full error traces during development
if not config['debug']:

    app.error_handlers[404] = root.error_404