예제 #1
0
파일: corner_case.py 프로젝트: rusi/mcdp
def check_mark_suggestions():
    s = """#comment
mcdp {
 provides a [Nat]
}"""
    parse_expr = Syntax.ndpt_dp_rvalue
    x = parse_wrap(Syntax.ndpt_dp_rvalue, s)[0]
    context = Context()
    xr = parse_ndp_refine(x, context)
    suggestions = get_suggestions(xr)

    # make sure we can apply them
    _s2 = apply_suggestions(s, suggestions)

    def postprocess(block):
        x = parse_ndp_refine(block, context)
        return x

    html = ast_to_html(s,
                       parse_expr=parse_expr,
                       add_line_gutter=False,
                       encapsulate_in_precode=False,
                       postprocess=postprocess)

    for where, replacement in suggestions:  # @UnusedVariable
        #print('suggestion: %r' % replacement)
        html = html_mark(html, where, "suggestion")

    assert 'suggestion' in html
예제 #2
0
파일: corner_case.py 프로젝트: rusi/mcdp
def mark_errors():
    s = """#comment
mcdp {
#@ syntax error
}"""
    parse_expr = Syntax.ndpt_dp_rvalue
    x = parse_wrap(Syntax.ndpt_dp_rvalue, s)[0]
    context = Context()
    xr = parse_ndp_refine(x, context)
    suggestions = get_suggestions(xr)

    # make sure we can apply them
    _s2 = apply_suggestions(s, suggestions)

    def postprocess(block):
        x = parse_ndp_refine(block, context)
        return x

    html = ast_to_html(s,
                       parse_expr=parse_expr,
                       add_line_gutter=False,
                       encapsulate_in_precode=False,
                       postprocess=postprocess)

    text = project_html(html)

    s2 = """#comment
mcdp {
 syntax error
}"""
    assert_equal(text, s2)

    assert not '#@' in text
예제 #3
0
def get_ast_as_pdf(s, parse_expr):
    s = s.replace('\t', '    ')
    contents = ast_to_html(s, complete_document=False, extra_css=None,
                       ignore_line=None, parse_expr=parse_expr,
                       add_line_gutter=False, add_css=False)
    html = get_minimal_document(contents)
    d = mkdtemp()
    
    f_html = os.path.join(d, 'file.html')
    with open(f_html, 'w') as f:
        f.write(html)
        
    try:
        f_pdf = os.path.join(d, 'file.pdf')
        cmd= ['wkhtmltopdf','-s','A1',f_html,f_pdf]
        system_cmd_result(
                d, cmd, 
                display_stdout=False,
                display_stderr=False,
                raise_on_error=True)

        with open(f_pdf) as f:
            data = f.read()
        
        data = crop_pdf(data, margins=0)

        return data
    except CmdException as e:
        raise e
예제 #4
0
파일: plot.py 프로젝트: kannode/mcdp
def syntax_frag(data):
    from mcdp_report.html import ast_to_html
    s = data['s']

    res = ast_to_html(s, parse_expr=Syntax.ndpt_dp_rvalue)
    res1 = ('html', 'syntax_frag', res)
    return [res1]
예제 #5
0
파일: test0.py 프로젝트: rusi/mcdp
def check_syntax(filename, source, parse_expr=Syntax.ndpt_dp_rvalue):  # @UnusedVariable
    
    # skip generated files (hack)
    if filename and 'drone_unc2_' in filename:
        return
    
    # print filename
    if filename is not None:
        source = open(filename).read()
    try:
        html = ast_to_html(source,
                            parse_expr,
                           ignore_line=lambda _lineno: False,
                           add_line_gutter=False, encapsulate_in_precode=True, 
                           )
#         print html.__repr__()
        source2 = project_html(html)
#         print source
#         print source2
        # print indent(html, 'html |')
        assert_equal_string(s2_expected=source, s2=source2)
    except:
        logger.error('This happened to %r' %  filename)
        
        raise
