示例#1
0
        def close(self):
            """Similar to 'close' of file descriptor.
            """
            if self._handle:
                try:
                    flags = win32pipe.GetNamedPipeInfo(self._handle)[0]
                except:
                    flags = 0

                if flags & win32con.PIPE_SERVER_END:
                    win32pipe.DisconnectNamedPipe(self._handle)
                # TODO: if pipe, need to call FlushFileBuffers?

                def _close_(self, rc, n):
                    win32file.CloseHandle(self._handle)
                    self._overlap = None
                    _AsyncFile._notifier.unregister(self._handle)
                    self._handle = None
                    self._read_result = self._write_result = None
                    self._read_coro = self._write_coro = None
                    self._buflist = []

                if self._overlap.object:
                    self._overlap.object = partial_func(_close_, self)
                    win32file.CancelIo(self._handle)
                else:
                    _close_(self, 0, 0)
示例#2
0
            def _read(size, full):
                if size > 0:
                    count = size
                else:
                    count = 16384
                try:
                    buf = os.read(self._fileno, count)
                except (OSError, IOError) as exc:
                    if exc.errno in (errno.EAGAIN, errno.EWOULDBLOCK):
                        return
                    else:
                        raise
                except:
                    self._notifier.clear(self, _AsyncPoller._Read)
                    self._read_coro.throw(*sys.exc_info())
                    self._read_coro = self._read_task = None
                    return

                if buf:
                    if size > 0:
                        size -= len(buf)
                        # assert size >= 0
                        if size == 0:
                            full = False
                    self._buflist.append(buf)
                    if full:
                        self._read_task = partial_func(_read, size, full)
                        return
                if self._buflist:
                    buf, self._buflist = ''.join(self._buflist), []
                self._notifier.clear(self, _AsyncPoller._Read)
                self._read_coro._proceed_(buf)
                self._read_coro = self._read_task = None
示例#3
0
    async def show_string_description(self, command_context, command_name,
                                      description):
        """
        Shows the given string description.
        
        Parameters
        ----------
        command_context : ``CommandContext``
            The called command's context.
        command_name : `str`
            The respective command's display name.
        description : `str`
            The respective command's description.
        """
        if len(description) < 2048:
            embed = Embed(command_name, description)

            embed_postprocessor = self.embed_postprocessor
            if (embed_postprocessor is not None):
                embed_postprocessor(command_context, embed)

            await Closer(command_context.client,
                         command_context.message.channel,
                         embed,
                         check=partial_func(check_user,
                                            command_context.message.author))

        else:
            description_parts = chunkify(description.splitlines())
            embeds = [
                Embed(command_name, description_part)
                for description_part in description_parts
            ]

            embed_postprocessor = self.embed_postprocessor
            if (embed_postprocessor is not None):
                for embed in embeds:
                    embed_postprocessor(command_context, embed)

            await Pagination(command_context.client,
                             command_context.message.channel,
                             embeds,
                             check=partial_func(
                                 check_user, command_context.message.author))
示例#4
0
def sanitize_mentions(content, guild=None):
    """
    Sanitizes the given content, removing the mentions from it.
    
    Parameters
    ----------
    content : `None`, `str`
        The content to sanitize.
    guild : `None`, ``Guild`` = `None`, Optional
        Respective context to look up guild specific names of entities.
    
    Returns
    -------
    content : `None`, `str`
    """
    if (content is None):
        return

    transformations = {
        '@everyone': '@\u200beveryone',
        '@here': '@\u200bhere',
    }

    for id_ in USER_MENTION_RP.findall(content):
        id_ = int(id_)
        user = USERS.get(id_, None)
        if (user is None) or user.partial:
            sanitized_mention = '@invalid-user'
        else:
            sanitized_mention = '@' + user.name_at(guild)

        transformations[f'<@{id_}>'] = sanitized_mention
        transformations[f'<@!{id_}>'] = sanitized_mention

    for id_ in CHANNEL_MENTION_RP.findall(content):
        id_ = int(id_)
        channel = CHANNELS.get(id_, None)
        if (channel is None) or channel.partial:
            sanitized_mention = '@deleted channel'
        else:
            sanitized_mention = '#' + channel.name

        transformations[f'<#{id_}>'] = sanitized_mention

    for id_ in ROLE_MENTION_RP.findall(content):
        id_ = int(id_)
        role = ROLES.get(id_, None)
        if (role is None) or role.partial:
            sanitized_mention = '@deleted role'
        else:
            sanitized_mention = '@' + role.name

        transformations[f'<@&{id_}>'] = sanitized_mention

    return re_compile('|'.join(transformations)).sub(
        partial_func(sanitise_mention_escaper, transformations), content)
示例#5
0
    async def show_command(self, command_context, command):
        """
        SHows the given command's description.
        
        Parameters
        ----------
        command_context : ``CommandContext``
            Respective command's context.
        command : ``Command``
            The respective command.
        """
        description = command.description
        if isinstance(description, str):
            await self.show_string_description(command_context,
                                               command.display_name,
                                               description)
            return

        if callable(description):
            embed = await description(command_context)
            embed_type = type(embed)
            if issubclass(embed_type, EmbedBase):
                await Closer(command_context.client,
                             command_context.message.channel,
                             embed,
                             check=partial_func(
                                 check_user, command_context.message.author))
                return

            if issubclass(embed_type, (tuple, set)) or hasattr(
                    embed_type, '__getitem__') and hasattr(
                        embed_type, '__len__'):
                await Pagination(command_context.client,
                                 command_context.message.channel,
                                 embed,
                                 check=partial_func(
                                     check_user,
                                     command_context.message.author))
                return

            # No more case, go back to default

        await self.show_autogenerated_description(command_context, command)
示例#6
0
文件: asyncfile.py 项目: pgiri/pycos
        def write(self, buf, full=False, timeout=None):
            """Write data in 'buf' to file. If 'full' is True, the function
            waits till all data in buf is written; otherwise, it waits
            until one write completes. It returns length of data written.

            If no data has been written before timeout, then
            IOError('timedout') will be thrown.

            If timeout is given and full is True and timeout expires
            before all the data could be written, it returns length of
            data written before timeout if any data has been written.

            Must be used with 'yield' as
            'n = yield fd.write(buf)' to write (some) data in buf.
            """

            def _write(view, written):
                try:
                    n = os.write(self._fileno, view)
                except (OSError, IOError) as exc:
                    if exc.errno in (errno.EAGAIN, errno.EINTR):
                        n = 0
                    else:
                        self._notifier.clear(self, _AsyncPoller._Write)
                        if full:
                            view.release()
                        self._write_task.throw(*sys.exc_info())
                        self._write_task = self._write_fn = None
                        return
                written += n
                if n == len(view) or not full:
                    self._notifier.clear(self, _AsyncPoller._Write)
                    if full:
                        view.release()
                    self._write_task._proceed_(written)
                    self._write_task = self._write_fn = None
                else:
                    view = view[n:]
                    self._write_fn = partial_func(_write, view, written)

            if not self._pycos:
                self._pycos = Pycos.scheduler()
                self._notifier = self._pycos._notifier
                if hasattr(self._fd, '_fileno'):
                    self._notifier.unregister(self._fd)
            if full:
                view = memoryview(buf)
            else:
                view = buf
            self._timeout = timeout
            self._write_task = Pycos.cur_task(self._pycos)
            self._write_task._await_()
            self._write_fn = partial_func(_write, view, 0)
            self._notifier.add(self, _AsyncPoller._Write)
示例#7
0
async def in_role(
    client,
    event,
    role_1: ('role', 'Select a role.'),
    role_2: ('role', 'Double role!') = None,
    role_3: ('role', 'Triple role!') = None,
    role_4: ('role', 'Quadra role!') = None,
    role_5: ('role', 'Penta role!') = None,
    role_6: ('role', 'Epic!') = None,
    role_7: ('role', 'Legendary!') = None,
    role_8: ('role', 'Mythical!') = None,
    role_9: ('role', 'Lunatic!') = None,
):
    """Shows the users with the given roles."""
    guild = event.guild
    if guild is None:
        abort('Guild only command.')

    if guild not in client.guild_profiles:
        abort('I must be in the guild to do this.')

    roles = set()
    for role in role_1, role_2, role_3, role_4, role_5, role_6, role_7, role_8, role_9:
        if role is None:
            continue

        if role.guild is guild:
            roles.add(role)
            continue

        abort(f'Role {role.name}, [{role.id}] is bound to an other guild.')

    users = []
    for user in guild.users.values():
        try:
            guild_profile = user.guild_profiles[guild]
        except KeyError:
            continue

        guild_profile_roles = guild_profile.roles
        if guild_profile_roles is None:
            continue

        if not roles.issubset(guild_profile_roles):
            continue

        users.append(user)

    pages = InRolePageGetter(users, guild, roles)

    await Pagination(client,
                     event,
                     pages,
                     check=partial_func(in_role_pagination_check, event.user))
