def get(self, use_base_env=False, **kwargs): l = self.filter(**kwargs).get_jiter() if len(l) == 0: return None for (o, _) in l: if o.jeeves_id != l[0][0].jeeves_id: raise Exception( "wow such error: get() found rows for more than one jeeves_id" ) cur = None for (o, conditions) in l: old = cur cur = FObject(o) for var_name, val in conditions.iteritems(): if val: cur = Facet( acquire_label_by_name(self.model._meta.app_label, var_name), cur, old) else: cur = Facet( acquire_label_by_name(self.model._meta.app_label, var_name), old, cur) try: return partialEval( cur, {} if use_base_env else JeevesLib.jeevesState.pathenv.getEnv()) except TypeError: raise Exception( "wow such error: could not find a row for every condition")
def jassign(old, new, base_env={}): res = new for vs in jeevesState.pathenv.conditions: (var, val) = (vs.var, vs.val) if var.name not in base_env: if val: res = Facet(var, res, old) else: res = Facet(var, old, res) if isinstance(res, FExpr): return partialEval(res, {}, True) else: return res
def jassign(old, new, base_env={}): res = new for vs in jeevesState.pathenv.conditions: (var, val) = (vs.var, vs.val) if var.name not in base_env: if val: res = Facet(var, res, old) else: res = Facet(var, old, res) if isinstance(res, FExpr): return res.partialEval({}, True) else: return res
def facetMapper(facet, fn, wrapper=fexpr_cast): """ """ if isinstance(facet, Facet): return Facet(facet.cond, facetMapper(facet.thn, fn, wrapper), facetMapper(facet.els, fn, wrapper)) elif isinstance(facet, Constant) or isinstance(facet, FObject): return wrapper(fn(facet.v))
def get(self, use_base_env=False, **kwargs): """Fetches a JList of rows that match the conditions. """ matches = self.filter(**kwargs).get_jiter() if len(matches) == 0: return None for (row, _) in matches: if row.jeeves_id != matches[0][0].jeeves_id: raise Exception("wow such error: \ get() found rows for more than one jeeves_id") viewer = JeevesLib.get_viewer() has_viewer = not isinstance(viewer, FNull) pathenv = JeevesLib.jeevesState.pathenv.getEnv() solverstate = JeevesLib.get_solverstate() cur = None for (row, conditions) in matches: old = cur cur = FObject(row) for var_name, (label, val) in conditions.iteritems(): # TODO: Figure out if we need to make obj the faceted value. ''' if has_viewer: if solverstate.assignLabel(label, pathenv): if not val: cur = old else: if val: cur = old else: ''' if val: cur = Facet(label, cur, old) else: cur = Facet(label, old, cur) try: return cur.partialEval({} if use_base_env \ else JeevesLib.jeevesState.pathenv.getEnv()) except TypeError: raise Exception("wow such error: \ could not find a row for every condition")
def liftTuple(t): t = fexpr_cast(t) if isinstance(t, FObject): return t.v elif isinstance(t, Facet): a = liftTuple(t.thn) b = liftTuple(t.els) return tuple([Facet(t.cond, a1, b1) for (a1, b1) in zip(a, b)]) else: raise TypeError("bad use of liftTuple")
def rec(cur_e, i): if i == len(all_vars): return FObject( [i for i, e in self.l if all(cur_e[v] == e[v] for v in e)]) else: cur_e1 = dict(cur_e) cur_e2 = dict(cur_e) cur_e1[all_vars[i]] = True cur_e2[all_vars[i]] = False return Facet(getLabel(all_vars[i]), rec(cur_e1, i + 1), rec(cur_e2, i + 1))
def jif2(cond, thn_fn, els_fn): if isinstance(cond, Constant): return thn_fn() if cond.v else els_fn() elif isinstance(cond, Facet): if not isinstance(cond.cond, Var): raise TypeError("facet conditional is of type %s" % cond.cond.__class__.__name__) with PositiveVariable(cond.cond): thn = jif2(cond.thn, thn_fn, els_fn) with NegativeVariable(cond.cond): els = jif2(cond.els, thn_fn, els_fn) return Facet(cond.cond, thn, els) else: raise TypeError("jif condition must be a constant or a var")
def mkSensitive(varLabel, vHigh, vLow): """Creates a sensitive value with two facets. :param varLabel: Label to associate with sensitive value. :type varLabel: Var :param vHigh: High-confidentiality facet for viewers with restricted access. :type vHigh: T :param vLow: Low-confidentiality facet for other viewers. :type vLow: T """ if isinstance(varLabel, Var): return Facet(varLabel, fexpr_cast(vHigh), fexpr_cast(vLow)) else: return JeevesLib.jif(varLabel, lambda: vHigh, lambda: vLow)
def jfun3(f, kw, it, key, val, args_concrete, kw_concrete): if isinstance(val, Constant) or isinstance(val, FObject): kw_c = dict(kw_concrete) kw_c[key] = val.v try: next_key = next(it) except StopIteration: return fexpr_cast(f(*args_concrete, **kw_c)) env = jeevesState.pathenv.getEnv() return jfun3(f, kw, it, next_key, partialEval(fexpr_cast(kw[next_key]), env), args_concrete, kw_c) else: it1, it2 = tee(it) with PositiveVariable(val.cond): thn = jfun3(f, kw, it1, key, val.thn, args_concrete, kw_concrete) with NegativeVariable(val.cond): els = jfun3(f, kw, it2, key, val.els, args_concrete, kw_concrete) return Facet(val.cond, thn, els)
def jfun2(f, args, kw, i, arg, args_concrete): if isinstance(arg, Constant) or isinstance(arg, FObject): env = jeevesState.pathenv.getEnv() if i < len(args) - 1: return jfun2(f, args, kw, i + 1, partialEval(fexpr_cast(args[i + 1]), env), tuple(list(args_concrete) + [arg.v])) else: it = kw.__iter__() try: fst = next(it) except StopIteration: return fexpr_cast(f(*tuple(list(args_concrete) + [arg.v]))) return jfun3(f, kw, it, fst, partialEval(fexpr_cast(kw[fst]), env), tuple(list(args_concrete) + [arg.v]), {}) else: with PositiveVariable(arg.cond): thn = jfun2(f, args, kw, i, arg.thn, args_concrete) with NegativeVariable(arg.cond): els = jfun2(f, args, kw, i, arg.els, args_concrete) return Facet(arg.cond, thn, els)
def jmap2(iterator, mapper): if isinstance(iterator, Facet): if jeevesState.pathenv.hasPosVar(iterator.cond): return jmap2(iterator.thn, mapper) if jeevesState.pathenv.hasNegVar(iterator.cond): return jmap2(iterator.els, mapper) with PositiveVariable(iterator.cond): thn = jmap2(iterator.thn, mapper) with NegativeVariable(iterator.cond): els = jmap2(iterator.els, mapper) return Facet(iterator.cond, thn, els) elif isinstance(iterator, FObject): return jmap2(iterator.v, mapper) elif isinstance(iterator, JList): return jmap2(iterator.l, mapper) elif isinstance(iterator, JList2): return jmap2(iterator.convert_to_jlist1().l, mapper) elif isinstance(iterator, list) or isinstance(iterator, tuple): return FObject([mapper(item) for item in iterator]) else: return jmap2(iterator.__iter__(), mapper)