예제 #6
0
def get_ast_as_pdf(s, parse_expr):
    s = s.replace('\t', '    ')
    contents = ast_to_html(s,
                       ignore_line=None, parse_expr=parse_expr,
                       add_line_gutter=False)
    html = get_minimal_document(contents)
    
    mcdp_tmp_dir = get_mcdp_tmp_dir()
    prefix = 'get_ast_as_pdf()'
    d = mkdtemp(dir=mcdp_tmp_dir, prefix=prefix)
    
    try:
        f_html = os.path.join(d, 'file.html')
        with open(f_html, 'w') as f:
            f.write(html)
            
        try:
            f_pdf = os.path.join(d, 'file.pdf')
            cmd= ['wkhtmltopdf','-s','A1',f_html,f_pdf]
            system_cmd_result(
                    d, cmd, 
                    display_stdout=False,
                    display_stderr=False,
                    raise_on_error=True)
    
            with open(f_pdf) as f:
                data = f.read()
            
            data = crop_pdf(data, margins=0)
    
            return data
        except CmdException as e:
            raise e
    finally:
        shutil.rmtree(d)
예제 #7
0
def ast_to_html_(s):
    from mcdp_report.html import ast_to_html

    parse_expr = Syntax.ndpt_dp_rvalue
    html = ast_to_html(s, complete_document=False, extra_css=None, ignore_line=None,
                add_line_gutter=False, encapsulate_in_precode=True, add_css=False,
                parse_expr=parse_expr, add_line_spans=False, postprocess=None)
    return html
예제 #8
0
def ast_to_html_(s):
    from mcdp_report.html import ast_to_html

    parse_expr = Syntax.ndpt_dp_rvalue
    html = ast_to_html(s,  ignore_line=None,
                add_line_gutter=False, encapsulate_in_precode=True,
                parse_expr=parse_expr, postprocess=None)
    return html
예제 #9
0
파일: plot.py 프로젝트: AndreaCensi/mcdp
def syntax_frag(data):
    from mcdp_report.html import ast_to_html
    s = data['s']
    
    res = ast_to_html(s,
                      parse_expr=Syntax.ndpt_dp_rvalue)
    res1 = ('html', 'syntax_frag', res)
    return [res1]
예제 #10
0
파일: plot.py 프로젝트: AndreaCensi/mcdp
def syntax_pdf(data):
    """ Returns a PDF string """
    from mcdp_report.html import ast_to_html
    s = data['s']
    s = s.replace('\t', '    ')
    
    lines_to_hide = get_lines_to_hide(data['params'])
    def ignore_line(lineno):
        return lineno  in lines_to_hide
    contents = ast_to_html(s,  
                       ignore_line=ignore_line, parse_expr=Syntax.ndpt_dp_rvalue,
                       )
    html = get_minimal_document(contents)

    d = mkdtemp()
    
    f_html = os.path.join(d, 'file.html')
    with open(f_html, 'w') as f:
        f.write(html)
        
    try:
        f_pdf = os.path.join(d, 'file.pdf')
        cmd= ['wkhtmltopdf','-s','A1',f_html,f_pdf]
        system_cmd_result(
                d, cmd, 
                display_stdout=False,
                display_stderr=False,
                raise_on_error=True)
          
        f_pdf_crop = os.path.join(d, 'file_crop.pdf')
        cmd = ['pdfcrop', '--margins', '4', f_pdf, f_pdf_crop]
        system_cmd_result(
                d, cmd, 
                display_stdout=False,
                display_stderr=False,
                raise_on_error=True)
    
        with open(f_pdf_crop) as f:
            data = f.read()
    
        f_png = os.path.join(d, 'file.png')
        cmd = ['convert', '-density', '600', f_pdf_crop,
                '-background', 'white', '-alpha', 'remove',
                '-resize', '50%', f_png]
        system_cmd_result(
                d, cmd,
                display_stdout=False,
                display_stderr=False,
                raise_on_error=True)
    
        with open(f_png) as f:
            pngdata = f.read()
    
        return [('pdf', 'syntax_pdf', data), ('png', 'syntax_pdf', pngdata)]
    except CmdException as e:
        raise e
