Exemplo n.º 1
0
def fixture_data():
    peerplays.clear()
    BettingMarkets.clear_cache()
    Rules.clear_cache()
    BettingMarketGroups.clear_cache()
    Proposals.clear_cache()
    Witnesses.clear_cache()
    Events.clear_cache()
    EventGroups.clear_cache()
    Sports.clear_cache()

    with open(os.path.join(os.path.dirname(__file__), "fixtures.yaml")) as fid:
        data = yaml.safe_load(fid)

    [Account(x) for x in data.get("accounts", [])]
    [Account(x).store(x, "name") for x in data.get("accounts", [])]
    Witnesses.cache_objects([Witness(x) for x in data.get("witnesses", [])])
    Sports.cache_objects([Sport(x) for x in data.get("sports", [])])
    EventGroups.cache_objects(
        [EventGroup(x) for x in data.get("eventgroups", [])])
    Events.cache_objects([Event(x) for x in data.get("events", [])])
    BettingMarketGroups.cache_objects(
        [BettingMarketGroup(x) for x in data.get("bettingmarketgroups", [])])
    BettingMarkets.cache_objects(
        [BettingMarket(x) for x in data.get("bettingmarkets", [])])
    Rules.cache_objects([Rule(x) for x in data.get("rules", [])])
    [Bet(x) for x in data.get("bets", [])]

    proposals = []
    for proposal in data.get("proposals", []):
        ops = list()
        for _op in proposal["operations"]:
            for opName, op in _op.items():
                ops.append([operations[opName], op])
        # Proposal!
        proposal_id = proposal["proposal_id"]
        proposal_data = {
            "available_active_approvals": [],
            "available_key_approvals": [],
            "available_owner_approvals": [],
            "expiration_time": "2018-05-29T10:23:13",
            "id": proposal_id,
            "proposed_transaction": {
                "expiration": "2018-05-29T10:23:13",
                "extensions": [],
                "operations": ops,
                "ref_block_num": 0,
                "ref_block_prefix": 0,
            },
            "proposer": "1.2.7",
            "required_active_approvals": ["1.2.1"],
            "required_owner_approvals": [],
        }
        proposals.append(Proposal(proposal_data))

    Proposals.cache_objects(proposals, "1.2.1")
    Proposals.cache_objects(proposals, "witness-account")
Exemplo n.º 2
0
    def __init__(self,
                 message,
                 lookup_instance,
                 config,
                 clear_caches=True,
                 **kwargs):
        self.message = message
        self.lookup = lookup_instance
        self.config = config

        # Obtain data for unique key
        # The "id" contains everything we need to identify an individual event
        # which itself contains at least the sport, and the teams

        # Get the id (internally used only)
        self.id = message.get("id")

        # Incident Storage
        if "storage" in kwargs and kwargs["storage"]:
            self.storage = kwargs["storage"]
        else:
            self.storage = factory.get_incident_storage(
                kwargs.get("mongodb", None), purge=kwargs.get("purge", False))

        # Normalize incident
        self.normalizer = IncidentsNormalizer(
            chain=lookup_instance._network_name)
        self.normalize(message)

        # Let's clear the caches for Proposals and events
        # We need to do so because of the internal pypeerplays cache.
        # The cache reduces the API calls to the backend and thus latency.
        # However, to be sure the event hasn't been created since before the
        # cache has expired, we force a refresh from the blockchain.
        if clear_caches:
            Events.clear_cache()
            Proposals.clear_cache()

        # Try obtain the sport
        self.sport = LookupSport(self.id.get("sport"))

        # Given the sport, try to obtain the league (event group)
        self.eventgroup = LookupEventGroup(self.sport,
                                           self.id.get("event_group_name"))

        self.event = None  # Will be filled in after receiving a trigger

        # Get Teams from query
        self.teams = [self.id.get("home"), self.id.get("away")]

        # Get start time from query
        self.start_time = parse(self.id.get("start_time", ""))
Exemplo n.º 3
0
    def test_evg(self):
        EventGroups("1.20.0")
        EventGroups("1.20.0")

        Sports()
        Sports()

        Events("1.21.12")
        Events("1.21.12")

        BettingMarketGroups("1.22.12")
        BettingMarketGroups("1.22.12")

        BettingMarkets("1.24.241")
        BettingMarkets("1.24.241")
