def multi(coll): def make_apply(el): return lambda f: f(el) if callable(f) else f if is_mapping(coll): return lambda el: walk_values(make_apply(el), coll) else: return lambda el: lmap(make_apply(el), coll)
def parse_value(value, grammar_path, context, render_strategy=texts.render_ftemplate): if is_list(value): mode, value = get_list_setting(value, grammar_path) if mode == "reuse": return db.Database( db.choose([ parse_value(v, grammar_path, context, render_strategy=render_strategy) for v in value ]), grammar_path, context, ) elif mode == "pick": return db.Database( db.pick([ parse_value(v, grammar_path, context, render_strategy=render_strategy) for v in value ]), grammar_path, context, ) elif mode == "markov": return db.Database( db.markovify([texts.RenderedStr(v) for v in value]), grammar_path, context, ) elif mode == "ratchet": return db.Database( db.ratchet([texts.RenderedStr(v) for v in value]), grammar_path, context) elif mode == "list": return db.Datalist(grammar_path, context, [texts.RenderedStr(v) for v in value]) elif is_mapping(value): return db.Databag( grammar_path, context, { k: parse_value( v, grammar_path, context, render_strategy=render_strategy) for k, v in value.items() }, ) elif is_bool(value): return value else: return texts.RenderableText(fix_text(value), grammar_path, context, render_strategy=render_strategy)
def flatten_dict(d, path='', sep='.'): res = {} for k, v in d.items(): new_path = path + sep + str(k) if path else k if is_mapping(v): res.update(flatten_dict(v, path=new_path, sep=sep)) else: res[new_path] = v return res
def is_node(item): """Tests whether item is a node.""" return is_mapping(item) and "type" in item
def get_list_setting(value, grammar_path): mode = "reuse" if value and is_mapping(value[0]): if len(value[0]) == 1 and "mode" in value[0]: mode = value.pop(0)["mode"] return mode, value