Ejemplo n.º 1
0
def sum_units(Fs, values, R):
    for Fi in Fs:
        check_isinstance(Fi, RcompUnits)
    res = 0.0
    for Fi, x in zip(Fs, values):
        if is_top(Fi, x):
            return R.get_top()

        # reasonably sure this is correct...
        try:
            factor = 1.0 / float(R.units / Fi.units)
        except Exception as e:  # pragma: no cover (DimensionalityError)
            raise_wrapped(Exception, e, 'some error', Fs=Fs, R=R)

        try:
            res += factor * x
        except FloatingPointError as e:
            if 'overflow' in str(e):
                res = np.inf
                break
            else:
                raise

    if np.isinf(res):
        return R.get_top()

    return res
Ejemplo n.º 2
0
    def __init__(self, dps):
        check_isinstance(dps, tuple)
        tu = get_types_universe()

        F1 = dps[0].get_fun_space()
        R1 = dps[0].get_res_space()

        for dp in dps:
            Fj = dp.get_fun_space()
            Rj = dp.get_res_space()

            try:
                tu.check_equal(F1, Fj)
                tu.check_equal(R1, Rj)
            except NotEqual:
                msg = 'Cannot form the co-product.'
                raise_desc(ValueError, msg, dps=dps)

        F = F1
        R = R1
        Ms = [dp.get_imp_space() for dp in dps]

        self.dps = dps
        self.M = Coproduct1(tuple(Ms))
        PrimitiveDP.__init__(self, F=F, R=R, I=self.M)
Ejemplo n.º 3
0
def definition_closure(jobs, db):
    """ The result does not contain jobs (unless one job defines another) """
    #print('definition_closure(%s)' % jobs)
    check_isinstance(jobs, (list, set))
    jobs = set(jobs)
    from compmake.jobs.uptodate import CacheQueryDB
    cq = CacheQueryDB(db)
    stack = set(jobs)
    result = set()
    while stack:
        #print('stack: %s' % stack)
        a = stack.pop()
        if not cq.job_exists(a):
            print('Warning: job %r does not exist anymore; ignoring.' % a)
            continue

        if cq.get_job_cache(a).state == Cache.DONE:
            a_d = cq.jobs_defined(a)
            #print('%s ->%s' % (a, a_d))
            for x in a_d:
                result.add(x)
                stack.add(x)

    #print('  result = %s' % result)
    return result
Ejemplo n.º 4
0
def get_function_possibly_converted(cf, P, context):
    """ Returns a resource possibly converted to the space P """
    check_isinstance(cf, CFunction)

    F = context.get_ftype(cf)
    tu = get_types_universe()
    
    if tu.equal(F, P):
        return cf
    else:
        try:
            tu.check_leq(P, F)
        except NotLeq as e:
            msg = 'Cannot convert %s to %s.' % (P, F)
            raise_wrapped(DPSemanticError, e, msg,P=P, F=F)

        conversion = get_conversion(P, F)
        if conversion is None:
            return cf
        else:
            cf2 = create_operation_lf(context, dp=conversion, 
                                      functions=[cf], 
                                      name_prefix='_conv_gfpc', 
                                      op_prefix='_op', res_prefix='_res')
            return cf2
Ejemplo n.º 5
0
def mult_table(a, b):
    check_isinstance(a, RcompUnits)
    check_isinstance(b, RcompUnits)

    unit2 = a.units * b.units
    s = ('%s' % unit2).encode('utf-8')
    return RcompUnits(unit2, s)
Ejemplo n.º 6
0
def embed_img_data(soup,
                   resolve,
                   img_extensions=['png', 'jpg', 'PNG', 'JPG', 'svg', 'SVG']):
    """
        resolve: ref -> str  or None --- how to get the data
    """

    for tag in soup.select('img[src]'):
        href = tag['src']
        if href.startswith('data:'):
            continue

        for ext in img_extensions:

            if not href.endswith('.' + ext):
                continue

            data = resolve(href)
            if data is None:
                logger.error('embed_img_data: Could not find file %s' % href)
                continue

            check_isinstance(data, str)
            tag['src'] = data_encoded_for_src(data, ext)
            break
Ejemplo n.º 7
0
    def from_yaml(data):
        """
            input:
            recipe:
            output:
        """
        check_isinstance(data, dict)
        data = copy.deepcopy(data)

        input_ = data.pop('input')
        output = data.pop('output')
        recipe = data.pop('recipe')
        purl_prefix = data.pop('purl_prefix')
        remove_status = data.pop('remove_status', [])
        show_removed = data.pop('show_removed', True)
        if not isinstance(remove_status, list):
            msg = 'I expected that remove_status was a list; found %r.' % remove_status
            raise ValueError(msg)

        recipe = Recipe.from_yaml(recipe)

        if data:
            msg = 'Spurious fields %s' % list(data)
            raise ValueError(msg)

        return ComposeConfig(recipe, input_, output, purl_prefix,
                             remove_status, show_removed)
