Пример #1
0
    def as_positional_function(self, name):
        e = self.namespace[name]
        uses = coalesce(parse_vars_arg(e, "uses"), [])  # FIXME encapsulation violation

        def _fn(*args, **bindings):
            kw = dict(zip(uses, args))
            kw.update(bindings)
            return self.invoke(name, **kw)

        return _fn
Пример #2
0
 def parse_match_args(expr, bindings, default_pattern=".*"):
     if expr.get("value"):
         value = expr.get("value")
     else:
         var_arg = parse_var_arg(expr)
         try:
             value = str(bindings[var_arg])
         except KeyError:  # the caller is attempting to match an unbound variable
             raise UnboundVariable(var_arg)
     timestamp = expr.get("timestamp")
     if timestamp is not None:
         pattern = timestamp2regex(timestamp)
     else:
         pattern = coalesce(expr.get("pattern"), default_pattern)
     return pattern, value
Пример #3
0
def interpolate(template, scope, fail_fast=True):
    if not "$" in coalesce(template, ""):
        return template
    s = StringIO()
    end = 0
    for m in re.finditer(LDR_INTERP_PATTERN, template):
        end = m.end()
        (plain, expr, key) = m.groups()
        s.write(plain)
        try:
            s.write(scope[key])
        except KeyError:
            if fail_fast:
                raise UnboundVariable(key)
    s.write(template[end:])
    interpolated = s.getvalue()
    #    interpolated = jinja2.Environment().from_string(interpolated).render(**scope.flatten())
    return interpolated
Пример #4
0
def parse_source_arg(expr):
    url = expr.get("url")
    file_path = coalesce(expr.get("file"), "-")
    return url, file_path
Пример #5
0
def parse_var_arg(expr, attr="var"):
    return coalesce(expr.get(attr), "_")
Пример #6
0
 def get_list(self, attr_name, delim=None):
     delim = coalesce(delim, LDR_WS_SEP_PATTERN)
     templates = re.split(delim, self.elt.get(attr_name))
     return map(lambda t: interpolate(t, self.bindings), templates)
Пример #7
0
     except UnboundVariable, uv:
         logging.warn('var %s: unbound variable in template "%s": %s' % (var_name, expr.raw_text, uv))
         return  # miss
 # The vars expression is the plural of var, for multiple assignment
 # with any regex as a delimiter between variable values.
 # <vars names="{name1} {name2} [delim="{delim}"]>{value1}{delim}{value2}</vars>
 # or
 # <vars names="{name1} {name2} [delim="{delim}"]>
 #   <vals>{value1}{delim}{value2}</vals>
 #   <vals>{value1}{delim}{value2}</vals>
 # </vars>
 elif expr.tag == "vars":
     try:
         var_names = re.split(LDR_WS_SEP_PATTERN, expr.get("names"))
         sub_val_exprs = expr.findall("vals")
         delim = coalesce(expr.get("delim"), LDR_WS_SEP_PATTERN)
         if len(sub_val_exprs) == 0:
             var_vals = map(lambda t: interpolate(t, bindings), re.split(delim, expr.raw_text))
             for s in rest(expr, dict(zip(var_names, var_vals))):
                 yield s
         else:
             for sub_val_expr in sub_val_exprs:
                 sub_val_expr = ScopedExpr(sub_val_expr, bindings)
                 var_vals = map(lambda t: interpolate(t, bindings), re.split(delim, sub_val_expr.raw_text))
                 for s in rest(expr, dict(zip(var_names, var_vals))):
                     yield s
     except UnboundVariable, uv:
         logging.warn("vars: unbound variable %s" % uv)
         return  # miss
 # all is a conjunction. it is like an unnamed namespace block
 # and will yield any solution that exists after all exprs are evaluated