Skip to content

Commit 06f8d69

Browse files
authored
Introduce logging and migration to uv for dependency management (#48)
- Added basic logging via custom exceptions. - Set the stages for deprecating the Constants attributes in favor of DataItems for game data collection - Migrated dependency management from pip to uv
2 parents 8771e4b + a167cc6 commit 06f8d69

10 files changed

Lines changed: 1092 additions & 156 deletions

File tree

.github/workflows/release.yml

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,54 @@ jobs:
3737
ref: ${{ github.ref_name }}
3838
fetch-depth: 0
3939

40-
- name: Action | Semantic Version Release
41-
id: release
42-
uses: python-semantic-release/python-semantic-release@v9.21.0
43-
with:
44-
github_token: ${{ secrets.GITHUB_TOKEN }}
45-
git_committer_name: "github-actions"
46-
git_committer_email: "actions@users.noreply.github.com"
40+
# Install dependencies
41+
- name: Install dependencies
42+
run: |
43+
python -m pip install --upgrade pip
44+
pip install -r requirements.txt
45+
pip install hatch
4746
48-
- name: Publish | Upload to GitHub Release Assets
49-
uses: python-semantic-release/publish-action@v9.21.0
50-
if: steps.release.outputs.released == 'true'
51-
with:
52-
github_token: ${{ secrets.GITHUB_TOKEN }}
53-
tag: ${{ steps.release.outputs.tag }}
47+
# Build the package using python3 -m build
48+
- name: Build the package
49+
run: |
50+
python3 -m pip install --upgrade setuptools wheel build uv
51+
python3 -m build
52+
53+
# Tag the release in GitHub
54+
- name: Tag the release
55+
run: |
56+
VERSION=$(hatch version)
57+
git config user.name "$(git log -n 1 --pretty=format:%an)"
58+
git config user.email "$(git log -n 1 --pretty=format:%ae)"
59+
git tag -a "v$VERSION" -m "Release version $VERSION"
60+
git push origin "v$VERSION"
61+
uv run hatch version "$VERSION"
62+
env:
63+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
64+
65+
# Update CHANGELOG.md
66+
- name: Update CHANGELOG.md
67+
run: |
68+
python3 install git-changelog
69+
git-changelog -B auto -Tio CHANGELOG.md -c angular -s build,deps,fix,feat,refactor -n semver
70+
71+
# Publish the package to PyPI
72+
pypi-publish:
73+
runs-on: ubuntu-latest
74+
needs:
75+
- release
76+
permissions:
77+
# IMPORTANT: this permission is mandatory for trusted publishing
78+
id-token: write
79+
contents: write
5480

81+
environment:
82+
name: pypi
83+
url: https://pypi.org/project/swgoh-comlink/
84+
85+
steps:
5586
- name: Retrieve release distributions
5687
uses: actions/download-artifact@v4
5788

58-
- name: Publish | Upload package to PyPI
89+
- name: Publish release distributions to PyPI
5990
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ player_name = player_data['name']
7070
- **_access_key_**: The "public" portion of the shared key used in HMAC request signing. Defaults to `None` which disables HMAC signing of requests. Can also be read from the ACCESS_KEY environment variable.
7171
- **_secret_key_**: The "private" portion of the key used in HMAC request signing. Defaults to `None` which disables HMAC signing of requests. Can also be read from the SECRET_KEY environment variable.
7272

73+
# Logging
74+
75+
Logging is handled by the [python logging module](https://docs.python.org/3/library/logging.html). For details on the
76+
logging implementation for this package, go [here](docs/logging.md).
77+
7378
See the online [wiki](https://github.com/swgoh-utils/swgoh-comlink/wiki) for more information.
7479

7580
## Support

docs/logging.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# SwgohComlink Logging
2+
3+
The `swgoh_comlink` package includes some basic logging functionality. By default, the package will create a logger
4+
instance with the name 'swgoh_comlink' and use the built-in logging functionality.
5+
6+
The default log level is set to `DEBUG`. If you would like to use the built-in logging but at a lower logging level or
7+
specialized output formatting, you can create your own logging instance with the name 'swgoh_comlink' and implement the
8+
custom logging setting appropriate for your needs.
9+
10+
For example:
11+
12+
```python
13+
import logging
14+
from swgoh_comlink import SwgohComlink
15+
16+
comlink_logger = logging.getLogger('swgoh_comlink')
17+
comlink_logger.setLevel(logging.CRITICAL)
18+
console_handler = logging.StreamHandler()
19+
log_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
20+
console_handler.setFormatter(log_format)
21+
comlink_logger.addHandler(console_handler)
22+
23+
comlink = SwgohComlink()
24+
```
25+
26+
The configuration above would capture all `CRITICAL` events and higher and display them on the console terminal.
27+
28+
If you wanted to capture events to a rotating log file, you could add a handler to the logger instance
29+
defined above.
30+
31+
```python
32+
import logging
33+
from logging.handlers import RotatingFileHandler
34+
from swgoh_comlink import SwgohComlink
35+
36+
comlink_logger = logging.getLogger('swgoh_comlink')
37+
comlink_logger.setLevel(logging.CRITICAL)
38+
console_handler = logging.StreamHandler()
39+
log_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
40+
console_handler.setFormatter(log_format)
41+
comlink_logger.addHandler(console_handler)
42+
43+
file_handler = RotatingFileHandler(filename="./comlink.log", encoding="utf-8", maxBytes=2500000, backupCount=5)
44+
file_handler.setFormatter(log_format)
45+
file_handler.setLevel(logging.DEBUG)
46+
comlink_logger.addHandler(file_handler)
47+
48+
comlink = SwgohComlink()
49+
50+
51+
```

pyproject.toml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["setuptools>=75.8.1", "requests>=2.32.4"]
2+
requires = ["setuptools>=75.8.1", "requests>=2.32.4", "sentinels"]
33
build-backend = "setuptools.build_meta"
44

55
[project]
@@ -20,8 +20,11 @@ classifiers = [
2020
"Operating System :: OS Independent",
2121
]
2222
dependencies = [
23-
"requests",
23+
"hatch>=1.14.2",
24+
"hatchling>=1.27.0",
25+
"requests>=2.32.4",
2426
"sentinels",
27+
"setuptools>=75.8.1",
2528
]
2629

2730
[project.urls]
@@ -36,6 +39,12 @@ addopts = [
3639
[tool.setuptools.dynamic]
3740
version = { attr = "swgoh_comlink.version" }
3841

42+
[tool.hatch.version]
43+
path = 'src/swgoh_comlink/version.py'
44+
45+
[tool.hatch.build.targets.wheel]
46+
packages = ["src/swgoh_comlink"]
47+
3948
[tool.semantic_release]
4049
version_variable = [# List of possible location of version
4150
"src/swgoh_comlink/version.py",

requirements.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/swgoh_comlink/exceptions.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# coding=utf-8
2+
"""
3+
Custom exceptions for logging
4+
"""
5+
6+
from __future__ import annotations
7+
8+
from .globals import get_logger
9+
10+
logger = get_logger(__name__)
11+
12+
13+
class SwgohComlinkException(Exception):
14+
"""Base class for exceptions in this module."""
15+
16+
def __init__(self, message) -> None:
17+
super().__init__(message)
18+
logger.exception(f"SwgohComlinkException: {message}", exc_info=True)
19+
20+
21+
class SwgohComlinkValueError(SwgohComlinkException, ValueError):
22+
...

src/swgoh_comlink/globals.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# coding=utf-8
2+
"""
3+
Global entities
4+
"""
5+
6+
from __future__ import annotations
7+
8+
import logging
9+
10+
11+
class LoggingFormatter(logging.Formatter):
12+
"""Custom logging formatter class"""
13+
14+
def format(self, record):
15+
log_message_format = \
16+
'{asctime} | {levelname:<9} | {name:15} | {module:<14} : {funcName:>30}() [{lineno:_>5}] | {message}'
17+
formatter = logging.Formatter(log_message_format, "%Y-%m-%d %H:%M:%S", style="{")
18+
return formatter.format(record)
19+
20+
21+
def get_logger(logger_name: str = __name__, log_level: str = "INFO") -> logging.Logger:
22+
"""Return the configured logger"""
23+
logger = logging.getLogger(logger_name)
24+
log_lvl = logging.getLevelName(log_level.upper())
25+
logger.setLevel(log_lvl)
26+
console_handler = logging.StreamHandler()
27+
console_handler.setFormatter(LoggingFormatter())
28+
logger.addHandler(console_handler)
29+
return logger

0 commit comments

Comments
 (0)