Exemple #1
0
def go():
    librarian = Librarian()
    librarian.find_libraries('../..')
    library = librarian.load_library('droneD_complete_templates')
    library.use_cache_dir('_cached/drone_unc2')
    context = library._generate_context_with_hooks()

    res = {}
    res['intervals'] = [0, 0.01, 0.1, 1.0, 5.0, 10.0, 50, 100.0, 250, 500, 1000]
    res['results'] = []
    for i, interval_mw in enumerate(res['intervals']):
        s = get_ndp_code(interval_mw=interval_mw)
        ndp = parse_ndp(s, context=context)

        basename = ('drone_unc2_%02d_%s_mw' % (i, interval_mw)).replace('.', '_')
        fn = os.path.join('generated', 'drone_unc2', basename + '.mcdp')
        dn = os.path.dirname(fn)
        if not os.path.exists(dn):
            os.makedirs(dn)
        with open(fn, 'w') as f:
            f.write(s)
        print('Generated %s' % fn)

        result = solve_stats(ndp)
        result['ndp'] = ndp
        res['results'].append(result)

    return res
Exemple #2
0
def feat_import5():
    """ Warnings in imports. """
    data = {
        'lib1.mcdplib/model0.mcdp': """
        mcdp {
            provides f [Nat]
            f <= Nat: 2
        }
        """,
        'lib1.mcdplib/model1.mcdp': 
        """
        mcdp {
            a = instance `model0
        }
        """
    }
   
    d = create_hierarchy(data)
    librarian = Librarian()
    librarian.find_libraries(d)
    lib = librarian.load_library('lib1')

    context = lib._generate_context_with_hooks()
    _model2 = lib.load_ndp('model1', context)
    for w in context.warnings:
        print w.format_user()
    assert_equal(len(context.warnings), 1)
Exemple #3
0
def feat_import5():
    """ Warnings in imports. """
    data = {
        'lib1.mcdplib/model0.mcdp':
        """
        mcdp {
            provides f [Nat]
            f <= Nat: 2
        }
        """,
        'lib1.mcdplib/model1.mcdp':
        """
        mcdp {
            a = instance `model0
        }
        """
    }

    d = create_hierarchy(data)
    librarian = Librarian()
    librarian.find_libraries(d)
    lib = librarian.load_library('lib1')

    context = lib._generate_context_with_hooks()
    _model2 = lib.load_ndp('model1', context)
    for w in context.warnings:
        print w.format_user()
    assert_equal(len(context.warnings), 1)
Exemple #4
0
def go():
    librarian = Librarian()
    librarian.find_libraries('../..')
    library = librarian.load_library('droneD_complete_templates')
    library.use_cache_dir('_cached/drone_unc2')
    context = library._generate_context_with_hooks()

    res = {}
    res['intervals'] = [
        0, 0.01, 0.1, 1.0, 5.0, 10.0, 50, 100.0, 250, 500, 1000
    ]
    res['results'] = []
    for i, interval_mw in enumerate(res['intervals']):
        s = get_ndp_code(interval_mw=interval_mw)
        ndp = parse_ndp(s, context=context)

        basename = ('drone_unc2_%02d_%s_mw' % (i, interval_mw)).replace(
            '.', '_')
        fn = os.path.join('generated', 'drone_unc2', basename + '.mcdp')
        dn = os.path.dirname(fn)
        if not os.path.exists(dn):
            os.makedirs(dn)
        with open(fn, 'w') as f:
            f.write(s)
        print('Generated %s' % fn)

        result = solve_stats(ndp)
        result['ndp'] = ndp
        res['results'].append(result)

    return res
Exemple #5
0
def process(s):
    librarian = Librarian()
    librarian.find_libraries('../..')
    library = librarian.load_library('droneD_complete_templates')
    library.use_cache_dir('_cached/drone_unc1')

    ndp = library.parse_ndp(s)

    combinations = {
        "endurance": (np.linspace(1, 1.5, 10), "hour"),
        "extra_payload": (100, "g"),
        "num_missions": (1000, "[]"),
        "velocity": (1.0, "m/s"),
        'extra_power': (0.5, 'W'),
    }

    result_like = dict(total_cost="USD", total_mass='kg')

    dataU = solve_combinations(ndp,
                               combinations,
                               result_like,
                               upper=1,
                               lower=None)
    dataL = solve_combinations(ndp,
                               combinations,
                               result_like,
                               upper=None,
                               lower=1)

    return dict(dataL=dataL, dataU=dataU)
Exemple #6
0
def get_test_librarian():
    package = dir_from_package_name('mcdp_data')
    folder = os.path.join(package, 'bundled.mcdp_repo')

    if not os.path.exists(folder):
        raise_desc(ValueError, 'Test folders not found.', folder=folder)

    librarian = Librarian()
    librarian.find_libraries(folder)

    libraries = librarian.libraries
    n = len(libraries)
    if n <= 1:
        msg = 'Expected more libraries.'
        raise_desc(ValueError, msg, folder, libraries=libraries)

    orig = list(libraries)
    vname = MCDPConstants.ENV_TEST_LIBRARIES

    if vname in os.environ:
        use = os.environ[vname].split(",")

        logger.debug('environment variable %s = %s' % (vname, use))

        logger.info('Because %s is set, I will use only %s instead of %s.' %
                    (vname, use, orig))

        for _ in orig:
            if not _ in use:
                del libraries[_]
    else:
        pass
        #logger.debug('environment variable %s is unset' % vname)

    vname2 = MCDPConstants.ENV_TEST_LIBRARIES_EXCLUDE
    if vname2 in os.environ:
        exclude = os.environ[vname2].split(',')
        logger.debug('environment variable %s = %s' % (vname2, exclude))
    else:
        exclude = []
        # logger.debug('environment variable %s is unset' % vname2)

    if exclude:
        for a in exclude:
            if not a in libraries:
                msg = '%s = %s but %r is not a library.' % (vname2, exclude, a)
                logger.error(msg)
            else:
                logger.info('Excluding %s' % vname2)
                del libraries[a]

    return librarian
Exemple #7
0
def feat_import4():
    data = {
        'lib1.mcdplib/model0.mcdp': "mcdp {}",
        'lib1.mcdplib/template1.mcdp_template': "template [] mcdp { a = instance `model0 }",
        'lib2.mcdplib/template2.mcdp_template': "`lib1.template1",
        'lib2.mcdplib/model2.mcdp': "specialize [] `template2",
    }
    d = create_hierarchy(data)
    librarian = Librarian()
    librarian.find_libraries(d)
    lib2 = librarian.load_library('lib2')
    context = lib2._generate_context_with_hooks()
    _model2 = lib2.load_ndp('model2', context)
Exemple #8
0
def feat_import3():
    """ NDP load """
    data = {
        'lib1.mcdplib/model0.mcdp': "mcdp {}",
        'lib1.mcdplib/model1.mcdp': "`model0",
        'lib2.mcdplib/model2.mcdp': "`lib1.model1",
    }
    d = create_hierarchy(data)
    librarian = Librarian()
    librarian.find_libraries(d)
    lib2 = librarian.load_library('lib2')
    context = lib2._generate_context_with_hooks()
    _model2 = lib2.load_ndp('model2', context)
Exemple #9
0
def feat_import3():
    """ NDP load """
    data = {
        'lib1.mcdplib/model0.mcdp': "mcdp {}",
        'lib1.mcdplib/model1.mcdp': "`model0",
        'lib2.mcdplib/model2.mcdp': "`lib1.model1",
    }
    d = create_hierarchy(data)
    librarian = Librarian()
    librarian.find_libraries(d)
    lib2 = librarian.load_library('lib2')
    context = lib2._generate_context_with_hooks()
    _model2 = lib2.load_ndp('model2', context)
Exemple #10
0
def feat_import4():
    data = {
        'lib1.mcdplib/model0.mcdp': "mcdp {}",
        'lib1.mcdplib/template1.mcdp_template':
        "template [] mcdp { a = instance `model0 }",
        'lib2.mcdplib/template2.mcdp_template': "`lib1.template1",
        'lib2.mcdplib/model2.mcdp': "specialize [] `template2",
    }
    d = create_hierarchy(data)
    librarian = Librarian()
    librarian.find_libraries(d)
    lib2 = librarian.load_library('lib2')
    context = lib2._generate_context_with_hooks()
    _model2 = lib2.load_ndp('model2', context)
