Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ responses = "*"

[packages]
aiocache = {extras = ["redis"],version = "*"}
aiofiles = "*"
aiohttp = "*"
asyncache = "*"
cachetools = "*"
Expand Down
45 changes: 27 additions & 18 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 32 additions & 4 deletions app/io.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
"""app.io.py"""
import json
import pathlib
from typing import Dict, Union
from typing import Dict, List, Union

import aiofiles

HERE = pathlib.Path(__file__)
DATA = HERE.joinpath("..", "data").resolve()


def save(
name: str, content: Union[str, Dict], write_mode: str = "w", indent: int = 2, **json_dumps_kwargs
name: str, content: Union[str, Dict, List], write_mode: str = "w", indent: int = 2, **json_dumps_kwargs
) -> pathlib.Path:
"""Save content to a file. If content is a dictionary, use json.dumps()."""
path = DATA / name
if isinstance(content, dict):
if isinstance(content, (dict, list)):
content = json.dumps(content, indent=indent, **json_dumps_kwargs)
with open(DATA / name, mode=write_mode) as f_out:
f_out.write(content)
return path


def load(name: str, **json_kwargs) -> Union[str, Dict]:
def load(name: str, **json_kwargs) -> Union[str, Dict, List]:
"""Loads content from a file. If file ends with '.json', call json.load() and return a Dictionary."""
path = DATA / name
with open(path) as f_in:
if path.suffix == ".json":
return json.load(f_in, **json_kwargs)
return f_in.read()


class AIO:
"""Asynsc compatible file io operations."""

@classmethod
async def save(
cls, name: str, content: Union[str, Dict, List], write_mode: str = "w", indent: int = 2, **json_dumps_kwargs
):
"""Save content to a file. If content is a dictionary, use json.dumps()."""
path = DATA / name
if isinstance(content, (dict, list)):
content = json.dumps(content, indent=indent, **json_dumps_kwargs)
async with aiofiles.open(DATA / name, mode=write_mode) as f_out:
await f_out.write(content)
return path

@classmethod
async def load(cls, name: str, **json_kwargs) -> Union[str, Dict, List]:
"""Loads content from a file. If file ends with '.json', call json.load() and return a Dictionary."""
path = DATA / name
async with aiofiles.open(path) as f_in:
content = await f_in.read()
if path.suffix == ".json":
content = json.loads(content, **json_kwargs)
return content
10 changes: 5 additions & 5 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-i https://pypi.org/simple
appdirs==1.4.3
astroid==2.3.3
astroid==2.4.0
async-asgi-testclient==1.4.4
async-generator==1.10
asyncmock==0.4.2
Expand All @@ -9,7 +9,7 @@ bandit==1.6.2
black==19.10b0
certifi==2020.4.5.1
chardet==3.0.4
click==7.1.1
click==7.1.2
coverage==5.1
coveralls==2.0.0
docopt==0.6.2
Expand All @@ -29,7 +29,7 @@ pathspec==0.8.0
pbr==5.4.5
pluggy==0.13.1
py==1.8.1
pylint==2.4.4
pylint==2.5.0
pyparsing==2.4.7
pytest-asyncio==0.11.0
pytest-cov==2.8.1
Expand All @@ -42,8 +42,8 @@ six==1.14.0
smmap==3.0.2
stevedore==1.32.0
toml==0.10.0
typed-ast==1.4.1
typed-ast==1.4.1 ; implementation_name == 'cpython' and python_version < '3.8'
urllib3==1.25.9
wcwidth==0.1.9
wrapt==1.11.2
wrapt==1.12.1
zipp==3.1.0
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-i https://pypi.org/simple
aiocache[redis]==0.11.1
aiofiles==0.5.0
aiohttp==3.6.2
aioredis==1.3.1
async-timeout==3.0.1
Expand All @@ -8,7 +9,7 @@ attrs==19.3.0
cachetools==4.1.0
certifi==2020.4.5.1
chardet==3.0.4
click==7.1.1
click==7.1.2
dataclasses==0.6 ; python_version < '3.7'
fastapi==0.54.1
gunicorn==20.0.4
Expand All @@ -25,7 +26,7 @@ requests==2.23.0
six==1.14.0
starlette==0.13.2
urllib3==1.25.9
uvicorn==0.11.3
uvicorn==0.11.4
uvloop==0.14.0 ; sys_platform != 'win32' and sys_platform != 'cygwin' and platform_python_implementation != 'PyPy'
websockets==8.1
yarl==1.4.2
37 changes: 27 additions & 10 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@

import app.io


@pytest.mark.parametrize(
IO_PARAMS = (
"name, content, kwargs",
[
("test_file.txt", string.ascii_lowercase, {}),
("test_json_file.json", {"a": 0, "b": 1, "c": 2}, {}),
("test_custom_json.json", {"z": -1, "b": 1, "y": -2, "a": 0}, {"indent": 4, "sort_keys": True}),
],
)


@pytest.mark.parametrize(*IO_PARAMS)
def test_save(tmp_path, name, content, kwargs):
test_path = tmp_path / name
assert not test_path.exists()
Expand All @@ -23,17 +25,32 @@ def test_save(tmp_path, name, content, kwargs):
assert test_path.exists()


@pytest.mark.parametrize(
"name, content, kwargs",
[
("test_file.txt", string.ascii_lowercase, {}),
("test_json_file.json", {"a": 0, "b": 1, "c": 2}, {}),
("test_custom_json.json", {"z": -1, "b": 1, "y": -2, "a": 0}, {"indent": 4, "sort_keys": True}),
],
)
@pytest.mark.parametrize(*IO_PARAMS)
def test_round_trip(tmp_path, name, content, kwargs):
test_path = tmp_path / name
assert not test_path.exists()

app.io.save(test_path, content, **kwargs)
assert app.io.load(test_path) == content


@pytest.mark.asyncio
@pytest.mark.parametrize(*IO_PARAMS)
async def test_async_save(tmp_path, name, content, kwargs):
test_path = tmp_path / name
assert not test_path.exists()

result = await app.io.AIO.save(test_path, content, **kwargs)
assert result == test_path
assert test_path.exists()


@pytest.mark.asyncio
@pytest.mark.parametrize(*IO_PARAMS)
async def test_async_round_trip(tmp_path, name, content, kwargs):
test_path = tmp_path / name
assert not test_path.exists()

await app.io.AIO.save(test_path, content, **kwargs)
load_results = await app.io.AIO.load(test_path)
assert load_results == content