<commit_before># This file is part of the MapProxy project.
# Copyright (C) 2013 Omniscale GmbH & Co. KG <http://omniscale.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import uuid
import json
import textwrap

from mp_renderd.task import Task
from mapproxy.request.base import Request as _Request
from mapproxy.response import Response

import logging
log = logging.getLogger(__name__)


try:
    from cherrypy.wsgiserver import CherryPyWSGIServer; CherryPyWSGIServer
except ImportError:
    from mp_renderd.ext.wsgiserver import CherryPyWSGIServer

__all__ = ['CherryPyWSGIServer', 'Request']

class Request(_Request):
    _body = None

    def body(self):
        if self._body is None:
            body_length = int(self.environ.get('CONTENT_LENGTH', 0))
            self._body = self.environ['wsgi.input'].read(body_length)
        return self._body

class RenderdApp(object):
    def __init__(self, broker):
        self.broker = broker

    def __call__(self, environ, start_response):
        req = Request(environ)
        try:
            if req.path == '/':
                resp = self.do_request(req)
            elif req.path == '/status':
                resp = self.do_status(req)
            else:
                resp = Response(json.dumps({'status': 'error', 'error_message': 'endpoint not found'}),
                    content_type='application/json', status=404)
        except Exception, ex:
            resp = Response(json.dumps({'status': 'error', 'error_message': 'internal error: %s' % ex.args[0]}),
                content_type='application/json', status=500)

        return resp(environ, start_response)

    def do_request(self, req):
        req = json.loads(req.body())
        log.info('got request: %s', req)

        req_id = req.get('id')
        if not req_id:
            req_id = uuid.uuid4().hex
        resp = self.broker.dispatch(Task(req_id, req, priority=req.get('priority', 10)))
        log.info('got resp: %s', resp)
        return Response(json.dumps(resp.doc), content_type='application/json')

    def do_status(self, req):
        body = """\
        running: %d
        waiting: %d
        worker: %d
        """ % (
            self.broker.render_queue.running,
            self.broker.render_queue.waiting,
            self.broker.worker.pool_size,
        )
        body = textwrap.dedent(body)

        return Response(body, content_type='text/plain')
<commit_msg>change status url to _status/<commit_after># This file is part of the MapProxy project.
# Copyright (C) 2013 Omniscale GmbH & Co. KG <http://omniscale.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import uuid
import json
import textwrap

from mp_renderd.task import Task
from mapproxy.request.base import Request as _Request
from mapproxy.response import Response

import logging
log = logging.getLogger(__name__)


try:
    from cherrypy.wsgiserver import CherryPyWSGIServer; CherryPyWSGIServer
except ImportError:
    from mp_renderd.ext.wsgiserver import CherryPyWSGIServer

__all__ = ['CherryPyWSGIServer', 'RenderdApp']

class Request(_Request):
    _body = None

    def body(self):
        if self._body is None:
            body_length = int(self.environ.get('CONTENT_LENGTH', 0))
            self._body = self.environ['wsgi.input'].read(body_length)
        return self._body

class RenderdApp(object):
    def __init__(self, broker):
        self.broker = broker

    def __call__(self, environ, start_response):
        req = Request(environ)
        try:
            if req.path == '/':
                resp = self.do_request(req)
            elif req.path == '/_status':
                resp = self.do_status(req)
            else:
                resp = Response(json.dumps({'status': 'error', 'error_message': 'endpoint not found'}),
                    content_type='application/json', status=404)
        except Exception, ex:
            resp = Response(json.dumps({'status': 'error', 'error_message': 'internal error: %s' % ex.args[0]}),
                content_type='application/json', status=500)

        return resp(environ, start_response)

    def do_request(self, req):
        req = json.loads(req.body())
        log.info('got request: %s', req)

        req_id = req.get('id')
        if not req_id:
            req_id = uuid.uuid4().hex
        resp = self.broker.dispatch(Task(req_id, req, priority=req.get('priority', 10)))
        log.info('got resp: %s', resp)
        return Response(json.dumps(resp.doc), content_type='application/json')

    def do_status(self, req):
        body = """\
        running: %d
        waiting: %d
        worker: %d
        """ % (
            self.broker.render_queue.running,
            self.broker.render_queue.waiting,
            self.broker.worker.pool_size,
        )
        body = textwrap.dedent(body)

        return Response(body, content_type='text/plain')

{'fraction_non_alphanumeric': 0.10889570552147239, 'fraction_numerical': 0.006475800954328562, 'mean_word_length': 2.7192648922686944, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3555198', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1701, 'n_words': 496}