예제 #11
0
    def _generate_view_syntax(self, request, name, ext, expr, parse_refine=None):
        filename = "%s.%s" % (name, ext)
        l = self.get_library(request)
        f = l._get_file_data(filename)
        source_code = f["data"]
        realpath = f["realpath"]

        md1 = "%s.%s" % (name, MCDPLibrary.ext_explanation1)
        if l.file_exists(md1):
            fd = l._get_file_data(md1)
            html1 = self.render_markdown(fd["data"])
        else:
            html1 = None

        md2 = "%s.%s" % (name, MCDPLibrary.ext_explanation2)
        if l.file_exists(md2):
            fd = l._get_file_data(md2)
            html2 = self.render_markdown(fd["data"])
        else:
            html2 = None

        context = Context()

        def postprocess(block):
            if parse_refine is None:
                return block
            try:
                x = parse_refine(block, context)
                return x
            except DPSemanticError:
                return block

        try:
            highlight = ast_to_html(
                source_code, complete_document=False, add_line_gutter=False, parse_expr=expr, postprocess=postprocess
            )

            highlight = self.add_html_links(request, highlight)
            error = ""
        except DPSyntaxError as e:
            highlight = '<pre class="source_code_with_error">%s</pre>' % source_code
            error = e.__str__()

        return {
            "source_code": source_code,
            "error": unicode(error, "utf-8"),
            "highlight": highlight,
            "realpath": realpath,
            "navigation": self.get_navigation_links(request),
            "current_view": "syntax",
            "explanation1_html": html1,
            "explanation2_html": html2,
        }
예제 #12
0
def check_comments06():
    s0 = 'mcdp {\n  }'
    
    for i in range(3):
        for j in range(3):
            s = '\n' * i + s0 + '\n' * j
            expr = Syntax.ndpt_dp_rvalue
            _html = ast_to_html(s, ignore_line=None,
                               add_line_gutter=False, 
                               encapsulate_in_precode=True,
                               parse_expr=expr, 
                               postprocess=None)
예제 #13
0
파일: test0.py 프로젝트: AndreaCensi/mcdp
def check_syntax(filename, source):  # @UnusedVariable
    # print filename
    source = open(filename).read()
    try:
        _html = ast_to_html(source,
                            parse_expr=Syntax.ndpt_dp_rvalue,
                           complete_document=False, extra_css="",
                           ignore_line=lambda _lineno: False,
                           add_line_gutter=True, encapsulate_in_precode=True, 
                           add_css=False)
    except:
        logger.error('This happened to %r' %  filename)
        raise
예제 #14
0
def check_comments06():
    s0 = 'mcdp {\n  }'
    
    for i in range(3):
        for j in range(3):
            s = '\n' * i + s0 + '\n' * j
            expr = Syntax.ndpt_dp_rvalue
            _html = ast_to_html(s, complete_document=False, 
                                extra_css=None, ignore_line=None,
                               add_line_gutter=False, 
                               encapsulate_in_precode=True, add_css=False,
                               parse_expr=expr, add_line_spans=False, 
                               postprocess=None)
예제 #15
0
def syntax_test(contents):
    from mcdp_report.html import ast_to_html

    html = ast_to_html(contents, parse_expr=Syntax.ndpt_dp_rvalue)

    s = StringIO(html)
    import xml.etree.ElementTree as ETree
    try:
        _doc = ETree.parse(s)
    except ETree.ParseError:  # pragma: no cover
        print('---')
        print(html)
        print('---')
        raise
예제 #16
0
파일: plot.py 프로젝트: AndreaCensi/mcdp
def syntax_doc(data):
    from mcdp_report.html import ast_to_html
    s = data['s']
    lines_to_hide = get_lines_to_hide(data['params'])
    def ignore_line(lineno):
        return lineno in lines_to_hide

    body_contents = ast_to_html(s,
                      ignore_line=ignore_line, parse_expr=Syntax.ndpt_dp_rvalue)
    res = get_minimal_document(body_contents, add_markdown_css=True)
    
    # TODO: add minimal document
    res1 = ('html', 'syntax_doc', res)
    return [res1]
