# Path: kotori/io/protocol/util.py
# def is_number(s):
#     """
#     Check string for being a numeric value.
#     http://pythoncentral.io/how-to-check-if-a-string-is-a-number-in-python-including-unicode/
#     """
#     try:
#         float(s)
#         return True
#     except ValueError:
#         pass
# 
#     try:
#         import unicodedata
#         unicodedata.numeric(s)
#         return True
#     except (TypeError, ValueError):
#         pass
# 
#     return False
# 
# Path: kotori/util/common.py
# def tdelta(input):
#     # https://blog.posativ.org/2011/parsing-human-readable-timedeltas-in-python/
#     keys = ["weeks", "days", "hours", "minutes", "seconds"]
#     regex = "".join(["((?P<%s>\d+)%s ?)?" % (k, k[0]) for k in keys])
#     kwargs = {}
#     for k,v in re.match(regex, input).groupdict(default="0").items():
#         kwargs[k] = int(v)
#     return timedelta(**kwargs)


import arrow
from arrow.parser import DateTimeParser
from datetime import datetime, timedelta
from twisted.logger import Logger
from kotori.io.protocol.util import is_number
from kotori.util.common import tdelta
        from pyinfluxql import Query
        from pyinfluxql.functions import Mean

        tags = {}
        #tags = InfluxDBAdapter.get_tags(data)

        expression = Query('*').from_(measurement).where(time__gte=time_begin, time__lte=time_end, **tags)

        result = {
            'expression': str(expression),
            'time_begin': time_begin,
            'time_end':   time_end,
        }

        return result


def get_timedelta(expression):

    # TODO: Use pandas' Timedelta. Timedelta('1m2s')
    # http://pandas.pydata.org/pandas-docs/stable/timedeltas.html

    # FIXME: Sanitize expression
    code = expression
    delta_raw = code.replace('now-', '')
    if code != delta_raw:
        code = code.replace(delta_raw, 'delta')

    # "code" should now be "now-delta"
    #print 'code:', code

    now = datetime.utcnow()

# Path: kotori/io/protocol/util.py
# def is_number(s):
#     """
#     Check string for being a numeric value.
#     http://pythoncentral.io/how-to-check-if-a-string-is-a-number-in-python-including-unicode/
#     """
#     try:
#         float(s)
#         return True
#     except ValueError:
#         pass
# 
#     try:
#         import unicodedata
#         unicodedata.numeric(s)
#         return True
#     except (TypeError, ValueError):
#         pass
# 
#     return False
# 
# Path: kotori/util/common.py
# def tdelta(input):
#     # https://blog.posativ.org/2011/parsing-human-readable-timedeltas-in-python/
#     keys = ["weeks", "days", "hours", "minutes", "seconds"]
#     regex = "".join(["((?P<%s>\d+)%s ?)?" % (k, k[0]) for k in keys])
#     kwargs = {}
#     for k,v in re.match(regex, input).groupdict(default="0").items():
#         kwargs[k] = int(v)
#     return timedelta(**kwargs)


# Path: kotori/io/protocol/influx.py
import arrow
from arrow.parser import DateTimeParser
from datetime import datetime, timedelta
from twisted.logger import Logger
from kotori.io.protocol.util import is_number
from kotori.util.common import tdelta
        from pyinfluxql import Query
        from pyinfluxql.functions import Mean


        tags = {}
        #tags = InfluxDBAdapter.get_tags(data)

        expression = Query('*').from_(measurement).where(time__gte=time_begin, time__lte=time_end, **tags)

        result = {
            'expression': str(expression),
            'time_begin': time_begin,
            'time_end':   time_end,
        }

        return result


def get_timedelta(expression):

    # TODO: Use pandas' Timedelta. Timedelta('1m2s')
    # http://pandas.pydata.org/pandas-docs/stable/timedeltas.html

    # FIXME: Sanitize expression
    code = expression
    delta_raw = code.replace('now-', '')
    if code != delta_raw:
        code = code.replace(delta_raw, 'delta')

    # "code" should now be "now-delta"
    #print 'code:', code

    now = datetime.utcnow()

    delta = tdelta(delta_raw)