Skip to content

Commit 2da7ab5

Browse files
authored
chore: update project structure (#2)
* chore: rename compose directory to docker * chore: add CLI commands for test, format and linting * chore: update readme file
1 parent 7a94cb0 commit 2da7ab5

38 files changed

+299
-243
lines changed

Pipfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ apispec-webframeworks = "==0.5.2"
3434
gunicorn = "==20.0.4"
3535
zappa = "==0.52.0"
3636

37-
[script]
37+
[scripts]
38+
format = "python3 cli/format.py quotes_api tests"
39+
lint = "python3 cli/lint.py quotes_api"
40+
test = "python3 cli/test.py tests"
3841

3942
[requires]
4043
python_version = "3.8"

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ Make sure to create a hidden folder like `envs.example` named `.envs`, with the
4747
docker-compose up -f local.yml up -d --build
4848
```
4949

50+
## Commands
51+
The project supports cli commands for formatting, linting, database initialization and more.
52+
53+
| **Action** | **Command** |
54+
| :--------: | :---------------: |
55+
| Test | pipenv run test |
56+
| Format | pipenv run format |
57+
| Lint | pipenv run lint |
58+
5059
## :rocket: Deployment
5160
This project includes configuration files for both Heroku and AWS using Zappa.
5261

cli/__init__.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Quotes API CLI Initialization."""
2+
import importlib
3+
import os
4+
5+
6+
def register_cli_commands(app):
7+
"""
8+
Register 0 or more Flask CLI commands (mutates the app passed in).
9+
10+
:param app: Flask application instance
11+
:return: None
12+
"""
13+
for file in os.listdir(os.path.dirname(__file__)):
14+
if file.startswith("flask_") and file.endswith(".py"):
15+
# Get the file name
16+
file_name = file[:-3]
17+
command_name = file[4:-3]
18+
19+
# Import the cli command module
20+
module = importlib.import_module(f"cli.{file_name}")
21+
22+
command = getattr(module, command_name)
23+
24+
# Register the cli command under the Flask app
25+
app.cli.add_command(command)

cli/format.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Formating (Black) CLI Command"""
2+
import subprocess
3+
4+
import click
5+
6+
7+
@click.command()
8+
@click.argument("path", default=".")
9+
def format(path):
10+
"""
11+
Run black code formatter on the codebase.
12+
13+
:param path: Execution path
14+
:return: Subprocess call result
15+
"""
16+
command = f"black {path}"
17+
return subprocess.call(command, shell=True)
18+
19+
20+
if __name__ == "__main__":
21+
format()

cli/lint.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Linter (Pylint) CLI Command"""
2+
import subprocess
3+
4+
import click
5+
6+
7+
@click.command()
8+
@click.argument("path", default="quots_api")
9+
def lint(path):
10+
"""
11+
Run pylint linter to analyze the codebase.
12+
13+
:param path: Execution path
14+
:return: Subprocess call result
15+
"""
16+
command = f"pylint {path}"
17+
return subprocess.call(command, shell=True)
18+
19+
20+
if __name__ == "__main__":
21+
lint()

cli/test.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Test (Pytest) CLI Command"""
2+
import subprocess
3+
4+
import click
5+
6+
7+
@click.command()
8+
@click.argument("path", default="tests")
9+
def test(path):
10+
"""
11+
Run tests with Pytest.
12+
13+
:param path: Test path
14+
:return: Subprocess call result
15+
"""
16+
command = f"pytest -s {path}"
17+
return subprocess.call(command, shell=True)

compose/local/application/Dockerfile renamed to docker/local/application/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ RUN pip install pipenv
3232
RUN pipenv install --ignore-pipfile --system && pipenv install --ignore-pipfile --dev --system
3333

3434
# Copy start bash script ahd change permissions
35-
COPY ./compose/local/application/start /start
35+
COPY ./docker/local/application/start /start
3636
RUN sed -i 's/\r$//g' /start
3737
RUN chmod +x /start
3838

File renamed without changes.

compose/production/application/Dockerfile renamed to docker/production/application/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ RUN pip install pipenv
3232
RUN pipenv install --ignore-pipfile --system && pipenv install --ignore-pipfile --dev --system
3333

3434
# Copy start bash script ahd change permissions
35-
COPY ./compose/production/application/start /start
35+
COPY ./docker/production/application/start /start
3636
RUN sed -i 's/\r$//g' /start
3737
RUN chmod +x /start
3838

local.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
application:
55
build:
66
context: .
7-
dockerfile: ./compose/local/application/Dockerfile
7+
dockerfile: ./docker/local/application/Dockerfile
88
image: quotes_api_local
99
container_name: quotes_api
1010
depends_on:

production.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
application:
55
build:
66
context: .
7-
dockerfile: ./compose/production/application/Dockerfile
7+
dockerfile: ./docker/production/application/Dockerfile
88
image: quotes_api_production
99
container_name: quotes_api
1010
volumes:

quotes_api/api/resources/author.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99

1010
class AuthorList(Resource):
11-
""" List of quote authors.
12-
11+
"""List of quote authors.
12+
1313
---
1414
get:
1515
tags:
@@ -22,7 +22,7 @@ class AuthorList(Resource):
2222
parameters:
2323
- in: query
2424
name: page
25-
schema:
25+
schema:
2626
type: integer
2727
default: 1
2828
description: Page number of the pagination.
@@ -38,7 +38,7 @@ class AuthorList(Resource):
3838
type: string
3939
enum: [asc, desc]
4040
default: asc
41-
description: Author's name sort order.
41+
description: Author's name sort order.
4242
responses:
4343
200:
4444
content:
@@ -64,7 +64,7 @@ class AuthorList(Resource):
6464

6565
@role_required([Role.BASIC, Role.ADMIN])
6666
def get(self):
67-
""" Get quote authors by alphabetical order. """
67+
"""Get quote authors by alphabetical order."""
6868

6969
args = request.args
7070
page = int(args.get("page", 1))
@@ -94,7 +94,7 @@ def get(self):
9494
)
9595

9696
def sort_order_parser(self, input):
97-
"""
97+
"""
9898
Parses a user query input for the sort_order parameter.
9999
100100
Checks if the sorting is ascending or descending.

0 commit comments

Comments
 (0)