Exemple #11
0
def get_test_library2(libnames):
    package = dir_from_package_name('mcdp_data')
    all_libraries = os.path.join(package, 'libraries')

    librarian = Librarian()
    librarian.find_libraries(all_libraries)

    l = MCDPLibrary()
    for lib in libnames:
        data = librarian.libraries[lib]
        path = data['path']
        l.add_search_dir(path)

    l.load_library_hooks.append(librarian.load_library)
    return l
Exemple #12
0
    def __init__(self, app, request):
        ''' dirnames: list of directories where to find shelves '''
        self.app = app
        self.shelves_all = {}
        self.request = request

        self.librarian = Librarian()
        self.authenticated_userid = request.authenticated_userid

        # used during auth procedure
        self.candidate_user = None
        self.soft_match = None
        self.next_location = None

        self.recompute_available()
Exemple #13
0
    def go(self):
        logger.setLevel(logging.DEBUG)

        options = self.get_options()

        if not options.contracts:
            disable_all()

        params = options.get_extra()

        if len(params) < 1:
            raise ValueError("Please specify name.")

        config_dirs = options.config_dirs.split(":")
        maindir = options.maindir
        out_dir = options.out

        if out_dir is None:
            out_dir = os.path.join("out", "mcdp_render")

        if options.cache:
            cache_dir = os.path.join(out_dir, "_cached", "solve")
        else:
            cache_dir = None

        librarian = Librarian()
        for e in config_dirs:
            librarian.find_libraries(e)

        library = librarian.get_library_by_dir(maindir)
        if cache_dir is not None:
            library.use_cache_dir(cache_dir)

        docs = params

        if not docs:
            msg = "At least one argument required."
            raise_desc(UserError, msg)

        for docname in docs:
            basename = docname + "." + MCDPLibrary.ext_doc_md
            f = library._get_file_data(basename)
            data = f["data"]
            realpath = f["realpath"]

            generate_pdf = options.pdf
            render(library, docname, data, realpath, out_dir, generate_pdf)
Exemple #14
0
def find_dependencies(config_dirs, maindir, seeds):
    """
        returns res, with res['fd'] ~ FindDependencies
    """
    librarian = Librarian()
    for e in config_dirs:
        librarian.find_libraries(e)

    default_library = librarian.get_library_by_dir(maindir)

    fd = FindDependencies(default_library)
    
    if seeds is None:
        # add all models for all libraries
        libnames = list(librarian.get_libraries())
        
        seeds = []
        for libname in libnames:
            library = librarian.load_library(libname)
            ndps = library.list_spec(SPEC_MODELS)
            
            for name in ndps:
                seeds.append('%s.%s' % (libname, name))
    else:
        pass
    
    fd.search(seeds)

    res = {}
    res['fd'] = fd
    return res
Exemple #15
0
def get_test_librarian():
    package = dir_from_package_name('mcdp_data')
    folder = os.path.join(package, 'libraries')

    if not os.path.exists(folder):
        raise_desc(ValueError, 'Test folders not found.' , folder=folder)

    librarian = Librarian()
    librarian.find_libraries(folder)
    
    libraries = librarian.get_libraries()

    n = len(libraries)
    if n <= 1:
        msg = 'Expected more libraries.'
        raise_desc(ValueError, msg, folder, libraries=libraries)


    return librarian
Exemple #16
0
 def _load_libraries(self):
     """ Loads libraries in the "self.dirname" dir. """
     self.librarian = Librarian()
     self.librarian.find_libraries(self.dirname)
     self.libraries = self.librarian.get_libraries()
     for _short, data in self.libraries.items():
         l = data['library']
         path = data['path']
         cache_dir = os.path.join(path, '_cached/mcdpweb_cache')
         l.use_cache_dir(cache_dir)
Exemple #17
0
def go():
 
    model_name = 'droneC'
    queries = []
    def add(q):
        queries.append(q)

    n = 10
    endurance = np.linspace(1, 20, n)
    payload = np.linspace(5, 50, n)

    for endurance, payload in zip(endurance, payload):
        q = {
            "num_missions": (1000, "[]"),
            "extra_power": (5, "W"),
            "extra_payload": (payload, "g"),
            "endurance": (endurance, "minutes"),
        }
        add(q)

    result_like = dict(total_cost="CHF", total_mass='kg')
    what_to_plot_res = result_like
    what_to_plot_fun = dict(extra_payload="g", endurance="minutes")

    librarian = Librarian()
    librarian.find_libraries('..')
    lib = librarian.load_library('droneC_cost_v1')
    ndp = lib.load_ndp(model_name)

    data = solve_queries(ndp, queries, result_like)

    r = Report()

    plot_all_directions(r,
                        queries=data['queries'],
                        results=data['results'],
                        what_to_plot_res=what_to_plot_res,
                        what_to_plot_fun=what_to_plot_fun)
    fn = 'out/droneC_c1.html'
    print('writing to %r' % fn)
    r.to_html(fn)
Exemple #18
0
def feat_import2():
    """ NDP load """
    data = {
        'lib1.mcdplib/model1.mcdp':
        "mcdp {}",
        'lib2.mcdplib/model2.mcdp':
        "`lib1.model1",
        'lib2.mcdplib/model3.mcdp':
        """\
        mcdp {
            a = instance `lib1.model1
        }
        """
    }
    d = create_hierarchy(data)
    librarian = Librarian()
    librarian.find_libraries(d)
    lib1 = librarian.load_library('lib1')

    _model1 = lib1.load_ndp('model1', context=Context())
    lib2 = librarian.load_library('lib2')

    context = lib1._generate_context_with_hooks()
    _model2 = lib2.load_ndp('model2', context)
    _model3 = lib2.load_ndp('model3', context)
Exemple #19
0
def find_dependencies(config_dirs, maindir, seeds):
    """
        returns res, with res['fd'] ~ FindDependencies
    """
    librarian = Librarian()
    for e in config_dirs:
        librarian.find_libraries(e)

    default_library = librarian.get_library_by_dir(maindir)

    fd = FindDependencies(default_library)
    
    if seeds is None:
        # add all models for all libraries
        libnames = list(librarian.get_libraries())
        
        seeds = []
        for libname in libnames:
            library = librarian.load_library(libname)
            ndps = library.list_ndps()
            
            for name in ndps:
                seeds.append('%s.%s' % (libname, name))
    else:
        pass
    
    fd.search(seeds)

    res = {}
    res['fd'] = fd
    return res
Exemple #20
0
def process(s):
    librarian = Librarian()
    librarian.find_libraries('../..')
    library = librarian.load_library('droneD_complete_templates')
    library.use_cache_dir('_cached/drone_unc1')

    ndp = library.parse_ndp(s)

    combinations = {
        "endurance": (np.linspace(1, 1.5, 10), "hour"),
        "extra_payload": (100, "g"),
        "num_missions": ( 1000, "[]"),
        "velocity": (1.0, "m/s"),
        'extra_power': (0.5, 'W'),
    }

    result_like = dict(total_cost="USD", total_mass='kg')

    dataU = solve_combinations(ndp, combinations, result_like, upper=1, lower=None)
    dataL = solve_combinations(ndp, combinations, result_like, upper=None, lower=1)

    return dict(dataL=dataL, dataU=dataU)
Exemple #21
0
def do_plots(logger, model_name, plots, outdir, extra_params, maindir,
             extra_dirs, use_cache):

    if '.mcdp' in model_name:
        model_name2 = model_name.replace('.mcdp', '')
        msg = 'Arguments should be model names, not file names.'
        msg += ' Interpreting %r as %r.' % (model_name, model_name2)
        logger.warn(msg)
        model_name = model_name2

    if use_cache:
        cache_dir = os.path.join(outdir, '_cached/mcdp_plot_cache')
        logger.info('using cache %s' % cache_dir)
    else:
        cache_dir = None

    librarian = Librarian()
    for e in extra_dirs:
        librarian.find_libraries(e)

    library = librarian.get_library_by_dir(maindir)
    if cache_dir is not None:
        library.use_cache_dir(cache_dir)

    assert library.library_name is not None

    is_ndp = library.file_exists(model_name + '.mcdp')
    is_poset = library.file_exists(model_name + '.mcdp_poset')

    if is_ndp:
        results = do_plots_ndp(model_name, library, plots, extra_params)
    elif is_poset:
        results = do_plots_poset(model_name, library, plots)
    else:
        msg = 'Cannot find anything corresponding to %r.' % model_name
        raise_desc(ValueError, msg)
        return

    write_results(results, model_name, outdir)
