Python lib2to3.pytree.LeafPattern() Examples

The following are 30 code examples of lib2to3.pytree.LeafPattern(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module lib2to3.pytree , or try the search function .
Example #1
Source File: test_pytree.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_has_key_example(self):
        pattern = pytree.NodePattern(331,
                                     (pytree.LeafPattern(7),
                                      pytree.WildcardPattern(name="args"),
                                      pytree.LeafPattern(8)))
        l1 = pytree.Leaf(7, "(")
        l2 = pytree.Leaf(3, "x")
        l3 = pytree.Leaf(8, ")")
        node = pytree.Node(331, [l1, l2, l3])
        r = {}
        self.assertTrue(pattern.match(node, r))
        self.assertEqual(r["args"], [l2]) 
Example #2
Source File: test_pytree.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_basic_patterns(self):
        # Build a tree
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern matching a leaf
        pl = pytree.LeafPattern(100, "foo", name="pl")
        r = {}
        self.assertFalse(pl.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n2, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pl.match(l1, results=r))
        self.assertEqual(r, {"pl": l1})
        r = {}
        self.assertFalse(pl.match(l2, results=r))
        self.assertEqual(r, {})
        # Build a pattern matching a node
        pn = pytree.NodePattern(1000, [pl], name="pn")
        self.assertFalse(pn.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pn.match(n2, results=r))
        self.assertEqual(r, {"pn": n2, "pl": l3})
        r = {}
        self.assertFalse(pn.match(l1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(l2, results=r))
        self.assertEqual(r, {}) 
Example #3
Source File: test_pytree.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_wildcard(self):
        # Build a tree for testing
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern
        pl = pytree.LeafPattern(100, "foo", name="pl")
        pn = pytree.NodePattern(1000, [pl], name="pn")
        pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
        r = {}
        self.assertFalse(pw.match_seq([root], r))
        self.assertEqual(r, {})
        self.assertFalse(pw.match_seq([n1], r))
        self.assertEqual(r, {})
        self.assertTrue(pw.match_seq([n2], r))
        # These are easier to debug
        self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
        self.assertEqual(r["pl"], l1)
        self.assertEqual(r["pn"], n2)
        self.assertEqual(r["pw"], [n2])
        # But this is equivalent
        self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
        r = {}
        self.assertTrue(pw.match_seq([l1, l3], r))
        self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
        self.assertIs(r["pl"], l3)
        r = {} 
Example #4
Source File: test_pytree.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_generate_matches(self):
        la = pytree.Leaf(1, "a")
        lb = pytree.Leaf(1, "b")
        lc = pytree.Leaf(1, "c")
        ld = pytree.Leaf(1, "d")
        le = pytree.Leaf(1, "e")
        lf = pytree.Leaf(1, "f")
        leaves = [la, lb, lc, ld, le, lf]
        root = pytree.Node(1000, leaves)
        pa = pytree.LeafPattern(1, "a", "pa")
        pb = pytree.LeafPattern(1, "b", "pb")
        pc = pytree.LeafPattern(1, "c", "pc")
        pd = pytree.LeafPattern(1, "d", "pd")
        pe = pytree.LeafPattern(1, "e", "pe")
        pf = pytree.LeafPattern(1, "f", "pf")
        pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
                                     [pa, pb], [pc, pd], [pe, pf]],
                                    min=1, max=4, name="pw")
        self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
                         [3, 5, 2, 4, 6])
        pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
        matches = list(pytree.generate_matches([pr], [root]))
        self.assertEqual(len(matches), 1)
        c, r = matches[0]
        self.assertEqual(c, 1)
        self.assertEqual(str(r["pr"]), "abcdef")
        self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
        for c in "abcdef":
            self.assertEqual(r["p" + c], pytree.Leaf(1, c)) 
