Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command and workflow to validate rates.yaml #3

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/validate-rates.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Validate rates file
on:
push:
paths:
- rates.yaml
pull_request:
paths:
- rates.yaml

jobs:
validate-rates-file:
name: Validate rates file
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up python
uses: actions/setup-python@v5
with:
python-version: '3.10'

- name: Install dependencies
run: |
pip install -e .

- name: Validate rates file
run: |
validate-rates-file -g rates.yaml
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ dependencies = [
"requests",
]

[project.entry-points."console_scripts"]
validate-rates-file = "nerc_rates.cmd.validate_rates_file:main"


[build-system]
requires = [
"setuptools>=42",
Expand Down
Empty file added src/nerc_rates/cmd/__init__.py
Empty file.
46 changes: 46 additions & 0 deletions src/nerc_rates/cmd/validate_rates_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import sys
import argparse
import pydantic
import yaml

from .. import rates


def pydantic_to_github(err, rates_file):
"""Produce a github error annotation from a pydantic ValidationError"""
for error in err.errors():
print(f"::error file={rates_file},title=Validation error::{error['msg']}")


def yaml_to_github(err, rates_file):
"""Produce a github error annotation from a YAML ParserError"""
line = err.problem_mark.line
print(
f"::error file={rates_file},line={line},title=Parser error::{err.context}: {err.problem}"
)


def main():
p = argparse.ArgumentParser()
p.add_argument(
"-g", "--github", action="store_true", help="Emit github workflow annotations"
)
p.add_argument("-u", "--url", action="store_true", help="Rate file is a url")
p.add_argument("rates_file", default="rates.yaml", nargs="?")
args = p.parse_args()

try:
if args.url:
r = rates.load_from_url(args.rates_file)
else:
r = rates.load_from_file(args.rates_file)

print(f"OK [{len(r.root)} entries]")
except pydantic.ValidationError as err:
if args.github:
pydantic_to_github(err, args.rates_file)
sys.exit(err)
except yaml.parser.ParserError as err:
if args.github:
yaml_to_github(err, args.rates_file)
sys.exit(err)
22 changes: 14 additions & 8 deletions src/nerc_rates/rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@

from .models import Rates

DEFAULT_URL = "https://raw.githubusercontent.com/knikolla/nerc-rates/main/rates.yaml"
DEFAULT_RATES_FILE = "rates.yaml"
DEFAULT_RATES_URL = (
"https://raw.githubusercontent.com/CCI-MOC/nerc-rates/main/rates.yaml"
)


def load_from_url(url=DEFAULT_URL) -> Rates:
def load_from_url(url: str | None = None) -> Rates:
if url is None:
url = DEFAULT_RATES_URL

r = requests.get(url, allow_redirects=True)
# Using the BaseLoader prevents conversion of numeric
# values to floats and loads them as strings.
r.raise_for_status()
config = yaml.safe_load(r.content.decode("utf-8"))
return Rates.model_validate(config)


def load_from_file() -> Rates:
with open("rates.yaml", "r") as f:
# Using the BaseLoader prevents conversion of numeric
# values to floats and loads them as strings.
def load_from_file(path: str | None = None) -> Rates:
if path is None:
path = DEFAULT_RATES_FILE

with open(path, "r") as f:
config = yaml.safe_load(f)
return Rates.model_validate(config)
2 changes: 1 addition & 1 deletion src/nerc_rates/tests/test_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_load_from_url():
from: 2023-06
"""
with requests_mock.Mocker() as m:
m.get(rates.DEFAULT_URL, text=mock_response_text)
m.get(rates.DEFAULT_RATES_URL, text=mock_response_text)
r = load_from_url()
assert r.get_value_at("CPU SU Rate", "2023-06") == "0.013"

Expand Down
Loading