Exemple #22
0
def feat_import2():
    """ NDP load """
    data = {
        'lib1.mcdplib/model1.mcdp': "mcdp {}",
        'lib2.mcdplib/model2.mcdp': "`lib1.model1",
    
        'lib2.mcdplib/model3.mcdp': """\
        mcdp {
            a = new lib1.model1
        }
        """
    }
    d = create_hierarchy(data)
    librarian = Librarian()
    librarian.find_libraries(d)
    lib1 = librarian.load_library('lib1')
    
    _model1 = lib1.load_ndp('model1', context=Context())
    lib2 = librarian.load_library('lib2')
    
    context = lib1._generate_context_with_hooks()
    _model2 = lib2.load_ndp('model2', context)
    _model3 = lib2.load_ndp('model3', context)
Exemple #23
0
def go(algo):
    librarian = Librarian()
    librarian.find_libraries('../..')
    library = librarian.load_library('droneD_complete_templates')
    library.use_cache_dir('_cached/drone_unc3')
    context = library._generate_context_with_hooks()

    res = {}
    res['n'] = [
        1, 5, 10, 15, 25, 50, 61, 75, 92, 100, 125, 150, 160, 175, 182, 200,
        300, 400, 500, 600, 1000, 1500
    ]

    #     res['n'] = [1, 5, 10, 15, 25, 50, 61, 100]
    #     res['n'] = [3000]
    res['results'] = []
    for n in res['n']:
        ndp = create_ndp(context)
        result = solve_stats(ndp=ndp, n=n, algo=algo)
        result['ndp'] = ndp
        res['results'].append(result)

    return res
Exemple #24
0
def feat_import1():
    """ Poset load """
    data = {
        'lib1.mcdplib/poset1.mcdp_poset': "product(mass:g, energy:J)",
        'lib2.mcdplib/poset2.mcdp_poset': "`lib1.poset1"
    }
    d = create_hierarchy(data)
    librarian = Librarian()
    librarian.find_libraries(d)
    libraries = librarian.get_libraries()
    assert 'lib1' in libraries
    assert 'lib2' in libraries
    assert 'path' in libraries['lib1']
    assert 'library' in libraries['lib1']

    lib1 = librarian.load_library('lib1')
    context = lib1._generate_context_with_hooks()
    _poset1 = lib1.load_poset('poset1', context)
    lib2 = librarian.load_library('lib2')
    _poset2 = lib2.load_poset('poset2', context)
Exemple #25
0
def feat_import1():
    """ Poset load """
    data = {
        'lib1.mcdplib/poset1.mcdp_poset': "product(mass:g, energy:J)",
        'lib2.mcdplib/poset2.mcdp_poset': "`lib1.poset1"
    }
    d = create_hierarchy(data)
    librarian = Librarian()
    librarian.find_libraries(d)
    libraries = librarian.get_libraries()
    assert 'lib1' in libraries
    assert 'lib2' in libraries
    assert 'path' in libraries['lib1']
    assert 'library' in libraries['lib1']

    lib1 = librarian.load_library('lib1')
    context = lib1._generate_context_with_hooks()
    _poset1 = lib1.load_poset('poset1', context)
    lib2 = librarian.load_library('lib2')
    _poset2 = lib2.load_poset('poset2', context)
Exemple #26
0
def do_plots(logger, model_name, plots, outdir, extra_params, 
             maindir, extra_dirs, use_cache):
    data = {}

    if '.mcdp' in model_name:
        model_name2 = model_name.replace('.mcdp', '')
        msg = 'Arguments should be model names, not file names.'
        msg += ' Interpreting %r as %r.' % (model_name, model_name2)
        logger.warn(msg)
        model_name = model_name2

    data['model_name'] = model_name

    if use_cache:
        cache_dir = os.path.join(outdir, '_cached/mcdp_plot_cache')
        logger.info('using cache %s' % cache_dir)
    else:
        cache_dir = None

    librarian = Librarian()
    for e in extra_dirs:
        librarian.find_libraries(e)

    library = librarian.get_library_by_dir(maindir)
    if cache_dir is not None:
        library.use_cache_dir(cache_dir)

    assert library.library_name is not None

    filename = model_name + '.mcdp'
    x = library._get_file_data(filename)
    data['s'] = x['data']
    data['filename'] = x['realpath']
    data['params'] = parse_params(extra_params)
    data['library'] = library

    d = dict(allplots)
    results = []
    for p in plots:    
        # print('plotting %r ' % p)
        try:
            if p in d:
                res = d[p](data)
            else:
                msg = 'Unknown plot.'
                raise_desc(ValueError, msg, plot=p, available=sorted(d.keys()))
        except CmdException as e:
            mcdp_dev_warning('Add better checks of error.')
            logger.error(e)
            continue
        except Exception as e:
            logger.error('While creating %r' % p)
            raise
        assert isinstance(res, list), res
        for r in res:
            assert isinstance(r, tuple), r
            mime, name, x = r
            assert isinstance(x, str), x
            ext = mime

            base = model_name + '-%s.%s' % (name, ext)

            out = os.path.join(outdir, base)
            logger.info('Writing to %s' % out)
            with open(out, 'w') as f:
                f.write(x)

            results.append(r)

    return results
