Exemplo n.º 1
0
 def __init__(self, poll_delay: int = 30):
     AsyncIOEventEmitter.__init__(self)
     self.poll_delay = poll_delay
     self.last_poll = 0
     self.tx = {}
     self.medium_fee = 0
     self.session = ...
Exemplo n.º 2
0
 def __init__(self, modules: list, loop, poll_delay: int = 2):
     """
     :param modules: modules to poll
     :param poll_delay: how long to wait after polling finishes until polling starts again
     """
     self.modules = modules
     self.poll_delay = poll_delay
     self.loop = loop
     AsyncIOEventEmitter.__init__(self, loop=loop)
Exemplo n.º 3
0
    def __init__(self, options: PuppetMockOptions, name: str = 'puppet-mock'):
        super().__init__(options, name)

        if not options.mocker:
            raise WechatyPuppetMockError('mocker in options is required')
        self.mocker: Mocker = options.mocker

        self.started: bool = False
        self.emitter = AsyncIOEventEmitter()
Exemplo n.º 4
0
 def __init__(self, initial_value, value_forwarder=None):
     """
     Initialize the object.
     initial_value -- the initial value
     value_forwarder -- the method that updates the actual value on the
                        thing
     """
     EventEmitter.__init__(self)
     self.last_value = initial_value
     self.value_forwarder = value_forwarder
Exemplo n.º 5
0
 def __init__(self,
              request,
              signing_secret,
              app_id,
              accepted_event_types=_ACCEPTED_EVENT_TYPES):
     AsyncIOEventEmitter.__init__(self)
     self.app_id = app_id
     self.request = request
     self.event = request['body']
     self.signing_secret = signing_secret
     self.package_info = self.get_package_info()
     self.accepted_event_types = accepted_event_types
Exemplo n.º 6
0
async def init_event_stream (
        event_stream : AsyncIOEventEmitter,
        puppet       : PuppetStub,
) -> None:
    """doc"""
    async for response in puppet.event():
        # print(response)
        if response is not None:
            event_type = EventType(response.type).name
            payload = response.payload
            # print(event_type, payload)
            event_stream.emit(event_type, payload)
Exemplo n.º 7
0
        def peerconn_oniceconnectionstatechange():
            state_change = AsyncIOEventEmitter()

            @state_change.once("failed")
            async def on_failed(error=None):
                log.warning(
                    "\n IceConnectionState failed with error %s "
                    "\n Closing connections to peer id %s", error, peerId)
                self.connection.emit(
                    ConnectionEventType.Error,
                    ConnectionError(
                        f"Negotiation of connection to {peerId} failed."))
                await self.connection.close()

            @state_change.once("closed")
            def on_closed():
                log.info("iceConnectionState is closed, "
                         f"closing connections to {peerId}")
                self.connection.emit(
                    ConnectionEventType.Error,
                    ConnectionError(f"Connection to {peerId} closed."))
                self.connection.close()

            @state_change.once("disconnected")
            def on_disconnected():
                log.info("iceConnectionState is disconnected, "
                         f"closing connections to {peerId}")
                self.connection.emit(
                    ConnectionEventType.Error,
                    ConnectionError(f"Connection to {peerId} disconnected."))
                self.connection.close()

            @state_change.once("completed")
            def on_completed():
                log.debug('iceConnectionState completed for peer id %s',
                          peerId)
                # this function needs to be implemented as in PeerJS
                # https://github.com/peers/peerjs/blob/5e36ba17be02a9af7c171399f7737d8ccb38fbbe/lib/negotiator.ts#L119
                # when the "icecandidate"
                # event handling issue above is resolved
                # peerConnection.remove_listener("icecandidate",
                #       peerconn_onicecandidate)
                pass

            log.debug('iceConnectionState event: %s',
                      peerConnection.iceConnectionState)
            # forward connection stage change event to local handlers
            state_change.emit(peerConnection.iceConnectionState)
            # notify higher level connection listeners
            self.connection.emit(ConnectionEventType.IceStateChanged,
                                 peerConnection.iceConnectionState)
Exemplo n.º 8
0
 def __init__(self,
              initial_value=None,
              read_forwarder=None,
              write_forwarder=None):
     """
     Initialize the object.
     initial_value -- the initial value
     value_forwarder -- the method that updates the actual value on the
                        thing
     """
     EventEmitter.__init__(self)
     self._value = initial_value
     self.read_forwarder = read_forwarder
     self.write_forwarder = write_forwarder