示例#8
0
        def write(self, buf, full=False, timeout=None):
            """Write data in 'buf' to file. If 'full' is True, the function
            waits till all data in buf is written; otherwise, it waits
            until one write completes. It returns length of data written.

            If no data has been written before timeout, then
            IOError('timedout') will be thrown.

            If timeout is given and full is True and timeout expires
            before all the data could be written, it returns length of
            data written before timeout if any data has been written.

            Must be used with 'yield' as
            'n = yield fd.write(buf)' to write (some) data in buf.
            """

            def _write(view, written):
                try:
                    n = os.write(self._fileno, view)
                except (OSError, IOError) as exc:
                    if exc.errno in (errno.EAGAIN, errno.EINTR):
                        n = 0
                    else:
                        self._notifier.clear(self, _AsyncPoller._Write)
                        if full:
                            view.release()
                        self._write_coro.throw(*sys.exc_info())
                        self._write_coro = self._write_task = None
                        return
                written += n
                if n == len(view) or not full:
                    self._notifier.clear(self, _AsyncPoller._Write)
                    if full:
                        view.release()
                    self._write_coro._proceed_(written)
                    self._write_coro = self._write_task = None
                else:
                    view = view[n:]
                    self._write_task = partial_func(_write, view, written)

            if not self._asyncoro:
                self._asyncoro = AsynCoro.scheduler()
                self._notifier = self._asyncoro._notifier
                if hasattr(self._fd, '_fileno'):
                    self._notifier.unregister(self._fd)
            if full:
                view = memoryview(buf)
            else:
                view = buf
            self._timeout = timeout
            self._write_coro = AsynCoro.cur_coro(self._asyncoro)
            self._write_coro._await_()
            self._write_task = partial_func(_write, view, 0)
            self._notifier.add(self, _AsyncPoller._Write)
示例#9
0
 async def snake_box(ctx, content, executable, config):
     code, is_exception = parse_code_content(content)
     if is_exception and (code is None):
         await eval_description(ctx)
         return
     
     user_id = ctx.author.id
     if user_id in ACTIVE_EXECUTORS:
         await ctx.send(embed=Embed('You have an eval job queued up', 'Please be patient.', color=SNEKBOX_COLOR))
         return
     
     if is_exception:
         await ctx.send(embed=Embed('Parsing error', code, color=SNEKBOX_COLOR))
         return
     
     with ctx.keep_typing(), EvalUserLock(user_id) as user_lock:
         async with EVAL_LOCK:
             process = await KOKORO.subprocess_exec(NSJAIL_EXECUTABLE,
                     '--config', config,
                     f'--cgroup_mem_max={MEM_MAX}',
                     '--cgroup_mem_mount', str(CGROUP_MEMORY_PARENT.parent),
                     '--cgroup_mem_parent', CGROUP_MEMORY_PARENT.name,
                     '--cgroup_pids_max=1',
                     '--cgroup_pids_mount', str(CGROUP_PIDS_PARENT.parent),
                     '--cgroup_pids_parent', CGROUP_PIDS_PARENT.name,
                     '--', executable, '-Iqu', '-c', code,
                 stdout=subprocess.PIPE,
                 stderr=subprocess.STDOUT,
                     )
             
             user_lock.register_input_source(ctx.client, ctx.message.channel, process)
             
             try:
                 await process.wait(timeout=MAX_TIMEOUT)
             except TimeoutExpired:
                 await process.kill()
             
             return_code = process.return_code
             if return_code is None or return_code == 255:
                 title = f'Your eval job has failed! Returncode: {return_code!r}'
                 description = 'A fatal NsJail error occurred'
             else:
                 if return_code == 137:
                     title = f'Your eval job timed out or ran out of memory. Returncode: {return_code!r}'
                 else:
                     title = f'Your eval job has completed with return code {return_code}.'
                 
                 output = await process.stdout.read()
                 description = build_output(output, return_code)
     
     author = ctx.message.author
     embed = Embed(title, description, color=SNEKBOX_COLOR).add_author(author.avatar_url, author.full_name)
     await Closer(ctx.client, ctx.message.channel, embed, check=partial_func(check_reactor, author))
示例#10
0
 def __new__(cls):
     """
     Creates a partial function to wrap a slash command.
     
     Subclasses should overwrite this method.
     
     Returns
     -------
     wrapper : `functools.partial` of ``SlashCommandWrapper._decorate``
         Partial function to wrap a slash command.
     """
     return partial_func(cls._decorate, cls)
示例#11
0
async def shared_guilds(client, event):
    """Returns the shared guilds between you and me."""
    permissions = event.channel.cached_permissions_for(client)
    if (not permissions.can_send_messages) or (
            not permissions.can_add_reactions):
        yield Embed(
            'Permission denied',
            'I require `send messages` and `add reactions` permissions to execute this command.',
            color=UTILITY_COLOR,
        )
        return

    # Ack the event, hell yeah!
    yield

    pages = []
    lines = []
    lines_count = 0

    user = event.user
    for guild, guild_profile in user.guild_profiles.items():
        nick = guild_profile.nick
        guild_name = guild.name
        if nick is None:
            line = guild_name
        else:
            line = f'{guild_name} [{nick}]'

        lines.append(line)
        lines_count += 1

        if lines_count == 10:
            pages.append('\n'.join(lines))
            lines.clear()
            lines_count = 0

    if lines_count:
        pages.append('\n'.join(lines))

    if not pages:
        pages.append('*none*')

    embeds = []
    embed_title = f'Shared guilds with {user.full_name}:'

    for page in pages:
        embed = Embed(embed_title, page, color=UTILITY_COLOR)
        embeds.append(embed)

    await Pagination(client,
                     event,
                     embeds,
                     check=partial_func(shared_guilds_pagination_check, user))
示例#12
0
    async def _create_socket(self, event):
        """
        Called when voice server update data is received from Discord.
        
        If full data was received, closes the actual socket if exists and creates a new one connected to the received
        address.
        
        If the voice client is already connected reconnects it, if not then marks it as connected.
        
        This method is a coroutine.
        
        Parameters
        ----------
        event : ``VoiceServerUpdateEvent``
            Voice server update event.
        """
        self.connected.clear()

        gateway = self.client.gateway_for(self.guild_id)

        self._session_id = gateway.session_id
        token = event.token
        self._token = token
        endpoint = event.endpoint

        if (endpoint is None) or (token is None):
            return

        self._endpoint = endpoint.replace(':80', '').replace(':443', '')

        self._maybe_close_socket()

        socket = module_socket.socket(module_socket.AF_INET,
                                      module_socket.SOCK_DGRAM)

        protocol = await KOKORO.create_datagram_connection_with(partial_func(
            DatagramMergerReadProtocol, KOKORO),
                                                                socket=socket)
        self._transport = protocol.get_transport()
        self._protocol = protocol
        self._socket = socket

        if self.reader is None:
            self.reader = AudioReader(self)

        handshake_complete = self._handshake_complete
        if handshake_complete.is_done():
            # terminate the websocket and handle the reconnect loop if necessary.
            handshake_complete.clear()
            await self.gateway.terminate()
        else:
            handshake_complete.set_result(None)
示例#13
0
文件: wrappers.py 项目: CuteFwan/hata
 def __new__(cls, parameter_name, type_or_choice, description, name=None):
     """
     Creates a partial function to wrap a slash command.
     
     Parameters
     ----------
     parameter_name : `str`
         The parameter's name to modify.
     type_or_choice : `str`, `type`, `list`, `dict`
         The annotation's value to use.
     description : `str`
         Description for the annotation.
     name : `None` or `str`, Optional
         Name to use instead of the parameter's.
     
     Returns
     -------
     wrapper : `functools.partial` of ``SlashCommandWrapper._decorate``
         Partial function to wrap a slash command.
     
     Raises
     ------
     TypeError
         - If `description`'s is not `str` instance.
         - If `parameter_type_or_choice` is `list` instance, but it's elements do not match the `tuple`
             (`str`, `str` or `int`) pattern.
         - If `parameter_type_or_choice` is `dict` instance, but it's items do not match the (`str`, `str` or `int`)
             pattern.
         - If `parameter_type_or_choice` is unexpected.
         - If `name`'s is neither `None` or `str` instance.
     ValueError
         - If `description`'s length is out of the expected range [2:100].
         - If `parameter_type_or_choice` is `str` instance, but not any of the expected ones.
         - If `parameter_type_or_choice` is `type` instance, but not any of the expected ones.
         - If `choice` amount is out of the expected range [1:25].
         - If `type_or_choice` is a choice, and a `choice` name is duped.
         - If `type_or_choice` is a choice, and a `choice` values are mixed types.
     """
     if type(parameter_name) is str:
         pass
     elif isinstance(parameter_name, str):
         parameter_name = str(parameter_name)
     else:
         raise TypeError(f'`parameter_name` can be `str`, got {parameter_name.__class__.__name__}.')
     
     type_, choices = parse_annotation_type_and_choice(type_or_choice, parameter_name)
     description = parse_annotation_description(description, parameter_name)
     name = parse_annotation_name(name, parameter_name)
     
     return partial_func(cls._decorate, cls, choices, description, name, parameter_name, type_)