Exemple #27
0
class WebApp(AppEditor, AppVisualization, 
             AppQR, AppSolver, AppInteractive,
             AppSolver2, AppEditorFancyGeneric, WebAppImages):

    def __init__(self, dirname):
        self.dirname = dirname

        self._load_libraries()


        logger.info('Found %d libraries underneath %r.' %
                        (len(self.libraries), self.dirname))

        AppEditor.__init__(self)
        AppVisualization.__init__(self)
        AppQR.__init__(self)
        AppSolver.__init__(self)
        AppInteractive.__init__(self)
        AppSolver2.__init__(self)
        AppEditorFancyGeneric.__init__(self)
        WebAppImages.__init__(self)

        # name -> dict(desc: )
        self.views = {}
        self.exceptions = []
        
        self.add_model_view('syntax', 'Simple display')
        self.add_model_view('edit_fancy', 'Fancy editor')
        self.add_model_view('edit', 'Simple editor for IE')
        self.add_model_view('ndp_graph', 'NDP Graph representation')
        self.add_model_view('ndp_repr', 'NDP Text representation')
        self.add_model_view('dp_graph', 'DP graph representation')
        self.add_model_view('dp_tree', 'DP tree representation')
        self.add_model_view('images', 'other image representations')
        self.add_model_view('solver', 'Simple solver')

    def add_model_view(self, name, desc):
        self.views[name] = dict(desc=desc, order=len(self.views))

    def _get_views(self):
        return sorted(self.views, key=lambda _:self.views[_]['order'])

    def get_current_library_name(self, request):
        library_name = str(request.matchdict['library'])  # unicod
        return library_name

    def get_library(self, request):
        library_name = self.get_current_library_name(request)
        if not library_name in self.libraries:
            msg = 'Could not find library %r.' % library_name
            raise_desc(ValueError, msg, available=self.libraries)
        return self.libraries[library_name]['library']

    def list_of_models(self, request):
        l = self.get_library(request)
        return l.get_models()
    
    @contract(returns='list(str)')
    def list_libraries(self):
        """ Returns the list of libraries """
        return sorted(self.libraries)

    def view_index(self, request):  # @UnusedVariable
        d = {}
        d['version'] = lambda: mocdp.__version__
        return d

    def view_list(self, request):
        res = {}
        res['navigation'] = self.get_navigation_links(request)
        res.update(self.get_jinja_hooks(request))
        return res

    def view_list_libraries(self, request):  # @UnusedVariable
        libraries = self.list_libraries()
        return {'libraries': sorted(libraries)}

    def _refresh_library(self, _request):
        # nuclear option
        self._load_libraries()

        for l in [_['library'] for _ in self.libraries.values()]:
            l.delete_cache()

        from mcdp_report.gdc import get_images
        assert hasattr(get_images, 'cache')  # in case it changes later
        get_images.cache = {}

    def _load_libraries(self):
        """ Loads libraries in the "self.dirname" dir. """
        self.librarian = Librarian()
        self.librarian.find_libraries(self.dirname)
        self.libraries = self.librarian.get_libraries()
        for _short, data in self.libraries.items():
            l = data['library']
            path = data['path']
            cache_dir = os.path.join(path, '_cached/mcdpweb_cache')
            l.use_cache_dir(cache_dir)

    def view_refresh_library(self, request):
        """ Refreshes the current library (if external files have changed) 
            then reloads the current url. """
        self._refresh_library(request)
        raise HTTPFound(request.referrer)

    def view_not_found(self, request):
        request.response.status = 404
        url = request.url
        referrer = request.referrer
        self.exceptions.append('Path not found.\n url: %s\n ref: %s' % (url, referrer))
        return {'url': url, 'referrer': referrer}

    def view_exceptions_occurred(self, request):  # @UnusedVariable
        exceptions = []
        for e in self.exceptions:
            u = unicode(e, 'utf-8')
            exceptions.append(u)
        return {'exceptions': exceptions}

    def view_exception(self, exc, request):
        request.response.status = 500  # Internal Server Error

        import traceback
        compact = (DPSemanticError, DPSyntaxError)
        if isinstance(exc, compact):
            s = exc.__str__()
        else:
            s = traceback.format_exc(exc)

        url = request.url
        referrer = request.referrer
        n = 'Error during serving this URL:'
        n += '\n url: %s' % url
        n += '\n referrer: %s' % referrer
        ss = traceback.format_exc(exc)
        n += '\n' + indent(ss, '| ')
        self.exceptions.append(n)

        if 'library' in request.matchdict:
            library = self.get_current_library_name(request)
            url_refresh = '/libraries/%s/refresh_library' % library
        else:
            url_refresh = None

        u = unicode(s, 'utf-8')
        logger.error(u)
        return {'exception': u, 'url_refresh': url_refresh}
    
    def png_error_catch2(self, request, func):
        """ func is supposed to return an image response.
            If it raises an exception, we create
            an image with the error and then we add the exception
            to the list of exceptions.
            
             """
        try:
            return func()
        except Exception as e:
            import traceback
            s = traceback.format_exc(e)

            try:
                logger.error(s)
            except UnicodeEncodeError:
                pass

            s = str(s)
            url = request.url
            referrer = request.referrer
            n = 'Error during rendering an image.'
            n+= '\n url: %s' % url
            n+= '\n referrer: %s' % referrer
            n += '\n' + indent(s, '| ')
            self.exceptions.append(n)
            from mcdp_web.utils.image_error_catch_imp import response_image
            return response_image(request, s)

    def view_docs(self, request):  # XXX check this
        docname = str(request.matchdict['document'])  # unicode
        # from pkg_resources import resource_filename  # @UnresolvedImport
        res = self.render_markdown_doc(docname)

        res.update(self.get_jinja_hooks(request))
        return res

    def render_markdown_doc(self, docname):
        package = dir_from_package_name('mcdp_data')
        docs = os.path.join(package, 'docs')
        f = os.path.join(docs, '%s.md' % docname)
        import codecs
        data = codecs.open(f, encoding='utf-8').read()  # XXX
        data_str = data.encode('utf-8')
        html = render_markdown(data_str)
        html_u = unicode(html, 'utf-8')
        return {'contents': html_u}

    # This is where we keep all the URLS
    def get_lmv_url(self, library, model, view):
        url = '/libraries/%s/models/%s/views/%s/' % (library, model, view)
        return url

    def get_ltv_url(self, library, template, view):
        url = '/libraries/%s/templates/%s/views/%s/' % (library, template, view)
        return url

    def get_lpv_url(self, library, poset, view):
        url = '/libraries/%s/posets/%s/views/%s/' % (library, poset, view)
        return url

    def get_lib_template_view_url(self, library, template, view):
        url = '/libraries/%s/templates/%s/views/%s/' % (library, template, view)
        return url

    def get_model_name(self, request):
        model_name = str(request.matchdict['model_name'])  # unicod
        return model_name

    def get_current_view(self, request):
        url = request.url
        for x in self._get_views():
            if '/' + x + '/' in url:
                return x
            
        assert False, request.url

    def get_navigation_links(self, request):
        """ Pass this as "navigation" to the page. """
        current_thing = None

        if 'model_name' in request.matchdict:
            current_thing = current_model = self.get_model_name(request)
            current_view = self.get_current_view(request)
        else:
            current_model = None
            current_view = None

        if 'template_name' in request.matchdict:
            current_thing = current_template = str(request.matchdict['template_name'])
        else:
            current_template = None

        if 'poset_name' in request.matchdict:
            current_thing = current_poset = str(request.matchdict['poset_name'])
        else:
            current_poset = None

        if 'value_name' in request.matchdict:
            current_thing = current_value = str(request.matchdict['value_name'])
        else:
            current_value = None



        current_library = self.get_current_library_name(request)
        library = self.get_library(request)

        d = {}
        
        models = self.list_of_models(request)
        
        d['current_thing'] = current_thing
        d['current_library'] = current_library
        d['current_template'] = current_template
        d['current_poset'] = current_poset
        d['current_model'] = current_model

        d['current_view'] = current_view

        documents = library._list_with_extension(MCDPLibrary.ext_doc_md)

        d['documents'] = []
        for id_doc in documents:
            url = '/libraries/%s/%s.html' % (current_library, id_doc)
            desc = dict(id_document=id_doc, name=id_doc, url=url, current=False)
            d['documents'].append(desc)

        d['models'] = []
        
        for m in natural_sorted(models):
            is_current = m == current_model

            url = self.get_lmv_url(library=current_library,
                                   model=m,
                                   view='syntax')

            name = "Model: %s" % m
            desc = dict(id_ndp=m, name=name, url=url, current=is_current)
            d['models'].append(desc)


        templates = library.list_templates()
        d['templates'] = []
        for t in natural_sorted(templates):
            is_current = (t == current_template)

            url = self.get_lib_template_view_url(library=current_library,
                                                 template=t,
                                                 view='syntax')  # XXX

            name = "Template: %s" % t
            desc = dict(name=name, url=url, current=is_current)
            d['templates'].append(desc)

        posets = library.list_posets()
        d['posets'] = []
        for p in natural_sorted(posets):
            is_current = (p == current_poset)
            url = self.get_lpv_url(library=current_library,
                                   poset=p,
                                   view='syntax')
            name = "Poset: %s" % p
            desc = dict(name=name, url=url, current=is_current)
            d['posets'].append(desc)

        values = library.list_values()
        d['values'] = []
        for v in natural_sorted(values):
            is_current = (v == current_value)
            url = '/libraries/%s/values/%s/views/edit_fancy/' % (current_library, v)
            name = "Value: %s" % v
            desc = dict(name=name, url=url, current=is_current)
            d['values'].append(desc)


        d['views'] = []
        views = self._get_views()
        for v in views:
            view = self.views[v]
            is_current = v == current_view

            url = self.get_lmv_url(library=current_library,
                                   model=current_model,
                                   view=v)

            name = "View: %s" % view['desc']
            desc = dict(name=name, url=url, current=is_current)
            d['views'].append(desc)

        libraries = self.list_libraries()

        # just the list of names
        d['libraries'] = []
        libname2desc = {}
        for l in natural_sorted(libraries):
            is_current = l == current_library
            url = '/libraries/%s/' % l
            #name = "Library: %s" % l
            name = l
            desc = dict(name=name, url=url, current=is_current)
            libname2desc[l] =desc
            d['libraries'].append(desc)

        indexed = self.get_libraries_indexed_by_dir()
        indexed = [(sup, [libname2desc[_] for _ in l]) 
                   for sup, l in indexed]
        
        # for sup, libraries in libraries_indexed
        #   for l in libraries
        #      l['name'], l['url']
        d['libraries_indexed'] = indexed
        
        return d
    
    
    def get_libraries_indexed_by_dir(self):
        """
            returns a list of tuples (dirname, list(libname))
        """
        from collections import defaultdict
        path2libraries = defaultdict(lambda: [])
        for libname, data in self.libraries.items():
            path = data['path']
            sup = os.path.basename(os.path.dirname(path))
            path2libraries[sup].append(libname)
                     
        res = []
        for sup in natural_sorted(path2libraries):
            r = (sup, natural_sorted(path2libraries[sup]))
            res.append(r)
        return res
        

    def get_jinja_hooks(self, request):
        """ Returns a set of useful template functions. """
        d = {}
        d['version'] = lambda: mocdp.__version__
        d['render_library_doc'] = lambda docname: self._render_library_doc(request, docname)
        d['has_library_doc'] = lambda docname: self._has_library_doc(request, docname)
        return d

    def _has_library_doc(self, request, document):
        l = self.get_library(request)
        filename = '%s.%s' % (document, MCDPLibrary.ext_doc_md)
        return l.file_exists(filename)

    @contract(returns=str)
    def _render_library_doc(self, request, document):
        import codecs
        l = self.get_library(request)

        strict = int(request.params.get('strict', '0'))

        filename = '%s.%s' % (document, MCDPLibrary.ext_doc_md)
        f = l._get_file_data(filename)
        realpath = f['realpath']
        # read unicode
        data_unicode = codecs.open(realpath, encoding='utf-8').read()
        data_str = data_unicode.encode('utf-8')
        raise_errors = bool(strict)
        html = render_complete(library=l, s=data_str, realpath=realpath, raise_errors=raise_errors)
        return html

    def view_library_doc(self, request):
        """ '/libraries/{library}/{document}.html' """
        # f['data'] not utf-8
        # reopen as utf-8
        document = str(request.matchdict['document'])
        html = self._render_library_doc(request, document)
        # we work with utf-8 strings
        assert isinstance(html, str)
        # but we need to convert to unicode later
        html = unicode(html, 'utf-8')
        res = {}
        res['contents'] = html
        res['title'] = document
        res['navigation'] = self.get_navigation_links(request)
        res['print'] = bool(request.params.get('print', False))
        res.update(self.get_jinja_hooks(request))
        return res

    def view_library_asset(self, request):
        l = self.get_library(request)
        asset = str(request.matchdict['asset'])
        ext = str(request.matchdict['ext'])
        filename = '%s.%s' % (asset, ext)
        f = l._get_file_data(filename)
        data = f['data']
        content_type = get_mime_for_format(ext)
        from mcdp_web.utils.response import response_data
        return response_data(request, data, content_type)

    def exit(self, request):  # @UnusedVariable
        setattr(self.server, '_BaseServer__shutdown_request', True)
        return {}

    def serve(self, port):
        config = Configurator()
        config.add_static_view(name='static', path='static')
        config.include('pyramid_jinja2')

        AppEditor.config(self, config)
        AppVisualization.config(self, config)
        AppQR.config(self, config)
        AppSolver.config(self, config)
        AppInteractive.config(self, config)
        AppEditorFancyGeneric.config(self, config)
        WebAppImages.config(self, config)

        AppSolver2.config(self, config)

        config.add_route('index', '/')
        config.add_view(self.view_index, route_name='index', renderer='index.jinja2')

        config.add_route('list_libraries', '/libraries/')
        config.add_view(self.view_list_libraries, route_name='list_libraries', renderer='list_libraries.jinja2')

        config.add_route('library_index', '/libraries/{library}/')
        config.add_view(self.view_list, route_name='library_index', renderer='library_index.jinja2')

        config.add_route('library_doc', '/libraries/{library}/{document}.html')
        config.add_view(self.view_library_doc,
                        route_name='library_doc',
                        renderer='library_doc.jinja2')

        config.add_route('library_asset', '/libraries/{library}/{asset}.{ext}')
        config.add_view(self.view_library_asset, route_name='library_asset')

        config.add_route('docs', '/docs/{document}/')
        config.add_view(self.view_docs, route_name='docs', renderer='language.jinja2')

        config.add_route('empty', '/empty')
        config.add_view(self.view_index, route_name='empty', renderer='empty.jinja2')

        config.add_route('refresh_library', '/libraries/{library}/refresh_library')
        config.add_view(self.view_refresh_library, route_name='refresh_library')

        config.add_view(self.view_exception, context=Exception, renderer='exception.jinja2')

        config.add_route('exit', '/exit')
        config.add_view(self.exit, route_name='exit', renderer='empty.jinja2')

        config.add_route('exceptions', '/exceptions')
        config.add_view(self.view_exceptions_occurred, route_name='exceptions', renderer='json')
        config.add_route('exceptions_formatted', '/exceptions_formatted')
        config.add_view(self.view_exceptions_occurred, route_name='exceptions_formatted', renderer='exceptions_formatted.jinja2')

        # mainly used for wget
        config.add_route('robots', '/robots.txt')
        def serve_robots(request):  # @UnusedVariable
            body = "User-agent: *\nDisallow:"
            return Response(content_type='text/plain', body=body)

        config.add_view(serve_robots, route_name='robots')

        config.add_notfound_view(self.view_not_found, renderer='404.jinja2')

        app = config.make_wsgi_app()
        self.server = make_server('0.0.0.0', port, app)
        self.server.serve_forever()