Ejemplo n.º 8
0
def eval_codespec(r, expect):
    assert isinstance(r, (CDP.CodeSpecNoArgs, CDP.CodeSpec))

    function = r.function.value

    if isinstance(r, CDP.CodeSpec):
        _args, kwargs = eval_arguments(r.arguments)
    else:
        kwargs = {}
    check_isinstance(function, str)

    try:
        res = instantiate_spec([function, kwargs])
    except InstantiationException as e:
        msg = 'Could not instantiate code spec.'
        raise_wrapped(DPSemanticError, e, msg, compact=True,
                      function=function, kwargs=kwargs)

    try:
        check_isinstance(res, expect)
    except ValueError as e:
        msg = 'The code did not return the correct type.'
        raise_wrapped(DPSemanticError, e, msg, r=r, res=res, expect=expect)

    return res
Ejemplo n.º 9
0
def test_holds_reference():
    class Foo(object):
        pass
    c = parse('$Foo')
    check_isinstance(c, CheckType)
    
    assert c.types == Foo
Ejemplo n.º 10
0
def eval_lfunction_invmult_sort_ops(ops, context, wants_constant):
    """ Divides in resources and constants 
    
        Returns functions, constants
    """ 
    from .eval_constant_imp import eval_constant
    constants = []
    functions = []

    for op in ops:
        try:
            x = eval_constant(op, context)
            check_isinstance(x, ValueWithUnits)
            constants.append(x)
            continue
        except NotConstant as e:
            pass
        except DPSemanticError as e:
            if 'Variable ref' in str(e):
                pass
            else:
                raise
        
        if wants_constant:
            msg = 'Product not constant because one op is not constant.'
            raise_wrapped(NotConstant, e, msg, op=op)
        x = eval_lfunction(op, context)
        assert isinstance(x, CFunction)
        functions.append(x)
    return functions, constants
Ejemplo n.º 11
0
 def _execute(self, dp1, dp2):
     check_isinstance(dp1, Constant)
     check_isinstance(dp2, Mux)
     f = dp1.get_value()
     r = dp2.amap(f)
     R = dp2.get_res_space()
     return Constant(R, r)
Ejemplo n.º 12
0
def eval_lfunction_invplus_sort_ops(ops, context, wants_constant):
    """
            pos_constants, neg_constants, functions = sort_ops(ops, context)
    """
    from .eval_constant_imp import eval_constant

    pos_constants = []
    neg_constants = []
    functions = []

    for op in ops:
        try:
            x = eval_constant(op, context)
            check_isinstance(x, ValueWithUnits)
            if isinstance(x.unit, (RcompUnits, Rcomp, Nat)):
                pos_constants.append(x)
            elif isinstance(x.unit, RbicompUnits):
                neg_constants.append(x)
            else:
                msg = 'Invalid addition - needs error'
                raise_desc(DPInternalError, msg, x=x)
        except NotConstant as e:
            if wants_constant:
                msg = 'Sum not constant because one op is not constant.'
                raise_wrapped(NotConstant, e, msg, op=op)
            x = eval_lfunction(op, context)
            assert isinstance(x, CFunction)
            functions.append(x)
            
    return pos_constants, neg_constants, functions
Ejemplo n.º 13
0
def eval_solve_r(op, context):
    check_isinstance(op, CDP.SolveRModel)
    from .eval_ndp_imp import eval_ndp
    
    ndp = eval_ndp(op.model, context)
    dp = ndp.get_dp()
    r0 = eval_constant(op.r, context)
    F = dp.get_fun_space()
    R = dp.get_res_space()

    tu = get_types_universe()
    try:
        tu.check_leq(r0.unit, R)
    except NotLeq as e:
        msg = 'Input not correct.'
        raise_wrapped(DPSemanticError, e, msg, compact=True)
    r = r0.cast_value(R)
    
    res = dp.solve_r(r)
    try:
        LF = LowerSets(F)
        return ValueWithUnits(res, LF)
    except NotBelongs as e:
        msg = 'Invalid result of solve_r().'
        raise_desc(DPInternalError, msg, res=res, dp=dp.repr_long())
Ejemplo n.º 14
0
 def __init__(self, F, Rs):
     for _ in Rs:
         check_isinstance(_, Nat)
     check_isinstance(F, Nat)
     R = PosetProduct(Rs)
     M = PosetProduct((F, R))
     PrimitiveDP.__init__(self, F=F, R=R, I=M)
Ejemplo n.º 15
0
 def found_cname(cname):
     check_isinstance(cname, CDP.CName)
     infer_debug('found constant: %s' % cname.value)
     if cname.value in constants:
         msg = 'Duplicated constants?'
         raise DPInternalError(msg, where=cname.where) 
     constants.add(cname.value) 
Ejemplo n.º 16
0
 def __init__(self, A, B, factor):
     check_isinstance(A, (Rcomp, RcompUnits))
     check_isinstance(B, (Rcomp, RcompUnits))
     Map.__init__(self, A, B)
     self.A = A
     self.B = B
     self.factor = factor
