def test_write_read(self): with lightgrep.Program(0) as prog1: with lightgrep.Pattern() as pat: with lightgrep.Fsm(0) as fsm: pat.parse("a+b", lightgrep.KeyOpts()) fsm.add_pattern(prog1, pat, 'UTF-8', 42) prog1.compile(fsm, lightgrep.ProgOpts()) buf = prog1.write() with lightgrep.Program(buf) as prog2: self.assertEqual(prog2.count(), prog1.count()) self.assertEqual(prog2.size(), prog1.size())
def setUp(self): self.prog = lightgrep.Program(0) self.addCleanup(self.prog.close) self.fsm = lightgrep.Fsm(0) self.addCleanup(self.fsm.close) self.pat = lightgrep.Pattern() self.addCleanup(self.pat.close)
def test_size(self): with lightgrep.Program(0) as prog: with lightgrep.Pattern() as pat: with lightgrep.Fsm(0) as fsm: pat.parse("a+b", lightgrep.KeyOpts()) fsm.add_pattern(prog, pat, 'UTF-8', 42) prog.compile(fsm, lightgrep.ProgOpts()) self.assertTrue(prog.size() > 0)
def setUp(self): self.prog = lightgrep.Program(0) self.addCleanup(self.prog.close) with lightgrep.Pattern() as pat: with lightgrep.Fsm(0) as fsm: pat.parse("a+b", lightgrep.KeyOpts()) fsm.add_pattern(self.prog, pat, 'UTF-8', 42) self.prog.compile(fsm, lightgrep.ProgOpts()) self.ctx = lightgrep.Context(self.prog, lightgrep.CtxOpts()) self.addCleanup(self.ctx.close)
withHits.reset() # Bad input results in exceptions print("---------------------------") print("Exception due to a malformed pattern") with lg.Pattern() as pat: try: pat.parse('+++', lg.KeyOpts()) except RuntimeError as e: print(e) print("---------------------------") print("Results creating program and pattern map separately from context") # Creating the program and pattern map separately from the context with lg.Program(0) as prog: with lg.Pattern() as pat: with lg.Fsm(0) as fsm: fsm.add_patterns(prog, pat, keys) prog.compile(fsm, lg.ProgOpts()) myHits = lg.HitAccumulator() with lg.Context(prog, lg.CtxOpts()) as ctx: myHitCount = ctx.searchBuffer(searchData, myHits) print(f"{myHitCount} hits found") for h in myHits.Hits: print( f"hit at [{h['start']},{h['end']}) on keyindex {h['keywordIndex']}, pattern is '{h['pattern']}' with encoding chain '{h['encChain']}'" ) myHits.reset()
def test_ctor_bad_args(self): with lightgrep.Program(0) as prog: arglist = [prog, lightgrep.CtxOpts()] subs = (None, 'bogus') fuzz_it(self, lightgrep.Context, arglist, subs)
def test_ctor_prog_closed(self): with lightgrep.Program(0) as prog: prog.close() with self.assertRaises(RuntimeError): lightgrep.Context(prog, lightgrep.CtxOpts())
def test_write_closed(self): with lightgrep.Program(0) as prog: prog.close() with self.assertRaises(RuntimeError): prog.write()
def test_count_unused(self): with lightgrep.Program(0) as prog: self.assertEqual(prog.count(), 0)
def test_close_unused(self): # test that closing an unused Program doesn't throw prog = lightgrep.Program(0) prog.close()