示例#14
0
    def __new__(cls, guild, target, allow):
        """
        Creates a partial function to wrap a slash command.
        
        Parameters
        ----------
        guild : ``Guild``, `int`
            The guild's identifier where the overwrite is applied.
        target : ``ClientUserBase``, ``Role``, ``ChannelBase``, `tuple` ((``ClientUserBase``, ``Role``, \
                ``ChannelBase``, `str` (`'Role'`, `'role'`, `'User'`, `'user'`, `'Channel'`, `'channel'`)), `int`)
            The target entity of the overwrite
            
            The expected type & value might be pretty confusing, but the target was it to allow relaxing creation.
            To avoid confusing, here is a list of the expected structures:
            
            - ``Role``
            - ``ClientUserBase``
            - ``ChannelBase``
            - `tuple` (``Role``, `int`)
            - `tuple` (``ClientUserBase``, `int`)
            - `tuple` (``ChannelBase``, `int`)
            - `tuple` (`'Role'`, `int`)
            - `tuple` (`'role'`, `int`)
            - `tuple` (`'User'`, `int`)
            - `tuple` (`'user'`, `int`)
            - `tuple` (`'Channel'`, `int`)
            - `tuple` (`'channel'`, `int`)
        
        allow : `bool`
            Whether the respective application command should be enabled for the respective entity.
        
        Returns
        -------
        wrapper : `functools.partial` of ``SlasherCommandWrapper._decorate``
            Partial function to wrap a slash command.
        """
        if isinstance(guild, Guild):
            guild_id = guild.id
        elif isinstance(guild, (int, str)):
            guild_id = preconvert_snowflake(guild, 'guild')
        else:
            raise TypeError(
                f'`guild` can be `{Guild.__class__.__name__}`, `int`, got {guild.__class__.__name__}; {guild!r}.'
            )

        overwrite = ApplicationCommandPermissionOverwrite(target, allow)

        return partial_func(cls._decorate, cls, guild_id, overwrite)
示例#15
0
    async def show_autogenerated_description(self, command_context, command):
        """
        Generates, then sends the description of the given description-less command.

        Parameters
        ----------
        command_context : ``CommandContext``
            The called command's context.
        command : ``Command``
            The respective command.
        """
        prefix = command_context.prefix

        description_lines = []

        sun_commands = get_sub_commands(command)
        if len(sun_commands) == 0:
            description_lines.append('*no usages*')
        else:
            if len(sun_commands) == 1:
                description_lines.append('Usage:')
            else:
                description_lines.append('Usages:')

            for command_path, command_function in sun_commands:
                description_line = ['`', prefix, command_path]
                command_parameters = generate_command_parameters(
                    command_function)
                if command_parameters is not None:
                    description_line.append(' ')
                    description_line.append(command_parameters)

                description_line.append('`')

                description_lines.append(''.join(description_line))

        embed = Embed(command.display_name, '\n'.join(description_lines))

        embed_postprocessor = self.embed_postprocessor
        if embed_postprocessor is not None:
            embed_postprocessor(command_context, embed)

        await Closer(
            command_context.client,
            command_context.message.channel,
            embed,
            check=partial_func(check_user, command_context.message.author),
        )
示例#16
0
文件: asyncfile.py 项目: Shmuma/pycos
            def _read(size, full, rc, n):
                if rc or n == 0:
                    if self._timeout:
                        self._notifier._del_timeout(self)
                    self._overlap.object = self._read_result = None
                    if rc != winerror.ERROR_OPERATION_ABORTED:
                        if (self._buflist or rc == winerror.ERROR_HANDLE_EOF
                                or rc == winerror.ERROR_BROKEN_PIPE):
                            buf, self._buflist = ''.join(self._buflist), []
                            self._read_task._proceed_(buf)
                            return
                        self._read_task.throw(IOError(rc, 'ReadFile', str(rc)))
                    self._overlap.object = self._read_task = self._read_result = None
                    return

                buf = self._read_result[:n]
                if size > 0:
                    size -= len(buf)
                    assert size >= 0
                    if size == 0:
                        full = False
                self._buflist.append(buf)
                self._overlap.Offset += n
                if full:
                    self._overlap.object = partial_func(_read, size, full)
                    try:
                        rc, _ = win32file.ReadFile(self._handle,
                                                   self._read_result,
                                                   self._overlap)
                    except pywintypes.error as exc:
                        rc = exc.winerror
                    if rc and rc != winerror.ERROR_IO_PENDING:
                        buf, self._buflist = ''.join(self._buflist), []
                        self._overlap.object = self._read_result = None
                        if self._timeout:
                            self._notifier._del_timeout(self)
                        self._read_task._proceed_(buf)
                        self._read_task = None
                    return

                if self._buflist:
                    buf, self._buflist = ''.join(self._buflist), []
                if self._timeout:
                    self._notifier._del_timeout(self)
                self._overlap.object = self._read_result = None
                self._read_task._proceed_(buf)
                self._read_task = None
示例#17
0
        def write(self, buf, full=False, timeout=None):
            """Write data in 'buf' to file. If 'full' is True, the function
            waits till all data in buf is written; otherwise, it waits
            until one write completes. It returns length of data written.

            If no data has been written before timeout, then
            IOError('timedout') will be thrown.

            If timeout is given and full is True and timeout expires
            before all the data could be written, it returns length of
            data written before timeout if any data has been written.

            Must be used with 'yield' as
            'n = yield fd.write(buf)' to write (some) data in buf.
            """
            def _write(self, view, written, full):
                try:
                    n = os.write(self._fileno, view)
                except (OSError, IOError) as exc:
                    if exc.errno in (errno.EAGAIN, errno.EINTR):
                        n = 0
                    else:
                        _AsyncFile._notifier.clear(self, _AsyncPoller._Write)
                        self._write_task = None
                        coro, self._write_coro = self._write_coro, None
                        coro.throw(*sys.exc_info())
                        return
                written += n
                if n == len(view) or not full:
                    _AsyncFile._notifier.clear(self, _AsyncPoller._Write)
                    self._write_coro._proceed_(written)
                    self._write_coro = self._write_task = None
                else:
                    view = view[n:]
                    self._write_task = partial_func(_write, self, view, written, full)

            if full:
                view = buffer(buf)
            else:
                view = buf
            self._timeout = timeout
            self._write_task = partial_func(_write, self, view, 0, full)
            if not self._asyncoro:
                self._asyncoro = AsynCoro.scheduler()
            self._write_coro = AsynCoro.cur_coro(self._asyncoro)
            self._write_coro._await_()
            _AsyncFile._notifier.add(self, _AsyncPoller._Write)
示例#18
0
            def _write(self, written, full, rc, n):
                if rc or n == 0:
                    if self._timeout:
                        _AsyncFile._notifier._del_timeout(self)
                    if rc != winerror.ERROR_OPERATION_ABORTED:
                        if written:
                            self._write_coro._proceed_(written)
                        else:
                            self._write_coro.throw(
                                IOError(rc, 'WriteFile', str(rc)))
                    self._write_result.release()
                    self._overlap.object = self._write_coro = self._write_result = None
                    return

                written += n
                self._overlap.Offset += n
                self._write_result = self._write_result[n:]
                if not full or len(self._write_result) == 0:
                    self._write_result.release()
                    self._overlap.object = self._write_result = None
                    if self._timeout:
                        _AsyncFile._notifier._del_timeout(self)
                    self._write_coro._proceed_(written)
                    self._write_coro = None
                    return

                self._overlap.object = partial_func(_write, self, written,
                                                    full)
                try:
                    rc, _ = win32file.WriteFile(self._handle,
                                                self._write_result,
                                                self._overlap)
                except pywintypes.error as exc:
                    rc = exc.winerror
                if rc and rc != winerror.ERROR_IO_PENDING:
                    self._write_result.release()
                    self._overlap.object = self._write_result = None
                    if self._timeout:
                        _AsyncFile._notifier._del_timeout(self)
                    if written:
                        self._write_coro._proceed_(written)
                    else:
                        self._write_coro.throw(
                            IOError(rc, 'WriteFile', str(rc)))
                    self._write_coro = None
                return
            def _read(size, full, rc, n):
                if rc or n == 0:
                    if self._timeout:
                        self._notifier._del_timeout(self)
                    self._overlap.object = self._read_result = None
                    if rc != winerror.ERROR_OPERATION_ABORTED:
                        if (self._buflist or rc == winerror.ERROR_HANDLE_EOF or
                           rc == winerror.ERROR_BROKEN_PIPE):
                            buf, self._buflist = ''.join(self._buflist), []
                            self._read_task._proceed_(buf)
                            return
                        self._read_task.throw(IOError(rc, 'ReadFile', str(rc)))
                    self._overlap.object = self._read_task = self._read_result = None
                    return

                buf = self._read_result[:n]
                if size > 0:
                    size -= len(buf)
                    assert size >= 0
                    if size == 0:
                        full = False
                self._buflist.append(buf)
                self._overlap.Offset += n
                if full:
                    self._overlap.object = partial_func(_read, size, full)
                    try:
                        rc, _ = win32file.ReadFile(self._handle, self._read_result, self._overlap)
                    except pywintypes.error as exc:
                        rc = exc.winerror
                    if rc and rc != winerror.ERROR_IO_PENDING:
                        buf, self._buflist = ''.join(self._buflist), []
                        self._overlap.object = self._read_result = None
                        if self._timeout:
                            self._notifier._del_timeout(self)
                        self._read_task._proceed_(buf)
                        self._read_task = None
                    return

                if self._buflist:
                    buf, self._buflist = ''.join(self._buflist), []
                if self._timeout:
                    self._notifier._del_timeout(self)
                self._overlap.object = self._read_result = None
                self._read_task._proceed_(buf)
                self._read_task = None