Ejemplo n.º 17
0
def note_error(tag0, e):
    check_isinstance(e, BaseException)
    add_class(tag0, 'errored')
    #     logger.error(str(e))  # XXX
    t = Tag(name='pre', attrs={'class': 'error %s' % type(e).__name__})
    t.string = traceback.format_exc(e)
    tag0.insert_after(t)
Ejemplo n.º 18
0
def invmultU_solve_options(F, R, f, n, algo):
    """ Returns a set of points in R that are on the line r1*r2=f. """
    from .dp_inv_mult import InvMult2

    assert algo in [InvMult2.ALGO_UNIFORM, InvMult2.ALGO_VAN_DER_CORPUT]
    if is_top(F, f):
        mcdp_dev_warning('FIXME Need much more thought about this')
        top1 = R[0].get_top()
        top2 = R[1].get_top()
        s = set([(top1, top2)])
        return s

    check_isinstance(f, float)
            
    if f == 0.0:
        return set([(0.0, 0.0)])

    if algo == InvMult2.ALGO_UNIFORM:
        mcdp_dev_warning('TODO: add ALGO as parameter. ')
        ps = samplec(n, f)
    elif algo == InvMult2.ALGO_VAN_DER_CORPUT:
        x1, x2 = generate_exp_van_der_corput_sequence(n=n, C=f)
        ps = zip(x1, x2)
    else: # pragma: no cover
        assert False
    return ps
Ejemplo n.º 19
0
def write_bytes_to_file_as_utf8(s, filename):
    """ Accept a string s (internally using utf-8) and writes
        it to a file in UTF-8 (first converting to unicode, to do it properly)."""
    check_isinstance(s, bytes)
    u = unicode(s, 'utf-8')
    with codecs.open(filename, 'w', encoding='utf-8') as ff:
        ff.write(u)
Ejemplo n.º 20
0
    def __init__(self, dps):
        check_isinstance(dps, tuple)
        tu = get_types_universe()

        F1 = dps[0].get_fun_space()
        R1 = dps[0].get_res_space()

        for dp in dps:
            Fj = dp.get_fun_space()
            Rj = dp.get_res_space()

            try:
                tu.check_equal(F1, Fj)
                tu.check_equal(R1, Rj)
            except NotEqual:
                msg = 'Cannot form the co-product.'
                raise_desc(ValueError, msg, dps=dps)

        F = F1
        R = R1
        Ms = [dp.get_imp_space() for dp in dps]

        self.dps = dps
        self.M = Coproduct1(tuple(Ms))
        PrimitiveDP.__init__(self, F=F, R=R, I=self.M)
Ejemplo n.º 21
0
 def found_instance(self, name, element):
     check_isinstance(name, str)
     where = element.where
     if name in self.instances:
         msg = 'Duplicated instances?'
         raise DPInternalError(msg, where=where) 
     self.instances[name] = SemanticInformationForEntity(element) 
Ejemplo n.º 22
0
def eval_PlusN_sort_ops(ops, context, wants_constant):
    """
            pos_constants, neg_constants, resources = sort_ops(ops, context)
    """
    from .eval_constant_imp import eval_constant

    pos_constants = []
    neg_constants = []
    resources = []

    for op in ops:
        try:
            x = eval_constant(op, context)
            check_isinstance(x, ValueWithUnits)

            if isinstance(x.unit, (RcompUnits, Rcomp, Nat)):
                pos_constants.append(x)
            elif isinstance(x.unit, RbicompUnits):
                neg_constants.append(x)
            else:
                msg = 'Cannot use the type %s in a sum.' % x.unit
                raise_desc(DPInternalError, msg, x=x)
                
        except NotConstant as e:
            if wants_constant:
                msg = 'Sum not constant because one op is not constant.'
                raise_wrapped(NotConstant, e, msg, op=op)
            x = eval_rvalue(op, context)
            assert isinstance(x, CResource)
            resources.append(x)
            
    return pos_constants, neg_constants, resources
Ejemplo n.º 23
0
 def _add_child(self, name, cs):
     cs._parent = self
     check_isinstance(name, str)
     if name in self.children:
         msg =  'I already know "%s".' % name
         raise ValueError(msg)
     self.children[name] = cs
Ejemplo n.º 24
0
 def __init__(self, c):
     check_isinstance(c, int)
     if c == 0:
         raise ValueError(c)
     cod = dom = Nat()
     Map.__init__(self, dom, cod)
     self.c = c
Ejemplo n.º 25
0
def RbicompUnits_reflect(P, x):
    check_isinstance(P, RbicompUnits)
    if is_top(P, x):
        return P.get_bottom()
    if is_bottom(P, x):
        return P.get_top()
    return -x
