Example #1
0
def check_editor_response(filename, source, libname):  # @UnusedVariable
    if libname in ['loading_python', 'making']:
        # mcdplib-loading_python-source_mcdp-load1.mcdp-check_editor_response
        # mcdplib-making-source_mcdp-test1.mcdp-check_editor_response
        return
    library = get_test_library(libname)
    string = source
    spec = filename2spec(filename)

    key = ()
    cache = {}
    make_relative = lambda x: x
    res = process_parse_request(library, string, spec, key, cache,
                                make_relative)

    if res['ok']:

        if 'highlight' in res:
            check_isinstance(res['highlight'], unicode)

    else:
        forgive = ['Could not find file', 'DPNotImplementedError']

        if any(_ in res['error'] for _ in forgive):
            pass
        else:
            msg = 'Failed'
            raise_desc(ValueError, msg, source=source, res=res)
Example #2
0
def define_tests_rendering(context, libname):
    library = get_test_library(libname)
    
    ext = MCDPLibrary.ext_doc_md
    for docname, realpath in list_library_files(library, ext):
        job_id = 'render-%s' % docname
        context.comp(check_rendering, libname=libname, filename=realpath, job_id=job_id)
Example #3
0
def plot_different_solutions(libname, ndpname, query, out, upper=None):
    if not os.path.exists(out):
        os.makedirs(out)
    library = get_test_library(libname)
    #library.use_cache_dir(os.path.join(out, 'cache'))
    context = Context()
    ndp = library.load_ndp(ndpname, context)

    context = library._generate_context_with_hooks()
    ndp_labelled = get_labelled_version(ndp)
    dp0 = ndp_labelled.get_dp()
    if upper is not None:
        _, dpU = get_dp_bounds(dp0, nl=1, nu=upper)
        dp = dpU
    else:
        dp = dp0

    M = dp.get_imp_space()

    with open(os.path.join(out, 'ndp.txt'), 'w') as f:
        f.write(ndp.repr_long())
    with open(os.path.join(out, 'M.txt'), 'w') as f:
        f.write(M.repr_long())
    with open(os.path.join(out, 'dp.txt'), 'w') as f:
        f.write(dp.repr_long())
    with open(os.path.join(out, 'dp0.txt'), 'w') as f:
        f.write(dp0.repr_long())

    f = convert_string_query(ndp=ndp, query=query, context=context)

    report = Report()

    res = dp.solve(f)
    print('num solutions: %s' % len(res.minimals))
    for ri, r in enumerate(res.minimals):
        ms = dp.get_implementations_f_r(f, r)

        for j, m in enumerate(ms):
            imp_dict = get_imp_as_recursive_dict(M, m)
            print imp_dict

            images_paths = library.get_images_paths()
            gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1)

            gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM,
                                images_paths=images_paths,
                                plotting_info=gv)

            with report.subsection('%s-%s' % (ri, j)) as rr:
                gg_figure(rr, 'figure', gg, do_png=True, do_pdf=False,
                          do_svg=False, do_dot=False)


    fn = os.path.join(out, 'solutions.html')
    print('writing to %s' % fn)
    report.to_html(fn)
Example #4
0
def define_tests_rendering(context, libname):
    library = get_test_library(libname)

    ext = MCDPConstants.ext_doc_md
    for docname, realpath in list_library_files(library, ext):
        job_id = 'render-%s' % docname
        context.comp(check_rendering,
                     libname=libname,
                     filename=realpath,
                     job_id=job_id)
Example #5
0
def allformats_posets_report(id_poset, poset, libname, which):
    from mcdp_web.images.images import get_mime_for_format
    from mcdp_library_tests.tests import get_test_library
    r = Report(id_poset + '-' + which)
    library = get_test_library(libname)
    image_source = ImagesFromPaths(library.get_images_paths())
    mf = MakeFiguresPoset(poset=poset, image_source=image_source)
    formats = mf.available_formats(which)
    res = mf.get_figure(which, formats)

    fig = r.figure()
    for f in formats:
        data = res[f]
        mime = get_mime_for_format(f)
        dn = DataNode(f, data=data, mime=mime)
        fig.add_child(dn)
    return r
Example #6
0
def check_generate_view_syntax(filename, source, libname):  # @UnusedVariable
    from mcdp_library.stdlib import get_test_db
    db_view = get_test_db()
    assert len(db_view.repos) == 1
    repo_name = list(db_view.repos)[0]
    library = get_test_library(libname)

    spec = filename2spec(filename)
    thing_name, _ext = os.path.splitext(os.path.basename(filename))
    make_relative = lambda x: x

    class SessionMockup(object):
        def __init__(self):
            pass

        def get_repo_shelf_for_libname(self, libname):  # @UnusedVariable
            for shelf_name, shelf in db_view.repos[repo_name].shelves.items():
                if libname in shelf.libraries:
                    return repo_name, shelf_name
            msg = 'Could not find repo,shelf for library %r.' % libname
            raise Exception(msg)

        def get_subscribed_shelves(self):
            return list(db_view.repos[repo_name].shelves)

    session = SessionMockup()
    repo_name, shelf_name = session.get_repo_shelf_for_libname(libname)

    class EnvironmentMockup(object):
        def __init__(self):
            self.library_name = libname
            self.spec = spec
            self.library = library
            self.session = session
            self.repo_name = repo_name
            self.shelf_name = shelf_name
            self.thing_name = thing_name
            self.thing = source
            self.db_view = db_view

    e = EnvironmentMockup()
    _res = generate_view_syntax(e, make_relative)
