Ejemplo n.º 1
0
def makeQueryGET(parameters):
    global routes

    # List all the accepted parameters
    allowedParams = ['net', 'network',
                     'sta', 'station',
                     'loc', 'location',
                     'cha', 'channel',
                     'start', 'starttime',
                     'end', 'endtime',
                     'service', 'format',
                     'alternative']

    for param in parameters:
        if param not in allowedParams:
            msg = 'Unknown parameter: %s' % param
            raise WIClientError(msg)

    try:
        if 'network' in parameters:
            net = parameters['network'].value.upper()
        elif 'net' in parameters:
            net = parameters['net'].value.upper()
        else:
            net = '*'

        net = net.split(',')
    except:
        net = ['*']

    try:
        if 'station' in parameters:
            sta = parameters['station'].value.upper()
        elif 'sta' in parameters:
            sta = parameters['sta'].value.upper()
        else:
            sta = '*'

        sta = sta.split(',')
    except:
        sta = ['*']

    try:
        if 'location' in parameters:
            loc = parameters['location'].value.upper()
        elif 'loc' in parameters:
            loc = parameters['loc'].value.upper()
        else:
            loc = '*'

        loc = loc.split(',')
    except:
        loc = ['*']

    try:
        if 'channel' in parameters:
            cha = parameters['channel'].value.upper()
        elif 'cha' in parameters:
            cha = parameters['cha'].value.upper()
        else:
            cha = '*'

        cha = cha.split(',')
    except:
        cha = ['*']

    try:
        if 'starttime' in parameters:
            start = datetime.datetime.strptime(
                parameters['starttime'].value[:19].upper(),
                '%Y-%m-%dT%H:%M:%S')
        elif 'start' in parameters:
            start = datetime.datetime.strptime(
                parameters['start'].value[:19].upper(),
                '%Y-%m-%dT%H:%M:%S')
        else:
            start = None
    except:
        msg = 'Error while converting starttime parameter.'
        raise WIClientError(msg)

    try:
        if 'endtime' in parameters:
            endt = datetime.datetime.strptime(
                parameters['endtime'].value[:19].upper(),
                '%Y-%m-%dT%H:%M:%S')
        elif 'end' in parameters:
            endt = datetime.datetime.strptime(
                parameters['end'].value[:19].upper(),
                '%Y-%m-%dT%H:%M:%S')
        else:
            endt = None
    except:
        msg = 'Error while converting endtime parameter.'
        raise WIClientError(msg)

    try:
        if 'service' in parameters:
            ser = parameters['service'].value.lower()
        else:
            ser = 'dataselect'
    except:
        ser = 'dataselect'

    try:
        if 'alternative' in parameters:
            alt = True if parameters['alternative'].value.lower() == 'true'\
                else False
        else:
            alt = False
    except:
        alt = False

    result = RequestMerge()
    # Expand lists in parameters (f.i., cha=BHZ,HHN) and yield all possible
    # values
    for (n, s, l, c) in lsNSLC(net, sta, loc, cha):
        try:
            result.extend(routes.getRoute(n, s, l, c, start, endt, ser, alt))
        except RoutingException:
            pass

    if len(result) == 0:
        raise WIContentError('No routes have been found!')
    return result
Ejemplo n.º 2
0
def makeQueryPOST(postText):
    global routes

    # This are the parameters accepted appart from N.S.L.C
    extraParams = ['format', 'service', 'alternative']

    # Defualt values
    ser = 'dataselect'
    alt = False

    result = RequestMerge()
    # Check if we are still processing the header of the POST body. This has a
    # format like key=value, one per line.
    inHeader = True

    for line in postText.splitlines():
        if not len(line):
            continue

        if (inHeader and ('=' not in line)):
            inHeader = False

        if inHeader:
            try:
                key, value = line.split('=')
                key = key.strip()
                value = value.strip()
            except:
                msg = 'Wrong format detected while processing: %s' % line
                raise WIClientError(msg)

            if key not in extraParams:
                msg = 'Unknown parameter "%s"' % key
                raise WIClientError(msg)

            if key == 'service':
                ser = value
            elif key == 'alternative':
                alt = True if value.lower() == 'true' else False

            continue

        # I'm already in the main part of the POST body, where the streams are
        # specified
        net, sta, loc, cha, start, endt = line.split()
        net = net.upper()
        sta = sta.upper()
        loc = loc.upper()
        try:
            start = None if start in ("''", '""') else \
                datetime.datetime.strptime(start[:19].upper(),
                                           '%Y-%m-%dT%H:%M:%S')
        except:
            msg = 'Error while converting %s to datetime' % start
            raise WIClientError(msg)

        try:
            endt = None if endt in ("''", '""') else \
                datetime.datetime.strptime(endt[:19].upper(),
                                           '%Y-%m-%dT%H:%M:%S')
        except:
            msg = 'Error while converting %s to datetime' % endt
            raise WIError(msg)

        try:
            result.extend(routes.getRoute(net, sta, loc, cha,
                                          start, endt, ser, alt))
        except RoutingException:
            pass

    if len(result) == 0:
        raise WIContentError('No routes have been found!')
    return result