Example #5
Source File: test_pytree.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_has_key_example(self):
        pattern = pytree.NodePattern(331,
                                     (pytree.LeafPattern(7),
                                      pytree.WildcardPattern(name="args"),
                                      pytree.LeafPattern(8)))
        l1 = pytree.Leaf(7, "(")
        l2 = pytree.Leaf(3, "x")
        l3 = pytree.Leaf(8, ")")
        node = pytree.Node(331, [l1, l2, l3])
        r = {}
        self.assertTrue(pattern.match(node, r))
        self.assertEqual(r["args"], [l2]) 
Example #6
Source File: test_pytree.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_basic_patterns(self):
        # Build a tree
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern matching a leaf
        pl = pytree.LeafPattern(100, "foo", name="pl")
        r = {}
        self.assertFalse(pl.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n2, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pl.match(l1, results=r))
        self.assertEqual(r, {"pl": l1})
        r = {}
        self.assertFalse(pl.match(l2, results=r))
        self.assertEqual(r, {})
        # Build a pattern matching a node
        pn = pytree.NodePattern(1000, [pl], name="pn")
        self.assertFalse(pn.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pn.match(n2, results=r))
        self.assertEqual(r, {"pn": n2, "pl": l3})
        r = {}
        self.assertFalse(pn.match(l1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(l2, results=r))
        self.assertEqual(r, {}) 
Example #7
Source File: test_pytree.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_wildcard(self):
        # Build a tree for testing
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern
        pl = pytree.LeafPattern(100, "foo", name="pl")
        pn = pytree.NodePattern(1000, [pl], name="pn")
        pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
        r = {}
        self.assertFalse(pw.match_seq([root], r))
        self.assertEqual(r, {})
        self.assertFalse(pw.match_seq([n1], r))
        self.assertEqual(r, {})
        self.assertTrue(pw.match_seq([n2], r))
        # These are easier to debug
        self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
        self.assertEqual(r["pl"], l1)
        self.assertEqual(r["pn"], n2)
        self.assertEqual(r["pw"], [n2])
        # But this is equivalent
        self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
        r = {}
        self.assertTrue(pw.match_seq([l1, l3], r))
        self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
        self.assertIs(r["pl"], l3)
        r = {} 
Example #8
Source File: test_pytree.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_generate_matches(self):
        la = pytree.Leaf(1, "a")
        lb = pytree.Leaf(1, "b")
        lc = pytree.Leaf(1, "c")
        ld = pytree.Leaf(1, "d")
        le = pytree.Leaf(1, "e")
        lf = pytree.Leaf(1, "f")
        leaves = [la, lb, lc, ld, le, lf]
        root = pytree.Node(1000, leaves)
        pa = pytree.LeafPattern(1, "a", "pa")
        pb = pytree.LeafPattern(1, "b", "pb")
        pc = pytree.LeafPattern(1, "c", "pc")
        pd = pytree.LeafPattern(1, "d", "pd")
        pe = pytree.LeafPattern(1, "e", "pe")
        pf = pytree.LeafPattern(1, "f", "pf")
        pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
                                     [pa, pb], [pc, pd], [pe, pf]],
                                    min=1, max=4, name="pw")
        self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
                         [3, 5, 2, 4, 6])
        pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
        matches = list(pytree.generate_matches([pr], [root]))
        self.assertEqual(len(matches), 1)
        c, r = matches[0]
        self.assertEqual(c, 1)
        self.assertEqual(str(r["pr"]), "abcdef")
        self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
        for c in "abcdef":
            self.assertEqual(r["p" + c], pytree.Leaf(1, c)) 
Example #9
Source File: test_pytree.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_has_key_example(self):
        pattern = pytree.NodePattern(331,
                                     (pytree.LeafPattern(7),
                                      pytree.WildcardPattern(name="args"),
                                      pytree.LeafPattern(8)))
        l1 = pytree.Leaf(7, "(")
        l2 = pytree.Leaf(3, "x")
        l3 = pytree.Leaf(8, ")")
        node = pytree.Node(331, [l1, l2, l3])
        r = {}
        self.assertTrue(pattern.match(node, r))
        self.assertEqual(r["args"], [l2]) 
