class TestAdvancedBag(unittest.TestCase): def setUp(self): self.bag = Bag() self.bag.setBackRef() self.bag["a.b.c.d"] = 4 self.bag["a.b.e"] = 5 def testparent(self): self.assertEqual(self.bag["a.b.c"].parent, self.bag["a.b"]) c = self.bag["a.b.c"] self.assertEqual(c["../e"], 5) self.bag["a.b"].delParentRef() self.assertFalse(self.bag["a.b"].backref) def testformula(self): self.bag["product"] = self.bag.formula("$x*$y", x="a.b.c.d", y="a.b.e") self.assertEqual(self.bag["product"], self.bag["a.b.c.d"] * self.bag["a.b.e"]) self.bag.defineFormula(calculate_perimeter="2*($base + $height)") self.bag.defineSymbol(base="a.b.c.d", height="a.b.e") self.bag["perimeter"] = self.bag.formula("calculate_perimeter") self.assertEqual(self.bag["perimeter"], 18) def testcallbackitem(self): def hello(): return "Hello!" self.bag.setCallBackItem("say_hello", hello) self.assertEqual(self.bag["say_hello"], hello()) def testnodetrigger(self): self.lastupdates = [] def mycallback(node, info=None, evt=None): self.lastupdates.append((node.getLabel(), info, node.getValue())) self.bag.getNode("a.b.c.d").subscribe("lastupdates", mycallback) self.bag["a.b.c.d"] = 20 self.assertEqual(self.lastupdates[-1], ("d", 4, 20)) def testbagtriggers(self): self.log = [] def log_upd(node, pathlist, oldvalue, evt): self.log.append((".".join(pathlist), node.getValue(), oldvalue, evt)) def log_ins(node, pathlist, ind, evt): self.log.append((".".join(pathlist), node.getValue(), ind, evt)) def log_del(node, pathlist, ind, evt): self.log.append((".".join(pathlist), node.getValue(), ind, evt)) self.bag.subscribe("log", update=log_upd, insert=log_ins, delete=log_del) self.bag["a.b.t"] = 45 self.assertEqual(self.log[-1], ("a.b", 45, 2, "ins")) self.bag["a.b.t"] = 56 self.assertEqual(self.log[-1], ("a.b.t", 56, 45, "upd_value")) self.bag.delItem("a.b.t") self.assertEqual(self.log[-1], ("a.b", 56, 2, "del"))
def sitemap(self): """Return the sitemap Bag (if there is no sitemap, creates it)""" if not hasattr(self, '_sitemap'): sitemap_path = os.path.join(self.site_path, 'sitemap.xml') if not os.path.isfile(sitemap_path): sitemap_path = os.path.join(self.site_path, 'automap.xml') _sitemap = Bag(sitemap_path) _sitemap.setBackRef() self._sitemap = _sitemap return self._sitemap
def test_fullpath(self): b = Bag() b['just.a.simple.test'] = 123 assert b.fullpath == None bag = b['just.a.simple'] assert isinstance(bag, Bag) assert bag.fullpath == None b.setBackRef() assert b['just.a.simple'].fullpath == 'just.a.simple'
def sitemap(self): """add???""" if not hasattr(self, '_sitemap'): sitemap_path = os.path.join(self.site_path, 'sitemap.xml') if not os.path.isfile(sitemap_path): sitemap_path = os.path.join(self.site_path, 'automap.xml') _sitemap = Bag(sitemap_path) _sitemap.setBackRef() self._sitemap = _sitemap return self._sitemap
class DocAnalyzer(ast.NodeVisitor): """Reads a python source file and collects information about docstrings.""" def __init__(self, filename): self.filename = filename with open(filename, 'r') as fd: ast_tree = ast.parse(fd.read(), filename) self.root = Bag() self.current = self.module = Bag() self.root['_'] = self.module self.root.setBackRef() self.module.label, _ = os.path.splitext(os.path.basename(filename)) self.lineno = 0 self.visit(ast_tree) def visit(self, node): """Visit nodes, gathering line numbers.""" lineno = getattr(node, 'lineno', None) if lineno is not None and lineno > self.lineno: self.lineno = lineno super(DocAnalyzer, self).visit(node) def visit_ClassDef(self, node): """Visits 'class' nodes""" parent, self.current = self.current, Bag() start = self.lineno self.generic_visit(node) end = self.lineno parent.addItem(node.name, self.current, kind='class', docstring=ast.get_docstring(node), start=start, end=end) self.current = parent def visit_FunctionDef(self, node): """Visits 'def' nodes""" parent, self.current = self.current, Bag() start = self.lineno self.generic_visit(node) end = self.lineno parent.addItem(node.name, self.current, kind='def', docstring=ast.get_docstring(node), start=start, end=end) self.current = parent
def loadLocalizationFile(self, path=None, currentLocalizationBlock=None, enabledLanguages=None, **kwargs): localizationpath = os.path.join(path, 'localization.xml') localization = Bag(localizationpath) if os.path.exists( localizationpath) else Bag() localization.setBackRef() enabledLanguages = enabledLanguages.split( ',') if enabledLanguages else [] griddata = Bag() treedata = Bag() treedata.setItem('root', Bag(), caption=currentLocalizationBlock) treeroot = treedata['root'] def cb(n): if n.attr.get('path'): treeroot.setItem(n.fullpath or n.label, None, path=n.attr['path'], ext=n.attr['ext'], caption=n.label) elif not n.value: row = griddata[n.label] parentNode = n.parentNode parentAttr = parentNode.attr module = parentAttr.get('path') if not row: row = Bag() griddata[n.label] = row row['base'] = n.attr.get('base') row['ext'] = parentAttr.get('ext') row['_lockey'] = n.label row['path'] = [] row['_pkey'] = n.label row['path'].append(module) #row['_treepath'] = '%s.%s' %((parentNode.fullpath or parentNode.label),parentAttr.get('ext')) for lang in enabledLanguages: row[lang] = row[lang] or n.attr.get(lang) localization.walk(cb) griddata.sort('base') return Bag(dict(content=Bag(griddata=griddata), treedata=treedata))
class DocAnalyzer(ast.NodeVisitor): """Reads a python source file and collects information about docstrings.""" def __init__(self, filename): self.filename = filename with open(filename,'r') as fd: ast_tree = ast.parse(fd.read(), filename) self.root = Bag() self.current = self.module = Bag() self.root['_'] = self.module self.root.setBackRef() self.module.label, _ = os.path.splitext(os.path.basename(filename)) self.lineno = 0 self.visit(ast_tree) def visit(self, node): """Visit nodes, gathering line numbers.""" lineno = getattr(node, 'lineno', None) if lineno is not None and lineno > self.lineno: self.lineno = lineno super(DocAnalyzer, self).visit(node) def visit_ClassDef(self, node): """Visits 'class' nodes""" parent, self.current = self.current, Bag() start = self.lineno self.generic_visit(node) end = self.lineno parent.addItem(node.name, self.current, kind='class', docstring=ast.get_docstring(node), start=start, end=end) self.current = parent def visit_FunctionDef(self, node): """Visits 'def' nodes""" parent, self.current = self.current, Bag() start = self.lineno self.generic_visit(node) end = self.lineno parent.addItem(node.name, self.current, kind='def', docstring=ast.get_docstring(node), start=start, end=end) self.current = parent
class TestAdvancedBag(unittest.TestCase): def setUp(self): self.bag = Bag() self.bag.setBackRef() self.bag['a.b.c.d'] = 4 self.bag['a.b.e'] = 5 def testparent(self): self.assertEqual(self.bag['a.b.c'].parent, self.bag['a.b']) c = self.bag['a.b.c'] self.assertEqual(c['../e'], 5) self.bag['a.b'].delParentRef() self.assertFalse(self.bag['a.b'].backref) def testformula(self): self.bag['product'] = self.bag.formula('$x*$y', x='a.b.c.d', y='a.b.e') self.assertEqual(self.bag['product'], self.bag['a.b.c.d'] * self.bag['a.b.e']) self.bag.defineFormula(calculate_perimeter='2*($base + $height)') self.bag.defineSymbol(base='a.b.c.d', height='a.b.e') self.bag['perimeter'] = self.bag.formula('calculate_perimeter') self.assertEqual(self.bag['perimeter'], 18) def testcallbackitem(self): def hello(): return 'Hello!' self.bag.setCallBackItem('say_hello', hello) self.assertEqual(self.bag['say_hello'], hello()) def testnodetrigger(self): self.lastupdates = [] def mycallback(node, info=None, evt=None): self.lastupdates.append((node.getLabel(), info, node.getValue())) self.bag.getNode('a.b.c.d').subscribe('lastupdates', mycallback) self.bag['a.b.c.d'] = 20 self.assertEqual(self.lastupdates[-1], ('d', 4, 20)) def testbagtriggers(self): self.log = [] def log_upd(node, pathlist, oldvalue, evt): self.log.append( ('.'.join(pathlist), node.getValue(), oldvalue, evt)) def log_ins(node, pathlist, ind, evt): self.log.append(('.'.join(pathlist), node.getValue(), ind, evt)) def log_del(node, pathlist, ind, evt): self.log.append(('.'.join(pathlist), node.getValue(), ind, evt)) self.bag.subscribe('log', update=log_upd, insert=log_ins, delete=log_del) self.bag['a.b.t'] = 45 self.assertEqual(self.log[-1], ('a.b', 45, 2, 'ins')) self.bag['a.b.t'] = 56 self.assertEqual(self.log[-1], ('a.b.t', 56, 45, 'upd_value')) self.bag.delItem('a.b.t') self.assertEqual(self.log[-1], ('a.b', 56, 2, 'del'))