Ejemplo n.º 26
0
def eval_space_finite_poset(r, context):  # @UnusedVariable
    chains = unwrap_list(r.chains) 

    universe = set()
    relations = set()
    for c in chains:
        check_isinstance(c, (CDP.FinitePosetChainLEQ, CDP.FinitePosetChainGEQ))
        
        ops = get_odd_ops(unwrap_list(c.ops))
        elements = [_.identifier for _ in ops]
        universe.update(elements)
        
        if isinstance(c, CDP.FinitePosetChainLEQ):
            leq = True
        elif isinstance(c, CDP.FinitePosetChainGEQ):
            leq = False
        else:
            assert False
        
        for a, b in zip(elements, elements[1:]):
            
            if leq:
                relations.add((a, b))
            else:
                relations.add((b, a))

    return FinitePoset(universe=universe, relations=relations)
Ejemplo n.º 27
0
def eval_solve_r(op, context):
    check_isinstance(op, CDP.SolveRModel)
    from .eval_ndp_imp import eval_ndp

    ndp = eval_ndp(op.model, context)
    dp = ndp.get_dp()
    r0 = eval_constant(op.r, context)
    F = dp.get_fun_space()
    R = dp.get_res_space()

    tu = get_types_universe()
    try:
        tu.check_leq(r0.unit, R)
    except NotLeq as e:
        msg = 'Input not correct.'
        raise_wrapped(DPSemanticError, e, msg, compact=True)
    r = r0.cast_value(R)

    res = dp.solve_r(r)
    try:
        LF = LowerSets(F)
        return ValueWithUnits(res, LF)
    except NotBelongs as e:
        msg = 'Invalid result of solve_r().'
        raise_desc(DPInternalError, msg, res=res, dp=dp.repr_long())
Ejemplo n.º 28
0
 def __init__(self, c):
     check_isinstance(c, int)
     if c == 0:
         raise ValueError(c)
     cod = dom = Nat()
     Map.__init__(self, dom, cod)
     self.c = c
Ejemplo n.º 29
0
def direct_parents(job_id, db):
    """ Returns the direct parents of the specified job.
        (Jobs that depend directly on this one) """
    check_isinstance(job_id, six.string_types)
    with trace_bugs('direct_parents(%r)' % job_id):
        computation = get_job(job_id, db=db)
        return set(computation.parents)
Ejemplo n.º 30
0
def event_dict_delitem_interpret(view, name, key):
    from mcdp_hdb.memdataview import ViewHash0
    v = get_view_node(view, name)
    check_isinstance(v, ViewHash0)
    # permissions
    v.check_can_write()
    del v._data[key] 
Ejemplo n.º 31
0
def note_error(tag0, e):
    check_isinstance(e, BaseException)
    add_class(tag0, 'errored')
    short = 'Error'
    long_error = traceback.format_exc(e)
    return insert_inset(tag0, short, long_error,
                        [ERROR_CLASS, type(e).__name__])
Ejemplo n.º 32
0
def extract_img_to_file_(soup, savefile, tagname, attrname):
    n = 0
    tot = 0
    for tag in soup.select(tagname):
        tot += 1
        src = tag[attrname]

        if not src.startswith('data:'):
            continue

        mime, data = get_mime_data_from_base64_string(src)

        # now we should make up the data
        if tag.has_attr('id'):
            basename = tag['id']
        else:
            md5 = get_md5(data)
            basename = 'data-from-%s-%s' % (tagname, md5)

        # Guess extension
        ext = get_ext_for_mime(mime)
        filename = basename + '.' + ext
        src = "%s" % filename
        # ask what we should be using
        use_src = savefile(filename, data)
        check_isinstance(use_src, str)
        tag[attrname] = use_src
        n += 1
    logger.debug(('extract_img_to_file: extracted %d/%d images from %r tags, '
                  ' attribute %r.') % (n, tot, tagname, attrname))
Ejemplo n.º 33
0
def note_error_msg(tag0, msg):
    check_isinstance(msg, bytes)
    add_class(tag0, 'errored')
    #     logger.error(str(bytes))  # XXX
    t = Tag(name='pre', attrs={'class': 'error'})
    t.string = msg
    tag0.insert_after(t)
Ejemplo n.º 34
0
def invmultU_solve_options(F, R, f, n, algo):
    """ Returns a set of points in R that are on the line r1*r2=f. """
    from .dp_inv_mult import InvMult2

    assert algo in [InvMult2.ALGO_UNIFORM, InvMult2.ALGO_VAN_DER_CORPUT]
    if is_top(F, f):
        mcdp_dev_warning('FIXME Need much more thought about this')
        top1 = R[0].get_top()
        top2 = R[1].get_top()
        s = set([(top1, top2)])
        return s

    check_isinstance(f, float)

    if f == 0.0:
        return set([(0.0, 0.0)])

    if algo == InvMult2.ALGO_UNIFORM:
        mcdp_dev_warning('TODO: add ALGO as parameter. ')
        ps = samplec(n, f)
    elif algo == InvMult2.ALGO_VAN_DER_CORPUT:
        x1, x2 = generate_exp_van_der_corput_sequence(n=n, C=f)
        ps = zip(x1, x2)
    else:  # pragma: no cover
        assert False
    return ps