Example #10
Source File: test_pytree.py    From odoo13-x64 with GNU General Public License v3.0 5 votes vote down vote up
def test_basic_patterns(self):
        # Build a tree
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern matching a leaf
        pl = pytree.LeafPattern(100, "foo", name="pl")
        r = {}
        self.assertFalse(pl.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n2, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pl.match(l1, results=r))
        self.assertEqual(r, {"pl": l1})
        r = {}
        self.assertFalse(pl.match(l2, results=r))
        self.assertEqual(r, {})
        # Build a pattern matching a node
        pn = pytree.NodePattern(1000, [pl], name="pn")
        self.assertFalse(pn.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pn.match(n2, results=r))
        self.assertEqual(r, {"pn": n2, "pl": l3})
        r = {}
        self.assertFalse(pn.match(l1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(l2, results=r))
        self.assertEqual(r, {}) 
Example #11
Source File: test_pytree.py    From odoo13-x64 with GNU General Public License v3.0 5 votes vote down vote up
def test_wildcard(self):
        # Build a tree for testing
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern
        pl = pytree.LeafPattern(100, "foo", name="pl")
        pn = pytree.NodePattern(1000, [pl], name="pn")
        pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
        r = {}
        self.assertFalse(pw.match_seq([root], r))
        self.assertEqual(r, {})
        self.assertFalse(pw.match_seq([n1], r))
        self.assertEqual(r, {})
        self.assertTrue(pw.match_seq([n2], r))
        # These are easier to debug
        self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
        self.assertEqual(r["pl"], l1)
        self.assertEqual(r["pn"], n2)
        self.assertEqual(r["pw"], [n2])
        # But this is equivalent
        self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
        r = {}
        self.assertTrue(pw.match_seq([l1, l3], r))
        self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
        self.assertIs(r["pl"], l3)
        r = {} 
Example #12
Source File: test_pytree.py    From odoo13-x64 with GNU General Public License v3.0 5 votes vote down vote up
def test_generate_matches(self):
        la = pytree.Leaf(1, "a")
        lb = pytree.Leaf(1, "b")
        lc = pytree.Leaf(1, "c")
        ld = pytree.Leaf(1, "d")
        le = pytree.Leaf(1, "e")
        lf = pytree.Leaf(1, "f")
        leaves = [la, lb, lc, ld, le, lf]
        root = pytree.Node(1000, leaves)
        pa = pytree.LeafPattern(1, "a", "pa")
        pb = pytree.LeafPattern(1, "b", "pb")
        pc = pytree.LeafPattern(1, "c", "pc")
        pd = pytree.LeafPattern(1, "d", "pd")
        pe = pytree.LeafPattern(1, "e", "pe")
        pf = pytree.LeafPattern(1, "f", "pf")
        pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
                                     [pa, pb], [pc, pd], [pe, pf]],
                                    min=1, max=4, name="pw")
        self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
                         [3, 5, 2, 4, 6])
        pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
        matches = list(pytree.generate_matches([pr], [root]))
        self.assertEqual(len(matches), 1)
        c, r = matches[0]
        self.assertEqual(c, 1)
        self.assertEqual(str(r["pr"]), "abcdef")
        self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
        for c in "abcdef":
            self.assertEqual(r["p" + c], pytree.Leaf(1, c)) 
Example #13
Source File: test_pytree.py    From odoo13-x64 with GNU General Public License v3.0 5 votes vote down vote up
def test_has_key_example(self):
        pattern = pytree.NodePattern(331,
                                     (pytree.LeafPattern(7),
                                      pytree.WildcardPattern(name="args"),
                                      pytree.LeafPattern(8)))
        l1 = pytree.Leaf(7, "(")
        l2 = pytree.Leaf(3, "x")
        l3 = pytree.Leaf(8, ")")
        node = pytree.Node(331, [l1, l2, l3])
        r = {}
        self.assertTrue(pattern.match(node, r))
        self.assertEqual(r["args"], [l2]) 