Exemple #28
0
def solve_main(
    logger,
    config_dirs,
    maindir,
    cache_dir,
    model_name,
    lower,
    upper,
    out_dir,
    max_steps,
    query_strings,
    intervals,
    _exp_advanced,
    expect_nres,
    imp,
    expect_nimp,
    plot,
    do_movie,
    # expect_res=None,
    expect_res,  # @UnusedVariable
    make,
):

    if out_dir is None:
        out = solve_get_output_dir(prefix="out/out")
    else:
        out = out_dir

    logger.info("Using output dir %r" % out)

    librarian = Librarian()
    logger.info("Looking for libraries in %s..." % config_dirs)
    for e in config_dirs:
        librarian.find_libraries(e)
    logger.info("Found %d libraries." % len(librarian.get_libraries()))

    library = librarian.get_library_by_dir(maindir)
    if cache_dir is not None:
        library.use_cache_dir(cache_dir)

    ndp = library.load_ndp(model_name)
    basename = model_name

    if make or (plot and imp):
        ndp_labelled = get_labelled_version(ndp)
    else:
        ndp_labelled = ndp

    basename, dp = solve_get_dp_from_ndp(basename=basename, ndp=ndp_labelled, lower=lower, upper=upper)

    F = dp.get_fun_space()
    R = dp.get_res_space()
    UR = UpperSets(R)

    query = " ".join(query_strings)
    c = library.parse_constant(query)
    tu = get_types_universe()
    try:
        tu.check_leq(c.unit, F)
    except NotLeq as e:
        msg = "The value given cannot be converted to functionality space."
        raise_wrapped(UserError, e, msg, unit=c.unit, F=F, compact=True)
    fg = express_value_in_isomorphic_space(c.unit, c.value, F)

    logger.info("query: %s" % F.format(fg))

    tracer = Tracer(logger=logger)
    res, trace = solve_meat_solve(tracer, ndp, dp, fg, intervals, max_steps, _exp_advanced)

    nres = len(res.minimals)

    if expect_nres is not None:
        if nres != expect_nres:
            msg = "Found wrong number of resources"
            raise_desc(ExpectationsNotMet, msg, expect_nres=expect_nres, nres=nres)

    if imp:
        M = dp.get_imp_space()
        nimplementations = 0
        for r in res.minimals:
            ms = dp.get_implementations_f_r(fg, r)
            nimplementations += len(ms)
            s = "r = %s " % R.format(r)
            for j, m in enumerate(ms):
                # print('m = %s' % str(m))
                s += "\n  implementation %d of %d: m = %s " % (j + 1, len(ms), M.format(m))

                if make:
                    imp_dict = get_imp_as_recursive_dict(M, m)  # , ignore_hidden=False)
                    print("imp dict: %r" % imp_dict)
                    context = {}
                    artifact = ndp_make(ndp, imp_dict, context)

                    print("artifact: %s" % artifact)

            tracer.log(s)

        if expect_nimp is not None:
            if expect_nimp != nimplementations:
                msg = "Found wrong number of implementations"
                raise_desc(ExpectationsNotMet, msg, expect_nimp=expect_nimp, nimplementations=nimplementations)

    #     if expect_res is not None:
    #         value = interpret_string(expect_res)
    #         tracer.log('value: %s' % value)
    #         res_expected = value.value
    #         tu = get_types_universe()
    #         # If it's a tuple of two elements, then we assume it's upper/lower bounds
    #         if isinstance(value.unit, PosetProduct):
    #             subs = value.unit.subs
    #             assert len(subs) == 2, subs
    #
    #             lower_UR_expected, upper_UR_expected = subs
    #             lower_res_expected, upper_res_expected = value.value
    #
    #             lower_bound = tu.get_embedding(lower_UR_expected, UR)[0](lower_res_expected)
    #             upper_bound = tu.get_embedding(upper_UR_expected, UR)[0](upper_res_expected)
    #
    #             tracer.log('lower: %s <= %s' % (UR.format(lower_bound), UR.format(res)))
    #             tracer.log('upper: %s <= %s' % (UR.format(upper_bound), UR.format(res)))
    #
    #             UR.check_leq(lower_bound, res)
    #             UR.check_leq(res, upper_bound)
    #         else:
    #             # only one element: equality
    #             UR_expected = value.unit
    #             tu.check_leq(UR_expected, UR)
    #             A_to_B, _B_to_A = tu.get_embedding(UR_expected, UR)
    #
    #             res_expected_f = A_to_B(res_expected)
    #             try:
    #                 UR.check_equal(res, res_expected_f)
    #             except NotEqual as e:
    #                 raise_wrapped(ExpectationsNotMet, e, 'res is different',
    #                               res=res, res_expected=res_expected, compact=True)

    if plot:
        r = Report()
        if _exp_advanced:
            from mcdp_report.generic_report_utils import generic_report

            generic_report(r, dp, trace, annotation=None, axis0=(0, 0, 0, 0))
        else:
            f = r.figure()
            from mcdp_report.generic_report_utils import generic_plot

            generic_plot(f, space=UR, value=res)
            from mcdp_report.generic_report_utils import generic_report_trace

            generic_report_trace(r, ndp, dp, trace, out, do_movie=do_movie)

        out_html = os.path.join(out, "report.html")
        logger.info("writing to %r" % out_html)
        r.to_html(out_html)

    if plot and imp:
        from mcdp_report_ndp_tests.test1 import GetValues
        from mcdp_report.gg_ndp import gvgen_from_ndp
        from mcdp_report.gdc import STYLE_GREENREDSYM
        from mcdp_report.gg_utils import gg_figure

        M = dp.get_imp_space()

        report_solutions = Report()
        for i, r in enumerate(res.minimals):
            ms = dp.get_implementations_f_r(fg, r)
            for j, m in enumerate(ms):

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

                setattr(ndp, "_hack_force_enclose", True)

                with report_solutions.subsection("sol-%s-%s" % (i, j)) as rr:
                    # Left right
                    gg = gvgen_from_ndp(
                        ndp=ndp, style=STYLE_GREENREDSYM, images_paths=images_paths, plotting_info=gv, direction="LR"
                    )

                    gg_figure(rr, "figure", gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False)

                    # Top-bottom
                    gg = gvgen_from_ndp(
                        ndp=ndp, style=STYLE_GREENREDSYM, images_paths=images_paths, plotting_info=gv, direction="TB"
                    )

                    gg_figure(rr, "figure2", gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False)

        out_html = os.path.join(out, "report_solutions.html")
        logger.info("writing to %r" % out_html)
        report_solutions.to_html(out_html)