示例#20
0
 def _write(view, written):
     try:
         n = os.write(self._fileno, view)
     except (OSError, IOError) as exc:
         if exc.errno in (errno.EAGAIN, errno.EINTR):
             n = 0
         else:
             self._notifier.clear(self, _AsyncPoller._Write)
             self._write_task.throw(*sys.exc_info())
             self._write_task = self._write_fn = None
             return
     written += n
     if n == len(view) or not full:
         self._notifier.clear(self, _AsyncPoller._Write)
         self._write_task._proceed_(written)
         self._write_task = self._write_fn = None
     else:
         view = view[n:]
         self._write_fn = partial_func(_write, view, written)
示例#21
0
 def _write(view, written):
     try:
         n = os.write(self._fileno, view)
     except (OSError, IOError) as exc:
         if exc.errno in (errno.EAGAIN, errno.EINTR):
             n = 0
         else:
             self._notifier.clear(self, _AsyncPoller._Write)
             self._write_coro.throw(*sys.exc_info())
             self._write_coro = self._write_task = None
             return
     written += n
     if n == len(view) or not full:
         self._notifier.clear(self, _AsyncPoller._Write)
         self._write_coro._proceed_(written)
         self._write_coro = self._write_task = None
     else:
         view = view[n:]
         self._write_task = partial_func(_write, view, written)
示例#22
0
    def error(self, exception_handler=None, *, first=False):
        """
        Registers an exception handler to the command.
        
        Parameters
        ----------
        exception_handler : `None`, `CoroutineFunction` = `None`, Optional
            Exception handler to register.
        first : `bool` = `False`, Optional (Keyword Only)
            Whether the exception handler should run first.
        
        Returns
        -------
        exception_handler / wrapper : `CoroutineFunction` / `functools.partial`
            If `exception_handler` is not given, returns a wrapper.
        """
        if exception_handler is None:
            return partial_func(_register_exception_handler, first)

        return self._register_exception_handler(exception_handler, first)
示例#23
0
async def getting_good(client, event):
    """Getting there."""
    main_component = Row(
        Button('cake', custom_id='cake', style=ButtonStyle.violet),
        Button('cat', custom_id='cat', style=ButtonStyle.gray),
        Button('snake', custom_id='snake', style=ButtonStyle.green),
        Button('eggplant', custom_id='eggplant', style=ButtonStyle.red),
        Button('eggplant', custom_id='eggplant', style=ButtonStyle.red, enabled=False),
    )
    
    yield InteractionResponse(embed=Embed('Choose your poison.'), components=main_component, show_for_invoking_user_only=True)
    
    try:
        component_interaction = await wait_for_component_interaction(event,
            timeout=4500., check=partial_func(check_user, event.user))
    except TimeoutError:
        await client.message_edit(event.message, components=None)
    else:
        emoji = BUILTIN_EMOJIS[component_interaction.interaction.custom_id]
        await client.message_edit(event.message, emoji.as_emoji, embed=None, components=None)
示例#24
0
            def _write(written, rc, n):
                if rc or n == 0:
                    if self._timeout:
                        self._notifier._del_timeout(self)
                    if rc != winerror.ERROR_OPERATION_ABORTED:
                        if written:
                            self._write_coro._proceed_(written)
                        else:
                            self._write_coro.throw(IOError(rc, 'WriteFile', str(rc)))
                    self._write_result.release()
                    self._overlap.object = self._write_coro = self._write_result = None
                    return

                written += n
                self._overlap.Offset += n
                self._write_result = self._write_result[n:]
                if not full or len(self._write_result) == 0:
                    self._write_result.release()
                    self._overlap.object = self._write_result = None
                    if self._timeout:
                        self._notifier._del_timeout(self)
                    self._write_coro._proceed_(written)
                    self._write_coro = None
                    return

                self._overlap.object = partial_func(_write, written)
                try:
                    rc, _ = win32file.WriteFile(self._handle, self._write_result, self._overlap)
                except pywintypes.error as exc:
                    rc = exc.winerror
                if rc and rc != winerror.ERROR_IO_PENDING:
                    self._write_result.release()
                    self._overlap.object = self._write_result = None
                    if self._timeout:
                        self._notifier._del_timeout(self)
                    if written:
                        self._write_coro._proceed_(written)
                    else:
                        self._write_coro.throw(IOError(rc, 'WriteFile', str(rc)))
                    self._write_coro = None
                return
示例#25
0
 def write_proc(fd, input, coro=None):
     size = 16384
     if isinstance(input, str):
         n = yield fd.write(input, full=True)
         if n != len(input):
             raise IOError('write failed')
     else:
         # TODO: how to know if 'input' is file object for
         # on-disk file?
         if hasattr(input, 'seek') and hasattr(input, 'fileno'):
             read_func = partial_func(os.read, input.fileno())
         else:
             read_func = input.read
         while 1:
             data = yield read_func(size)
             if not data:
                 break
             n = yield fd.write(data, full=True)
             if n != len(data):
                 raise IOError('write failed')
         input.close()
     fd.close()
示例#26
0
 def write_proc(fd, input, task=None):
     size = 16384
     if isinstance(input, str):
         n = yield fd.write(input, full=True)
         if n != len(input):
             raise IOError('write failed')
     else:
         # TODO: how to know if 'input' is file object for
         # on-disk file?
         if hasattr(input, 'seek') and hasattr(input, 'fileno'):
             read_func = partial_func(os.read, input.fileno())
         else:
             read_func = input.read
         while 1:
             data = yield read_func(size)
             if not data:
                 break
             n = yield fd.write(data, full=True)
             if n != len(data):
                 raise IOError('write failed')
         input.close()
     fd.close()
示例#27
0
 def _write(self, view, written, full):
     try:
         n = os.write(self._fileno, view)
     except (OSError, IOError) as exc:
         if exc.errno in (errno.EAGAIN, errno.EINTR):
             n = 0
         else:
             _AsyncFile._notifier.clear(self, _AsyncPoller._Write)
             if full:
                 view.release()
             self._write_task = None
             coro, self._write_coro = self._write_coro, None
             coro.throw(*sys.exc_info())
             return
     written += n
     if n == len(view) or not full:
         _AsyncFile._notifier.clear(self, _AsyncPoller._Write)
         if full:
             view.release()
         self._write_coro._proceed_(written)
         self._write_coro = self._write_task = None
     else:
         view = view[n:]
         self._write_task = partial_func(_write, self, view, written, full)
示例#28
0
async def docs_search(client, event,
        search_for: ('str', 'Search term'),
            ):
    """Searchers the given query from hata docs."""
    guild = event.guild
    if guild is None:
        yield Embed('Error', 'Guild only command', color=COLOR__KOISHI_HELP)
        return
    
    if guild not in client.guild_profiles:
        yield Embed('Error', 'I must be in the guild to execute this command.', color=COLOR__KOISHI_HELP)
        return
    
    permissions = event.channel.cached_permissions_for(client)
    if (not permissions.can_send_messages) or (not permissions.can_add_reactions):
        yield Embed('Permission denied',
            'I need `send messages` and `add reactions` permission to execute this command.',
            color=COLOR__KOISHI_HELP)
        return
    
    if len(search_for) < 4:
        yield Embed('Ohoho', 'Please give a longer query', color=COLOR__KOISHI_HELP)
        return
    
    yield
    
    async with client.http.get(HATA_DOCS_SEARCH_API, params={'search_for': search_for}) as response:
        datas = await response.json()
    
    if not datas:
        embed = Embed(f'No search result for: `{search_for}`', color=COLOR__KOISHI_HELP)
        await Closer(client, event, embed)
        return
    
    sections = []
    section_parts = []
    for data in datas:
        section_parts.append('[**')
        name = data['name']
        name = name.replace('_', '\_')
        section_parts.append(name)
        section_parts.append('**](')
        section_parts.append(HATA_DOCS_BASE_URL)
        url = data['url']
        section_parts.append(url)
        section_parts.append(') *')
        type_ = data['type']
        section_parts.append(type_)
        section_parts.append('*')
        preview = data.get('preview', None)
        if (preview is not None):
            preview = preview.replace('_', '\_')
            section_parts.append('\n')
            section_parts.append(preview)
        
        section = ''.join(section_parts)
        sections.append(section)
        section_parts.clear()
    
    
    descriptions = []
    description_parts = []
    description_length = 0
    
    for section in sections:
        section_length = len(section)
        description_length += section_length
        if description_length > 2000:
            description = ''.join(description_parts)
            descriptions.append(description)
            description_parts.clear()
            
            description_parts.append(section)
            description_length = section_length
            continue
        
        if description_parts:
            description_parts.append('\n\n')
            description_length += 2
        
        description_parts.append(section)
        continue
    
    if description_parts:
        description = ''.join(description_parts)
        descriptions.append(description)
    
    
    title = f'Search results for `{search_for}`'
    
    embeds = []
    for index, description in enumerate(descriptions, 1):
        embed = Embed(title, description, color=COLOR__KOISHI_HELP).add_footer(f'Page {index}/{len(descriptions)}')
        embeds.append(embed)
    
    await Pagination(client, event, embeds, check=partial_func(docs_search_pagination_check, event.user))
