summaryrefslogtreecommitdiff
path: root/ATRI/utils/file.py
blob: 7afbeadac37958183a47b8ff4a01121a333dc71d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import aiofiles
import urllib
from pathlib import Path

from ATRI.exceptions import RequestTimeOut, WriteError

from .request import get_content


async def write_file(path: Path, text, encoding='utf-8') -> None:
    try:
        async with aiofiles.open(path, 'w', encoding=encoding) as target:
            await target.write(text)
    except WriteError:
        raise WriteError("Writing file failed!")


async def open_file(path: Path, method, encoding='utf-8'):
    try:
        async with aiofiles.open(path, 'r', encoding=encoding) as target:
            if method == "read":
                return target.read()
            elif method == "readlines":
                return await target.readlines()
            elif method == "readline":
                return await target.readline()
            else:
                return target.readable()
    except EOFError:
        raise EOFError("File not fond!")