Exemple #1
0
def arc2fdsnws(filein, fileout, config='../routing.cfg'):
    """Read the routing file in XML format and add the Dataselect and Station
routes based on the Arclink information. The resulting table is stored in the
file passed in the second parameter.

:param filein: Input file with routes (usually from an Arclink server).
:type filein: str
:param fileout: Output file with all routes from the input file plus new
                Station and Dataselect routes based on the Arclink route.
:type fileout: str
"""
    rc = RoutingCache(filein, config=config)
    rOut = RoutingCache()
    for st, lr in rc.routingTable.iteritems():
        toAdd = list()
        for r in lr:
            if r.service == 'arclink':
                stat = Route('station', '%s/fdsnws/station/1/query' %
                             mapArcFDSN(r.address), r.tw, r.priority)
                toAdd.append(stat)

                data = Route('dataselect', '%s/fdsnws/dataselect/1/query' %
                             mapArcFDSN(r.address), r.tw, r.priority)
                toAdd.append(data)

        try:
            rOut.routingTable[st].extend(toAdd)
        except:
            rOut.routingTable[st] = toAdd

    rOut.toXML(fileout)
Exemple #2
0
def application(environ, start_response):
    """Main WSGI handler. Process requests and calls proper functions."""
    global routes
    fname = environ['PATH_INFO']

    config = configparser.RawConfigParser()
    here = os.path.dirname(__file__)
    config.read(os.path.join(here, 'routing.cfg'))
    verbo = config.get('Service', 'verbosity')
    baseURL = config.get('Service', 'baseURL')
    # Warning is the default value
    verboNum = getattr(logging, verbo.upper(), 30)
    logging.info('Verbosity configured with %s' % verboNum)
    logging.basicConfig(level=verboNum)

    # Among others, this will filter wrong function names,
    # but also the favicon.ico request, for instance.
    if fname is None:
        raise WIClientError('Method name not recognized!')
        # return send_html_response(status, 'Error! ' + status, start_response)

    if len(environ['QUERY_STRING']) > 1000:
        return send_error_response("414 Request URI too large",
                                   "maximum URI length is 1000 characters",
                                   start_response)

    try:
        outForm = 'xml'

        if environ['REQUEST_METHOD'] == 'GET':
            form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
            if 'format' in form:
                outForm = form['format'].value.lower()
        elif environ['REQUEST_METHOD'] == 'POST':
            try:
                length = int(environ.get('CONTENT_LENGTH', '0'))
            except ValueError:
                length = 0

            # If there is a body to read
            if length:
                form = environ['wsgi.input'].read(length).decode()
            else:
                form = environ['wsgi.input'].read().decode()

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

                if '=' not in line:
                    break
                k, v = line.split('=')
                if k.strip() == 'format':
                    outForm = v.strip()

        else:
            raise Exception

    except ValueError as e:
        if str(e) == "Maximum content length exceeded":
            # Add some user-friendliness (this message triggers an alert
            # box on the client)
            return send_error_response("400 Bad Request",
                                       "maximum request size exceeded",
                                       start_response)

        return send_error_response("400 Bad Request", str(e), start_response)

    # Check whether the function called is implemented
    implementedFunctions = ['query', 'application.wadl', 'localconfig',
                            'globalconfig', 'version', 'info', '',
                            'virtualnets', 'endpoints']

    if routes is None:
        # Add routing cache here, to be accessible to all modules
        routesFile = os.path.join(here, 'data', 'routing.xml')
        configFile = os.path.join(here, 'routing.cfg')
        routes = RoutingCache(routesFile, configFile)

    fname = environ['PATH_INFO'].split('/')[-1]
    if fname not in implementedFunctions:
        return send_error_response("400 Bad Request",
                                   'Function "%s" not implemented.' % fname,
                                   start_response)

    if fname == '':
        here = os.path.dirname(__file__)
        helpFile = os.path.join(here, 'help.html')
        with open(helpFile, 'r') as helpHandle:
            iterObj = helpHandle.read()
            status = '200 OK'
            return send_html_response(status, iterObj, start_response)

    elif fname == 'application.wadl':
        here = os.path.dirname(__file__)
        appWadl = os.path.join(here, 'application.wadl')
        with open(appWadl, 'r') \
                as appFile:
            tomorrow = datetime.date.today() + datetime.timedelta(days=1)
            iterObj = appFile.read() % (baseURL, tomorrow)
            status = '200 OK'
            return send_xml_response(status, iterObj, start_response)

    elif fname == 'query':
        makeQuery = globals()['makeQuery%s' % environ['REQUEST_METHOD']]
        try:
            iterObj = makeQuery(form)

            iterObj = applyFormat(iterObj, outForm)

            status = '200 OK'
            if outForm == 'xml':
                return send_xml_response(status, iterObj, start_response)
            elif outForm == 'json':
                return send_json_response(status, iterObj, start_response)
            else:
                return send_plain_response(status, iterObj, start_response)

        except WIError as w:
            return send_error_response(w.status, w.body, start_response)

    elif fname == 'endpoints':
        result = routes.endpoints()
        return send_plain_response('200 OK', result, start_response)

    elif fname == 'localconfig':
        result = routes.localConfig()
        if outForm == 'xml':
            return send_xml_response('200 OK', result,
                                     start_response)

    elif fname == 'globalconfig':
        result = routes.globalConfig()
        if outForm == 'fdsn':
            return send_json_response('200 OK', result,
                                      start_response)

        # Only FDSN format is supported for the time being
        text = 'Only format=FDSN is supported'
        return send_error_response("400 Bad Request", text, start_response)

    elif fname == 'virtualnets':
        result = routes.virtualNets()
        return send_json_response('200 OK', result,
                                  start_response)

    elif fname == 'version':
        text = "1.2.1-b2"
        return send_plain_response('200 OK', text, start_response)

    elif fname == 'info':
        config = configparser.RawConfigParser()
        here = os.path.dirname(__file__)
        config.read(os.path.join(here, 'routing.cfg'))

        text = config.get('Service', 'info')
        return send_plain_response('200 OK', text, start_response)

    raise Exception('This point should have never been reached!')