Example #14
Source File: test_pytree.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_basic_patterns(self):
        # Build a tree
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern matching a leaf
        pl = pytree.LeafPattern(100, "foo", name="pl")
        r = {}
        self.assertFalse(pl.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n2, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pl.match(l1, results=r))
        self.assertEqual(r, {"pl": l1})
        r = {}
        self.assertFalse(pl.match(l2, results=r))
        self.assertEqual(r, {})
        # Build a pattern matching a node
        pn = pytree.NodePattern(1000, [pl], name="pn")
        self.assertFalse(pn.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pn.match(n2, results=r))
        self.assertEqual(r, {"pn": n2, "pl": l3})
        r = {}
        self.assertFalse(pn.match(l1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(l2, results=r))
        self.assertEqual(r, {}) 
Example #15
Source File: test_pytree.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_wildcard(self):
        # Build a tree for testing
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern
        pl = pytree.LeafPattern(100, "foo", name="pl")
        pn = pytree.NodePattern(1000, [pl], name="pn")
        pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
        r = {}
        self.assertFalse(pw.match_seq([root], r))
        self.assertEqual(r, {})
        self.assertFalse(pw.match_seq([n1], r))
        self.assertEqual(r, {})
        self.assertTrue(pw.match_seq([n2], r))
        # These are easier to debug
        self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
        self.assertEqual(r["pl"], l1)
        self.assertEqual(r["pn"], n2)
        self.assertEqual(r["pw"], [n2])
        # But this is equivalent
        self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
        r = {}
        self.assertTrue(pw.match_seq([l1, l3], r))
        self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
        self.assertTrue(r["pl"] is l3)
        r = {} 
Example #16
Source File: test_pytree.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_generate_matches(self):
        la = pytree.Leaf(1, "a")
        lb = pytree.Leaf(1, "b")
        lc = pytree.Leaf(1, "c")
        ld = pytree.Leaf(1, "d")
        le = pytree.Leaf(1, "e")
        lf = pytree.Leaf(1, "f")
        leaves = [la, lb, lc, ld, le, lf]
        root = pytree.Node(1000, leaves)
        pa = pytree.LeafPattern(1, "a", "pa")
        pb = pytree.LeafPattern(1, "b", "pb")
        pc = pytree.LeafPattern(1, "c", "pc")
        pd = pytree.LeafPattern(1, "d", "pd")
        pe = pytree.LeafPattern(1, "e", "pe")
        pf = pytree.LeafPattern(1, "f", "pf")
        pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
                                     [pa, pb], [pc, pd], [pe, pf]],
                                    min=1, max=4, name="pw")
        self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
                         [3, 5, 2, 4, 6])
        pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
        matches = list(pytree.generate_matches([pr], [root]))
        self.assertEqual(len(matches), 1)
        c, r = matches[0]
        self.assertEqual(c, 1)
        self.assertEqual(str(r["pr"]), "abcdef")
        self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
        for c in "abcdef":
            self.assertEqual(r["p" + c], pytree.Leaf(1, c)) 
Example #17
Source File: test_pytree.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_has_key_example(self):
        pattern = pytree.NodePattern(331,
                                     (pytree.LeafPattern(7),
                                      pytree.WildcardPattern(name="args"),
                                      pytree.LeafPattern(8)))
        l1 = pytree.Leaf(7, "(")
        l2 = pytree.Leaf(3, "x")
        l3 = pytree.Leaf(8, ")")
        node = pytree.Node(331, [l1, l2, l3])
        r = {}
        self.assertTrue(pattern.match(node, r))
        self.assertEqual(r["args"], [l2]) 