Exemple #29
0
def other_reports(outdir, maindir, config_dirs, entry):
    print('config dirs: {}'.format(config_dirs))
    librarian = Librarian()
    for e in config_dirs:
        librarian.find_libraries(e)

    default_library = librarian.get_library_by_dir(maindir)

    tex = ''

    library = default_library.load_library(entry.libname)

    if isinstance(entry, EntryTemplate):
        context = library._generate_context_with_hooks()
        template = library.load_spec(SPEC_TEMPLATES, entry.name, context)
        pdf = ndp_template_graph_enclosed(library=library,
                                          template=template,
                                          style=STYLE_GREENREDSYM,
                                          yourname=None,
                                          data_format='pdf',
                                          direction='TB',
                                          enclosed=True)
        base = entry.libname + '-' + entry.name + '-ndp_template_graph_enclosed.pdf'
        out = os.path.join(outdir, base)
        write_to_file(out, pdf)
        tex += '\n\\includegraphics{%s}' % base

        source_code = library._get_file_data(entry.name +
                                             '.mcdp_template')['data']
        code_pdf = get_ast_as_pdf(s=source_code, parse_expr=Syntax.template)

        base = entry.libname + '-' + entry.name + '-syntax_pdf.pdf'
        out = os.path.join(outdir, base)
        write_to_file(out, code_pdf)
        tex += '\n\\includegraphics{%s}' % base

    if isinstance(entry, EntryNDP):

        filename = entry.name + '.mcdp'
        x = library._get_file_data(filename)
        data = {}
        data['model_name'] = entry.name
        data['s'] = x['data'].strip()
        data['filename'] = x['realpath']
        data['params'] = {}
        data['library'] = library

        d = dict(allplots)
        plots = list(d.keys())
        plots.remove('ndp_clean')
        plots.remove('ndp_default')
        plots.remove('ndp_greenred')
        plots.remove('dp_graph_tree_compact_labeled')
        #         plots.remove('dp_graph_compact_labeled')

        for p in plots:
            # print('plotting %r ' % p)
            try:
                res = d[p](data)
            except DPSemanticError as e:
                logger.error(str(e))
                continue
            except CmdException as e:
                logger.error(str(e))
                continue
            assert isinstance(res, list), res
            for r in res:
                assert isinstance(r, tuple), r
                mime, name, x = r
                assert isinstance(x, str), x
                ext = mime

                base = entry.libname + '-' + entry.name + '-%s.%s' % (name,
                                                                      ext)
                out = os.path.join(outdir, base)
                write_to_file(out, x)
                if ext == 'pdf':
                    tex += '\n\\includegraphics{%s}' % base

    print('outdir: %s' % outdir)
    print('entry: {}'.format(entry))

    return tex
Exemple #30
0
def solve_main(
        logger,
        config_dirs,
        maindir,
        cache_dir,
        model_name,
        lower,
        upper,
        out_dir,
        max_steps,
        query_strings,
        intervals,
        _exp_advanced,
        expect_nres,
        imp,
        expect_nimp,
        plot,
        do_movie,

        # expect_res=None,
        expect_res,  # @UnusedVariable
        make):

    if out_dir is None:
        out = solve_get_output_dir(prefix='out/out')
    else:
        out = out_dir

    logger.info('Using output dir %r' % out)

    librarian = Librarian()
    logger.info('Looking for libraries in %s...' % config_dirs)
    for e in config_dirs:
        librarian.find_libraries(e)
    logger.info('Found %d libraries.' % len(librarian.get_libraries()))

    library = librarian.get_library_by_dir(maindir)
    if cache_dir is not None:
        library.use_cache_dir(cache_dir)

    ndp = library.load_ndp(model_name)
    basename = model_name

    if make or (plot and imp):
        ndp_labelled = get_labelled_version(ndp)
    else:
        ndp_labelled = ndp

    basename, dp = solve_get_dp_from_ndp(basename=basename,
                                         ndp=ndp_labelled,
                                         lower=lower,
                                         upper=upper)

    F = dp.get_fun_space()
    R = dp.get_res_space()
    UR = UpperSets(R)

    query = " ".join(query_strings)
    c = library.parse_constant(query)
    tu = get_types_universe()
    try:
        tu.check_leq(c.unit, F)
    except NotLeq as e:
        msg = 'The value given cannot be converted to functionality space.'
        raise_wrapped(UserError, e, msg, unit=c.unit, F=F, compact=True)
    fg = express_value_in_isomorphic_space(c.unit, c.value, F)

    logger.info('query: %s' % F.format(fg))

    tracer = Tracer(logger=logger)
    res, trace = solve_meat_solve_ftor(tracer, ndp, dp, fg, intervals,
                                       max_steps, _exp_advanced)

    nres = len(res.minimals)

    if expect_nres is not None:
        if nres != expect_nres:
            msg = 'Found wrong number of resources'
            raise_desc(ExpectationsNotMet,
                       msg,
                       expect_nres=expect_nres,
                       nres=nres)

    if imp:
        M = dp.get_imp_space()
        nimplementations = 0
        for r in res.minimals:
            ms = dp.get_implementations_f_r(fg, r)
            nimplementations += len(ms)
            s = 'r = %s ' % R.format(r)
            for j, m in enumerate(ms):
                # print('m = %s' % str(m))
                s += "\n  implementation %d of %d: m = %s " % (j + 1, len(ms),
                                                               M.format(m))

                if make:
                    imp_dict = get_imp_as_recursive_dict(
                        M, m)  # , ignore_hidden=False)
                    print('imp dict: %r' % imp_dict)
                    context = {}
                    artifact = ndp_make(ndp, imp_dict, context)

                    print('artifact: %s' % artifact)

            tracer.log(s)

        if expect_nimp is not None:
            if expect_nimp != nimplementations:
                msg = 'Found wrong number of implementations'
                raise_desc(ExpectationsNotMet,
                           msg,
                           expect_nimp=expect_nimp,
                           nimplementations=nimplementations)


