Exemple #1
0
async def encrypt_file_aio(filename: Union[str, Path],
                           password: Union[str, bytes],
                           output: Union[str, Path],
                           compress: bool = False) -> None:
    async with aio_open(str(filename), mode='rb') as plain:
        async with aio_open(str(output), mode='wb') as encrypted:
            await encrypted.write(
                encrypt(await plain.read(), password, compress))
Exemple #2
0
 async def get_old_exceptions(self, year: int, month: int, day: int) -> Optional[str]:
     """Return the old exceptions, If can't found the file, return None."""
     _, old_filename = self._old_filename(year, month, day)
     if old_filename.is_file():
         async with aio_open(str(old_filename), mode='r', encoding='utf-8') as log_file:
             return await log_file.read()
     else:
         return None
Exemple #3
0
        async def download(api_client, semaphore, file):
            async with semaphore:
                filename = f"{save_to}/{file.name}"
                url = f"{api_client.api_url}{file.path}"

                async with aio_open(filename, "wb") as download_file:
                    async with api_client.client.stream("GET", url) as stream:
                        async for chunk in stream.aiter_bytes():
                            await download_file.write(chunk)
async def aio_wget(url: str,
                   filename: Union[Path, str],
                   timeout: int = 5) -> bool:
    """
    Download a file from url.
    if the http status code is 200 return true.
    """
    try:
        async with ClientSession() as session:
            async with session.get(url, allow_redirects=True,
                                   timeout=timeout) as res:
                if res.status == 200:
                    async with aio_open(str(filename), mode='wb') as file:
                        await file.write(await res.read())
                    return True
                else:
                    return False
    except ClientError:
        return False
Exemple #5
0
async def aio_get_str_from_file(filename: Union[str, Path],
                                encoding: str = ENCODING) -> str:
    async with aio_open(str(filename), mode='r', encoding=encoding) as file:
        return await file.read()
Exemple #6
0
async def aio_append_str_to_file(filename: Union[str, Path],
                                 text: str,
                                 encoding: str = ENCODING) -> None:
    async with aio_open(str(filename), mode='a', encoding=encoding) as file:
        await file.write(text)
Exemple #7
0
async def aio_write_bytes_to_file(filename: Union[str, Path],
                                  data: bytes) -> None:
    async with aio_open(str(filename), mode='wb') as file:
        await file.write(data)
Exemple #8
0
 async def get_all_log(self) -> str:
     async with aio_open(self._filename, mode='r',
                         encoding=self._encoding) as file:
         return await file.read()
Exemple #9
0
async def decrypt_file_aio(filename: Union[str, Path], password: Union[str,
                                                                       bytes],
                           output: Union[str, Path]) -> None:
    async with aio_open(str(filename), mode='rb') as encrypted:
        async with aio_open(str(output), mode='wb') as plain:
            await plain.write(decrypt(await encrypted.read(), password))
Exemple #10
0
 async def get_all_logs(self) -> str:
     """Return today's logs'"""
     async with aio_open(str(self._filename), mode='r', encoding='utf-8') as log_file:
         return await log_file.read()
async def moca_aio_load(filename: Union[Path, str]) -> Any:
    """Load serialized object."""
    async with aio_open(str(filename), mode='rb') as file:
        return moca_loads(await file.read())
async def moca_aio_dump(obj: Any, filename: Union[Path, str]) -> None:
    """serialize and compress."""
    async with aio_open(str(filename), mode='wb') as file:
        await file.write(moca_dumps(obj))
Exemple #13
0
async def load_with_encryption_aio(filename: Union[str, Path],
                                   password: Union[str, bytes]) -> Any:
    async with aio_open(str(filename), mode='rb') as file:
        return loads_with_encryption(await file.read(), password)
Exemple #14
0
async def dump_with_encryption_aio(data: Any,
                                   filename: Union[str, Path],
                                   password: Union[str, bytes],
                                   compress: bool = False) -> None:
    async with aio_open(str(filename), 'wb') as file:
        await file.write(dumps_with_encryption(data, password, compress))
Exemple #15
0
async def aio_get_bytes_from_file(filename: Union[str, Path]) -> bytes:
    async with aio_open(str(filename), mode='rb') as file:
        return await file.read()
Exemple #16
0
 async def open_async(file, mode="r", **kwargs):
     return aio_open(file, mode, **kwargs)
Exemple #17
0
 async def get_all_exceptions(self) -> str:
     """Return today's exceptions'"""
     async with aio_open(str(self._exc_filename), mode='r', encoding='utf-8') as exc_file:
         return await exc_file.read()