Exemplo n.º 4
0
 def test_lists(self):
     self.assertIsInstance(Sports(), list)
     self.assertIsInstance(EventGroups("1.20.2"), list)
     self.assertIsInstance(Events("1.21.2"), list)
     self.assertIsInstance(BettingMarketGroups("1.22.2"), list)
     self.assertIsInstance(Rules(), list)
     self.assertIsInstance(BettingMarkets("1.24.2"), list)
Exemplo n.º 5
0
    def find_id(self, **kwargs):
        """ Try to find an id for the object of the  lookup on the
            blockchain

            .. note:: This only checks if a sport exists with the same name in
                       **ENGLISH**!
        """
        # In case the parent is a proposal, we won't
        # be able to find an id for a child
        parent_id = self.parent_id
        if not self.valid_object_id(parent_id):
            return

        events = Events(self.parent_id, peerplays_instance=self.peerplays)

        find_id_search = kwargs.get(
            "find_id_search",
            [
                comparators.cmp_name("en"),
                comparators.cmp_start_time(),
                comparators.cmp_event_group(),
            ],
        )

        for event in events:
            if all([
                    # compare by using 'all' the funcs in find_id_search
                    func(self, event) for func in find_id_search
            ]):
                return event["id"]
Exemplo n.º 6
0
    def find_event(cls, sport_identifier, eventgroup_identifier, teams,
                   start_time):
        """ This class method is used to find an event by providing:

            :param str sport_identifier: Identifier string for the sport
            :param str eventgroup_identifier: Identifier string for the
                eventgroup/league
            :param list teams: list of teams
            :param datetime.datetime start_time: Time of start

        """
        sport = LookupSport(sport_identifier)
        eventgroup = LookupEventGroup(sport, eventgroup_identifier)
        events = Events(eventgroup.id)  # This is a pypeerplays class!
        # Format teams into proper names according to event scheme
        names = substitution(teams, eventgroup["eventscheme"]["name"])
        names = [[k, v] for k, v in names.items()]
        for event in events:
            if (any([x in event["name"] for x in names])
                    and formatTime(start_time) == event["start_time"]):
                return cls(
                    id=event["id"],
                    teams=teams,
                    eventgroup_identifier=eventgroup_identifier,
                    sport_identifier=sport_identifier,
                    start_time=start_time,
                    season={x[0]: x[1]
                            for x in event["season"]},
                )
    def retrieve_data(self):
        """Depending on your config file returns all the events of the blockchain as a list of dicts"""
        self._set_data()
        data = []

        if self.sport_id is None and self.eventgroup_id is None:
            # no sport id is given, therefore we want to check all events
            for sport in Sports():
                for eventgroup in EventGroups(sport["id"]):
                    for event in Events(eventgroup["id"]):
                        self._append_event(data, event)

        # sport id is given: only check its' events
        elif self.eventgroup_id is None:
            # no eventgroup id is given, therefore we check all of the sports events
            try:
                eventgroups = EventGroups(self.sport_id)
            except UnhandledRPCError:
                logging.warning(
                    "Unhandled RPC Error in {0} for sport id. Sport ids have to start with 1.20.<>"
                    .format(self.get_source_name()))
                return
            for eventgroup in eventgroups:
                for event in Events(eventgroup["id"]):
                    self._append_event(data, event)

        # Eventgroup id is given: only check its' events
        else:
            try:
                events = Events(self.eventgroup_id)
            except UnhandledRPCError:
                logging.warning(
                    "Unhandled RPC Error in {0} for event id. Eventgroup ids have start with 1.21.<>"
                    .format(self.get_source_name()))
            for event in events:
                self._append_event(data, event)

        self._set_data(data)
Exemplo n.º 8
0
 def getEvents(self, eventGroupId):
     if eventGroupId == "all":
         all_events = []
         sports = self.getSports()
         for sport in sports:
             eventgroups = self.getEventGroups(sport["id"])
             for eventgroup in eventgroups:
                 events = self.getEvents(eventgroup["id"])
                 for event in events:
                     all_events.append(event)
         return all_events
     if not eventGroupId:
         raise NonScalableRequest
     try:
         return Events(eventGroupId,
                       peerplays_instance=self.get_node()).events
     except Exception as ex:
         raise NodeException("Events could not be loaded: {}".format(
             self._get_exception_message(ex)))