Example #18
Source File: test_pytree.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_basic_patterns(self):
        # Build a tree
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern matching a leaf
        pl = pytree.LeafPattern(100, "foo", name="pl")
        r = {}
        self.assertFalse(pl.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n2, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pl.match(l1, results=r))
        self.assertEqual(r, {"pl": l1})
        r = {}
        self.assertFalse(pl.match(l2, results=r))
        self.assertEqual(r, {})
        # Build a pattern matching a node
        pn = pytree.NodePattern(1000, [pl], name="pn")
        self.assertFalse(pn.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pn.match(n2, results=r))
        self.assertEqual(r, {"pn": n2, "pl": l3})
        r = {}
        self.assertFalse(pn.match(l1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(l2, results=r))
        self.assertEqual(r, {}) 
Example #19
Source File: test_pytree.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_generate_matches(self):
        la = pytree.Leaf(1, "a")
        lb = pytree.Leaf(1, "b")
        lc = pytree.Leaf(1, "c")
        ld = pytree.Leaf(1, "d")
        le = pytree.Leaf(1, "e")
        lf = pytree.Leaf(1, "f")
        leaves = [la, lb, lc, ld, le, lf]
        root = pytree.Node(1000, leaves)
        pa = pytree.LeafPattern(1, "a", "pa")
        pb = pytree.LeafPattern(1, "b", "pb")
        pc = pytree.LeafPattern(1, "c", "pc")
        pd = pytree.LeafPattern(1, "d", "pd")
        pe = pytree.LeafPattern(1, "e", "pe")
        pf = pytree.LeafPattern(1, "f", "pf")
        pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
                                     [pa, pb], [pc, pd], [pe, pf]],
                                    min=1, max=4, name="pw")
        self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
                         [3, 5, 2, 4, 6])
        pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
        matches = list(pytree.generate_matches([pr], [root]))
        self.assertEqual(len(matches), 1)
        c, r = matches[0]
        self.assertEqual(c, 1)
        self.assertEqual(str(r["pr"]), "abcdef")
        self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
        for c in "abcdef":
            self.assertEqual(r["p" + c], pytree.Leaf(1, c)) 
Example #20
Source File: test_pytree.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_has_key_example(self):
        pattern = pytree.NodePattern(331,
                                     (pytree.LeafPattern(7),
                                      pytree.WildcardPattern(name="args"),
                                      pytree.LeafPattern(8)))
        l1 = pytree.Leaf(7, "(")
        l2 = pytree.Leaf(3, "x")
        l3 = pytree.Leaf(8, ")")
        node = pytree.Node(331, [l1, l2, l3])
        r = {}
        self.assertTrue(pattern.match(node, r))
        self.assertEqual(r["args"], [l2]) 
Example #21
Source File: test_pytree.py    From android_universal with MIT License 5 votes vote down vote up
def test_basic_patterns(self):
        # Build a tree
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern matching a leaf
        pl = pytree.LeafPattern(100, "foo", name="pl")
        r = {}
        self.assertFalse(pl.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n2, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pl.match(l1, results=r))
        self.assertEqual(r, {"pl": l1})
        r = {}
        self.assertFalse(pl.match(l2, results=r))
        self.assertEqual(r, {})
        # Build a pattern matching a node
        pn = pytree.NodePattern(1000, [pl], name="pn")
        self.assertFalse(pn.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pn.match(n2, results=r))
        self.assertEqual(r, {"pn": n2, "pl": l3})
        r = {}
        self.assertFalse(pn.match(l1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(l2, results=r))
        self.assertEqual(r, {}) 
Example #22
Source File: test_pytree.py    From android_universal with MIT License 5 votes vote down vote up
def test_wildcard(self):
        # Build a tree for testing
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern
        pl = pytree.LeafPattern(100, "foo", name="pl")
        pn = pytree.NodePattern(1000, [pl], name="pn")
        pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
        r = {}
        self.assertFalse(pw.match_seq([root], r))
        self.assertEqual(r, {})
        self.assertFalse(pw.match_seq([n1], r))
        self.assertEqual(r, {})
        self.assertTrue(pw.match_seq([n2], r))
        # These are easier to debug
        self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
        self.assertEqual(r["pl"], l1)
        self.assertEqual(r["pn"], n2)
        self.assertEqual(r["pw"], [n2])
        # But this is equivalent
        self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
        r = {}
        self.assertTrue(pw.match_seq([l1, l3], r))
        self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
        self.assertIs(r["pl"], l3)
        r = {} 
Example #23
Source File: test_pytree.py    From android_universal with MIT License 5 votes vote down vote up
def test_generate_matches(self):
        la = pytree.Leaf(1, "a")
        lb = pytree.Leaf(1, "b")
        lc = pytree.Leaf(1, "c")
        ld = pytree.Leaf(1, "d")
        le = pytree.Leaf(1, "e")
        lf = pytree.Leaf(1, "f")
        leaves = [la, lb, lc, ld, le, lf]
        root = pytree.Node(1000, leaves)
        pa = pytree.LeafPattern(1, "a", "pa")
        pb = pytree.LeafPattern(1, "b", "pb")
        pc = pytree.LeafPattern(1, "c", "pc")
        pd = pytree.LeafPattern(1, "d", "pd")
        pe = pytree.LeafPattern(1, "e", "pe")
        pf = pytree.LeafPattern(1, "f", "pf")
        pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
                                     [pa, pb], [pc, pd], [pe, pf]],
                                    min=1, max=4, name="pw")
        self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
                         [3, 5, 2, 4, 6])
        pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
        matches = list(pytree.generate_matches([pr], [root]))
        self.assertEqual(len(matches), 1)
        c, r = matches[0]
        self.assertEqual(c, 1)
        self.assertEqual(str(r["pr"]), "abcdef")
        self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
        for c in "abcdef":
            self.assertEqual(r["p" + c], pytree.Leaf(1, c)) 
