def test_dtab_read_ignores_comment_line(self): withComments = Dtab.read(""" # a comment /#foo => /biz # another comment | ( /bliz & # yet another comment /bluth ) # duh bluths ; #finalmente #/ignore=>/me; """) dtab = Dtab([ Dentry( Path.Utf8("#foo"), NameTree.Alt( NameTree.Leaf(Path.Utf8("biz")), NameTree.Union( NameTree.Weighted( NameTree.Weighted.defaultWeight, NameTree.Leaf(Path.Utf8("bliz")), ), NameTree.Weighted( NameTree.Weighted.defaultWeight, NameTree.Leaf(Path.Utf8("bluth")), ), ), ), ) ]) s = "Dtab(Label(#foo)=>NameTree.Leaf(Path(/biz))," s += "NameTree.Union(NameTree.Weighted(1.0,NameTree.Leaf(Path(/bliz)))," s += "NameTree.Weighted(1.0,NameTree.Leaf(Path(/bluth)))))" self.assertEqual(repr(dtab), s) self.assertEqual(withComments, dtab)
def parse_simple(self): self.eat_whitespace() c = chr(self.peek) if c == u('('): self.next() tree = self.parse_tree() self.eat_whitespace() self.eat(')') return tree if c == u('/'): return NameTree.Leaf(self.parse_path()) if c == u('!'): self.next() return NameTree.Fail if c == u('~'): self.next() return NameTree.Neg if c == u('$'): self.next() return NameTree.Empty self.illegal("simple", c)
def test_parseDtab(self): self.assertTrue(NameTreeParsers.parseDtab("") == Dtab.empty) self.assertTrue( NameTreeParsers.parseDtab(" /=>! ") == Dtab( [Dentry(Path.empty, NameTree.Fail)])) self.assertTrue( NameTreeParsers.parseDtab("/=>!;") == Dtab( [Dentry(Path.empty, NameTree.Fail)])) self.assertTrue( NameTreeParsers.parseDtab("/=>!;/foo=>/bar") == Dtab([ Dentry(Path.empty, NameTree.Fail), Dentry(Path.Utf8("foo"), NameTree.Leaf(Path.Utf8("bar"))) ]))
def lookup(self, path): """Lookup the given `path` with this dtab""" if isinstance(path, NameTree) and hasattr(path, "value"): path = path.value elif not isinstance(path, Path): raise TypeError("Input must be a `dtab.path.Path`") matches = [] # don't use public dentries for dentry in self._dentries: if dentry.prefix.matches(path): suffix = path.elems[dentry.prefix.size:] matches.append( dentry.nametree.map( lambda pfx: NameTree.Leaf(Name.Path(pfx + suffix)))) if not len(matches): return NameTree.Neg elif len(matches) == 1: return matches[0] return NameTree.Alt(*matches)
def test_parseNameTree(self): defaultWeight = NameTree.Weighted.defaultWeight self.assertTrue( NameTreeParsers.parseNameTree("! | ~ | $") == NameTree.Alt( NameTree.Fail, NameTree.Neg, NameTree.Empty)) self.assertTrue( NameTreeParsers.parseNameTree("/foo/bar") == NameTree.Leaf( Path.Utf8("foo", "bar"))) self.assertTrue( NameTreeParsers.parseNameTree(" /foo & /bar ") == NameTree.Union( NameTree.Weighted(defaultWeight, NameTree.Leaf(Path.Utf8( "foo"))), NameTree.Weighted(defaultWeight, NameTree.Leaf(Path.Utf8( "bar"))))) self.assertTrue( NameTreeParsers.parseNameTree(" /foo | /bar ") == NameTree.Alt( NameTree.Leaf(Path.Utf8("foo")), NameTree.Leaf(Path.Utf8( "bar")))) self.assertTrue( NameTreeParsers.parseNameTree("/foo & /bar | /bar & /baz") == NameTree.Alt( NameTree.Union( NameTree.Weighted(defaultWeight, NameTree.Leaf(Path.Utf8("foo"))), NameTree.Weighted(defaultWeight, NameTree.Leaf(Path.Utf8("bar")))), NameTree.Union( NameTree.Weighted(defaultWeight, NameTree.Leaf(Path.Utf8("bar"))), NameTree.Weighted(defaultWeight, NameTree.Leaf(Path.Utf8("baz")))))) self.assertTrue( NameTreeParsers.parseNameTree( "1 * /foo & 2 * /bar | .5 * /bar & .5 * /baz") == NameTree.Alt( NameTree.Union( NameTree.Weighted(1, NameTree.Leaf(Path.Utf8("foo"))), NameTree.Weighted(2, NameTree.Leaf(Path.Utf8("bar")))), NameTree.Union( NameTree.Weighted(0.5, NameTree.Leaf(Path.Utf8("bar"))), NameTree.Weighted(0.5, NameTree.Leaf(Path.Utf8("baz")))))) with self.assertRaises(IllegalArgumentException): NameTreeParsers.parseNameTree("") with self.assertRaises(IllegalArgumentException): NameTreeParsers.parseNameTree("#") with self.assertRaises(IllegalArgumentException): NameTreeParsers.parseNameTree("/foo &") with self.assertRaises(IllegalArgumentException): NameTreeParsers.parseNameTree("/foo & 0.1.2 * /bar") with self.assertRaises(IllegalArgumentException): NameTreeParsers.parseNameTree("/foo & . * /bar")
def test_dtab_rewrites_with_wildcard(self): dtab = Dtab.read("/a/*/c => /d") nametree = dtab.lookup(Path.read("/a/b/c/e/f")) leaf = NameTree.Leaf(Name.Path(Path.read("/d/e/f"))) self.assertEqual(nametree, leaf)