示例#29
0
        def read(self, size=0, full=False, timeout=None):
            """Read at most 'size' bytes from file; if 'size' <= 0,
            all data up to EOF is read and returned. If 'full' is
            True, exactly 'size' bytes are returned (unless EOF or
            timeout occur before). If EOF is encountered before any
            more data is available, empty buffer is returned.

            If no data has been read before timeout, then
            IOError('timedout') will be thrown.

            If timeout is given and full is True and timeout expires
            before all the data could be read, it returns partial data
            read before timeout if any data has been read.

            Must be used in a task with 'yield' as
            'data = yield fd.read(1024)'
            """

            def _read(size, full):
                if size > 0:
                    count = size
                else:
                    count = 16384
                try:
                    buf = os.read(self._fileno, count)
                except (OSError, IOError) as exc:
                    if exc.errno in (errno.EAGAIN, errno.EWOULDBLOCK):
                        return
                    else:
                        raise
                except Exception:
                    self._notifier.clear(self, _AsyncPoller._Read)
                    self._read_task.throw(*sys.exc_info())
                    self._read_task = self._read_fn = None
                    return

                if buf:
                    if size > 0:
                        size -= len(buf)
                        # assert size >= 0
                        if size == 0:
                            full = False
                    self._buflist.append(buf)
                    if full:
                        self._read_fn = partial_func(_read, size, full)
                        return

                if self._buflist:
                    buf, self._buflist = ''.join(self._buflist), []
                self._notifier.clear(self, _AsyncPoller._Read)
                self._read_task._proceed_(buf)
                self._read_task = self._read_fn = None

            if not self._pycos:
                self._pycos = Pycos.scheduler()
                self._notifier = self._pycos._notifier
                if hasattr(self._fd, '_fileno'):
                    self._notifier.unregister(self._fd)
            if not size or size < 0:
                size = 0
                full = True
            elif self._buflist:
                buf, self._buflist = ''.join(self._buflist), []
                if len(buf) > size:
                    buf, self._buflist = buf[:size], [buf[size:]]
                if (not full) or (len(buf) == size):
                    return buf
                self._buflist = [buf]
                size -= len(buf)
            self._timeout = timeout
            self._read_task = Pycos.cur_task(self._pycos)
            self._read_task._await_()
            self._read_fn = partial_func(_read, size, full)
            self._notifier.add(self, _AsyncPoller._Read)
示例#30
0
        def read(self, size=0, full=False, timeout=None):
            """Read at most 'size' bytes from file; if 'size' <= 0,
            all data up to EOF is read and returned. If 'full' is
            True, exactly 'size' bytes are returned (unless EOF or
            timeout occur before). If EOF is encountered before any
            more data is available, empty buffer is returned.

            If no data has been read before timeout, then
            IOError('timedout') will be thrown.

            If timeout is given and full is True and timeout expires
            before all the data could be read, it returns partial data
            read before timeout if any data has been read.

            Must be used in a task with 'yield' as
            'data = yield fd.read(1024)'
            """

            def _read(size, full, rc, n):
                if rc or n == 0:
                    if self._timeout:
                        self._notifier._del_timeout(self)
                    self._overlap.object = self._read_result = None
                    if rc != winerror.ERROR_OPERATION_ABORTED:
                        if (self._buflist or rc == winerror.ERROR_HANDLE_EOF or
                           rc == winerror.ERROR_BROKEN_PIPE):
                            buf, self._buflist = ''.join(self._buflist), []
                            self._read_task._proceed_(buf)
                            return
                        self._read_task.throw(IOError(rc, 'ReadFile', str(rc)))
                    self._overlap.object = self._read_task = self._read_result = None
                    return

                buf = self._read_result[:n]
                if size > 0:
                    size -= len(buf)
                    assert size >= 0
                    if size == 0:
                        full = False
                self._buflist.append(buf)
                self._overlap.Offset += n
                if full:
                    self._overlap.object = partial_func(_read, size, full)
                    try:
                        rc, _ = win32file.ReadFile(self._handle, self._read_result, self._overlap)
                    except pywintypes.error as exc:
                        rc = exc.winerror
                    if rc and rc != winerror.ERROR_IO_PENDING:
                        buf, self._buflist = ''.join(self._buflist), []
                        self._overlap.object = self._read_result = None
                        if self._timeout:
                            self._notifier._del_timeout(self)
                        self._read_task._proceed_(buf)
                        self._read_task = None
                    return

                if self._buflist:
                    buf, self._buflist = ''.join(self._buflist), []
                if self._timeout:
                    self._notifier._del_timeout(self)
                self._overlap.object = self._read_result = None
                self._read_task._proceed_(buf)
                self._read_task = None

            if not self._pycos:
                self._pycos = Pycos.scheduler()
                self._notifier = self._pycos._notifier
                self._notifier.register(self._handle)
            if not size or size < 0:
                count = 16384
                full = True
            else:
                if self._buflist:
                    buf, self._buflist = ''.join(self._buflist), []
                    if len(buf) > size:
                        buf, self._buflist = buf[:size], [buf[size:]]
                    if (not full) or (len(buf) == size):
                        return buf
                    self._buflist = [buf]
                    size -= len(buf)
                count = size
            self._read_result = win32file.AllocateReadBuffer(count)
            self._overlap.object = partial_func(_read, size, full)
            self._read_task = Pycos.cur_task(self._pycos)
            self._read_task._await_()
            try:
                rc, _ = win32file.ReadFile(self._handle, self._read_result, self._overlap)
            except pywintypes.error as exc:
                if exc.winerror == winerror.ERROR_BROKEN_PIPE:
                    buf, self._buflist = ''.join(self._buflist), []
                    self._read_task._proceed_(buf)
                    self._read_result = self._read_task = self._overlap.object = None
                    return
                else:
                    rc = exc.winerror
            if rc and rc != winerror.ERROR_IO_PENDING:
                self._overlap.object = self._read_result = self._read_task = None
                self._read_task.throw(IOError(rc, 'ReadFile', str(rc)))
            if timeout:
                self._timeout = timeout
                self._notifier._add_timeout(self)
示例#31
0
        def write(self, buf, full=False, timeout=None):
            """Write data in 'buf' to file. If 'full' is True, the function
            waits till all data in buf is written; otherwise, it waits
            until one write completes. It returns length of data written.

            If no data has been written before timeout, then
            IOError('timedout') will be thrown.

            If timeout is given and full is True and timeout expires
            before all the data could be written, it returns length of
            data written before timeout if any data has been written.

            Must be used with 'yield' as
            'n = yield fd.write(buf)' to write (some) data in buf.
            """

            def _write(written, rc, n):
                if rc or n == 0:
                    if self._timeout:
                        self._notifier._del_timeout(self)
                    if rc != winerror.ERROR_OPERATION_ABORTED:
                        if written:
                            self._write_task._proceed_(written)
                        else:
                            self._write_task.throw(IOError(rc, 'WriteFile', str(rc)))
                    self._overlap.object = self._write_task = self._write_result = None
                    return

                written += n
                self._overlap.Offset += n
                self._write_result = self._write_result[n:]
                if not full or len(self._write_result) == 0:
                    self._overlap.object = self._write_result = None
                    if self._timeout:
                        self._notifier._del_timeout(self)
                    self._write_task._proceed_(written)
                    self._write_task = None
                    return

                self._overlap.object = partial_func(_write, written)
                try:
                    rc, _ = win32file.WriteFile(self._handle, self._write_result, self._overlap)
                except pywintypes.error as exc:
                    rc = exc.winerror
                if rc and rc != winerror.ERROR_IO_PENDING:
                    self._overlap.object = self._write_result = None
                    if self._timeout:
                        self._notifier._del_timeout(self)
                    if written:
                        self._write_task._proceed_(written)
                    else:
                        self._write_task.throw(IOError(rc, 'WriteFile', str(rc)))
                    self._write_task = None
                return

            self._write_result = buffer(buf)
            self._overlap.object = partial_func(_write, 0)
            if not self._pycos:
                self._pycos = Pycos.scheduler()
                self._notifier = self._pycos._notifier
                self._notifier.register(self._handle)
            self._write_task = Pycos.cur_task(self._pycos)
            self._write_task._await_()
            try:
                rc, _ = win32file.WriteFile(self._handle, self._write_result, self._overlap)
            except pywintypes.error as exc:
                if exc.winerror == winerror.ERROR_BROKEN_PIPE:
                    self._write_task._proceed_(0)
                    self._write_result = self._write_task = self._overlap.object = None
                    return
                else:
                    rc = exc.winerror
            if rc and rc != winerror.ERROR_IO_PENDING:
                self._overlap.object = self._write_result = self._write_task = None
                self._write_task._proceed_(None)
                raise IOError(rc, 'WriteFile', str(rc))
            if timeout:
                self._timeout = timeout
                self._notifier._add_timeout(self)