Example #24
Source File: test_pytree.py    From android_universal with MIT License 5 votes vote down vote up
def test_has_key_example(self):
        pattern = pytree.NodePattern(331,
                                     (pytree.LeafPattern(7),
                                      pytree.WildcardPattern(name="args"),
                                      pytree.LeafPattern(8)))
        l1 = pytree.Leaf(7, "(")
        l2 = pytree.Leaf(3, "x")
        l3 = pytree.Leaf(8, ")")
        node = pytree.Node(331, [l1, l2, l3])
        r = {}
        self.assertTrue(pattern.match(node, r))
        self.assertEqual(r["args"], [l2]) 
Example #25
Source File: test_pytree.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_basic_patterns(self):
        # Build a tree
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern matching a leaf
        pl = pytree.LeafPattern(100, "foo", name="pl")
        r = {}
        self.assertFalse(pl.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n2, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pl.match(l1, results=r))
        self.assertEqual(r, {"pl": l1})
        r = {}
        self.assertFalse(pl.match(l2, results=r))
        self.assertEqual(r, {})
        # Build a pattern matching a node
        pn = pytree.NodePattern(1000, [pl], name="pn")
        self.assertFalse(pn.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pn.match(n2, results=r))
        self.assertEqual(r, {"pn": n2, "pl": l3})
        r = {}
        self.assertFalse(pn.match(l1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(l2, results=r))
        self.assertEqual(r, {}) 
Example #26
Source File: test_pytree.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_wildcard(self):
        # Build a tree for testing
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern
        pl = pytree.LeafPattern(100, "foo", name="pl")
        pn = pytree.NodePattern(1000, [pl], name="pn")
        pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
        r = {}
        self.assertFalse(pw.match_seq([root], r))
        self.assertEqual(r, {})
        self.assertFalse(pw.match_seq([n1], r))
        self.assertEqual(r, {})
        self.assertTrue(pw.match_seq([n2], r))
        # These are easier to debug
        self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
        self.assertEqual(r["pl"], l1)
        self.assertEqual(r["pn"], n2)
        self.assertEqual(r["pw"], [n2])
        # But this is equivalent
        self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
        r = {}
        self.assertTrue(pw.match_seq([l1, l3], r))
        self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
        self.assertTrue(r["pl"] is l3)
        r = {} 
Example #27
Source File: test_pytree.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_generate_matches(self):
        la = pytree.Leaf(1, "a")
        lb = pytree.Leaf(1, "b")
        lc = pytree.Leaf(1, "c")
        ld = pytree.Leaf(1, "d")
        le = pytree.Leaf(1, "e")
        lf = pytree.Leaf(1, "f")
        leaves = [la, lb, lc, ld, le, lf]
        root = pytree.Node(1000, leaves)
        pa = pytree.LeafPattern(1, "a", "pa")
        pb = pytree.LeafPattern(1, "b", "pb")
        pc = pytree.LeafPattern(1, "c", "pc")
        pd = pytree.LeafPattern(1, "d", "pd")
        pe = pytree.LeafPattern(1, "e", "pe")
        pf = pytree.LeafPattern(1, "f", "pf")
        pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
                                     [pa, pb], [pc, pd], [pe, pf]],
                                    min=1, max=4, name="pw")
        self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
                         [3, 5, 2, 4, 6])
        pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
        matches = list(pytree.generate_matches([pr], [root]))
        self.assertEqual(len(matches), 1)
        c, r = matches[0]
        self.assertEqual(c, 1)
        self.assertEqual(str(r["pr"]), "abcdef")
        self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
        for c in "abcdef":
            self.assertEqual(r["p" + c], pytree.Leaf(1, c)) 