예제 #17
0
def syntax_test(contents):
    from mcdp_report.html import ast_to_html

    html = ast_to_html(contents, parse_expr=Syntax.ndpt_dp_rvalue, complete_document=False, add_css=False)

    s = StringIO(html)
    import xml.etree.ElementTree as ETree

    try:
        _doc = ETree.parse(s)
    except ETree.ParseError:  # pragma: no cover
        print("---")
        print(html)
        print("---")
        raise
예제 #18
0
def format_syntax_error2(parse_expr, string, e):
    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)

    return res
예제 #19
0
파일: plot.py 프로젝트: kannode/mcdp
def syntax_doc(data, mark_warnings=True):
    from mcdp_report.html import ast_to_html
    s = data['s']
    lines_to_hide = get_lines_to_hide(data['params'])

    def ignore_line(lineno):
        return lineno in lines_to_hide

    library = data['library']
    context = library._generate_context_with_hooks()

    class Tmp:
        string_nospaces_parse_tree_interpreted = None

    def postprocess(block):
        x = parse_ndp_refine(block, context)
        Tmp.string_nospaces_parse_tree_interpreted = x

        return x

    body = ast_to_html(s,
                       ignore_line=ignore_line,
                       parse_expr=Syntax.ndpt_dp_rvalue,
                       postprocess=postprocess)

    from mcdp_web.editor_fancy.app_editor_fancy_generic import html_mark

    if mark_warnings:
        for w in context.warnings:
            if w.where is not None:
                body = html_mark(body,
                                 w.where,
                                 "language_warning",
                                 tooltip=w.format_user())

    res = get_minimal_document(body, add_markdown_css=True)

    # TODO: add minimal document
    res1 = ('html', 'syntax_doc', res)
    return [res1]
예제 #20
0
def process_parse_request(library, string, spec, key, cache, make_relative):
    """ returns a dict to be used as the request,
        or raises an exception """
    from mcdp_report.html import sanitize

    parse_expr = spec.parse_expr
    parse_refine = spec.parse_refine
    parse_eval = spec.parse_eval

    context0 = library._generate_context_with_hooks()
    try:

        class Tmp:
            string_nospaces_parse_tree_interpreted = None

        def postprocess(block):
            try:
                x = parse_refine(block, context0)
            except DPSemanticError:
                x = block
            Tmp.string_nospaces_parse_tree_interpreted = x
            return x

        try:
            highlight = ast_to_html(string,
                                    parse_expr=parse_expr,
                                    add_line_gutter=False,
                                    encapsulate_in_precode=False,
                                    postprocess=postprocess)

            thing = parse_eval(Tmp.string_nospaces_parse_tree_interpreted,
                               context0)

            if isinstance(thing, NamedDP):
                x = Tmp.string_nospaces_parse_tree_interpreted
                generate_unconnected_warnings(thing, context0, x)

        except DPSyntaxError as e:
            return format_syntax_error2(parse_expr, string, e)
        except (DPSemanticError, DPNotImplementedError) as e:
            highlight_marked = html_mark(highlight, e.where, "semantic_error")

            cache[key] = None  # meaning we failed
            res = format_exception_for_ajax_response(e,
                                                     quiet=(DPSemanticError,
                                                            DPInternalError))
            res['highlight'] = highlight_marked
            return res
        except DPInternalError:
            raise

        cache[key] = thing
    except:
        cache[key] = None  # XXX
        raise

    if Tmp.string_nospaces_parse_tree_interpreted:
        xri = Tmp.string_nospaces_parse_tree_interpreted
        highlight, string_with_suggestions = \
            get_suggestions_for_string(highlight, string, xri)
    else:
        string_with_suggestions = None

    warnings = []
    for w in context0.warnings:
        if w.where is not None:
            tooltip = w.msg
            highlight = html_mark(highlight,
                                  w.where,
                                  "language_warning",
                                  tooltip=tooltip)
        warning = w.format_user()

        warnings.append(sanitize(warning.strip()))

    x = ['<div class="language_warning_entry">%s</div>' % w for w in warnings]
    language_warnings_html = "\n".join(x)

    def get_link(specname, libname, thingname):
        spec = specs[specname]
        url0 = ("/libraries/%s/%s/%s/views/syntax/" %
                (libname, spec.url_part, thingname))
        return make_relative(url0)