示例#32
0
    async def list_commands(self, command_context, command_names,
                            category_name):
        """
        Lists the given commands.
        
        Parameters
        ----------
        command_context : ``CommandContext``
            The called command's context.
        command_names : `list` of `str`
            The command's names to display.
        category_name : `None or `str`
            The respective category's name.
        """
        pages = []
        page = []
        page_line_count = 0
        prefix = self.prefix
        for command_name in command_names:
            page.append(prefix)
            page.append(' ')
            page.append(command_name)

            page_line_count += 1

            if page_line_count == MAX_LINE_PER_PAGE:
                pages.append(''.join(page))
                page.clear()
                page_line_count = 0
            else:
                page.append('\n')

        if page_line_count:
            del page[-1]
            pages.append(''.join(page))

        page_count = len(pages)

        field_name = f'Use `{command_context.prefix}help <command>` for more information.'

        if category_name is None:
            title = 'Commands'
        else:
            title = f'Commands of {category_name}'

        embeds = [
            Embed(title, page).add_field(field_name,
                                         f'page {index}/{page_count}')
            for index, page in enumerate(pages, 1)
        ]

        embed_postprocessor = self.embed_postprocessor
        if (embed_postprocessor is not None):
            for embed in embeds:
                embed_postprocessor(command_context, embed)

        await Pagination(command_context.client,
                         command_context.message.channel,
                         embeds,
                         check=partial_func(check_user,
                                            command_context.message.author))
示例#33
0
    async def __call__(self, client, message, content):
        if not client.is_owner(message.author):
            await client.message_create(message.channel,
                                        'You are not my boss!')
            return

        if self.lock.locked():
            await client.message_create(message.channel,
                                        'An execution is already running.')
            return

        async with self.lock:
            result, is_exception = parse_code_content(content,
                                                      'No code to execute.')
            if is_exception:
                await client.message_create(message.channel,
                                            embed=Embed(
                                                'Parsing error', result))
                return

            with StringIO() as buffer:
                try:
                    code_object = compile(result,
                                          'online_interpreter',
                                          'exec',
                                          flags=COMPILE_FLAGS)
                except SyntaxError as err:
                    buffer.write(
                        f'{err.__class__.__name__} at line {err.lineno}: {err.msg}\n'
                        f'{result[err.lineno-1]}\n'
                        f'{" "*(err.offset-1)}^')

                else:
                    locals_ = self.locals
                    locals_['print'] = partial_func(raw_print, buffer)
                    locals_['input'] = partial_func(raw_input)

                    try:
                        function = FunctionType(code_object, locals_)
                        coroutine = function()
                        if is_awaitable(coroutine):
                            await coroutine
                    except BaseException as err:
                        await KOKORO.render_exc_async(err, file=buffer)

                page_contents = get_buffer_value(buffer)

            pages = []

            if (page_contents is not None) and page_contents:
                amount = len(page_contents)
                for index, page_content in enumerate(page_contents, 1):
                    pages.append(
                        Embed(
                            'Output:',
                            page_content).add_footer(f'page {index}/{amount}'))

            else:
                pages.append(Embed('No output'))

        await Pagination(client, message.channel, pages, timeout=240.)
示例#34
0
文件: laplace.py 项目: Dalar/GPy
    def rasm_mode(self, K, MAX_ITER=40):
        """
        Rasmussen's numerically stable mode finding
        For nomenclature see Rasmussen & Williams 2006
        Influenced by GPML (BSD) code, all errors are our own

        :param K: Covariance matrix evaluated at locations X
        :type K: NxD matrix
        :param MAX_ITER: Maximum number of iterations of newton-raphson before forcing finish of optimisation
        :type MAX_ITER: scalar
        :returns: f_hat, mode on which to make laplace approxmiation
        :rtype: NxD matrix
        """
        #old_Ki_f = np.zeros((self.N, 1))

        #Start f's at zero originally of if we have gone off track, try restarting
        if self.old_Ki_f is None or self.bad_fhat:
            old_Ki_f = np.random.rand(self.N, 1)/50.0
            #old_Ki_f = self.Y
            f = np.dot(K, old_Ki_f)
        else:
            #Start at the old best point
            old_Ki_f = self.old_Ki_f.copy()
            f = self.f_hat.copy()

        new_obj = -np.inf
        old_obj = np.inf

        def obj(Ki_f, f):
            return -0.5*np.dot(Ki_f.T, f) + self.noise_model.logpdf(f, self.data, extra_data=self.extra_data)

        difference = np.inf
        epsilon = 1e-7
        #step_size = 1
        #rs = 0
        i = 0

        while difference > epsilon and i < MAX_ITER:
            W = -self.noise_model.d2logpdf_df2(f, self.data, extra_data=self.extra_data)

            W_f = W*f
            grad = self.noise_model.dlogpdf_df(f, self.data, extra_data=self.extra_data)

            b = W_f + grad
            W12BiW12Kb, _ = self._compute_B_statistics(K, W.copy(), np.dot(K, b))

            #Work out the DIRECTION that we want to move in, but don't choose the stepsize yet
            full_step_Ki_f = b - W12BiW12Kb
            dKi_f = full_step_Ki_f - old_Ki_f

            f_old = f.copy()
            def inner_obj(step_size, old_Ki_f, dKi_f, K):
                Ki_f = old_Ki_f + step_size*dKi_f
                f = np.dot(K, Ki_f)
                # This is nasty, need to set something within an optimization though
                self.tmp_Ki_f = Ki_f.copy()
                self.tmp_f = f.copy()
                return -obj(Ki_f, f)

            i_o = partial_func(inner_obj, old_Ki_f=old_Ki_f, dKi_f=dKi_f, K=K)
            #Find the stepsize that minimizes the objective function using a brent line search
            #The tolerance and maxiter matter for speed! Seems to be best to keep them low and make more full
            #steps than get this exact then make a step, if B was bigger it might be the other way around though
            #new_obj = sp.optimize.minimize_scalar(i_o, method='brent', tol=1e-4, options={'maxiter':5}).fun
            new_obj = sp.optimize.brent(i_o, tol=1e-4, maxiter=10)
            f = self.tmp_f.copy()
            Ki_f = self.tmp_Ki_f.copy()

            #Optimize without linesearch
            #f_old = f.copy()
            #update_passed = False
            #while not update_passed:
                #Ki_f = old_Ki_f + step_size*dKi_f
                #f = np.dot(K, Ki_f)

                #old_obj = new_obj
                #new_obj = obj(Ki_f, f)
                #difference = new_obj - old_obj
                ##print "difference: ",difference
                #if difference < 0:
                    ##print "Objective function rose", np.float(difference)
                    ##If the objective function isn't rising, restart optimization
                    #step_size *= 0.8
                    ##print "Reducing step-size to {ss:.3} and restarting optimization".format(ss=step_size)
                    ##objective function isn't increasing, try reducing step size
                    #f = f_old.copy() #it's actually faster not to go back to old location and just zigzag across the mode
                    #old_obj = new_obj
                    #rs += 1
                #else:
                    #update_passed = True

            #old_Ki_f = self.Ki_f.copy()

            #difference = abs(new_obj - old_obj)
            #old_obj = new_obj.copy()
            difference = np.abs(np.sum(f - f_old)) + np.abs(np.sum(Ki_f - old_Ki_f))
            #difference = np.abs(np.sum(Ki_f - old_Ki_f))/np.float(self.N)
            old_Ki_f = Ki_f.copy()
            i += 1

        self.old_Ki_f = old_Ki_f.copy()

        #Warn of bad fits
        if difference > epsilon:
            self.bad_fhat = True
            warnings.warn("Not perfect f_hat fit difference: {}".format(difference))
        elif self.bad_fhat:
            self.bad_fhat = False
            warnings.warn("f_hat now perfect again")

        self.Ki_f = Ki_f
        return f
示例#35
0
    async def list_categories(self, command_context):
        """
        Lists the categories of the respective client's command preprocessor.
        
        Parameters
        ----------
        command_context : ``CommandContext``
            The called command's context.
        """
        categories = command_context.client.command_processor.categories

        displayable_categories = []

        for category in categories:
            if await should_show_category(command_context, category):
                displayable_categories.append(category)

        displayable_categories_length = len(displayable_categories)
        if displayable_categories_length == 1:
            await self.list_category(command_context,
                                     displayable_categories[0],
                                     display_category_name=False)
            return

        category_names = sorted(category.display_name
                                for category in displayable_categories)
        pages = []

        if displayable_categories_length == 0:
            pages.append('*[No available category or command]*')
        else:
            page = []
            page_line_count = 0
            for index, category_name in enumerate(category_names, 1):
                page.append(str(index))
                page.append('.: ')
                page.append(category_name)

                page_line_count += 1

                if page_line_count == MAX_LINE_PER_PAGE:
                    pages.append(''.join(page))
                    page.clear()
                    page_line_count = 0
                else:
                    page.append('\n')

            if page_line_count:
                del page[-1]
                pages.append(''.join(page))

        page_count = len(pages)

        field_name = f'Use `{command_context.prefix}help <category/command>` for more information.'

        embeds = [
            Embed('Categories', page).add_field(field_name,
                                                f'page {index}/{page_count}')
            for index, page in enumerate(pages, 1)
        ]

        embed_postprocessor = self.embed_postprocessor
        if (embed_postprocessor is not None):
            for embed in embeds:
                embed_postprocessor(command_context, embed)

        await Pagination(command_context.client,
                         command_context.message.channel,
                         embeds,
                         check=partial_func(check_user,
                                            command_context.message.author))