Example #7
0
def check_rendering(libname, filename):
    library = get_test_library(libname)
    data = read_file_encoded_as_utf8(filename)

    expected = get_expected_exceptions(data)

    with with_library_cache_dir(library):
        try:
            contents = render_complete(library,
                                       data,
                                       raise_errors=True,
                                       realpath=filename)
            # write html
            html = get_minimal_document(contents, add_markdown_css=True)
            basename = os.path.basename(filename)
            fn = os.path.join('out', 'check_rendering', libname,
                              basename + '.html')
            write_file_encoded_as_utf8(fn, html)
        except expected:
            return
Example #8
0
def allformats_report(id_ndp, ndp, libname, which):
    from mcdp_web.images.images import get_mime_for_format
    r = Report(id_ndp + '-' + which)
    from mcdp_library_tests.tests import get_test_library
    library = get_test_library(libname)
    mf = MakeFiguresNDP(ndp=ndp, library=library, yourname=id_ndp)
    formats = mf.available_formats(which)
    try:
        res = mf.get_figure(which, formats)
    except DPSemanticError as e:
        if 'Cannot abstract' in str(e):
            r.text('warning', 'Not connected. \n\n %s' % e)
            return r
    print('%s -> %s %s ' % (which, formats, map(len, [res[f] for f in formats])))
    fig = r.figure()
    for f in formats:
        data = res[f]
        mime = get_mime_for_format(f)
        dn = DataNode(f, data=data, mime=mime)
        fig.add_child(dn)
    return r    
Example #9
0
def check_rendering(libname, filename):
    library = get_test_library(libname)
    import codecs
    data = codecs.open(filename, encoding='utf-8').read().encode('utf-8')
    
    tmpdir = tempfile.mkdtemp(prefix='mcdplibrary_cache')
    library.use_cache_dir(tmpdir)

    contents = render_complete(library, data, raise_errors=True, realpath=filename)
    html = get_minimal_document(contents, add_markdown_css=True)
    
    basename = os.path.basename(filename)
    fn = os.path.join('out', 'check_rendering', libname, basename + '.html')
    d = os.path.dirname(fn)
    if not os.path.exists(d):
        os.makedirs(d)
    with open(fn, 'w') as f:
        f.write(html)
    print('written to %r ' % fn)

    shutil.rmtree(tmpdir)
Example #10
0
def allformats_report(id_ndp, ndp, libname, which):
    from mcdp_web.images.images import get_mime_for_format
    from mcdp_library_tests.tests import get_test_library
    r = Report(id_ndp + '-' + which)
    library = get_test_library(libname)
    image_source = ImagesFromPaths(library.get_images_paths())
    mf = MakeFiguresNDP(ndp=ndp, image_source=image_source, yourname=id_ndp)
    formats = mf.available_formats(which)
    try:
        res = mf.get_figure(which, formats)
    except DPSemanticError as e:
        if 'Cannot abstract' in str(e):
            r.text('warning', 'Not connected. \n\n %s' % e)
            return r
    print('%s -> %s %s ' % (which, formats, map(len, [res[f]
                                                      for f in formats])))
    fig = r.figure()
    for f in formats:
        data = res[f]
        mime = get_mime_for_format(f)
        dn = DataNode(f, data=data, mime=mime)
        fig.add_child(dn)
    return r
Example #11
0
File: test1.py Project: rusi/mcdp
def plot_different_solutions(libname, ndpname, query, out, upper=None):
    if not os.path.exists(out):
        os.makedirs(out)
    library = get_test_library(libname)
    #library.use_cache_dir(os.path.join(out, 'cache'))
    context = Context()
    ndp = library.load_ndp(ndpname, context)

    context = library._generate_context_with_hooks()
    ndp_labelled = get_labelled_version(ndp)
    dp0 = ndp_labelled.get_dp()
    if upper is not None:
        _, dpU = get_dp_bounds(dp0, nl=1, nu=upper)
        dp = dpU
    else:
        dp = dp0

    M = dp.get_imp_space()

    with open(os.path.join(out, 'ndp.txt'), 'w') as f:
        f.write(ndp.repr_long())
    with open(os.path.join(out, 'M.txt'), 'w') as f:
        f.write(M.repr_long())
    with open(os.path.join(out, 'dp.txt'), 'w') as f:
        f.write(dp.repr_long())
    with open(os.path.join(out, 'dp0.txt'), 'w') as f:
        f.write(dp0.repr_long())

    f = convert_string_query(ndp=ndp, query=query, context=context)

    report = Report()

    res = dp.solve(f)
    print('num solutions: %s' % len(res.minimals))
    for ri, r in enumerate(res.minimals):
        ms = dp.get_implementations_f_r(f, r)

        for j, m in enumerate(ms):
            imp_dict = get_imp_as_recursive_dict(M, m)
            logger.info(imp_dict)

            images_paths = library.get_images_paths()
            image_source = ImagesFromPaths(images_paths)
            gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1)

            gg = gvgen_from_ndp(ndp=ndp,
                                style=STYLE_GREENREDSYM,
                                image_source=image_source,
                                plotting_info=gv)

            with report.subsection('%s-%s' % (ri, j)) as rr:
                gg_figure(rr,
                          'figure',
                          gg,
                          do_png=True,
                          do_pdf=False,
                          do_svg=False,
                          do_dot=False)

    fn = os.path.join(out, 'solutions.html')
    print('writing to %s' % fn)
    report.to_html(fn)