#     highlight2 = add_html_links(highlight, library_name, get_link)
#     print highlight2

    res = {
        'ok': True,
        'highlight': unicode(highlight, 'utf8'),
        'language_warnings': language_warnings_html,
        'string_with_suggestions': string_with_suggestions,
    }
    return res
예제 #21
0
파일: plot.py 프로젝트: kannode/mcdp
def syntax_pdf(data):
    """ Returns a PDF string """
    from mcdp_report.html import ast_to_html
    s = data['s']
    s = s.replace('\t', '    ')

    lines_to_hide = get_lines_to_hide(data['params'])

    def ignore_line(lineno):
        return lineno in lines_to_hide

    contents = ast_to_html(
        s,
        ignore_line=ignore_line,
        parse_expr=Syntax.ndpt_dp_rvalue,
    )
    html = get_minimal_document(contents)

    d = mkdtemp()

    f_html = os.path.join(d, 'file.html')
    with open(f_html, 'w') as f:
        f.write(html)

    try:
        f_pdf = os.path.join(d, 'file.pdf')
        cmd = ['wkhtmltopdf', '-s', 'A1', f_html, f_pdf]
        system_cmd_result(d,
                          cmd,
                          display_stdout=False,
                          display_stderr=False,
                          raise_on_error=True)

        f_pdf_crop = os.path.join(d, 'file_crop.pdf')
        cmd = ['pdfcrop', '--margins', '4', f_pdf, f_pdf_crop]
        system_cmd_result(d,
                          cmd,
                          display_stdout=False,
                          display_stderr=False,
                          raise_on_error=True)

        with open(f_pdf_crop) as f:
            data = f.read()

        f_png = os.path.join(d, 'file.png')
        cmd = [
            'convert', '-density', '600', f_pdf_crop, '-background', 'white',
            '-alpha', 'remove', '-resize', '50%', f_png
        ]
        system_cmd_result(d,
                          cmd,
                          display_stdout=False,
                          display_stderr=False,
                          raise_on_error=True)

        with open(f_png) as f:
            pngdata = f.read()

        return [('pdf', 'syntax_pdf', data), ('png', 'syntax_pdf', pngdata)]
    except CmdException as e:
        raise e