Example #28
Source File: test_pytree.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_has_key_example(self):
        pattern = pytree.NodePattern(331,
                                     (pytree.LeafPattern(7),
                                      pytree.WildcardPattern(name="args"),
                                      pytree.LeafPattern(8)))
        l1 = pytree.Leaf(7, "(")
        l2 = pytree.Leaf(3, "x")
        l3 = pytree.Leaf(8, ")")
        node = pytree.Node(331, [l1, l2, l3])
        r = {}
        self.assertTrue(pattern.match(node, r))
        self.assertEqual(r["args"], [l2]) 
Example #29
Source File: test_pytree.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_basic_patterns(self):
        # Build a tree
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern matching a leaf
        pl = pytree.LeafPattern(100, "foo", name="pl")
        r = {}
        self.assertFalse(pl.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pl.match(n2, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pl.match(l1, results=r))
        self.assertEqual(r, {"pl": l1})
        r = {}
        self.assertFalse(pl.match(l2, results=r))
        self.assertEqual(r, {})
        # Build a pattern matching a node
        pn = pytree.NodePattern(1000, [pl], name="pn")
        self.assertFalse(pn.match(root, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(n1, results=r))
        self.assertEqual(r, {})
        self.assertTrue(pn.match(n2, results=r))
        self.assertEqual(r, {"pn": n2, "pl": l3})
        r = {}
        self.assertFalse(pn.match(l1, results=r))
        self.assertEqual(r, {})
        self.assertFalse(pn.match(l2, results=r))
        self.assertEqual(r, {}) 
Example #30
Source File: test_pytree.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_generate_matches(self):
        la = pytree.Leaf(1, "a")
        lb = pytree.Leaf(1, "b")
        lc = pytree.Leaf(1, "c")
        ld = pytree.Leaf(1, "d")
        le = pytree.Leaf(1, "e")
        lf = pytree.Leaf(1, "f")
        leaves = [la, lb, lc, ld, le, lf]
        root = pytree.Node(1000, leaves)
        pa = pytree.LeafPattern(1, "a", "pa")
        pb = pytree.LeafPattern(1, "b", "pb")
        pc = pytree.LeafPattern(1, "c", "pc")
        pd = pytree.LeafPattern(1, "d", "pd")
        pe = pytree.LeafPattern(1, "e", "pe")
        pf = pytree.LeafPattern(1, "f", "pf")
        pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
                                     [pa, pb], [pc, pd], [pe, pf]],
                                    min=1, max=4, name="pw")
        self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
                         [3, 5, 2, 4, 6])
        pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
        matches = list(pytree.generate_matches([pr], [root]))
        self.assertEqual(len(matches), 1)
        c, r = matches[0]
        self.assertEqual(c, 1)
        self.assertEqual(str(r["pr"]), "abcdef")
        self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
        for c in "abcdef":
            self.assertEqual(r["p" + c], pytree.Leaf(1, c))