示例#1
0
def query(request):
    global definitions
    from mathics.core.parser import MultiLineFeeder

    input = request.POST.get("query", "")
    if settings.DEBUG and not input:
        input = request.GET.get("query", "")

    if settings.LOG_QUERIES:
        query_log = Query(
            query=input,
            error=True,
            browser=request.META.get("HTTP_USER_AGENT", ""),
            remote_user=request.META.get("REMOTE_USER", ""),
            remote_addr=request.META.get("REMOTE_ADDR", ""),
            remote_host=request.META.get("REMOTE_HOST", ""),
            meta=str(request.META),
            log="",
        )
        query_log.save()

    evaluation = get_session_evaluation(request.session)
    feeder = MultiLineFeeder(input, "<notebook>")
    results = []
    try:
        while not feeder.empty():
            expr = evaluation.parse_feeder(feeder)
            if expr is None:
                results.append(Result(evaluation.out, None,
                                      None))  # syntax errors
                evaluation.out = []
                continue
            result = evaluation.evaluate(expr, timeout=settings.TIMEOUT)
            if result is not None:
                results.append(result)
    except SystemExit as e:
        results = []
        result = None
        definitions = Definitions(add_builtin=True,
                                  extension_modules=default_pymathics_modules)
        evaluation.definitions = definitions
    except Exception as exc:
        if settings.DEBUG and settings.DISPLAY_EXCEPTIONS:
            info = traceback.format_exception(*sys.exc_info())
            info = "\n".join(info)
            msg = "Exception raised: %s\n\n%s" % (exc, info)
            results.append(
                Result([Message("System", "exception", msg)], None, None))
        else:
            raise
    result = {
        "results": [result.get_data() for result in results],
    }
    if settings.LOG_QUERIES:
        query_log.timeout = evaluation.timeout
        query_log.result = str(result)  # evaluation.results
        query_log.error = False
        query_log.save()

    return JsonResponse(result)
示例#2
0
def query(request):
    from mathics.core.parser import MultiLineFeeder

    input = request.POST.get('query', '')
    if settings.DEBUG and not input:
        input = request.GET.get('query', '')

    if settings.LOG_QUERIES:
        query_log = Query(
            query=input,
            error=True,
            browser=request.META.get('HTTP_USER_AGENT', ''),
            remote_user=request.META.get('REMOTE_USER', ''),
            remote_addr=request.META.get('REMOTE_ADDR', ''),
            remote_host=request.META.get('REMOTE_HOST', ''),
            meta=six.text_type(request.META),
            log='',
        )
        query_log.save()

    user_definitions = request.session.get('definitions')
    definitions.set_user_definitions(user_definitions)
    evaluation = Evaluation(definitions, format='xml', output=WebOutput())
    feeder = MultiLineFeeder(input, '<notebook>')
    results = []
    try:
        while not feeder.empty():
            expr = evaluation.parse_feeder(feeder)
            if expr is None:
                results.append(Result(evaluation.out, None,
                                      None))  # syntax errors
                evaluation.out = []
                continue
            result = evaluation.evaluate(expr, timeout=settings.TIMEOUT)
            if result is not None:
                results.append(result)
    except Exception as exc:
        if settings.DEBUG and settings.DISPLAY_EXCEPTIONS:
            info = traceback.format_exception(*sys.exc_info())
            info = '\n'.join(info)
            msg = 'Exception raised: %s\n\n%s' % (exc, info)
            results.append(
                Result([Message('System', 'exception', msg)], None, None))
        else:
            raise
    result = {
        'results': [result.get_data() for result in results],
    }
    request.session['definitions'] = definitions.get_user_definitions()

    if settings.LOG_QUERIES:
        query_log.timeout = evaluation.timeout
        query_log.result = six.text_type(result)  # evaluation.results
        query_log.error = False
        query_log.save()

    return JsonResponse(result)
