Example #1
0
 def test_exact_match(self):
     matcher = Matcher(self.folders)
     self.assertEqual(4, len(matcher.keys))
     rv = list(matcher.options({"pos": 3}))
     self.assertEqual(1, len(rv), rv)
     self.assertIsInstance(rv[0], SceneScript.Folder)
     self.assertEqual({"pos": 3}, rv[0].metadata)
Example #2
0
def rehearse(
    folders, references, handler,
    repeat=0, roles=1, strict=False,
    loop=None
):
    """Cast a set of objects into a sequence of scene scripts. Deliver the performance.

    :param folders: A sequence of
        :py:class:`turberfield.dialogue.model.SceneScript.Folder` objects.
    :param references: A sequence of Python objects.
    :param handler: A callable object. This will be invoked with every event from the
                    performance.
    :param int repeat: Extra repetitions of each folder.
    :param int roles: Maximum number of roles permitted each character.
    :param bool strict: Only fully-cast scripts to be performed.

    This function is a generator. It yields events from the performance.

    """
    if isinstance(folders, SceneScript.Folder):
        folders = [folders]

    yield from handler(references, loop=loop)

    matcher = Matcher(folders)
    performer = Performer(folders, references)
    while True:
        folder, index, script, selection, interlude = performer.next(
            folders, references, strict=strict, roles=roles
        )
        yield from handler(script, loop=loop)

        for item in performer.run(react=False, strict=strict, roles=roles):
            yield from handler(item, loop=loop)

        if isinstance(interlude, Callable):
            metadata = next(handler(
                interlude, folder, index, references, loop=loop
            ), None)
            yield metadata
            if metadata is None:
                return

            branch = next(matcher.options(metadata))
            if branch != folder:
                performer = Performer([branch], references)

        if not repeat:
            break
        else:
            repeat -= 1
Example #3
0
    def frame(session, entities):
        """Return the next frame of action for presentation handling."""
        while not session.frames:
            matcher = Matcher(carmen.logic.episodes)
            branching = list(matcher.options(session.cache.get("metadata",
                                                               {})))
            performer = Performer(branching, entities)
            folder, index, script, selection, interlude = performer.next(
                branching, entities)
            scene = performer.run(react=False)
            frames = list(
                Handler.frames(folder.paths[index], scene, dwell=0.3, pause=1))
            if frames and interlude:
                frames[-1].append(
                    Handler.Element(
                        None,
                        functools.partial(interlude, folder, index, entities,
                                          **session.cache), None, None, None))
            session.frames.extend(frames)

        return session.frames.popleft()
Example #4
0
 def simulate(self, folders, n_cycles=1):
     matcher = Matcher(folders)
     state = tor.rules.State(
         folders[0].metadata["area"],
         self.settings.HAIR_M,
         self.settings.HAIR_D,
         self.settings.CUT_M,
         0,
         self.settings.COINS_N,
         self.settings.HEALTH_MAX
     )
     folder = folders[0]
     for f in self.pathway(folders, n_cycles):
         metadata = state._asdict()
         rv = tor.rules.apply_rules(folder, None, [], self.settings, state)
         if rv:
             metadata.update(rv)
         else:
             continue
         metadata["area"] = f.metadata["area"]
         folder = next(matcher.options(metadata))
         state = tor.rules.State(**metadata)
         yield state
Example #5
0
def main(args):
    log_manager = LogManager()
    log = log_manager.get_logger("main")

    if args.log_path:
        log.set_route(args.log_level, LogAdapter(), sys.stderr)
        log.set_route(log.Level.NOTSET, LogAdapter(), args.log_path)
    else:
        log.set_route(args.log_level, LogAdapter(), sys.stderr)

    folders, references = resolve_objects(args)
    matcher = Matcher(folders)
    performer = Performer(folders, references)
    interlude = None
    handler = HTMLHandler(dwell=args.dwell, pause=args.pause)
    items = []
    folder = True
    log.info("Reading sources...")
    while folder and not performer.stopped:
        for i in range(args.repeat + 1):
            if performer.script:
                log.info("Script {0.fP}".format(performer.script))

            folder, index, script, selection, interlude = performer.next(
                folders, references, strict=args.strict, roles=args.roles)
            for item in performer.run(strict=args.strict, roles=args.roles):
                items.extend(list(handler(item)))

            if interlude is not None:
                metadata = interlude(folder, index, references)
                folder = next(matcher.options(metadata))

    log.info("Writing {0} items to output...".format(len(items)))
    print(handler.to_html(metadata=performer.metadata))
    log.info("Done.")
    return 0
Example #6
0
 def folder(self):
     location = self.narrator.state.area
     selector = {"area": location}
     matcher = Matcher(self.folders)
     return next(matcher.options(selector))
Example #7
0
 def test_bisect_complex_values(self):
     self.assertGreater(abs(Surface.b.value), abs(Surface.a.value))
     seq = ({"a": Surface.o}, OrderedDict([("a", Surface.b)]))
     keys = [Matcher.mapping_key(i) for i in seq]
     self.assertEqual(1, bisect.bisect_left(keys, Matcher.mapping_key({"a": Surface.a})))
Example #8
0
 def test_bisect(self):
     seq = ({"a": 1}, OrderedDict([("a", 2)]))
     keys = [Matcher.mapping_key(i) for i in seq]
     self.assertEqual(1, bisect.bisect_left(keys, Matcher.mapping_key({"a": 1.5})))
Example #9
0
 def test_single_match(self):
     matcher = Matcher(self.folders)
     rv = list(matcher.options({"pos": 0}))
     self.assertEqual(1, len(rv), rv)
     self.assertEqual({"pos": 0.5}, rv[0].metadata)
Example #10
0
 def test_multi_match(self):
     matcher = Matcher(self.folders)
     rv = list(matcher.options({"pos": 1.5}))
     self.assertEqual(2, len(rv))
     self.assertEqual({"pos": 1}, rv[0].metadata)
     self.assertEqual({"pos": 2}, rv[1].metadata)