Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTraveylan authored Oct 1, 2024
0 parents commit baf2bfd
Show file tree
Hide file tree
Showing 30 changed files with 705 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: <Modify it by your name>
on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Set PYTHONPATH
run: |
echo "PYTHONPATH=${{ github.workspace }}" >> $GITHUB_ENV
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest bandit
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Run tests
run: |
pytest
- name: Run Bandit security checks
run: |
bandit -r ./ -ll -ii -x B101
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Python cache files
__pycache__/
*_cache/

# Logs
*.log
log*.jsonl

# Editor files
# .vscode/


# virtual environment
env*/
.env*
venv*/
.venv*/

# coverage
.coverage
htmlcov/
*.egg-info/
dist/
build/
5 changes: 5 additions & 0 deletions .pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
tests
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"ms-python.python",
"ms-python.vscode-pylance",
"charliermarsh.ruff"
]
}
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
27 changes: 27 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.ruff": "always",
"source.organizeImports.ruff": "always",
"source.unusedImports.ruff": "never",
}
},
"terminal.integrated.env.windows": {
"PYTHONPATH": "${workspaceFolder}"
},
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"editor.rulers": [
88,
90
],
"files.exclude": {
"**/__pycache__": true,
"**/*_cache": true
},
}
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Use an official Python runtime as a parent image
FROM python:3.10

# Set the working directory in the container to /app
WORKDIR /app

# Add the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Run app.py when the container launches
CMD ["python", "app/main.py"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 AlexTraveylan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Python Template

## Description

This is a Python project that uses pytest for testing and is configured for development in Visual Studio Code. It also includes a Dockerfile for containerization.

## Project Structure

The main application code is located in the `app` directory, with the entry point in `app/main.py`. The `app` directory also contains a `core` subdirectory for core functionality and an `adapter` subdirectory for adapters.

The `tests` directory contains unit, integration, and end-to-end tests.

## Setup

### Requirements

- Python 3.10
- pip

### Installation

1. Clone the repository.
2. Install the dependencies:

```sh
pip install -r dev_requirements.txt
```

### Running the Application

To run the application:

```sh
python app/main.py
```

### Running the Tests

To run the tests:

```sh
pytest
```

## Development

This project is configured for development in Visual Studio Code with settings for the Python extension, including formatting and linting settings. The `.vscode` directory contains the configuration files.

## Docker

A Dockerfile is included for building a Docker image of the application. To build the image:

```sh
docker build -t <image-name> .
```

To run the application in a Docker container:

```sh
docker run -p 80:80 <image-name>
```

## Continuous Integration

The project includes a GitHub Actions workflow for continuous integration, which runs tests and security checks on push and pull request events to the main branch.

## Logging

The application uses Python's built-in logging module, with configuration in `app/adapter/logger/config_log.json`.

## package

To create the package, run the following command:
```bash
python setup.py sdist bdist_wheel
```

The package is available on whl file in the dist folder. To install it, run the following command:
```bash
pip install dist/<app-name>-0.1-py3-none-any.whl
```
Note : 0.1 is the version of the package, change it if needed.

## Contributing

Contributions are welcome. Please submit a pull request or create an issue to discuss the changes.

## License

[MIT](LICENSE)
9 changes: 9 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Entry point for the application."""

import sys

from app.adapter.logger.activation_condition import is_on_site_packages
from app.adapter.logger.setup_logging import init_logger

if not is_on_site_packages(__file__) and "pytest" not in sys.modules:
init_logger()
Empty file added app/adapter/__init__.py
Empty file.
Empty file.
32 changes: 32 additions & 0 deletions app/adapter/exception/app_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Module for the AppException class
:author: Alex Traveylan
:date: 2024
"""


class AppException(Exception):
"""
Exception class for the application
Attributes
----------
message : str
The message of the exception
"""

def __init__(self, message: str):
"""
Constructor for AppException
Parameters
----------
message : str
The message of the exception
"""
super().__init__(message)
self.message = message

def __str__(self):
return self.message
Empty file added app/adapter/logger/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions app/adapter/logger/activation_condition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Module to determine if a file is in the site-packages directory.
Usage: not initiate the logger if the file is in the site-packages directory.
:author: Alex Traveylan
:date: 2024
"""

from pathlib import Path


def is_on_site_packages(actual_file_path: str | Path) -> bool:
"""
This function checks if the file is in the site-packages directory.
Parameters
----------
actual_file_path : str | Path
Path to the file to check.
Returns
-------
bool
True if the file is in the site-packages directory, False otherwise.
"""
absolute_file_path = Path(actual_file_path).resolve()

is_lib_in_path = "Lib" in str(absolute_file_path)
is_site_packages_in_path = "site-packages" in str(absolute_file_path)

return is_lib_in_path and is_site_packages_in_path
48 changes: 48 additions & 0 deletions app/adapter/logger/config_log.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"simple": {
"format": "[%(levelname)s|%(module)s|L%(lineno)d] %(asctime)s: %(message)s",
"datefmt": "%Y-%m-%dT%H:%M:%S%z"
},
"json": {
"()": "app.adapter.logger.mylogger.MyJSONFormatter",
"fmt_keys": {
"level": "levelname",
"message": "message",
"timestamp": "timestamp",
"logger": "name",
"module": "module",
"function": "funcName",
"line": "lineno",
"thread_name": "threadName"
}
}
},
"handlers": {
"stderr": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "simple",
"stream": "ext://sys.stderr"
},
"file_json": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "json",
"filename": "app/adapter/logger/logs/log.jsonl",
"maxBytes": 100000,
"backupCount": 3
}
},
"loggers": {
"root": {
"level": "DEBUG",
"handlers": [
"stderr",
"file_json"
]
}
}
}
Empty file.
Loading

0 comments on commit baf2bfd

Please sign in to comment.