Exemplo n.º 9
0
async def main():
    """doc"""
    channel = Channel(host="127.0.0.1", port=8788)
    puppet = PuppetStub(channel)

    event_stream = AsyncIOEventEmitter()
    event_stream.on('EVENT_TYPE_DONG', \
        lambda payload: print('on(dong) %s' % payload))

    await asyncio.gather(
        loop(lambda: puppet.ding(data='haha')),
        init_event_stream(event_stream, puppet),
    )

    channel.close()
Exemplo n.º 10
0
    def __init__(self, options: PuppetOptions, name: str = 'puppet_service'):
        """init PuppetService from options or envrionment

        Args:
            options (PuppetOptions): the configuration of PuppetService
            name (str, optional): [description]. Defaults to 'service_puppet'.

        Raises:
            WechatyPuppetConfigurationError: raise Error when configuraiton occur error
        """
        token, endpoint = get_token(), get_endpoint()

        if not options.token and not token:
            raise WechatyPuppetConfigurationError(
                'wechaty-puppet-service: token not found. please set '
                'environment<WECHATY_PUPPET_SERVICE_TOKEN|TOKEN|token> as token'
            )

        options.token = options.token or token
        options.end_point = options.end_point or endpoint

        super().__init__(options, name)

        self.channel: Optional[Channel] = None
        self._puppet_stub: Optional[PuppetStub] = None

        self._event_stream: AsyncIOEventEmitter = AsyncIOEventEmitter()

        self.login_user_id: Optional[str] = None
Exemplo n.º 11
0
async def init_app(aiohttp_client, queue_ttl=30):
    app = web.Application()
    app.event_emitter = AsyncIOEventEmitter()

    # Migration routes as a subapp
    migration_app = web.Application()
    MigrationAdminApi(migration_app)
    app.add_subapp("/migration/", migration_app)

    # init a db adapter explicitly to be used for the api requests.
    # Skip all creation processes, these are handled with migration service and init_db
    db_conf = get_test_dbconf()
    db = AsyncPostgresDB(name='api')
    await db._init(db_conf=db_conf, create_triggers=False)

    cache_store = CacheStore(db=db, event_emitter=app.event_emitter)

    app.AutoCompleteApi = AutoCompleteApi(app, db)
    FlowApi(app, db)
    RunApi(app, db)
    StepApi(app, db)
    TaskApi(app, db, cache_store)
    MetadataApi(app, db)
    ArtificatsApi(app, db)
    TagApi(app, db)
    FeaturesApi(app)
    PluginsApi(app)
    LogApi(app, db)
    CardsApi(app, db)

    Websocket(app, db, app.event_emitter, queue_ttl)

    AdminApi(app, cache_store)

    return await aiohttp_client(app)
Exemplo n.º 12
0
    def __init__(
        self,
        id: str,
        localId: str,
        track: MediaStreamTrack,
        rtpParameters: RtpParameters,
        stopTracks: bool,
        disableTrackOnPause: bool,
        zeroRtpOnPause: bool,
        rtpSender: Optional[RTCRtpSender] = None,
        appData: Optional[dict] = None,
        loop=None
    ):
        super(Producer, self).__init__(loop=loop)

        # Closed flag.
        self._closed: bool = False
        # Observer instance.
        self._observer: AsyncIOEventEmitter = AsyncIOEventEmitter()

        self._id = id
        self._localId = localId
        self._rtpSender = rtpSender
        self._track = track
        self._rtpParameters = rtpParameters
        # NOTE: 'AudioStreamTrack' object has no attribute 'enabled'
        # self._paused = (not track.enabled) if disableTrackOnPause else False
        self._paused = False if disableTrackOnPause else False
        self._maxSpatialLayer: Optional[int] = None
        self._stopTracks = stopTracks
        self._disableTrackOnPause = disableTrackOnPause
        self._zeroRtpOnPause = zeroRtpOnPause
        self._appData = appData

        self._handleTrack()
    def __init__(self, options: Optional[OfficialAccountPuppetOptions]):
        if not options:
            options = OfficialAccountPuppetOptions(
                app_id=config.app_id,
                app_secret=config.app_secret,
                token=config.token,
                port=config.port)
        if not options.app_id:
            raise WechatyPuppetConfigurationError(
                'WECHATY_PUPPET_OA_APP_ID environment variable not found')
        if not options.app_secret:
            raise WechatyPuppetConfigurationError(
                'WECHATY_PUPPET_OA_APP_SECRET environment variable not found')
        if not options.token:
            raise WechatyPuppetConfigurationError(
                'WECHATY_PUPPET_OA_TOKEN environment variable not found')

        super().__init__(options, 'puppet-official-account')

        self.oa: OfficialAccount = OfficialAccount(
            options=OfficialAccountOption(app_id=options.app_id,
                                          app_secret=options.app_secret,
                                          port=options.port,
                                          token=options.token))
        self._event_emitter: AsyncIOEventEmitter = AsyncIOEventEmitter()