예제 #22
0
    def go(selector, parse_expr, extension, use_pre=True, refine=None):
        for tag in soup.select(selector):
            source_code = '<unset>'  # XXX
            try:
                if tag.string is None:  # or not tag.string.strip():
                    if not tag.has_attr('id'):
                        msg = "If <pre> is empty then it needs to have an id."
                        raise_desc(ValueError, msg, tag=describe_tag(tag))

                    # load it
                    tag_id = tag['id'].encode('utf-8')
                    if '.' in tag_id:
                        i = tag_id.index('.')
                        libname, name = tag_id[:i], tag_id[i + 1:]
                        use_library = library.load_library(libname)
                    else:
                        name = tag_id
                        use_library = library
                    basename = '%s.%s' % (name, extension)
                    data = use_library._get_file_data(basename)
                    source_code = data['data']
                else:
                    source_code = get_source_code(tag)

                # prettify.
                # remove spurious indentation
                source_code = source_code.strip()

                do_apply_suggestions = (not tag.has_attr('noprettify')
                                        and not tag.has_attr('np'))
                # then apply suggestions
                try:
                    if do_apply_suggestions:
                        x = parse_wrap(parse_expr, source_code)[0]
                        xr = parse_ndp_refine(x, Context())
                        suggestions = get_suggestions(xr)
                        source_code = apply_suggestions(
                            source_code, suggestions)
                except DPSyntaxError as e:
                    if raise_errors:
                        raise
                    else:
                        res.note_error(str(e), HTMLIDLocation.for_element(tag))
                        continue
                # we don't want the browser to choose different tab size
                # source_code = source_code.replace('\t', ' ' * 4)

                # we are not using it
                _realpath = realpath
                context = Context()

                def postprocess(x):
                    if refine is not None:
                        return refine(x, context=context)
                    else:
                        return x

                #                 print('rendering source code %r' % source_code)
                html = ast_to_html(source_code,
                                   parse_expr=parse_expr,
                                   add_line_gutter=False,
                                   postprocess=postprocess)

                for w in context.warnings:
                    if w.where is not None:
                        from mcdp_web.editor_fancy.app_editor_fancy_generic import html_mark
                        html = html_mark(html, w.where, "language_warning")

                frag2 = BeautifulSoup(html, 'lxml', from_encoding='utf-8')

                if use_pre:
                    rendered = Tag(name='div', attrs={'class': 'rendered'})
                    pre = frag2.pre
                    pre.extract()
                    rendered.append(pre)
                    if not rendered.has_attr('class'):
                        rendered['class'] = ""
                    if tag.has_attr('label'):
                        text = tag['label']
                        tag_label = Tag(name='span')
                        add_class(tag_label, 'label')
                        add_class(tag_label, 'label_inside')
                        tag_label.append(NavigableString(text))

                        pre.insert(0, tag_label)

                        tag_label_outside = Tag(name='span')
                        add_class(tag_label_outside, 'label')
                        add_class(tag_label_outside, 'label_outside')
                        tag_label_outside.append(NavigableString(text))
                        rendered.insert(0, tag_label_outside)

                    max_len = max_len_of_pre_html(html)

                    if tag.has_attr('label'):
                        add_class(rendered, 'has_label')
                        max_len = max(max_len, len(tag['label']) + 6)

                    style = ''
                else:
                    # using <code>
                    rendered = frag2.pre.code
                    rendered.extract()
                    if not rendered.has_attr('class'):
                        rendered['class'] = ""

                    style = ''

                if tag.has_attr('style'):
                    style = style + tag['style']

                if style:
                    rendered['style'] = style

                if tag.has_attr('class'):
                    add_class(rendered, tag['class'])

                if tag.has_attr('id'):
                    rendered['id'] = tag['id']

                if use_pre:
                    if generate_pdf:
                        pdf = get_ast_as_pdf(source_code, parse_expr)
                        if tag.has_attr('id'):
                            basename = tag['id']
                        else:
                            hashcode = hashlib.sha224(
                                source_code).hexdigest()[-8:]
                            basename = 'code-%s' % hashcode

                        docname = os.path.splitext(
                            os.path.basename(realpath))[0]
                        download = docname + '.' + basename + '.source_code.pdf'
                        a = create_a_to_data(download=download,
                                             data_format='pdf',
                                             data=pdf)
                        a['class'] = 'pdf_data'
                        a.append(NavigableString(download))
                        div = Tag(name='div')
                        div.append(rendered)
                        div.append(a)
                        tag.replaceWith(div)
                    else:
                        tag.replaceWith(rendered)
                else:
                    tag.replaceWith(rendered)

            except DPSyntaxError as e:
                if raise_errors:
                    raise
                else:
                    res.note_error(str(e), HTMLIDLocation.for_element(tag))
                    # note_error(tag, e)
                    if tag.string is None:
                        tag.string = "`%s" % tag['id']
                    continue

            except DPSemanticError as e:
                if raise_errors:
                    raise
                else:
                    res.note_error(str(e), HTMLIDLocation.for_element(tag))
                    # note_error(tag, e)
                    if tag.string is None:
                        tag.string = "`%s" % tag['id']
                    continue

            except DPInternalError as ex:
                msg = 'Error while interpreting the code:\n\n'
                msg += indent(source_code, '  | ')
                raise_wrapped(DPInternalError, ex, msg, exc=sys.exc_info())
