Exemple #1
0
    def view_solver2_submit(self, request):
        def go():
            string = request.json_body['string'].encode('utf-8')
            nl = int(request.json_body['nl'])
            nu = int(request.json_body['nu'])
            return self.process(request, string, nl, nu)

        return ajax_error_catch(go)
    def editor_fancy_save_generic(self, request, spec):
        widget_name = self.get_widget_name(request, spec)
        string = self.get_text_from_request2(request)
        def go():
            l = self.get_library(request)
            spec.write(l, widget_name, string)
            return {'ok': True}

        return ajax_error_catch(go)
Exemple #3
0
    def view_solver2_submit(self, e):
        def go():
            state = e.request.json_body['ui_state']
            area_F = state['area_F'].encode('utf-8')
            area_R = state['area_R'].encode('utf-8')
            is_ftor = state['ftor_checkbox']
            is_rtof = state['rtof_checkbox']
            if is_ftor:
                pass
            elif is_rtof:
                pass
            else:
                msg = 'Cannot establish query type. '
                raise_desc(DPInternalError, msg, state=state)

            do_approximations = state['do_approximations']
            nl = state['nl']
            nu = state['nu']

            if is_ftor:
                key = dict(type=QUERY_TYPE_FTOR,
                           string=area_F,
                           do_approximations=do_approximations,
                           nu=nu,
                           nl=nl)

                data, res = self.process_ftor(e, area_F, do_approximations, nl,
                                              nu)
            elif is_rtof:
                key = dict(type=QUERY_TYPE_RTOF,
                           string=area_R,
                           do_approximations=do_approximations,
                           nu=nu,
                           nl=nl)

                data, res = self.process_rtof(e, area_R, do_approximations, nl,
                                              nu)
            else:
                raise_desc(DPInternalError, 'Inconsistent state', state=state)

            key_stable = sorted(tuple(key.items()))
            h = base64.b64encode(json.dumps(key_stable), altchars=altchars)

            data['state'] = state
            data['key'] = key

            self.solutions[h] = data

            res['output_image'] = 'display.png?hash=%s' % h
            res['ok'] = True
            return res

        quiet = (DPSyntaxError, DPSemanticError, NeedsApprox)
        return ajax_error_catch(go, quiet=quiet, environment=e)
Exemple #4
0
    def save(self, e):
        string = get_text_from_request2(e.request)

        def go():
            db_view = e.app.hi.db_view
            library = db_view.repos[e.repo_name].shelves[
                e.shelf_name].libraries[e.library_name]
            things = library.things.child(e.spec_name)
            things[e.thing_name] = string
            return {'ok': True, 'saved_string': string}

        return ajax_error_catch(go, environment=e)
Exemple #5
0
    def ajax_parse(self, e):
        string = get_text_from_request2(e.request)
        text = e.request.json_body['text'].encode('utf8')
        req = {'text': e.request.json_body['text']}
        text_hash = get_sha1(text)
        key = (e.library_name, e.spec.url_part, e.thing_name, text_hash)

        cache = self.last_processed2

        make_relative = lambda s: self.make_relative(e.request, s)
        library = library_from_env(e)

        def go():
            with timeit_wall('process_parse_request'):
                res = process_parse_request(library, string, e.spec, key,
                                            cache, make_relative)
            res['request'] = req
            return res

        return ajax_error_catch(go, environment=e)
    def ajax_parse_generic(self, request, spec):
        widget_name = self.get_widget_name(request, spec)
        string = self.get_text_from_request2(request)
        req = {'text': request.json_body['text']}
        library_name = self.get_current_library_name(request)
        key = (library_name, spec, widget_name)

        parse_expr = spec.parse_expr
        parse_refine = spec.parse_refine
        parse_eval = spec.parse_eval
        
        library = self.get_library(request)

        context = library._generate_context_with_hooks()

        def go():
            try:
                # XXX: inefficient; we parse twice
                parse_tree = parse_wrap(parse_expr, string)[0]
            except DPSemanticError as e:
                msg = 'I only expected a DPSyntaxError'
                raise_wrapped(DPInternalError, e, msg, exc=sys.exc_info())
            except DPSyntaxError as e:
                # This is the case in which we could not even parse
                from mcdp_report.html import mark_unparsable
                string2, expr, _commented_lines = mark_unparsable(string, parse_expr)
                
                res = format_exception_for_ajax_response(e, quiet=(DPSyntaxError,))
                if expr is not None:
                    try:
                        html = ast_to_html(string2,    ignore_line=None,
                                    add_line_gutter=False, encapsulate_in_precode=False, 
                                    parse_expr=parse_expr,   postprocess=None)
                
                        res['highlight'] = html
                    except DPSyntaxError:
                        assert False, string2
                else:
                    res['highlight'] = html_mark_syntax_error(string, e)
                 
                res['request'] = req
                return res
            
            try:
                class Tmp:
                    parse_tree_interpreted = None
                     
                def postprocess(block):
                    Tmp.parse_tree_interpreted = parse_refine(block, context)
                    return Tmp.parse_tree_interpreted
                
                try:
                    try:
                        highlight = ast_to_html(string,
                                                parse_expr=parse_expr,
                                                add_line_gutter=False,
                                                encapsulate_in_precode=False,
                                                postprocess=postprocess)
                    except DPSemanticError:
                        # Do it again without postprocess
                        highlight = ast_to_html(string,
                                                parse_expr=parse_expr,
                                                add_line_gutter=False,
                                                encapsulate_in_precode=False,
                                                postprocess=None)
                        raise
                    
                    thing = parse_eval(Tmp.parse_tree_interpreted, context)
                except (DPSemanticError, DPInternalError) as e:
                    highlight_marked = html_mark(highlight, e.where, "semantic_error")
                    self.last_processed2[key] = None  # XXX
                    res = format_exception_for_ajax_response(e, quiet=(DPSemanticError, DPInternalError))
                    res['highlight'] = highlight_marked
                    res['request'] = req
                    return res
                
                self.last_processed2[key] = thing
            except:
                self.last_processed2[key] = None  # XXX
                raise
            
            warnings = []
            for w in context.warnings:
                # w.msg
                warning = w.format_user()
                if w.where is not None:
                    highlight = html_mark(highlight, w.where, "language_warning")
                    
#                     wheres = format_where(w.where, context_before=0, mark=None, arrow=False, 
#                                           use_unicode=True, no_mark_arrow_if_longer_than=3)
#                     warning += '\n\n' + indent(wheres, '   ')
                warnings.append(warning.strip())
                    
            sep = '-' * 80
            language_warnings = ("\n\n" + sep + "\n\n").join(warnings)
            language_warnings_html = "\n".join(['<div class="language_warning">%s</div>' % w
                                      for w in warnings])
            
            return {'ok': True, 
                    'highlight': highlight,
                    'language_warnings': language_warnings, 
                    'language_warnings_html': language_warnings_html,
                    'request': req}

        return ajax_error_catch(go)