Esempio n. 1
0
async def test_readme_example4_read_write():
    text: str = 'example'

    async with NamedTemporaryFile() as temp:
        path = AsyncPath(temp.name)

        async with path.open(mode='w') as file:
            await file.write(text)

        async with path.open(mode='r') as file:
            result: str = await file.read()

        assert result == text

    async with NamedTemporaryFile() as temp:
        path = AsyncPath(temp.name)

        await path.write_text(text)
        result: str = await path.read_text()
        assert result == text

        content: bytes = text.encode()

        await path.write_bytes(content)
        result: bytes = await path.read_bytes()
        assert result == content
Esempio n. 2
0
    async def _to_zip_server(self, ctx: commands.Context):
        """
        Get a `.zip` archive of all custom emojis in the server.

        The returned `.zip` archive can be used for the `[p]emojitools add fromzip` command.
        """

        async with ctx.typing():
            count = 0
            async with TemporaryDirectory() as temp_dir:
                for e in ctx.guild.emojis:
                    count += 1
                    if e.animated:
                        await e.url.save(
                            os.path.join(temp_dir, f"{e.name}.gif"))
                    else:
                        await e.url.save(
                            os.path.join(temp_dir, f"{e.name}.png"))

                aiozip = AioZipStream(await self.getfiles(temp_dir),
                                      chunksize=32768)
                async with NamedTemporaryFile('wb+') as z:
                    async for chunk in aiozip.stream():
                        await z.write(chunk)
                    await z.seek(0)
                    zip_file_obj = discord.File(
                        z.name, filename=f"{ctx.guild.name}.zip")

        return await ctx.send(
            f"{count} emojis were saved to this `.zip` archive!",
            file=zip_file_obj)
Esempio n. 3
0
    async def _to_zip_emojis(self, ctx: commands.Context, *emojis: str):
        """
        Get a `.zip` archive of a list of emojis.

        The returned `.zip` archive can be used for the `[p]emojitools add fromzip` command.
        """

        async with ctx.typing():
            async with TemporaryDirectory() as temp_dir:
                for e in emojis:
                    try:
                        em = await commands.PartialEmojiConverter().convert(
                            ctx=ctx, argument=e)
                    except commands.BadArgument:
                        return await ctx.send(f"Invalid emoji: {e}")

                    if em.animated:
                        await em.url.save(
                            os.path.join(temp_dir, f"{em.name}.gif"))
                    else:
                        await em.url.save(
                            os.path.join(temp_dir, f"{em.name}.png"))

                aiozip = AioZipStream(await self.getfiles(temp_dir),
                                      chunksize=32768)
                async with NamedTemporaryFile('wb+') as z:
                    async for chunk in aiozip.stream():
                        await z.write(chunk)
                    await z.seek(0)
                    zip_file_obj = discord.File(z.name, filename="emojis.zip")

        return await ctx.send(
            f"{len(emojis)} emojis were saved to this `.zip` archive!",
            file=zip_file_obj)
Esempio n. 4
0
async def test_readme_example1_basic():
    async with NamedTemporaryFile() as temp:
        path, apath = get_paths(temp.name)

        # check existence
        ## sync
        assert path.exists()
        ## async
        assert await apath.exists()

        # check if file
        ## sync
        assert path.is_file()
        ## async
        assert await apath.is_file()

        # touch
        path.touch()
        await apath.touch()

        # PurePath methods are not async
        assert path.is_absolute() == apath.is_absolute()
        assert path.as_uri() == apath.as_uri()

        # read and write text
        text: str = 'example'
        await apath.write_text(text)
        assert await apath.read_text() == text

    assert not path.exists()
    assert not await apath.exists()
Esempio n. 5
0
async def session_source(
    event_loop: asyncio.AbstractEventLoop,
    session_data: typing.Dict[str, typing.Any]
) -> typing.Generator[NamedTemporaryFile, None, None]:
    """Create a predefined session data source."""
    async with NamedTemporaryFile("wb") as session_file:
        await asyncio.wait_for(
            event_loop.run_in_executor(
                None,
                partial(pickle.dump, obj=session_data, file=session_file.raw)),
            timeout=None,
        )
        yield session_file
Esempio n. 6
0
async def file_paths() -> Paths:
  async with NamedTemporaryFile() as temp:
    yield get_paths(temp.name)