# 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
# 
# class ConfigurationError(Exception):
#     """ A general configuration exception """
#     def __init__(self, value):
#         self.value = value
# 
#     def __str__(self):
#         return self.value


import httplib2
import re
import logging
import unicodedata
import base64
import xml.etree.ElementTree as ET
from datetime import datetime as dt
from helpers.config import load_config, ConfigurationError
from oauth2client.client import OAuth2WebServerFlow, OAuth2Credentials
from oauth2client.anyjson import simplejson as json
from apiclient.discovery import build
    # Strip XML namespace    
    gsx = '{http://schemas.google.com/spreadsheets/2006/extended}'
    heading = heading.replace(gsx, '')
    # Replace hyphens with underscores
    heading = heading.replace('-','_')
    # Convert accented letter to ASCII lower-case equivalents
    return unicodedata.normalize('NFD', unicode(heading)).encode('ascii', 'ignore')


# Convert cell values to int or float if possible
# Normaises dates and times to "YYYY-MM-DD HH-MM-SS" format 
# including with millisecond precision if available
#
#  value: str/unicode
#                          .__
#  Also: Skiing :)         |__]
#                          |
#                          |
#                          |                              _
def _val(val):    #::::....|...                       ___(_)
    if val is None: return ""  #:.                   /  / /\
    try: return int(val)          #:.               /   `\\ |
    except ValueError:               #:.               \\// |
        try:                            #:.             \\\
            return float(val)              #:.           \\\
        except ValueError:                     #:.        ``
            try:                                   #:.                         ^
                return str(dt.strptime(val,"%H:%M:%S"))#:.                     ^
            except ValueError:                            #:.                 ^^^
                try:                                         #:.              ^^^

# 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
# 
# class ConfigurationError(Exception):
#     """ A general configuration exception """
#     def __init__(self, value):
#         self.value = value
# 
#     def __str__(self):
#         return self.value


# Path: helpers/google_api.py
import httplib2
import re
import logging
import unicodedata
import base64
import xml.etree.ElementTree as ET
from datetime import datetime as dt
from helpers.config import load_config, ConfigurationError
from oauth2client.client import OAuth2WebServerFlow, OAuth2Credentials
from oauth2client.anyjson import simplejson as json
from apiclient.discovery import build

    # Strip XML namespace    
    gsx = '{http://schemas.google.com/spreadsheets/2006/extended}'
    heading = heading.replace(gsx, '')
    # Replace hyphens with underscores
    heading = heading.replace('-','_')
    # Convert accented letter to ASCII lower-case equivalents
    return unicodedata.normalize('NFD', unicode(heading)).encode('ascii', 'ignore')


# Convert cell values to int or float if possible
# Normaises dates and times to "YYYY-MM-DD HH-MM-SS" format 
# including with millisecond precision if available
#
#  value: str/unicode
#                          .__
#  Also: Skiing :)         |__]
#                          |
#                          |
#                          |                              _
def _val(val):    #::::....|...                       ___(_)
    if val is None: return ""  #:.                   /  / /\
    try: return int(val)          #:.               /   `\\ |
    except ValueError:               #:.               \\// |
        try:                            #:.             \\\
            return float(val)              #:.           \\\
        except ValueError:                     #:.        ``
            try:                                   #:.                         ^
                return str(dt.strptime(val,"%H:%M:%S"))#:.                     ^
            except ValueError:                            #:.                 ^^^
                try:                                         #:.              ^^^

                    return str(dt.strptime(val, "%d/%m/%Y"))    #:.          ^^^^^