def testStash(self): count = [20] def boz(): count[0] += 10 return count[0] def biz(*args): return args and args[0] or "<undef>" data = { "foo": 10, "bar": { "baz": 20 }, "baz": lambda: { "boz": boz, "biz": biz }, "obj": AnObject(name="an object"), "hashobj": HashObject(planet="World") } stash = Stash(data) self.assertEqual(10, stash.get("foo").value()) self.assertEqual(20, stash.get(["bar", 0, "baz", 0]).value()) self.assertEqual(20, stash.get("bar.baz").value()) self.assertEqual(20, stash.get("bar(10).baz").value()) self.assertEqual(30, stash.get("baz.boz").value()) self.assertEqual(40, stash.get("baz.boz").value()) self.assertEqual("<undef>", stash.get("baz.biz").value()) self.assertEqual("<undef>", stash.get("baz(50).biz").value()) # args are ignored stash.set("bar.buz", 100) self.assertEqual(100, stash.get("bar.buz").value()) ttlist = (("default", Template()), ("warn", Template({"DEBUG": constants.DEBUG_UNDEF, "DEBUG_FORMAT": ""}))) self.Expect(DATA, ttlist, data)
def __init__(self, config): self.__load_templates = util.listify( config.get("LOAD_TEMPLATES") or Provider(config)) self.__load_plugins = util.listify( config.get("LOAD_PLUGINS") or Plugins(config)) self.__load_filters = util.listify( config.get("LOAD_FILTERS") or Filters(config)) prefix_map = config.get("PREFIX_MAP") or {} self.__filter_cache = {} self.__prefix_map = {} for key, value in prefix_map.items(): if isinstance(value, str): self.__prefix_map[key] = util.slice( self.__load_templates, [int(x) for x in re.split(r"\D+", value)]) else: self.__prefix_map[key] = value if "STASH" in config: self.__stash = config["STASH"] else: predefs = config.get("VARIABLES") or config.get("PRE_DEFINE") or {} predefs.setdefault("_DEBUG", int(bool(config.get("DEBUG", 0) & DEBUG_UNDEF))) self.__stash = Stash(predefs) # compile any template BLOCKS specified as text blocks = config.get("BLOCKS") or {} b = {} for key, block in blocks.items(): if isinstance(block, str): block = self.template(util.Literal(block)) b[key] = block self.__init_blocks = self.__blocks = b self.__recursion = config.get("RECURSION", False) self.__eval_python = config.get("EVAL_PYTHON", False) self.__trim = config.get("TRIM", False) self.__blkstack = [] self.__config = config if config.get("EXPOSE_BLOCKS") is not None: self.__expose_blocks = config.get("EXPOSE_BLOCKS") else: self.__expose_blocks = False self.__debug_format = config.get("DEBUG_FORMAT") self.__debug_dirs = config.get("DEBUG", 0) & DEBUG_DIRS if config.get("DEBUG") is not None: self.__debug = config["DEBUG"] & (DEBUG_CONTEXT | DEBUG_FLAGS) else: self.__debug = self.DEBUG