def go(): image_source = image_source_from_env(e) library = library_from_env(e) with timeit_wall('graph_generic', 1.0): key = (e.library_name, e.spec.url_part, e.thing_name, text_hash) if not key in self.last_processed2: logger.error('Cannot find key %s' % str(key)) logger.error('keys: %s' % list(self.last_processed2)) context = e.library._generate_context_with_hooks() thing = e.spec.load(e.library, e.thing_name, context=context) else: thing = self.last_processed2[key] if thing is None: return response_image(e.request, 'Could not parse.') with timeit_wall('graph_generic - get_png_data', 1.0): data = e.spec.get_png_data(image_source=image_source, name=e.thing_name, thing=thing, data_format=data_format, library=library) mime = get_mime_for_format(data_format) return response_data(e.request, data, mime)
def go(): library = library_from_env(e) image_source = image_source_from_env(e) ndp = library.load_ndp(e.thing_name) mf = MakeFiguresNDP(ndp=ndp, image_source=image_source, yourname=e.thing_name) data = mf.get_figure(which, data_format) mime = get_mime_for_format(data_format) return response_data(request=e.request, data=data, content_type=mime)
def view_model_ndp_repr(self, e): res = {} try: library = library_from_env(e) ndp = library.load_ndp(e.thing_name) ndp_string = ndp.__repr__() ndp_string = ndp_string.decode("utf8", 'ignore') res['content'] = ndp_string except (DPSyntaxError, DPSemanticError, DPNotImplementedError) as exc: self.note_exception(exc, request=e.request, context=e.context) s = str(exc) res['error'] = s.decode('utf8') return res
def get_ndp_dp(self, e, repo_name, shelf_name, library_name, model_name): # # self._xxx_session = session # return self._get_ndp_dp(repo_name, shelf_name, library_name, model_name) # # @memoize_simple # def _get_ndp_dp(self, repo_name, shelf_name, library_name, model_name): # library = self._xxx_session.libraries[library_name]['library'] # mcdp_library = library_from_env(e) ndp = e.spec.load(mcdp_library, e.thing_name) # ndp = library.load_ndp(model_name) dp = ndp.get_dp() return ndp, dp
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 process_ftor(self, e, string, do_approximations, nl, nu): mcdp_library = library_from_env(e) parsed = mcdp_library.parse_constant(string) space = parsed.unit value = parsed.value ndp, dp = self.get_ndp_dp_e(e) F = dp.get_fun_space() UR = UpperSets(dp.get_res_space()) try: f = parsed.cast_value(F) except NotLeq: msg = 'Space %s cannot be converted to %s' % (parsed.unit, F) raise DPSemanticError(msg) logger.info('query rtof: %s ...' % F.format(f)) tracer = Tracer(logger=logger) intervals = False max_steps = 10000 res = {} if do_approximations: dpl, dpu = get_dp_bounds(dp, nl, nu) result_l, _trace = solve_meat_solve_ftor(tracer, ndp, dpl, f, intervals, max_steps, False) result_u, trace = solve_meat_solve_ftor(tracer, ndp, dpu, f, intervals, max_steps, False) data = dict(result_l=result_l, result_u=result_u, dpl=dpl, dpu=dpu) res['output_result'] = 'Lower: %s\nUpper: %s' % ( UR.format(result_l), UR.format(result_u)) else: try: result, trace = solve_meat_solve_ftor(tracer, ndp, dp, f, intervals, max_steps, False) except NotSolvableNeedsApprox: msg = 'The design problem has infinite antichains. Please use approximations.' raise NeedsApprox(msg) data = dict(result=result, dp=dp) res['output_result'] = UR.format(result) e = cgi.escape res['output_space'] = e(space.__repr__() + '\n' + str(type(space))) res['output_raw'] = e(value.__repr__() + '\n' + str(type(value))) res['output_formatted'] = e(space.format(value)) res['output_trace'] = str(trace) return data, res
def generate_view_syntax(e, make_relative): expr = e.spec.parse_expr parse_refine = e.spec.parse_refine source_code = e.thing context = Context() class Tmp: refined = None def postprocess(block): if parse_refine is None: return block try: Tmp.refined = parse_refine(block, context) return Tmp.refined except DPSemanticError: return block try: highlight = ast_to_html(source_code, add_line_gutter=False, parse_expr=expr, postprocess=postprocess) def get_link_library(libname): try: rname, sname = e.session.get_repo_shelf_for_libname(libname) except NoSuchLibrary: raise url0 = "/repos/%s/shelves/%s/libraries/%s/" % (rname, sname, libname) return make_relative(url0) def get_link(specname, libname, thingname): # find library. Returns a string or raises error try: rname, sname = e.session.get_repo_shelf_for_libname(libname) except NoSuchLibrary: msg = 'No such library %r' % libname logger.debug(msg) raise # return None things = e.db_view.repos[rname].shelves[sname].libraries[ libname].things.child(specname) if thingname in things: # check if the thing exists res = get_link_library( libname) + '%s/%s/views/syntax/' % (specname, thingname) # logger.debug(' link for %s = %s' % (thingname, res)) return res else: msg = 'No such thing %r' % thingname logger.debug(msg) raise NoSuchLibrary(msg) highlight = add_html_links(highlight, e.library_name, get_link, get_link_library) parses = True error = '' except (DPSyntaxError, DPNotImplementedError) as exc: highlight = '<pre class="source_code_with_error">%s</pre>' % source_code error = exc.__str__() parses = False if parses: mcdp_library = library_from_env(e) image_source = image_source_from_env(e) try: thing = e.spec.load(mcdp_library, e.thing_name, context=context) svg_data = get_svg_for_visualization(e, image_source, e.library_name, e.spec, e.thing_name, thing, Tmp.refined, make_relative, library=mcdp_library) except (DPSemanticError, DPNotImplementedError) as exc: logger.error(exc) from mcdp_web.editor_fancy.app_editor_fancy_generic import html_mark if exc.where.string != source_code: msg = 'This exception refers to another file.' msg += '\n source_code: %r' % source_code msg += '\n exception.where.string: %r' % exc.where.string msg += '\n' + indent(traceback.format_exc(exc), 'exc > ') raise DPInternalError(msg) try: highlight = html_mark(highlight, exc.where, "semantic_error") except NoLocationFound as e: msg = 'While trying to annotate the exception:' msg += '\n' + indent(exc, 'exc > ') raise_wrapped(NoLocationFound, e, msg) error = exc.error + "\n" + format_where(exc.where) svg_data = None else: svg_data = None check_isinstance(highlight, str) res = { 'source_code': source_code, 'error': unicode(error, 'utf-8'), 'highlight': unicode(highlight, 'utf-8'), # 'realpath': realpath, 'current_view': 'syntax', 'explanation1_html': None, 'explanation2_html': None, 'svg_data': unicode(svg_data, 'utf-8') if svg_data is not None else None, 'parses': parses, # whether it parses } return res