示例#3
0
    def do_execute(self,
                   code,
                   silent,
                   store_history=True,
                   user_expressions=None,
                   allow_stdin=False):

        if not silent:
            from mathics.core.parser import MultiLineFeeder

            definitions = Definitions(add_builtin=True)
            evaluation = Evaluation(definitions, format='xml')

            feeder = MultiLineFeeder(code, '<notebook>')
            results = []
            try:
                while not feeder.empty():
                    expr = evaluation.parse_feeder(feeder)
                    if expr is None:
                        results.append(Result(evaluation.out, None, None))
                        evaluation.out = []
                        continue
                    result = evaluation.evaluate(expr, timeout=20)
                    if result is not None:
                        results.append(result)
            except Exception as exc:
                raise

            for result in results:
                result_data = result.get_data()

                result_html = self.preprocess_output(result_data['result'])

                display_data = {
                    'data': {
                        'text/html': result_html
                    },
                    'metadata': {},
                }

                self.send_response(self.iopub_socket, 'display_data',
                                   display_data)

        return {
            'status': 'ok',
            'execution_count': self.execution_count,
            'payload': [],
            'user_expressions': {},
        }
示例#4
0
def query(request):
    input = request.POST.get('query', '')
    if settings.DEBUG and not input:
        input = request.GET.get('query', '')

    if settings.LOG_QUERIES:
        query_log = Query(query=input, error=True,
                          browser=request.META.get('HTTP_USER_AGENT', ''),
                          remote_user=request.META.get('REMOTE_USER', ''),
                          remote_addr=request.META.get('REMOTE_ADDR', ''),
                          remote_host=request.META.get('REMOTE_HOST', ''),
                          meta=six.text_type(request.META),
                          log='',
                          )
        query_log.save()

    user_definitions = request.session.get('definitions')
    definitions.set_user_definitions(user_definitions)
    evaluation = Evaluation(definitions, format='xml')
    try:
        results = evaluation.parse_evaluate(input, timeout=settings.TIMEOUT)
    except Exception as exc:
        if settings.DEBUG and settings.DISPLAY_EXCEPTIONS:
            info = traceback.format_exception(*sys.exc_info())
            info = '\n'.join(info)
            msg = 'Exception raised: %s\n\n%s' % (exc, info)
            results = [Result([Message('System', 'exception', msg)], None, None)]
        else:
            raise
    result = {
        'results': [result.get_data() for result in results],
    }
    request.session['definitions'] = definitions.get_user_definitions()

    if settings.LOG_QUERIES:
        query_log.timeout = evaluation.timeout
        query_log.result = six.text_type(result)  # evaluation.results
        query_log.error = False
        query_log.save()

    return JsonResponse(result)
示例#5
0
文件: views.py 项目: nonohry/Mathics
def query(request):
    input = request.POST.get('query', '')
    if settings.DEBUG and not input:
        input = request.GET.get('query', '')

    if settings.LOG_QUERIES:
        query_log = Query(
            query=input,
            error=True,
            browser=request.META.get('HTTP_USER_AGENT', ''),
            remote_user=request.META.get('REMOTE_USER', ''),
            remote_addr=request.META.get('REMOTE_ADDR', ''),
            remote_host=request.META.get('REMOTE_HOST', ''),
            meta=unicode(request.META),
            log='',
        )
        query_log.save()

    user_definitions = request.session.get('definitions')
    definitions.set_user_definitions(user_definitions)
    try:
        evaluation = Evaluation(input,
                                definitions,
                                timeout=settings.TIMEOUT,
                                format='xml')
    except Exception, exc:
        if settings.DEBUG and settings.DISPLAY_EXCEPTIONS:
            evaluation = Evaluation()
            info = traceback.format_exception(*sys.exc_info())
            info = '\n'.join(info)
            msg = 'Exception raised: %s\n\n%s' % (exc, info)
            evaluation.results = [
                Result([Message('System', 'exception', msg)], None, None)
            ]
        else:
            raise