Skip to content

Commit

Permalink
Replace print statements with logging (#4)
Browse files Browse the repository at this point in the history
* Replace print statements with logging

- defaults to only console logging
- defaults to WARNING level
   - configurable with: `export APP_LOG_LEVEL=[INFO|DEBUG]`

* Merge awoods-logging into logging (#3)

* linter for box reader after logging

* comment out diff cover

remove socket

* skip processing of files where n value is not 1

* tests with 65% cvg

* add in github actions install of diff cover globally

* update paths for diff cover

* test diff cover install and check path

* uncomment diff cover test

* uncomment diff cover test 2

* new coverage test

* full test coverage

* flake8 passes all files

* check flake8 version

* mod flake8 command for 120 line length

* add additional test coverage, stops early if not 85

---------

Co-authored-by: kim pham <[email protected]>
  • Loading branch information
awoods and kimpham54 committed Nov 15, 2024
1 parent 9bfc71d commit 47f5496
Show file tree
Hide file tree
Showing 9 changed files with 559 additions and 254 deletions.
24 changes: 13 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ jobs:
- name: Run flake8
run: |
pip install flake8
flake8 --version
# stop the build if there are flake8 errors
flake8 . --count --show-source --statistics
flake8 . --count --show-source --statistics --max-line-length 120
- name: Run unit tests
run: |
Expand All @@ -45,25 +46,26 @@ jobs:
pip install coverage
python -m coverage run -p -m pytest src/jp2_remediator/tests/unit
python -m coverage combine
python -m coverage report -m --skip-covered
python -m coverage report -m --skip-covered --fail-under=85
python -m coverage xml
# Fetch base branch for comparison (e.g., main)
- name: Fetch base branch
run: git fetch origin main

# Compare coverage with the base branch
- name: Install diff-cover
run: |
pip install --user diff-cover
find $HOME -name "diff-cover" || echo "diff-cover not found"
- name: Add diff-cover to PATH
run: echo "$HOME/.local/bin" >> $GITHUB_PATH

# Compare coverage with the base branch, if decreases fails, if under 85 percent fails
- name: Compare coverage
run: |
pip install diff-cover
git checkout main
python -m coverage run -p -m pytest src/jp2_remediator/tests/unit
python -m coverage xml -o coverage-base.xml
git checkout -
python diff-cover --compare-branch=main coverage.xml
# Fail if coverage decreases
- name: Fail if coverage decreases
run: |
python diff-cover --compare-branch=main coverage.xml --fail-under=100
diff-cover --compare-branch=main coverage.xml --fail-under=85
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
*~
*.swp

logs/

input/*
output/*
.coverage
coverage.*


dist/*
*/*.egg-info/*
__pycache__
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ pip install jp2_remediator==0.0.2
## Usage

## Process one file
`python3 box_reader.py --file tests/test-images/7514499.jp2`
`python3 main.py --file tests/test-images/7514499.jp2`

`python3 box_reader.py --file tests/test-images/481014278.jp2`
`python3 main.py --file tests/test-images/481014278.jp2`

## Process directory
`python3 box_reader.py --directory tests/test-images/`
`python3 main.py --directory tests/test-images/`

## Process Amazon S3 bucket
`python3 box_reader.py --bucket your-bucket-name --prefix optional-prefix`
`python3 main.py --bucket your-bucket-name --prefix optional-prefix`

## Process all .jp2 files in the bucket:
`python3 box_reader.py --bucket remediation-folder`
`python3 main.py --bucket remediation-folder`

## Process only files with a specific prefix (folder):
`python3 box_reader.py --bucket remediation-folder --prefix testbatch_20240923`
`python3 main.py --bucket remediation-folder --prefix testbatch_20240923`

`python3 box_reader.py --help`
`python3 main.py --help`

## Run Tests
`python3 test_aws_connection.py`

### Run from src folder
`python3 -m unittest jp2_remediator.tests.test_box_reader`
`python3 -m unittest jp2_remediator.tests.unit.test_box_reader`

## Docker environment

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ omit = [

[tool.project-paths]
dir_unit_out = "src/jp2_remediator/tests/out/"
dir_unit_resources = "src/jp2_remediator/tests/resources/"
dir_unit_resources = "src/jp2_remediator/tests/resources/"
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
boto3==1.35.39
botocore==1.35.39
flake8==7.1.1
jmespath==1.0.1
jpylyzer==2.2.1
mccabe==0.7.0
project-paths==1.1.1
pycodestyle==2.12.1
pyflakes==3.2.0
python-dateutil==2.9.0.post0
s3transfer==0.10.3
setuptools==73.0.1
six==1.16.0
toml==0.10.2
urllib3==2.2.3
41 changes: 41 additions & 0 deletions src/jp2_remediator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import logging
from logging.handlers import TimedRotatingFileHandler
import os
from datetime import datetime

LOG_FILE_BACKUP_COUNT = int(os.getenv('LOG_FILE_BACKUP_COUNT', '30'))
LOG_ROTATION = "midnight"

timestamp = datetime.today().strftime('%Y-%m-%d')


def configure_logger(name): # pragma: no cover
log_level = os.getenv("APP_LOG_LEVEL", "WARNING")
log_dir = os.getenv("LOG_DIR", "logs/")
# create log directory if it doesn't exist
if not os.path.exists(log_dir):
os.makedirs(log_dir)

log_file_path = os.path.join(log_dir, "jp2_remediator.log")
formatter = logging.Formatter(
'%(levelname)s - %(asctime)s - %(name)s - %(message)s')

console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)

logger = logging.getLogger(name)
logger.addHandler(console_handler)
# Defaults to console logging
if os.getenv("CONSOLE_LOGGING_ONLY", "true") == "false":
# make log_file_path if it doesn't exist
# os.makedirs(log_file_path, exist_ok=True)
file_handler = TimedRotatingFileHandler(
filename=log_file_path,
when=LOG_ROTATION,
backupCount=LOG_FILE_BACKUP_COUNT
)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

logger.setLevel(log_level)
return logger
Loading

0 comments on commit 47f5496

Please sign in to comment.