Exemplo n.º 14
0
    def __init__(self, app, db, event_emitter=None):
        self.event_emitter = event_emitter or AsyncIOEventEmitter()
        self.db = db
        self.logger = logging.getLogger("ListenNotify")

        self.loop = asyncio.get_event_loop()
        self.loop.create_task(self._init(self.db.pool))
Exemplo n.º 15
0
async def test_asyncio_once_emit(event_loop):
    """Test that EventEmitters also wrap coroutines when using once
    """

    ee = AsyncIOEventEmitter(loop=event_loop)

    should_call = Future(loop=event_loop)

    @ee.once('event')
    async def event_handler():
        should_call.set_result(True)

    ee.emit('event')

    result = await wait_for(should_call, 0.1)

    assert result == True
Exemplo n.º 16
0
    def __init__(
            self,
            id: str,
            input: T,
            runner: AbstractJobRunner,
            # TODO: Typing callable kwargs?
            coro: Callable[[T, Job[T, S], ThreadPoolExecutor], Awaitable[S]],
            threadpool: Union[ThreadPoolExecutor, None] = None):
        """Initialises and starts Job

        :param self: class instance
        :param id: unique job ID for the runner
        :param input: input data for the job
        :param runner: runner in which the job is being executed
        :param coro: the async function to execute with input and context, returning the result
        :param threadpool: optional threadpool to pass to coro if supplied
        """
        self.id = id
        self.input = input
        self.runner = runner
        AsyncIOEventEmitter.__init__(self)
        self.task = create_task(coro(input, self, threadpool=threadpool))

        def onTaskDone(task):
            """Task done handler to publish complete (success) & critical (fail) events"""
            try:
                err = task.exception()
                if (err):
                    self.emit("critical", err)
                else:
                    result = task.result()
                    self.emit("complete", result)
            except CancelledError as err:
                self.emit(
                    "warning",
                    CancelledError("onTaskDone called after task cancelled").
                    with_traceback(err.__traceback__))
                LOGGER.warning("Shouldn't see onTaskDone when cancelled??")
            except InvalidStateError as err:
                self.emit(
                    "error",
                    InvalidStateError(
                        "onTaskDone called before task finished: Risk of zombie task"
                    ).with_traceback(err.__traceback__))

        self.task.add_done_callback(onTaskDone)
Exemplo n.º 17
0
 def __init__(self, backtesting=False, symbol='tBTCUSD', indicators={}, logLevel='INFO',
     exchange_type=ExchangeType.EXCHANGE):
   self.exchange_type = exchange_type
   self.marketData = {}
   self.positions = {}
   self.lastPrice = {}
   self.closedPositions = []
   self.is_ready = False
   self.indicators = indicators
   self.candle_price_key = 'close'
   self.backtesting = backtesting
   self.symbol = symbol
   self.events = AsyncIOEventEmitter()
   # initialise custom logger
   self.logLevel = logLevel
   self.logger = CustomLogger('HFStrategy', logLevel=logLevel)
   super(Strategy, self).__init__()
Exemplo n.º 18
0
async def test_asyncio_emit(event_loop):
    """Test that AsyncIOEventEmitter can handle wrapping
    coroutines
    """

    ee = AsyncIOEventEmitter(loop=event_loop)

    should_call = Future(loop=event_loop)

    @ee.on('event')
    async def event_handler():
        should_call.set_result(True)

    ee.emit('event')

    result = await wait_for(should_call, 0.1)

    assert result is True
Exemplo n.º 19
0
    def __init__(self, token, session=None):
        AsyncIOEventEmitter.__init__(self)

        self.logger = logging.getLogger('Telegram')

        self.token = token
        self.update_id = 0

        self.runner = None
        self.webhook = False
        self.webhook_url = ''

        if session is None:
            self.logger.debug('Creating new session')
            self.session = aiohttp.ClientSession()
            self.user_provided_session = False
        else:
            self.logger.debug('Using user provided session')
            self.session = session
            self.user_provided_session = True