#     if expect_res is not None:
#         value = interpret_string(expect_res)
#         tracer.log('value: %s' % value)
#         res_expected = value.value
#         tu = get_types_universe()
#         # If it's a tuple of two elements, then we assume it's upper/lower bounds
#         if isinstance(value.unit, PosetProduct):
#             subs = value.unit.subs
#             assert len(subs) == 2, subs
#
#             lower_UR_expected, upper_UR_expected = subs
#             lower_res_expected, upper_res_expected = value.value
#
#             lower_bound = tu.get_embedding(lower_UR_expected, UR)[0](lower_res_expected)
#             upper_bound = tu.get_embedding(upper_UR_expected, UR)[0](upper_res_expected)
#
#             tracer.log('lower: %s <= %s' % (UR.format(lower_bound), UR.format(res)))
#             tracer.log('upper: %s <= %s' % (UR.format(upper_bound), UR.format(res)))
#
#             UR.check_leq(lower_bound, res)
#             UR.check_leq(res, upper_bound)
#         else:
#             # only one element: equality
#             UR_expected = value.unit
#             tu.check_leq(UR_expected, UR)
#             A_to_B, _B_to_A = tu.get_embedding(UR_expected, UR)
#
#             res_expected_f = A_to_B(res_expected)
#             try:
#                 UR.check_equal(res, res_expected_f)
#             except NotEqual as e:
#                 raise_wrapped(ExpectationsNotMet, e, 'res is different',
#                               res=res, res_expected=res_expected, compact=True)

    if plot:
        r = Report()
        if _exp_advanced:
            from mcdp_report.generic_report_utils import generic_report
            generic_report(r, dp, trace, annotation=None, axis0=(0, 0, 0, 0))
        else:
            f = r.figure()
            from mcdp_report.generic_report_utils import generic_plot
            generic_plot(f, space=UR, value=res)
            from mcdp_report.generic_report_utils import generic_report_trace
            generic_report_trace(r, ndp, dp, trace, out, do_movie=do_movie)

        out_html = os.path.join(out, 'report.html')
        logger.info('writing to %r' % out_html)
        r.to_html(out_html)

    if plot and imp:
        from mcdp_report_ndp_tests.test1 import GetValues
        from mcdp_report.gg_ndp import gvgen_from_ndp
        from mcdp_report.gdc import STYLE_GREENREDSYM
        from mcdp_report.gg_utils import gg_figure
        M = dp.get_imp_space()

        report_solutions = Report()
        for i, r in enumerate(res.minimals):
            ms = dp.get_implementations_f_r(fg, r)
            for j, m in enumerate(ms):

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

                setattr(ndp, '_hack_force_enclose', True)

                with report_solutions.subsection('sol-%s-%s' % (i, j)) as rr:
                    # Left right
                    gg = gvgen_from_ndp(ndp=ndp,
                                        style=STYLE_GREENREDSYM,
                                        image_source=image_source,
                                        plotting_info=gv,
                                        direction='LR')

                    gg_figure(rr,
                              'figure',
                              gg,
                              do_png=True,
                              do_pdf=True,
                              do_svg=False,
                              do_dot=False)

                    # Top-bottom
                    gg = gvgen_from_ndp(ndp=ndp,
                                        style=STYLE_GREENREDSYM,
                                        image_source=image_source,
                                        plotting_info=gv,
                                        direction='TB')

                    gg_figure(rr,
                              'figure2',
                              gg,
                              do_png=True,
                              do_pdf=True,
                              do_svg=False,
                              do_dot=False)

        out_html = os.path.join(out, 'report_solutions.html')
        logger.info('writing to %r' % out_html)
        report_solutions.to_html(out_html)
Exemple #31
0
    def go(self):
        logger.setLevel(logging.DEBUG)

        options = self.get_options()

        symbols = self.options.symbols
        if symbols is not None:
            symbols = open(symbols).read()
        else:
            symbols = ''

        if not options.contracts:
            disable_all()

        stylesheet = options.stylesheet
        # make sure it exists
        get_css_filename('compiled/%s' % stylesheet)

        params = options.get_extra()

        if len(params) < 1:
            raise ValueError('Please specify name.')

        config_dirs = options.config_dirs.split(":")
        maindir = options.maindir
        out_dir = options.out

        if options.cache:
            cache_dir = os.path.join(out_dir, '_cached', 'solve')
        else:
            cache_dir = None

        librarian = Librarian()
        for e in config_dirs:
            librarian.find_libraries(e)

        library = librarian.get_library_by_dir(maindir)
        if cache_dir is not None:
            library.use_cache_dir(cache_dir)

        docs = params

        if not docs:
            msg = 'At least one argument required.'
            raise_desc(UserError, msg)

        for docname in docs:
            if '/' in docname:
                docname0 = os.path.split(docname)[-1]
                logger.info("Using %r rather than %r" % (docname0, docname))
                docname = docname0
            suffix = '.' + MCDPConstants.ext_doc_md
            if docname.endswith(suffix):
                docname = docname.replace(suffix, '')
            basename = docname + suffix
            f = library._get_file_data(basename)
            data = f['data']
            realpath = f['realpath']

            generate_pdf = options.pdf_figures
            if out_dir is None:
                use_out_dir = os.path.dirname(realpath)
            else:
                use_out_dir = os.path.join('out', 'mcdp_render')

            raise_errors = not options.forgiving
            use_mathjax = bool(options.mathjax)

            html_filename = render(library,
                                   docname,
                                   data,
                                   realpath,
                                   use_out_dir,
                                   generate_pdf,
                                   stylesheet=stylesheet,
                                   symbols=symbols,
                                   raise_errors=raise_errors,
                                   use_mathjax=use_mathjax)
            if options.pdf:
                run_prince(html_filename)
Exemple #32
0
    def recompute_available(self):
        # all the ones we can discover
        repos = self.app.hi.db_view.repos
        for repo_name, repo in repos.items():
            for shelf_name, shelf in repo.shelves.items():
                self.shelves_all[shelf_name] = shelf
        # shelf:repo/shelfname
        # library:repo/shelf/library
        self.shelves_available = OrderedDict()

        # the ones that are actually in use
        self.shelves_used = OrderedDict()
        userstruct = self.get_user_struct()
        ui = userstruct.info
        for sname, shelf in self.shelves_all.items():
            if shelf.get_acl().allowed2(Privileges.DISCOVER, ui):
                self.shelves_available[sname] = shelf
            else:
                #print('hiding shelf %r from %r' % (sname, user))
                print shelf.get_acl()

        #print('shelves all: %s' % list(self.shelves_all))
        #print('shelves available: %s' % list(self.shelves_available))

        for sname in ui.get_subscriptions():
            if sname in self.shelves_available:
                shelf = self.shelves_available[sname]
                acl = shelf.get_acl()
                if acl.allowed2(Privileges.READ, ui):
                    self.shelves_used[sname] = self.shelves_available[sname]
                else:
                    msg = 'User %r does not have %r for %r' % (
                        ui.username, Privileges.READ, sname)
                    msg += '\n%s' % acl
                    logger.error(msg)
            else:
                msg = 'Could not find shelf %r to which user %r is subscribed to.' % (
                    sname, ui.username)
                msg += '\n Available: %s' % list(self.shelves_available)
                logger.error(msg)

        #print('shelves used: %s' % list(self.shelves_used))

        self.librarian = Librarian()

        self.libname2shelfname = {}
        self.shelfname2reponame = {}
        repos = self.app.hi.db_view.repos
        for repo_name, repo in repos.items():
            for shelf_name, shelf in repo.shelves.items():
                if shelf_name in self.shelfname2reponame:
                    o = self.shelfname2reponame[shelf_name]
                    msg = ('Two repos with same shelf %r: %r and %r.' %
                           (shelf_name, repo_name, o))

                    for r in [o, repo_name]:
                        msg += '\n Shelves for %r: %s' % (
                            r, format_list(sorted(self.repos[r].shelves)))

                    raise ValueError(msg)
                self.shelfname2reponame[shelf_name] = repo_name

        for sname, shelf in self.shelves_all.items():
            for libname in shelf.libraries:
                self.libname2shelfname[libname] = sname

        for sname, shelf in self.shelves_used.items():
            if False:  #
                logger.error('need to reimplement this part')
                for libname, libpath in shelf.get_libraries_path().items():
                    self.librarian.add_lib_by_path(libpath)

        self.libraries = self.librarian.get_libraries()
        for _short, data in self.libraries.items():
            l = data['library']
            path = data['path']
            cache_dir = os.path.join(path, '_cached/mcdpweb_cache')
            l.use_cache_dir(cache_dir)
