diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..a77cb08 --- /dev/null +++ b/.flake8 @@ -0,0 +1,4 @@ +[flake8] +max-line-length = 80 +extend-select = B950 +extend-ignore = E203,E501,E701 diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index fce1b33..5b27c19 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -8,6 +8,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 + - name: Check lint + run: ./scripts/lint.sh - name: Docker Login uses: docker/login-action@v3.0.0 with: diff --git a/bootstrap.py b/bootstrap.py index 3488849..eb59a48 100644 --- a/bootstrap.py +++ b/bootstrap.py @@ -16,19 +16,26 @@ async def bootstrap(collection_api: CollectionApi, imdb_api: ImdbApi): """ - This function is called at the start of the application to regularly send HTTP requests - and cache the results. This can help saving time on incoming requests. + This function is called at the start of the application to regularly send + HTTP requests and cache the results. + + This can help saving time on incoming requests. """ while True: logging.info("Bootstrapping...") all_collections = deque(COMMON_COLLECTIONS) visited_collections = set() - while all_collections and len(visited_collections) < app_config.bootstrap_collections_max: + while ( + all_collections + and len(visited_collections) < app_config.bootstrap_collections_max + ): collection_id = all_collections.popleft() visited_collections.add(collection_id) try: - collection_info = await collection_api.get_collection_info(collection_id) + collection_info = await collection_api.get_collection_info( + collection_id + ) for related_collection in collection_info["related_charts"]["items"]: related_collection_id = related_collection["id"] if related_collection_id not in visited_collections: diff --git a/collection.py b/collection.py index e91447f..89ee782 100644 --- a/collection.py +++ b/collection.py @@ -22,7 +22,6 @@ def __exit__(self, exc_type, exc_value, traceback): self.client.close() self.cache.close() - async def get_collection_info(self, collection_id: str): return await get_json(self.client, f"/{collection_id}") diff --git a/requirements.txt b/requirements.txt index 3a12c8f..6dd23df 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,8 @@ uvicorn[standard]==0.27.1 httpx[socks]==0.24.1 diskcache==5.6.3 pydantic-settings==2.2.0 + +# for linting +black==23.3.0 +flake8==6.1.0 +flake8-bugbear==24.2.6 diff --git a/scripts/hooks/pre-commit b/scripts/hooks/pre-commit new file mode 100755 index 0000000..0eda5f7 --- /dev/null +++ b/scripts/hooks/pre-commit @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -euo pipefail + +scripts/lint.sh --check diff --git a/scripts/init_repo.sh b/scripts/init_repo.sh new file mode 100755 index 0000000..6bea1b3 --- /dev/null +++ b/scripts/init_repo.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -euo pipefail + +cd $(git rev-parse --show-toplevel) + +rm -f .git/hooks/pre-commit +ln -s ../../scripts/hooks/pre-commit .git/hooks/pre-commit diff --git a/scripts/lint.sh b/scripts/lint.sh new file mode 100755 index 0000000..ccbddf1 --- /dev/null +++ b/scripts/lint.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# read the `--check` arg from the command line +check=false +if [ $# -gt 0 ] && [[ $1 == "--check" ]]; then + check=true +fi + +black_args=() +if [[ "$check" == "true" ]]; then + black_args+=(--check --diff) +fi + +cd $(git rev-parse --show-toplevel) + +# find all Python files +python_files=$(git ls-files | grep '\.py$' || true) + +black "${black_args[@]}" $python_files +flake8 $python_files