Exemplo n.º 20
0
async def test_sync_error(event_loop):
    """Test that regular functions have the same error handling as coroutines
    """
    ee = AsyncIOEventEmitter(loop=event_loop)

    should_call = Future(loop=event_loop)

    @ee.on('event')
    def sync_handler():
        raise PyeeTestError()

    @ee.on('error')
    def handle_error(exc):
        should_call.set_result(exc)

    ee.emit('event')

    result = await wait_for(should_call, 0.1)

    assert isinstance(result, PyeeTestError)
Exemplo n.º 21
0
    def __init__(self, event_name: str, db, event_emitter=None, cache=None):
        self.watched = {}
        # Handle HB Events
        self.event_emitter = event_emitter or AsyncIOEventEmitter()
        event_emitter.on(event_name, self.heartbeat_handler)
        self.db = db
        self.cache = cache

        # Start heartbeat watcher
        self.loop = asyncio.get_event_loop()
        self.loop.create_task(self.check_heartbeats())
Exemplo n.º 22
0
async def test_sync_error(event_loop):
    """Test that regular functions have the same error handling as coroutines
    """
    ee = AsyncIOEventEmitter(loop=event_loop)

    should_call = Future(loop=event_loop)

    @ee.on('event')
    def sync_handler():
        raise PyeeTestError()

    @ee.on('error')
    def handle_error(exc):
        should_call.set_result(exc)

    ee.emit('event')

    result = await wait_for(should_call, 0.1)

    assert isinstance(result, PyeeTestError)
Exemplo n.º 23
0
async def test_asyncio_error(event_loop):
    """Test that AsyncIOEventEmitter can handle errors when
    wrapping coroutines
    """
    ee = AsyncIOEventEmitter(loop=event_loop)

    should_call = Future(loop=event_loop)

    @ee.on('event')
    async def event_handler():
        raise PyeeTestError()

    @ee.on('error')
    def handle_error(exc):
        should_call.set_result(exc)

    ee.emit('event')

    result = await wait_for(should_call, 0.1)

    assert isinstance(result, PyeeTestError)
    def __init__(self,
                 name=None,
                 emitter=None,
                 thread_weights=None,
                 executor_factory: _executor_factory_type = None):
        self.name = name or self.__class__.__name__
        self.thread_weights = thread_weights or [1]

        self._emitter = emitter or AsyncIOEventEmitter()
        self._flag = ThreadFlag(ThreadFlag.pending)
        self._handlers: Dict[Callable[..., None], Parameter] = {}
        self._executor_factory = executor_factory or (
            lambda: ThreadPoolExecutor(thread_name_prefix=self.name))
Exemplo n.º 25
0
 def __init__(
     self,
     address: str = None,
     testnet: bool = False,
     keepalive: bool = True,
     loop: asyncio.AbstractEventLoop = None,
     url: str = None,
 ) -> None:
     if not url:
         self.url = TESTNET_URL if testnet else MAINNET_URL
     else:
         self.url = url
     self.address = address
     self._session = aiohttp.ClientSession()
     self._ws: Optional[aiohttp.ClientWebSocketResponse] = None
     self._loop = loop or asyncio.get_event_loop()
     self._events = AsyncIOEventEmitter(loop=self._loop)
     self._sub_queue: List[Tuple[str, dict]] = []
     self._keepalive = keepalive
     self._keepalive_task: Optional[asyncio.Future] = None
     self._open = False
     self._testnet = testnet
Exemplo n.º 26
0
    def __init__(
        self,
        token: str,
        last_date: datetime = None,
        handlers: List[Handler] = None,
        webhook_url: str = None,
        loop=None,
        emitter: AsyncIOEventEmitter = None,
        disable_groupfeed: bool = False,
        disable_mapfeed: bool = False,
        session=None,
    ):
        self._closed = False
        if not handlers:
            self.handlers = []
        else:
            self.handlers = handlers

        self.webhook_url = webhook_url
        self.apitoken = token

        self.last_date = last_date or datetime.utcfromtimestamp(0)
        self.loop = loop or asyncio.get_event_loop()
        self.last_event = None
        self.disable_user = disable_groupfeed
        self.disable_map = disable_mapfeed
        self.tasks = []

        self.session = session or aiohttp.ClientSession()
        self.api = APIClient(self.session, self.apitoken)
        self.web = WebClient(self.session, app=self)

        self.group_ids = [
            # https://github.com/ppy/osu-web/blob/master/app/Models/UserGroup.php
            4,  # GMT
            7,  # NAT
            16,  # Alumni
            28,  # Full BN
            32,  # Probation BN
        ]
        self.last_users = dict()

        for gid in self.group_ids:
            self.last_users[gid] = list()

        self.emitter = emitter or AsyncIOEventEmitter()
        if handlers:
            for handler in handlers:
                handler.register_emitter(self.emitter)
                handler.app = self