Exemple #33
0
def other_reports(outdir, maindir, config_dirs, entry):
    print('config dirs: {}'.format(config_dirs))
    librarian = Librarian()
    for e in config_dirs:
        librarian.find_libraries(e)

    default_library = librarian.get_library_by_dir(maindir)

    tex = ''

    library = default_library.load_library(entry.libname)

    if isinstance(entry, EntryTemplate):
        context = library._generate_context_with_hooks()
        template = library.load_template(entry.name, context)
        pdf = ndp_template_graph_enclosed(library=library, template=template,
                                    style=STYLE_GREENREDSYM, yourname=None,
                                    data_format='pdf', direction='TB', enclosed=True)
        base = entry.libname + '-' + entry.name + '-ndp_template_graph_enclosed.pdf'
        out = os.path.join(outdir, base)
        write_to_file(out, pdf)
        tex += '\n\\includegraphics{%s}' % base

        source_code = library._get_file_data(entry.name +'.mcdp_template')['data']
        code_pdf = get_ast_as_pdf(s=source_code, parse_expr=Syntax.template)

        base = entry.libname + '-' + entry.name + '-syntax_pdf.pdf'
        out = os.path.join(outdir, base)
        write_to_file(out, code_pdf)
        tex += '\n\\includegraphics{%s}' % base

    if isinstance(entry, EntryNDP):
        
        
        filename = entry.name + '.mcdp'
        x = library._get_file_data(filename)
        data = {}
        data['model_name'] = entry.name
        data['s'] = x['data'].strip()
        data['filename'] = x['realpath']
        data['params'] = {}
        data['library'] = library

        d = dict(allplots)
        plots = list(d.keys())
        plots.remove('ndp_clean')
        plots.remove('ndp_default')
        plots.remove('ndp_greenred')
        plots.remove('dp_graph_tree_compact_labeled')
#         plots.remove('dp_graph_compact_labeled')


        for p in plots:
            # print('plotting %r ' % p)
            try:
                res = d[p](data)
            except DPSemanticError as e:
                print e
                continue
            except CmdException as e:
                print e
                continue
            assert isinstance(res, list), res
            for r in res:
                assert isinstance(r, tuple), r
                mime, name, x = r
                assert isinstance(x, str), x
                ext = mime

                base = entry.libname + '-' + entry.name + '-%s.%s' % (name, ext)
                out = os.path.join(outdir, base)
                write_to_file(out, x)
                if ext == 'pdf':
                    tex += '\n\\includegraphics{%s}' % base


    print('outdir: %s' % outdir)
    print('entry: {}'.format(entry))

    return tex
Exemple #34
0
class Session(object):
    def __init__(self, app, request):
        ''' dirnames: list of directories where to find shelves '''
        self.app = app
        self.shelves_all = {}
        self.request = request

        self.librarian = Librarian()
        self.authenticated_userid = request.authenticated_userid

        # used during auth procedure
        self.candidate_user = None
        self.soft_match = None
        self.next_location = None

        self.recompute_available()

    def set_last_request(self, request):
        self.request = request
        if self.authenticated_userid != request.authenticated_userid:
            logger.debug('login/logout detected')
            self.authenticated_userid = request.authenticated_userid
            self.recompute_available()

    def _get_app(self):
        from mcdp_web.main import WebApp
        return WebApp.singleton

#     @contract(returns='isinstance(UserInfo)')

    def get_user_struct(self, username=None):
        ''' 
        
             It is the user 'anonymous' if no login was given.
        
            self.request.authenticated_userid == None == get_user().info.username == 'anonymous'
        '''
        app = self.app
        user_db = app.hi.db_view.user_db

        if username is None:
            username = self.request.authenticated_userid
            if username is not None and not username in user_db:
                logger.error('User appears to have not existent id "%s".' %
                             username)
                username = None

        if username is not None:
            username = username.encode('utf8')
            if username in user_db:
                return user_db.users[username]
            else:
                schema = DB.user
                data = schema.generate_empty(
                    info=dict(username=username, name=username))

                view = DB.view_manager.create_view_instance(schema, data)
                view.set_root()
                return view
        else:
            # username is None:
            return user_db.users[MCDPConstants.USER_ANONYMOUS]

    def notify_created_library(self, shelf_name,
                               library_name):  # @UnusedVariable
        ''' Called when we just created the library. '''
        self.get_shelf(shelf_name).update_libraries()
        self.recompute_available()

    @contract(returns='seq(str)')
    def get_subscribed_shelves(self):
        return list(self.shelves_used)

    def recompute_available(self):
        # all the ones we can discover
        repos = self.app.hi.db_view.repos
        for repo_name, repo in repos.items():
            for shelf_name, shelf in repo.shelves.items():
                self.shelves_all[shelf_name] = shelf
        # shelf:repo/shelfname
        # library:repo/shelf/library
        self.shelves_available = OrderedDict()

        # the ones that are actually in use
        self.shelves_used = OrderedDict()
        userstruct = self.get_user_struct()
        ui = userstruct.info
        for sname, shelf in self.shelves_all.items():
            if shelf.get_acl().allowed2(Privileges.DISCOVER, ui):
                self.shelves_available[sname] = shelf
            else:
                #print('hiding shelf %r from %r' % (sname, user))
                print shelf.get_acl()

        #print('shelves all: %s' % list(self.shelves_all))
        #print('shelves available: %s' % list(self.shelves_available))

        for sname in ui.get_subscriptions():
            if sname in self.shelves_available:
                shelf = self.shelves_available[sname]
                acl = shelf.get_acl()
                if acl.allowed2(Privileges.READ, ui):
                    self.shelves_used[sname] = self.shelves_available[sname]
                else:
                    msg = 'User %r does not have %r for %r' % (
                        ui.username, Privileges.READ, sname)
                    msg += '\n%s' % acl
                    logger.error(msg)
            else:
                msg = 'Could not find shelf %r to which user %r is subscribed to.' % (
                    sname, ui.username)
                msg += '\n Available: %s' % list(self.shelves_available)
                logger.error(msg)

        #print('shelves used: %s' % list(self.shelves_used))

        self.librarian = Librarian()

        self.libname2shelfname = {}
        self.shelfname2reponame = {}
        repos = self.app.hi.db_view.repos
        for repo_name, repo in repos.items():
            for shelf_name, shelf in repo.shelves.items():
                if shelf_name in self.shelfname2reponame:
                    o = self.shelfname2reponame[shelf_name]
                    msg = ('Two repos with same shelf %r: %r and %r.' %
                           (shelf_name, repo_name, o))

                    for r in [o, repo_name]:
                        msg += '\n Shelves for %r: %s' % (
                            r, format_list(sorted(self.repos[r].shelves)))

                    raise ValueError(msg)
                self.shelfname2reponame[shelf_name] = repo_name

        for sname, shelf in self.shelves_all.items():
            for libname in shelf.libraries:
                self.libname2shelfname[libname] = sname

        for sname, shelf in self.shelves_used.items():
            if False:  #
                logger.error('need to reimplement this part')
                for libname, libpath in shelf.get_libraries_path().items():
                    self.librarian.add_lib_by_path(libpath)

        self.libraries = self.librarian.get_libraries()
        for _short, data in self.libraries.items():
            l = data['library']
            path = data['path']
            cache_dir = os.path.join(path, '_cached/mcdpweb_cache')
            l.use_cache_dir(cache_dir)

    def get_repo_shelf_for_libname(self, libname):
        sname = self.get_shelf_for_libname(libname)
        rname = self.shelfname2reponame[sname]
        return rname, sname

    def get_shelf_for_libname(self, libname):
        ''' Returns the name of the shelf for the given libname. '''
        if not libname in self.libname2shelfname:
            msg = 'Could not find library %r.' % libname
            msg += '\n Available: %s' % sorted(self.libname2shelfname)
            raise NoSuchLibrary(msg)
        return self.libname2shelfname[libname]

    def get_shelf(self, shelfname):
        return self.shelves_all[shelfname]

    @contract(returns='list(str)')
    def list_libraries(self):
        """ Returns the list of libraries """
        return sorted(self.libraries)

#     def get_library(self, library_name):
#         if not library_name in self.libraries:
#             msg = 'Could not find library %r.' % library_name
#             msg += '\n available: ' + format_list(sorted(self.libraries)) + '.'
#             raise_desc(ValueError, msg)
#         return self.libraries[library_name]['library']

    def refresh_libraries(self):
        for l in [_['library'] for _ in self.libraries.values()]:
            l.delete_cache()

        from mcdp_report.gdc import get_images
        assert hasattr(get_images, 'cache')  # in case it changes later
        get_images.cache = {}

#     def get_libraries_indexed_by_shelf(self):
#         """
#             returns a list of tuples (dirname, list(libname))
#         """
#         res = []
#         for sname, shelf in self.shelves_used.items():
#             libnames = natural_sorted(shelf.libraries)
#             res.append((sname, libnames))
#         return res

    def get_shelves_used(self):
        ''' Returns an ordered dict of shelves '''
        return self.shelves_used

    def get_shelves_available(self):
        ''' Returns an ordered dict of shelves '''
        return self.shelves_available