Skip to content

Commit 68e88b4

Browse files
committed
Refactor.
1 parent 6ffe436 commit 68e88b4

8 files changed

Lines changed: 83 additions & 50 deletions

File tree

.github/workflows/isort.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Run isort
2+
on:
3+
- push
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: actions/setup-python@v2
11+
with:
12+
python-version: 3.9
13+
- uses: jamescurtin/isort-action@master
14+
with:
15+
requirementsFiles: "requirements.txt"

asyncio_tutorial/fetcher.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
"""Fetch and save HTML pages asynchronously."""
1+
"""Fetch and save files to disk asynchronously."""
22
from aiohttp import ClientError, ClientSession, InvalidURL
33

44
from asyncio_tutorial.logger import LOGGER
5-
from asyncio_tutorial.writer import write_fetched_file_locally
5+
from asyncio_tutorial.writer import write_file
66

77

88
async def fetch_and_save_url(
@@ -27,7 +27,7 @@ async def fetch_and_save_url(
2727
.replace("text/", "")
2828
)
2929
LOGGER.info(f"Successfully fetched URL {i + 1} of {total_count}: {url}")
30-
await write_fetched_file_locally(url, body, filetype, directory)
30+
await write_file(url, body, filetype, directory)
3131
except InvalidURL as e:
3232
LOGGER.error(f"Unable to fetch invalid URL `{url}`: {e}")
3333
except ClientError as e:

asyncio_tutorial/logger.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
"""Create custom logger."""
1+
"""Custom logger."""
2+
from os import path
23
from sys import stdout
34

45
from loguru import logger as custom_logger
56

7+
from config import ENVIRONMENT
8+
69

710
def formatter(log: dict) -> str:
811
"""
@@ -46,6 +49,14 @@ def create_logger() -> custom_logger:
4649
"""
4750
custom_logger.remove()
4851
custom_logger.add(stdout, colorize=True, format=formatter)
52+
if ENVIRONMENT == "production":
53+
# Datadog JSON logs
54+
custom_logger.add(
55+
"/var/log/asyncio_tutorial/info.json",
56+
format=formatter,
57+
rotation="500 MB",
58+
compression="zip",
59+
)
4960
return custom_logger
5061

5162

asyncio_tutorial/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Prepare tasks to be executed asynchronously."""
1+
"""Prepare tasks to be executed."""
22
import asyncio
33
from asyncio import Task
44
from typing import List

asyncio_tutorial/writer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
from .logger import LOGGER
55

66

7-
async def write_fetched_file_locally(
8-
url: str, body: bytes, filetype: str, directory: str
9-
):
7+
async def write_file(url: str, body: bytes, filetype: str, directory: str):
108
"""
119
Write contents of fetched URL to new file in local directory.
1210

config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@
1616
"connection": "keep-alive",
1717
"accept": "*/*",
1818
}
19+
20+
# Determine environment based on log folder
21+
ENVIRONMENT = "development"
22+
if path.isdir("/var/log/asyncio_tutorial"):
23+
ENVIRONMENT = "production"

poetry.lock

Lines changed: 42 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ issues = "https://github.com/hackersandslackers/asyncio-tutorial/issues"
4343
[build-system]
4444
requires = ["poetry>=1.18"]
4545
build-backend = "poetry.masonry.api"
46+
47+
[tool.isort]
48+
profile = "black"
49+
src_paths = ["asyncio_tutorial", "data", "main", "config"]

0 commit comments

Comments
 (0)