Ejemplo n.º 35
0
    def subfloat_replace(args, opts):
        contents = args[0]
        caption = opts[0]
        check_isinstance(contents, str)

        if caption is None:
            label = None
        else:
            caption, label = get_s_without_label(caption, labelprefix="fig:")
            if label is None:
                caption, label = get_s_without_label(caption,
                                                     labelprefix="subfig:")
            if label is not None and not label.startswith('subfig:'):
                msg = 'Subfigure labels should start with "subfig:"; found %r.' % (
                    label)
                label = 'sub' + label
                msg += 'I will change to %r.' % label
                logger.debug(msg)

        # we need to make up an ID
        if label is None:
            label = 'subfig:' + get_md5(contents)
#             print('making up label %r' % label)
#         if label is not None:
        idpart = ' id="%s"' % label
        #         else:
        #             idpart = ""

        if caption is None:
            caption = 'no subfloat caption'
        res = '<figure class="subfloat"%s>%s<figcaption>%s</figcaption></figure>' % (
            idpart, contents, caption)
        return res
Ejemplo n.º 36
0
def generic_mult_constantsN(seq):
    """ Multiplies a sequence of constants that could be either Nat, Rcomp, or RCompUnits """
    for c in seq:
        if isinstance(c.unit, RbicompUnits):
            assert c.value < 0
            msg = "Cannot multiply by negative number %s." % c
            raise_desc(DPSemanticError, msg)

    posets = [_.unit for _ in seq]
    for p in posets:
        check_isinstance(p, (Nat, Rcomp, RcompUnits))

    promoted, R = generic_mult_table(posets)

    if isinstance(R, Nat):
        values = [_.value for _ in seq]
        from functools import reduce

        res = reduce(Nat_mult_uppersets_continuous, values)
        return ValueWithUnits(res, R)
    else:
        res = 1.0
        for vu, F2 in zip(seq, promoted):
            value2 = express_value_in_isomorphic_space(vu.unit, vu.value, F2)
            if F2.equal(value2, F2.get_top()):
                res = R.get_top()
                break
            res *= value2
        return ValueWithUnits(res, R)
        # XXX needs to check overflow
    return res
Ejemplo n.º 37
0
def eval_ndp_CoproductWithNames(r, context):
    check_isinstance(r, CDP.CoproductWithNames)
    elements = unwrap_list(r.elements)
    names = [_.value for _ in elements[0::2]]
    ndps = [eval_ndp(_, context) for _ in elements[1::2]]
    labels = tuple(names)
    return NamedDPCoproduct(tuple(ndps), labels=labels)
Ejemplo n.º 38
0
def check_lang89d(): # TODO: rename

    # All of these should be equivalent to Min1(Nat, 2)
    min3s = [ """
    mcdp {
        provides f [Nat] 
        requires r [Nat]         
        r >= min(f, Nat:2) 
    } ""","""
    mcdp {
        provides f [Nat] 
        requires r [Nat]         
        r >= min(f, Nat:2, Nat:3) 
    } ""","""
    mcdp {
        provides f [Nat] 
        requires r [Nat]         
        r >= min(Nat:2, f, Nat:3) 
    }"""]

    for s in min3s:
#         print '-' * 10
#         print s
        dp = parse_ndp(s).get_dp()
#         print dp.repr_long()
        check_isinstance(dp, MinF1DP)
        assert dp.value == 2
Ejemplo n.º 39
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)
Ejemplo n.º 40
0
def vu_rcomp_mult_constants2(a, b):
    """ Multiplies two ValueWithUnits that are also RcompUnits """
    check_isinstance(a.unit, RcompUnits)
    check_isinstance(b.unit, RcompUnits)
    R = mult_table(a.unit, b.unit)
    value = a.value * b.value
    return ValueWithUnits(value=value, unit=R)
Ejemplo n.º 41
0
def eval_lfunction_FValuePlusOrMinus(r, context):
    from mcdp_dp import UncertainGateSym, MinusValueDP, PlusValueDP

    from .eval_lfunction_imp import eval_lfunction
    from .eval_constant_imp import eval_constant
    from .helpers import create_operation_lf
    
    check_isinstance(r, CDP.FValuePlusOrMinus)
    median = eval_lfunction(r.median, context)
    extent = eval_constant(r.extent, context)
    
    F = context.get_ftype(median)

    # provides f = between 10 g and 20 g
    
    # MinusValueDP :   r + c ≥ f
    # PlusValueDP :   r ≥ f + c 
    
    # f <= f0 - c # pessimistic
    # f <= f0 + c # optimistic 
    dpl =  MinusValueDP(F, extent.value, extent.unit)
    dpu =  PlusValueDP(F, extent.value, extent.unit)
    
    fl = create_operation_lf(context, dpl, functions=[median])
    fu = create_operation_lf(context, dpu, functions=[median])
     
    dp = UncertainGateSym(F)
    return create_operation_lf(context, dp=dp, functions=[fl, fu])