示例#36
0
async def delete_(
        client,
        event,
        sticker_name: ('str', 'The sticker\'s name to delete', 'sticker'),
):
    """Deletes the given sticker. (You must have emoji-council role)"""
    if not event.user.has_role(ROLE__NEKO_DUNGEON__EMOJI_MANAGER):
        abort(
            f'You must have {ROLE__NEKO_DUNGEON__EMOJI_MANAGER:m} role to invoke this command.'
        )

    sticker = event.guild.get_sticker_like(sticker_name)
    if (sticker is None):
        abort(f'No sticker matched the given name: {sticker_name!r}.')

    embed = Embed(
        'Confirmation',
        f'Are you sure to delete {sticker.name!r} ({sticker.id}) sticker forever?'
    )

    message = yield InteractionResponse(embed=embed,
                                        components=STICKER_DELETE_COMPONENTS,
                                        allowed_mentions=None)

    try:
        component_interaction = await wait_for_component_interaction(
            message,
            timeout=300.0,
            check=partial_func(check_sticker_deleter, event.user))

    except TimeoutError:
        embed.title = 'Timeout'
        embed.description = f'Sticker {sticker.name!r} was not deleted.'

        # Edit the source message with the source interaction
        yield InteractionResponse(embed=embed,
                                  components=None,
                                  allowed_mentions=None,
                                  message=message)
        return

    if component_interaction.interaction == STICKER_DELETE_BUTTON_CANCEL:
        embed.title = 'Cancelled'
        embed.description = f'Sticker {sticker.name!r} was not deleted.'

        # Edit the source message with the component interaction
        yield InteractionResponse(embed=embed,
                                  components=None,
                                  allowed_mentions=None,
                                  event=component_interaction)
        return

    # Acknowledge the event
    await client.interaction_component_acknowledge(component_interaction)

    try:
        await client.sticker_guild_delete(sticker)
    except ConnectionError:
        # No internet, let it be
        return

    except DiscordException as err:
        if err.code == ERROR_CODES.unknown_sticker:
            failure = False
        else:
            failure = True
            embed.title = 'Failure'
            embed.description = repr(err)
    else:
        failure = False

    if not failure:
        embed.title = 'Success'
        embed.description = f'Sticker {sticker.name!r} has been deleted successfully.'

    # Edit the source message
    yield InteractionResponse(embed=embed, message=message, components=None)
示例#37
0
async def edit_(
    client,
    event,
    sticker_name: ('str', 'The sticker\'s name to delete', 'sticker'),
    new_name: (
        'str',
        'New name for the sticker',
    ) = None,
    new_emoji_value: (str, 'Emoji representation of the sticker.',
                      'new_emoji') = None,
    new_description: (str, 'Description for the sticker.') = None,
):
    """Edits the given sticker. (You must have emoji-council role)"""
    if not event.user.has_role(ROLE__NEKO_DUNGEON__EMOJI_MANAGER):
        abort(
            f'You must have {ROLE__NEKO_DUNGEON__EMOJI_MANAGER:m} role to invoke this command.'
        )

    sticker = event.guild.get_sticker_like(sticker_name)
    if (sticker is None):
        abort(f'No sticker matched the given name: {sticker_name!r}.')

    anything_to_edit = False

    if (new_name is not None):
        if (sticker.name != new_name):
            name_length = len(new_name)
            if (name_length < 2) or (name_length > 32):
                abort(
                    f'Sticker name\'s length can be in range [2:32], got {name_length!r}, {new_name!r}.'
                )

            anything_to_edit = True
        else:
            new_name = None

    if (new_emoji_value is not None):
        new_emoji = parse_emoji(new_emoji_value)

        if new_emoji is None:
            abort(f'{new_emoji_value} cannot be interpreted as an emoji.')

        if new_emoji.is_custom_emoji():
            abort(f'Only unicode can be used, got {new_emoji:e}')

        tags = sticker.tags
        if (tags is None) or (len(tags) != 1) or (next(iter(tags)) !=
                                                  new_emoji.name):
            anything_to_edit = True
        else:
            new_emoji = None
    else:
        new_emoji = None

    if (new_description is not None):
        description_length = len(new_description)
        if (description_length > 100):
            abort(
                f'Sticker description\'s length can be in range [0:100], got {description_length!r}, '
                f'{new_description!r}.')

        if (sticker.description != new_description):
            anything_to_edit = True
        else:
            new_description = None

    if not anything_to_edit:
        abort('No differences were provided.')

    embed = Embed('Confirmation',
                  f'Are you sure to edit {sticker.name!r} sticker?')

    if (new_name is not None):
        embed.add_field('Name', f'{sticker.name} -> {new_name}')

    if (new_emoji is not None):
        embed.add_field('Tags',
                        f'{", ".join(sticker.tags)} -> {new_emoji.name}')

    if (new_description is not None):
        embed.add_field('Description',
                        f'{sticker.description} -> {new_description}')

    message = yield InteractionResponse(embed=embed,
                                        components=STICKER_EDIT_COMPONENTS,
                                        allowed_mentions=None)

    try:
        component_interaction = await wait_for_component_interaction(
            message,
            timeout=300.0,
            check=partial_func(check_sticker_editor, event.user))

    except TimeoutError:
        embed.title = 'Timeout'
        embed.description = f'Sticker {sticker.name!r} was not edited.'

        # Edit the source message with the source interaction
        yield InteractionResponse(embed=embed,
                                  components=None,
                                  allowed_mentions=None,
                                  message=message)
        return

    if component_interaction.interaction == STICKER_EDIT_BUTTON_CANCEL:
        embed.title = 'Cancelled'
        embed.description = f'Sticker {sticker.name!r} was not edited.'

        # Edit the source message with the component interaction
        yield InteractionResponse(embed=embed,
                                  components=None,
                                  allowed_mentions=None,
                                  event=component_interaction)
        return

    # Acknowledge the event
    await client.interaction_component_acknowledge(component_interaction)

    kwargs = {}

    if (new_name is not None):
        kwargs['name'] = new_name

    if (new_emoji is not None):
        kwargs['emoji_representation'] = new_emoji

    if (new_description is not None):
        kwargs['description'] = new_description

    try:
        await client.sticker_guild_edit(sticker, **kwargs)
    except ConnectionError:
        # No internet, let it be
        return

    except DiscordException as err:
        if err.code == ERROR_CODES.unknown_sticker:
            failure = False
        else:
            failure = True
            embed.title = 'Failure'
            embed.description = repr(err)
    else:
        failure = False

    if not failure:
        embed.title = 'Success'
        embed.description = f'Sticker {sticker.name!r} has been successfully edited.'

    # Edit the source message
    yield InteractionResponse(embed=embed, message=message, components=None)
示例#38
0
        def read(self, size=0, full=False, timeout=None):
            """Read at most 'size' bytes from file; if 'size' <= 0,
            all data up to EOF is read and returned. If 'full' is
            True, exactly 'size' bytes are returned (unless EOF or
            timeout occur before). If EOF is encountered before any
            more data is available, empty buffer is returned.

            If no data has been read before timeout, then
            IOError('timedout') will be thrown.

            If timeout is given and full is True and timeout expires
            before all the data could be read, it returns partial data
            read before timeout if any data has been read.

            Must be used in a coroutine with 'yield' as
            'data = yield fd.read(1024)'
            """

            def _read(size, full, rc, n):
                if rc or n == 0:
                    if self._timeout:
                        self._notifier._del_timeout(self)
                    self._overlap.object = self._read_result = None
                    if rc != winerror.ERROR_OPERATION_ABORTED:
                        if (self._buflist or rc == winerror.ERROR_HANDLE_EOF or
                           rc == winerror.ERROR_BROKEN_PIPE):
                            buf, self._buflist = ''.join(self._buflist), []
                            self._read_coro._proceed_(buf)
                            return
                        self._read_coro.throw(IOError(rc, 'ReadFile', str(rc)))
                    self._overlap.object = self._read_coro = self._read_result = None
                    return

                buf = self._read_result[:n]
                if size > 0:
                    size -= len(buf)
                    assert size >= 0
                    if size == 0:
                        full = False
                self._buflist.append(buf)
                self._overlap.Offset += n
                if full:
                    self._overlap.object = partial_func(_read, size, full)
                    try:
                        rc, _ = win32file.ReadFile(self._handle, self._read_result, self._overlap)
                    except pywintypes.error as exc:
                        rc = exc.winerror
                    if rc and rc != winerror.ERROR_IO_PENDING:
                        buf, self._buflist = ''.join(self._buflist), []
                        self._overlap.object = self._read_result = None
                        if self._timeout:
                            self._notifier._del_timeout(self)
                        self._read_coro._proceed_(buf)
                        self._read_coro = None
                    return
                if self._buflist:
                    buf, self._buflist = ''.join(self._buflist), []
                if self._timeout:
                    self._notifier._del_timeout(self)
                self._overlap.object = self._read_result = None
                self._read_coro._proceed_(buf)
                self._read_coro = None

            if not self._asyncoro:
                self._asyncoro = AsynCoro.scheduler()
                self._notifier = self._asyncoro._notifier
                self._notifier.register(self._handle)
            if not size or size < 0:
                count = 16384
                full = True
            else:
                if self._buflist:
                    buf, self._buflist = ''.join(self._buflist), []
                    if len(buf) > size:
                        buf, self._buflist = buf[:size], [buf[size:]]
                    if (not full) or (len(buf) == size):
                        return buf
                    self._buflist = [buf]
                    size -= len(buf)
                count = size
            self._read_result = win32file.AllocateReadBuffer(count)
            self._overlap.object = partial_func(_read, size, full)
            self._read_coro = AsynCoro.cur_coro(self._asyncoro)
            self._read_coro._await_()
            try:
                rc, _ = win32file.ReadFile(self._handle, self._read_result, self._overlap)
            except pywintypes.error as exc:
                if exc.winerror == winerror.ERROR_BROKEN_PIPE:
                    buf, self._buflist = ''.join(self._buflist), []
                    self._read_coro._proceed_(buf)
                    self._read_result = self._read_coro = self._overlap.object = None
                    return
                else:
                    rc = exc.winerror
            if rc and rc != winerror.ERROR_IO_PENDING:
                self._overlap.object = self._read_result = self._read_coro = None
                self._read_coro.throw(IOError(rc, 'ReadFile', str(rc)))
            if timeout:
                self._timeout = timeout
                self._notifier._add_timeout(self)
