示例#1
0
    def build(cls, now=None, middle=None, long_ago=None, get_earliest=False):
        """
        Create TimeGhost objects from triplets of events.
        """
        timeghost = TimeGhost(now=now, middle=middle, long_ago=long_ago)

        # Set the .now Event to "now" if not specified:
        if timeghost.now is None:
            timeghost.set(Event.now(), "now")

        # Generate a .middle Event if not specified:
        if timeghost.middle is None:
            timeghost.set(Event.get_random(), "middle")

        # Generate a .long_ago Event if not specified:
        if timeghost.long_ago is None:
            try:
                long_ago = timeghost.find_best_long_ago(get_earliest)
            except TimeGhostError:
                long_ago = Event.get_earliest()
            timeghost.set(long_ago, "long_ago")

        # Return a new instance to insure the init validation is run properlhy:
        return TimeGhost(timeghost.now, timeghost.middle, timeghost.long_ago)
示例#2
0
    def build_from_timeghost(cls, timeghost):
        """Return a completed TimeGhost based on the partial TimeGhost
        request."""

        logging.debug("TimeGhostFactory.build: %s", timeghost)

        # Timeghost between now and a random event
        if timeghost.middle is None:
            timeghost.middle = Event.get_random()
        # Timeghost for now and given middle, no long_ago
        elif timeghost.now is not None and \
             timeghost.middle is not None and \
             timeghost.long_ago is None:
            pass
        # Fully specified timeghost. Just return it.
        elif timeghost.now is not None and \
             timeghost.middle is not None and \
             timeghost.long_ago is not None:
            return timeghost
        # Otherwise error
        else:
            raise TimeGhostError("bad case in TimeGhostFactory.build for %s",
                                 timeghost)

        logging.debug("TimeGhostFactory.build: %s", timeghost)
        try:
            best_long_ago = timeghost.find_best_long_ago()
        except TimeGhostError:
            best_long_ago = Event.get_earliest()
        logging.debug("TimeGhostFactory.build: %s", best_long_ago)

        output_timeghost = TimeGhost(now=timeghost.now,
                                     middle=timeghost.middle,
                                     long_ago=best_long_ago)
        logging.debug("TimeGhostFactory.build: %s", output_timeghost)
        return output_timeghost