Ejemplo n.º 42
0
def eval_ndp_CoproductWithNames(r, context):
    check_isinstance(r, CDP.CoproductWithNames)
    elements = unwrap_list(r.elements)
    names = [_.value for _ in elements[0::2]]
    ndps = [eval_ndp(_, context) for _ in elements[1::2]]
    labels = tuple(names)
    return NamedDPCoproduct(tuple(ndps), labels=labels)
Ejemplo n.º 43
0
def eval_statement_SetNameFvalue(r, context):
    check_isinstance(r, CDP.SetNameFValue)
    name = r.name.value
    right_side = r.right_side

    if name in context.constants:
        msg = 'Constant %r already set.' % name
        raise DPSemanticError(msg, where=r.where)

    if name in context.var2resource:
        msg = 'Resource %r already set.' % name
        raise DPSemanticError(msg, where=r.where)

    if name in context.var2function:
        msg = 'Name %r already used.' % name
        raise DPSemanticError(msg, where=r.where)

    from .eval_lfunction_imp import eval_lfunction

    fv = eval_lfunction(right_side, context)

    ndp = context.names[fv.dp]
    # TODO: check that is not used anywhere

    current = fv.s
    updated = name
    try:
        ndp2 = ndp_rename_function(ndp, current=current, updated=updated)
        context.names[fv.dp] = ndp2
        fv = CFunction(fv.dp, updated)
    except CouldNotRename:
        pass

    context.set_var2function(name, fv)
def disk_events_from_data_event_inside_yaml(disk_map, data_event, view, p):
    # we now know that we are inside a YAML
    # Just checking though:
    p_schema = view._schema.get_descendant(p)
    p_hint = disk_map.get_hint(p_schema)
    assert isinstance(p_hint, HintFileYAML)

    # make the data_event relative
    relative_data_event = deepcopy(data_event)
    name = data_event['arguments']['name']
    relative_name = name[len(p):]
    relative_data_event['arguments']['name'] = relative_name

    parent_of_yaml = p[:-1]
    parent_of_yaml_schema = view._schema.get_descendant(parent_of_yaml)
    parent_of_yaml_hint = disk_map.get_hint(parent_of_yaml_schema)
    dirname = disk_map.dirname_from_data_url_(view._schema, parent_of_yaml)
    filename = parent_of_yaml_hint.filename_for_key(p[-1])

    # this is the current data to go in yaml
    relative_data_view = view.get_descendant(p)
    # make a copy of the data
    relative_data_view._data = deepcopy(relative_data_view._data)
    # now make the change by applying the relative_data_event
    relative_data_view.set_root()
    event_interpret_(relative_data_view, relative_data_event)
    # now create the YAML file
    fh = disk_map.create_hierarchy_(p_schema, relative_data_view._data)
    check_isinstance(fh, ProxyFile)
    contents = fh.contents
    _id = relative_data_event['id'] + '-tran'
    who = relative_data_event['who']
    disk_event = disk_event_file_modify(_id, who, dirname, filename, contents)
    return [disk_event]
Ejemplo n.º 45
0
def describe_tag(tag):
    check_isinstance(tag, Tag)

    def c(tag):
        x = unicode(tag).encode('utf-8')
        return x

    s = "This is the tag:"
    s += '\n\n'
    s += indent(c(tag), 'tag |')
    s += '\n\n' + 'This is the tag in context:' + '\n\n'

    sc = ""
    if tag.previousSibling is not None:
        sc += c(tag.previousSibling)
    else:
        sc += '<!-- no prev sibling -->'
    sc += c(tag)
    if tag.nextSibling is not None:
        sc += c(tag.next)
    else:
        sc += '<!-- no next sibling -->'

    s += indent(sc, 'tag in context |')
    return s
def disk_events_from_dict_delitem(disk_map, view, _id, who, name, key):
    view_parent = get_view_node(view, name)
    schema_parent = view_parent._schema
    check_isinstance(schema_parent, SchemaHash)
    prototype = schema_parent.prototype

    hint = disk_map.get_hint(schema_parent)
    if isinstance(hint, HintDir):
        dirname = disk_map.dirname_from_data_url_(view._schema, name)
        its_name = hint.filename_for_key(key)
        # I just need to find out whether it would be ProxyDir or ProxyFile
        value = view_parent._data[key]
        d = disk_map.create_hierarchy_(prototype, value)
        if isinstance(d, ProxyFile):
            disk_event = disk_event_file_delete(_id,
                                                who,
                                                dirname=dirname,
                                                name=its_name)
            return [disk_event]
        elif isinstance(d, ProxyDirectory):
            disk_event = disk_event_dir_delete(_id,
                                               who,
                                               dirname=dirname,
                                               name=its_name)
            return [disk_event]
        else:
            assert False
    else:
        raise NotImplementedError(hint)