예제 #23
0
    def go(selector, parse_expr, extension, use_pre=True):
        for tag in soup.select(selector):
            try:
                if tag.string is None:
                    if not tag.has_attr('id'):
                        msg = "If <pre> is empty then it needs to have an id."
                        raise_desc(ValueError, msg, tag=str(tag))
                    # load it
                    tag_id = tag['id'].encode('utf-8')
                    basename = '%s.%s' % (tag_id, extension)
                    data = library._get_file_data(basename)
                    source_code = data['data']
                    source_code = source_code.replace('\t', ' ' * 4)
                else:
                    source_code = get_source_code(tag)

                # we are not using it
                _realpath = realpath
                html = ast_to_html(source_code, parse_expr=parse_expr,
                                                complete_document=False,
                                                add_line_gutter=False,
                                                add_css=False)

                frag2 = BeautifulSoup(html, 'lxml', from_encoding='utf-8')

                if use_pre:
                    rendered = frag2.pre
                    if tag.has_attr('label'):
                        tag_label = soup.new_tag('span', **{'class': 'label'})
                        tag_label.append(tag['label'])
                        rendered.insert(0, tag_label)

                    max_len = max(map(len, source_code.split('\n')))
                    # account for the label
                    if tag.has_attr('label'):
                        max_len = max(max_len, len(tag['label']) + 6)

                    # need at least 1 to account for padding etc.
                    bonus = 1
                    style = 'width: %dch;' % (max_len + bonus)
                else:
                    # using <code>
                    rendered = frag2.pre.code
                    style = ''

                if tag.has_attr('style'):
                    style = style + tag['style'] 
                    
                if style:
                    rendered['style'] = style

                if tag.has_attr('class'):
                    rendered['class'] = tag['class']

                if tag.has_attr('id'):
                    rendered['id'] = tag['id']

                if use_pre:
                    if generate_pdf:
                        pdf = get_ast_as_pdf(source_code, parse_expr)
                        if tag.has_attr('id'):
                            basename = tag['id']
                        else:
                            hashcode = hashlib.sha224(source_code).hexdigest()[-8:]
                            basename = 'code-%s' % (hashcode)

                        docname = os.path.splitext(os.path.basename(realpath))[0]
                        download = docname + '.' + basename + '.source_code.pdf'
                        a = create_a_to_data(soup, download=download,
                                             data_format='pdf', data=pdf)
                        a['class'] = 'pdf_data'
                        a.append(NavigableString(download))
                        div = soup.new_tag('div')
                        div.append(rendered)
                        div.append(a)
                        tag.replaceWith(div)
                    else:
                        tag.replaceWith(rendered)
                else:
                    tag.replaceWith(rendered)

            except DPSyntaxError as e:
                if raise_errors:
                    raise
                logger.error(unicode(e.__str__(), 'utf-8'))
                t = soup.new_tag('pre', **{'class': 'error %s' % type(e).__name__})
                t.string = str(e)
                tag.insert_after(t)

                if tag.string is None:
                    tag.string = "`%s" % tag['id']

            except DPSemanticError as e:
                if raise_errors:
                    raise
                logger.error(unicode(e.__str__(), 'utf-8'))
                t = soup.new_tag('pre', **{'class': 'error %s' % type(e).__name__})
                t.string = str(e)
                tag.insert_after(t)
                if tag.string is None:
                    tag.string = "`%s" % tag['id']
                    
            except DPInternalError as e:
                msg = 'Error while interpreting the code:\n\n'
                msg += indent(source_code, '  | ')
                raise_wrapped(DPInternalError, e,msg, exc=sys.exc_info())
예제 #24
0
        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}
예제 #25
0
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