Exemple #3
0
def application(environ, start_response):
    """Main WSGI handler that processes client requests and calls
    the proper functions.

    """

    global routes
    fname = environ['PATH_INFO']

    # Among others, this will filter wrong function names,
    # but also the favicon.ico request, for instance.
    if fname is None:
        raise WIClientError('Method name not recognized!')
        # return send_html_response(status, 'Error! ' + status, start_response)

    if len(environ['QUERY_STRING']) > 1000:
        return send_error_response("414 Request URI too large",
                                   "maximum URI length is 1000 characters",
                                   start_response)

    try:
        outForm = 'xml'

        if environ['REQUEST_METHOD'] == 'GET':
            form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
            if 'format' in form:
                outForm = form['format'].value.lower()
        elif environ['REQUEST_METHOD'] == 'POST':
            form = ''
            try:
                length = int(environ.get('CONTENT_LENGTH', '0'))
            except ValueError:
                length = 0

            # If there is a body to read
            if length != 0:
                form = environ['wsgi.input'].read(length)
            else:
                form = environ['wsgi.input'].read()

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

                if '=' not in line:
                    break
                k, v = line.split('=')
                if k.strip() == 'format':
                    outForm = v.strip()

        else:
            raise Exception

    except ValueError as e:
        if str(e) == "Maximum content length exceeded":
            # Add some user-friendliness (this message triggers an alert
            # box on the client)
            return send_error_response("400 Bad Request",
                                       "maximum request size exceeded",
                                       start_response)

        return send_error_response("400 Bad Request", str(e), start_response)

    # Check whether the function called is implemented
    implementedFunctions = ['query', 'application.wadl', 'localconfig',
                            'version', 'info']

    config = configparser.RawConfigParser()
    here = os.path.dirname(__file__)
    config.read(os.path.join(here, 'routing.cfg'))
    verbo = config.get('Service', 'verbosity')
    # Warning is the default value
    verboNum = getattr(logging, verbo.upper(), 30)
    logging.info('Verbosity configured with %s' % verboNum)
    logging.basicConfig(level=verboNum)

    if routes is None:
        # Add routing cache here, to be accessible to all modules
        routesFile = os.path.join(here, 'data', 'routing.xml')
        masterFile = os.path.join(here, 'data', 'masterTable.xml')
        configFile = os.path.join(here, 'routing.cfg')
        routes = RoutingCache(routesFile, masterFile, configFile)

    fname = environ['PATH_INFO'].split('/')[-1]
    if fname not in implementedFunctions:
        return send_error_response("400 Bad Request",
                                   'Function "%s" not implemented.' % fname,
                                   start_response)

    if fname == 'application.wadl':
        iterObj = ''
        here = os.path.dirname(__file__)
        appWadl = os.path.join(here, 'application.wadl')
        with open(appWadl, 'r') \
                as appFile:
            iterObj = appFile.read()
            status = '200 OK'
            return send_xml_response(status, iterObj, start_response)

    elif fname == 'query':
        makeQuery = globals()['makeQuery%s' % environ['REQUEST_METHOD']]
        try:
            iterObj = makeQuery(form)

            # print iterObj
            iterObj = applyFormat(iterObj, outForm)

            status = '200 OK'
            if outForm == 'xml':
                return send_xml_response(status, iterObj, start_response)
            else:
                return send_plain_response(status, iterObj, start_response)

        except WIError as w:
            return send_error_response(w.status, w.body, start_response)

    elif fname == 'localconfig':
        return send_xml_response('200 OK', routes.localConfig(),
                                 start_response)

    elif fname == 'version':
        text = "1.0.3"
        return send_plain_response('200 OK', text, start_response)

    elif fname == 'info':
        config = configparser.RawConfigParser()
        here = os.path.dirname(__file__)
        config.read(os.path.join(here, 'routing.cfg'))

        text = config.get('Service', 'info')
        return send_plain_response('200 OK', text, start_response)

    raise Exception('This point should have never been reached!')
Exemple #4
0
 def setUp(cls):
     "Setting up test"
     if hasattr(cls, 'rc'):
         return
     cls.rc = RoutingCache('../data/routing.xml.sample')