Exemplo n.º 27
0
async def test_asyncio_cancellation(event_loop):
    """Test that AsyncIOEventEmitter can handle Future cancellations"""

    cancel_me = Future(loop=event_loop)
    should_not_call = Future(loop=event_loop)

    ee = AsyncIOEventEmitter(loop=event_loop)

    @ee.on('event')
    async def event_handler():
        cancel_me.cancel()

    @ee.on('error')
    def handle_error(exc):
        should_not_call.set_result(None)

    ee.emit('event')

    try:
        await wait_for(should_not_call, 0.1)
    except TimeoutError:
        pass
    else:
        raise PyeeTestError()
Exemplo n.º 28
0
    def __init__(self, options: PuppetOptions, name: str = 'hostie_puppet'):
        super().__init__(options, name)

        if options.token is None:
            if WECHATY_PUPPET_HOSTIE_TOKEN is None:
                raise WechatyPuppetConfigurationError('wechaty-puppet-hostie: token not found.')
            options.token = WECHATY_PUPPET_HOSTIE_TOKEN

        if options.end_point is None and WECHATY_PUPPET_HOSTIE_ENDPOINT is not None:
            options.end_point = WECHATY_PUPPET_HOSTIE_ENDPOINT

        self.channel: Optional[Channel] = None
        self._puppet_stub: Optional[PuppetStub] = None

        self._event_stream: AsyncIOEventEmitter = AsyncIOEventEmitter()

        self.login_user_id: Optional[str] = None
    def __init__(self, options: PuppetOptions, name: str = 'padplus_puppet'):
        super(PadPlusPuppet, self).__init__(options, name)

        if options.token is None:
            if WECHATY_PUPPET_PADPLUS_TOKEN is None:
                raise Exception('wechaty-puppet-hostie: token not found.')
            options.token = WECHATY_PUPPET_PADPLUS_TOKEN

        if options.end_point is None \
                and WECHATY_PUPPET_PADPLUS_ENDPOINT is not None:
            options.end_point = WECHATY_PUPPET_PADPLUS_ENDPOINT

        self.channel: Optional[Channel] = None
        self.puppet_stub: Optional[PuppetStub] = None

        self._event_stream: AsyncIOEventEmitter = AsyncIOEventEmitter()

        self.login_user_id: Optional[str] = None
Exemplo n.º 30
0
    def __init__(self,
                 app,
                 db,
                 event_emitter=None,
                 queue_ttl: int = WS_QUEUE_TTL_SECONDS,
                 cache=None):
        self.event_emitter = event_emitter or AsyncIOEventEmitter()
        self.db = db
        self.queue = TTLQueue(queue_ttl)
        self.task_refiner = TaskRefiner(
            cache=cache.artifact_cache) if cache else None
        self.artifact_refiner = ArtifactRefiner(
            cache=cache.artifact_cache) if cache else None
        self.logger = logging.getLogger("Websocket")

        event_emitter.on('notify', self.event_handler)
        app.router.add_route('GET', '/ws', self.websocket_handler)
        self.loop = asyncio.get_event_loop()
Exemplo n.º 31
0
class MockWebsocket():
    def __init__(self):
        self.events = AsyncIOEventEmitter()
        self.saved_items = []
        self.emitted_items = []

    def on(self, *args, **kwargs):
        self.events.on(*args, **kwargs)

    def once(self, *args, **kwargs):
        self.events.once(*args, **kwargs)

    def _emit(self, event, *args, **kwargs):
        # save published items for testing
        self.emitted_items += [{
            'time': int(round(time.time() * 1000)),
            'data': {
                'event': event,
                'args': args,
                'kwargs': kwargs
            }
        }]
        self.events.emit(event, *args, **kwargs)

    def remove_all_listeners(self, *args, **kwargs):
        self.events.remove_all_listeners(*args, **kwargs)

    def cancel_order(self, *args, **kawargs):
        pass

    def submit_order(self, *args, **kawargs):
        pass

    def get_emitted_items(self):
        return self.emitted_items

    def get_last_emitted_item(self):
        return self.emitted_items[-1:][0]

    def get_emitted_items_count(self):
        return len(self.emitted_items)