示例#39
0
        def write(self, buf, full=False, timeout=None):
            """Write data in 'buf' to file. If 'full' is True, the function
            waits till all data in buf is written; otherwise, it waits
            until one write completes. It returns length of data written.

            If no data has been written before timeout, then
            IOError('timedout') will be thrown.

            If timeout is given and full is True and timeout expires
            before all the data could be written, it returns length of
            data written before timeout if any data has been written.

            Must be used with 'yield' as
            'n = yield fd.write(buf)' to write (some) data in buf.
            """

            def _write(written, rc, n):
                if rc or n == 0:
                    if self._timeout:
                        self._notifier._del_timeout(self)
                    if rc != winerror.ERROR_OPERATION_ABORTED:
                        if written:
                            self._write_coro._proceed_(written)
                        else:
                            self._write_coro.throw(IOError(rc, 'WriteFile', str(rc)))
                    self._overlap.object = self._write_coro = self._write_result = None
                    return

                written += n
                self._overlap.Offset += n
                self._write_result = self._write_result[n:]
                if not full or len(self._write_result) == 0:
                    self._overlap.object = self._write_result = None
                    if self._timeout:
                        self._notifier._del_timeout(self)
                    self._write_coro._proceed_(written)
                    self._write_coro = None
                    return

                self._overlap.object = partial_func(_write, written)
                try:
                    rc, _ = win32file.WriteFile(self._handle, self._write_result, self._overlap)
                except pywintypes.error as exc:
                    rc = exc.winerror
                if rc and rc != winerror.ERROR_IO_PENDING:
                    self._overlap.object = self._write_result = None
                    if self._timeout:
                        self._notifier._del_timeout(self)
                    if written:
                        self._write_coro._proceed_(written)
                    else:
                        self._write_coro.throw(IOError(rc, 'WriteFile', str(rc)))
                    self._write_coro = None
                return

            if not self._asyncoro:
                self._asyncoro = AsynCoro.scheduler()
                self._notifier = self._asyncoro._notifier
                self._notifier.register(self._handle)
            self._write_result = buffer(buf)
            self._overlap.object = partial_func(_write, 0)
            self._write_coro = AsynCoro.cur_coro(self._asyncoro)
            self._write_coro._await_()
            try:
                rc, _ = win32file.WriteFile(self._handle, self._write_result, self._overlap)
            except pywintypes.error as exc:
                if exc.winerror == winerror.ERROR_BROKEN_PIPE:
                    self._write_coro._proceed_(0)
                    self._write_result = self._write_coro = self._overlap.object = None
                    return
                else:
                    rc = exc.winerror
            if rc and rc != winerror.ERROR_IO_PENDING:
                self._overlap.object = self._write_result = self._write_coro = None
                self._write_coro._proceed_(None)
                raise IOError(rc, 'WriteFile', str(rc))
            if timeout:
                self._timeout = timeout
                self._notifier._add_timeout(self)
示例#40
0
        def read(self, size=0, full=False, timeout=None):
            """Read at most 'size' bytes from file; if 'size' <= 0,
            all data up to EOF is read and returned. If 'full' is
            True, exactly 'size' bytes are returned (unless EOF or
            timeout occur before). If EOF is encountered before any
            more data is available, empty buffer is returned.

            If no data has been read before timeout, then
            IOError('timedout') will be thrown.

            If timeout is given and full is True and timeout expires
            before all the data could be read, it returns partial data
            read before timeout if any data has been read.

            Must be used in a coroutine with 'yield' as
            'data = yield fd.read(1024)'
            """

            def _read(size, full):
                if size > 0:
                    count = size
                else:
                    count = 16384
                try:
                    buf = os.read(self._fileno, count)
                except (OSError, IOError) as exc:
                    if exc.errno in (errno.EAGAIN, errno.EWOULDBLOCK):
                        return
                    else:
                        raise
                except:
                    self._notifier.clear(self, _AsyncPoller._Read)
                    self._read_coro.throw(*sys.exc_info())
                    self._read_coro = self._read_task = None
                    return

                if buf:
                    if size > 0:
                        size -= len(buf)
                        # assert size >= 0
                        if size == 0:
                            full = False
                    self._buflist.append(buf)
                    if full:
                        self._read_task = partial_func(_read, size, full)
                        return
                if self._buflist:
                    buf, self._buflist = ''.join(self._buflist), []
                self._notifier.clear(self, _AsyncPoller._Read)
                self._read_coro._proceed_(buf)
                self._read_coro = self._read_task = None

            if not self._asyncoro:
                self._asyncoro = AsynCoro.scheduler()
                self._notifier = self._asyncoro._notifier
                if hasattr(self._fd, '_fileno'):
                    self._notifier.unregister(self._fd)
            if not size or size < 0:
                size = 0
                full = True
            elif self._buflist:
                buf, self._buflist = ''.join(self._buflist), []
                if len(buf) > size:
                    buf, self._buflist = buf[:size], [buf[size:]]
                if (not full) or (len(buf) == size):
                    return buf
                self._buflist = [buf]
                size -= len(buf)
            self._timeout = timeout
            self._read_coro = AsynCoro.cur_coro(self._asyncoro)
            self._read_coro._await_()
            self._read_task = partial_func(_read, size, full)
            self._notifier.add(self, _AsyncPoller._Read)
示例#41
0
文件: action.py 项目: Dunes/janitor
 def as_partial(self, end_time=None, **kwargs):
     if kwargs.get("duration") == 0:
         return None
     obj = self.copy_with(partial=True, **kwargs)
     object.__setattr__(obj, "apply", partial_func(obj.partially_apply, deadline=obj.end_time))
     return obj
示例#42
0
    'is_owner_or_is_guild_owner',
    'is_user_account',
    'is_user_account_or_is_client',
    *checks.__all__,
    *utils.__all__,
)


from .checks import CheckHasRole, CheckIsOwnerOrHasRole, CheckHasAnyRole, CheckIsOwnerOrHasAnyRole, CheckIsInGuild, \
    CheckIsInPrivate, CheckIsOwner, CheckIsGuildOwner, CheckIsOwnerOrIsGuildOwner, CheckHasPermission, \
    CheckIsOwnerOrHasPermission, CheckHasGuildPermission, CheckIsOwnerHasGuildPermission, CheckHasClientPermission, \
    CheckHasClientGuildPermission, CheckIsGuild, CheckIsAnyGuild, CheckCustom, CheckIsChannel, CheckIsAnyChannel, \
    CheckIsNsfwChannel, CheckIsAnnouncementChannel, CheckIsInVoice, CheckIsBooster, CheckIsClient, CheckUserAccount, \
    CheckBotAccount, CheckIsUserAccountOrIsClient, CheckIsCategory, CheckIsAnyCategory

has_role = partial_func(CommandCheckWrapper, CheckHasRole)
is_owner_or_has_role = partial_func(CommandCheckWrapper, CheckIsOwnerOrHasRole)
has_any_role = partial_func(CommandCheckWrapper, CheckHasAnyRole)
is_owner_or_has_any_role = partial_func(CommandCheckWrapper,
                                        CheckIsOwnerOrHasAnyRole)
is_in_guild = partial_func(CommandCheckWrapper, CheckIsInGuild)
is_in_private = partial_func(CommandCheckWrapper, CheckIsInPrivate)
is_owner = partial_func(CommandCheckWrapper, CheckIsOwner)
is_guild_owner = partial_func(CommandCheckWrapper, CheckIsGuildOwner)
is_owner_or_is_guild_owner = partial_func(CommandCheckWrapper,
                                          CheckIsOwnerOrIsGuildOwner)
has_permission = partial_func(CommandCheckWrapper, CheckHasPermission)
is_owner_or_has_permission = partial_func(CommandCheckWrapper,
                                          CheckIsOwnerOrHasPermission)
has_guild_permission = partial_func(CommandCheckWrapper,
                                    CheckHasGuildPermission)