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_tree(self): trees = [] while True: trees.append(self.parse_tree1()) self.eat_whitespace() if not self.maybe_eat('|'): break if len(trees) > 1: return NameTree.Alt(*trees) return trees[0]
def lookup(self, path): """Lookup the given `path` with this dtab""" 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: Name.Path(pfx + suffix))) if not len(matches): return NameTree.Neg elif len(matches) == 1: return matches[0] return NameTree.Alt(*matches)
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")