def disk_events_from_list_append(disk_map, view, _id, who, name, value):
    logger.debug('list append to %s for value %s' % (name, value))
    view_parent = get_view_node(view, name)
    schema_parent = view_parent._schema
    check_isinstance(schema_parent, SchemaList)
    hint = disk_map.get_hint(schema_parent)
    if isinstance(hint, HintDir):
        sub = disk_map.create_hierarchy_(schema_parent.prototype, value)
        dirname = disk_map.dirname_from_data_url_(view._schema, name)
        next_index = len(view_parent._data)
        filename = hint.filename_for_key(str(next_index))
        if isinstance(sub, ProxyFile):
            contents = sub.contents
            disk_event = disk_event_file_create(_id, who, dirname, filename,
                                                contents)
            return [disk_event]
        elif isinstance(sub, ProxyDirectory):
            # create hierarchy
            events = list(
                disk_events_for_creating(_id, who, sub,
                                         tuple(dirname) + (filename, )))
            e = disk_event_disk_event_group(_id, who, events=events)
            return [e]
        else:
            assert False
    else:
        raise NotImplementedError(hint)
def disk_events_from_leaf_set(disk_map, view, _id, who, name, leaf, value):
    view_parent = get_view_node(view, name)
    schema_parent = view_parent._schema
    check_isinstance(schema_parent, SchemaContext)
    view_child = view_parent.child(leaf)
    schema_child = view_child._schema

    hint = disk_map.get_hint(schema_parent)
    if isinstance(hint, HintDir):
        sub = disk_map.create_hierarchy_(schema_child, value)
        dirname = disk_map.dirname_from_data_url_(view._schema, name)

        if isinstance(sub, ProxyFile):
            filename = leaf
            contents = sub.contents
            disk_event = disk_event_file_modify(_id, who, dirname, filename,
                                                contents)
            return [disk_event]
        else:
            assert False
    elif isinstance(hint, HintFileYAML):
        sub = disk_map.create_hierarchy_(schema_child, value)
        check_isinstance(sub, ProxyFile)
        dirname = disk_map.dirname_from_data_url_(view._schema, name)
        filename = leaf
        contents = sub.contents
        disk_event = disk_event_file_modify(_id, who, dirname, filename,
                                            contents)
        return [disk_event]
    else:
        raise NotImplementedError(hint)
Ejemplo n.º 49
0
def add_resource(rname, R, context, r):
    check_isinstance(rname, str)
    if rname in context.rnames:
        msg = 'Repeated resource name %r.' % rname
        raise DPSemanticError(msg, where=r.where)
    context.add_ndp_res_node(rname, R)
    return context.make_function(get_name_for_res_node(rname), rname)
Ejemplo n.º 50
0
def eval_statement_SetNameFvalue(r, context):
    check_isinstance(r, CDP.SetNameFValue)
    name = r.name.value
    right_side = r.right_side

    if name in context.constants:
        msg = 'Constant %r already set.' % name
        raise DPSemanticError(msg, where=r.where)

    if name in context.var2resource:
        msg = 'Resource %r already set.' % name
        raise DPSemanticError(msg, where=r.where)

    if name in context.var2function:
        msg = 'Name %r already used.' % name
        raise DPSemanticError(msg, where=r.where)

    from .eval_lfunction_imp import eval_lfunction
    
    fv = eval_lfunction(right_side, context)
    
    ndp = context.names[fv.dp]
    # TODO: check that is not used anywhere
    
    current = fv.s
    updated = name
    try: 
        ndp2 = ndp_rename_function(ndp, current=current, updated=updated)
        context.names[fv.dp] = ndp2
        fv = CFunction(fv.dp, updated)
    except CouldNotRename:
        pass
    
    context.set_var2function(name, fv)
Ejemplo n.º 51
0
def direct_parents(job_id, db):
    """ Returns the direct parents of the specified job.
        (Jobs that depend directly on this one) """
    check_isinstance(job_id,six.string_types)
    with trace_bugs('direct_parents(%r)' % job_id):
        computation = get_job(job_id, db=db)
        return set(computation.parents)
Ejemplo n.º 52
0
def definition_closure(jobs, db):
    """ The result does not contain jobs (unless one job defines another) """
    #print('definition_closure(%s)' % jobs)
    check_isinstance(jobs, (list, set))
    jobs = set(jobs)
    from compmake.jobs.uptodate import CacheQueryDB
    cq = CacheQueryDB(db)
    stack = set(jobs)
    result = set()
    while stack:
        #print('stack: %s' % stack)
        a = stack.pop()
        if not cq.job_exists(a):
            print('Warning: job %r does not exist anymore; ignoring.' % a)
            continue

        if cq.get_job_cache(a).state == Cache.DONE:
            a_d = cq.jobs_defined(a)
            #print('%s ->%s' % (a, a_d))
            for x in a_d:
                result.add(x)
                stack.add(x)

    #print('  result = %s' % result)
    return result
Ejemplo n.º 53
0
def eval_lfunction_invmult_sort_ops(ops, context, wants_constant):
    """ Divides in resources and constants 
    
        Returns functions, constants
    """
    from .eval_constant_imp import eval_constant
    constants = []
    functions = []

    for op in ops:
        try:
            x = eval_constant(op, context)
            check_isinstance(x, ValueWithUnits)
            constants.append(x)
            continue
        except NotConstant as e:
            pass
        except DPSemanticError as e:
            if 'Variable ref' in str(e):
                pass
            else:
                raise

        if wants_constant:
            msg = 'Product not constant because one op is not constant.'
            raise_wrapped(
                NotConstant,
                e,
                msg,  # op=op, 
                compact=True)
        x = eval_lfunction(op, context)
        assert isinstance(x, CFunction)
        functions.append(x)
    return functions, constants
Ejemplo n.º 54
0
def eval_lfunction_invplus_sort_ops(ops, context, wants_constant):
    """
            pos_constants, neg_constants, functions = sort_ops(ops, context)
    """
    from .eval_constant_imp import eval_constant

    pos_constants = []
    neg_constants = []
    functions = []

    for op in ops:
        try:
            x = eval_constant(op, context)
            check_isinstance(x, ValueWithUnits)
            if isinstance(x.unit, (RcompUnits, Rcomp, Nat)):
                pos_constants.append(x)
            elif isinstance(x.unit, RbicompUnits):
                neg_constants.append(x)
            else:
                msg = 'Invalid addition - needs error'
                raise_desc(DPInternalError, msg, x=x)
        except NotConstant as e:
            if wants_constant:
                msg = 'Sum not constant because one op is not constant.'
                raise_wrapped(NotConstant, e, msg, op=op, compact=True)
            x = eval_lfunction(op, context)
            assert isinstance(x, CFunction)
            functions.append(x)

    return pos_constants, neg_constants, functions
Ejemplo n.º 55
0
    def _update_file(self, f):
        basename = os.path.basename(f)
        check_isinstance(basename, str)
        # This will fail because then in pyparsing everything is unicode
        # import codecs
        # data = codecs.open(f, encoding='utf-8').read()
        data = open(f).read()
        realpath = os.path.realpath(f)
        res = dict(data=data, realpath=realpath, path=f)

        strict = False
        if basename in self.file_to_contents:
            realpath1 = self.file_to_contents[basename]["realpath"]
            path1 = self.file_to_contents[basename]["path"]
            if res["realpath"] == realpath1:
                msg = "File %r reached twice." % basename
                if not strict:
                    logger.warning(msg + "\n" + format_obs(dict(path1=path1, path2=res["path"])))
                else:
                    raise_desc(DPSemanticError, msg, path1=path1, path2=res["path"])

            else:
                msg = "Found duplicated file %r." % basename
                if not strict:
                    if log_duplicates:
                        logger.warning(msg + "\n" + format_obs(dict(path1=realpath1, path2=res["realpath"])))
                else:
                    raise_desc(DPSemanticError, msg, path1=realpath1, path2=res["realpath"])

        assert isinstance(basename, str), basename

        self.file_to_contents[basename] = res
Ejemplo n.º 56
0
def add_function(fname, F, context, r):
    check_isinstance(fname, str)
    if fname in context.fnames:
        msg = 'Repeated function name %r.' % fname
        raise DPSemanticError(msg, where=r.where)

    context.add_ndp_fun_node(fname, F)
    return context.make_resource(get_name_for_fun_node(fname), fname)
Ejemplo n.º 57
0
def eval_statement_FunShortcut5(r, context):
    check_isinstance(r, CDP.FunShortcut5)
    # provides f1, f2 [Nat] 'comment'
    F = eval_space(r.unit, context)
    
    fnames = get_odd_ops(unwrap_list(r.fnames))
    for fname in fnames:
        add_function(fname.value, F, context, r=fname)
Ejemplo n.º 58
0
def eval_statement_ResShortcut5(r, context):
    # requires r1, r2 [Nat] 'comment'
    check_isinstance(r, CDP.ResShortcut5)
    R = eval_space(r.unit, context)
    
    rnames = get_odd_ops(unwrap_list(r.rnames))
    for rname in rnames:
        add_resource(rname.value, R, context, r=rname)
Ejemplo n.º 59
0
def plus_constants2_rcompunits(a, b):
    check_isinstance(a.unit, RcompUnits)
    check_isinstance(b.unit, RcompUnits)
    R = a.unit
    Fs = [a.unit, b.unit]
    values = [a.value, b.value]
    res = sum_units(Fs, values, R)
    return ValueWithUnits(value=res, unit=R)
Ejemplo n.º 60
0
def eval_rvalue_DerivResourceRef(rvalue, context):
    check_isinstance(rvalue, CDP.DerivResourceRef)
    _ = rvalue.drname.value
    if _  in context.var2resource:
        return context.var2resource[_]
    else:
        msg = 'Derivative resource %r not found.' % _
        raise DPSemanticError(msg, where=rvalue.where) # or internal?