Skip to content

Commit

Permalink
Merge pull request #12 from vladistan/add-pre-commit
Browse files Browse the repository at this point in the history
Add pre commit
  • Loading branch information
AdamFinkle authored Jul 30, 2024
2 parents 5554422 + 9409b40 commit f82c41a
Show file tree
Hide file tree
Showing 37 changed files with 330 additions and 239 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/pre-commit-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow runs pre-commit hooks on pushes and pull requests to main
# to enforce coding style. To ensure correct configuration, please refer to:
# https://lincc-ppt.readthedocs.io/en/latest/practices/ci_precommit.html
name: Run pre-commit hooks

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
pre-commit-ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install dependencies
run: |
sudo apt-get update
python -m pip install --upgrade pip
pip install .[dev]
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- uses: pre-commit/[email protected]
with:
extra_args: --all-files --verbose
67 changes: 67 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
repos:

- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.12.1
hooks:
- id: validate-pyproject

- repo: https://github.com/rhysd/actionlint
rev: v1.6.26
hooks:
- id: actionlint

- repo: https://github.com/sirosen/check-jsonschema
rev: 0.27.3
hooks:
- id: check-github-actions
- id: check-github-workflows
- id: check-readthedocs

- repo: https://github.com/pappasam/toml-sort
rev: v0.23.1
hooks:
- id: toml-sort-fix
stages: ["pre-commit"]

- repo: meta
hooks:
- id: check-useless-excludes

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.5
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix, --show-fixes]
- id: ruff-format

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-added-large-files
args: ['--maxkb=500']
- id: check-ast
- id: check-builtin-literals
- id: check-case-conflict
- id: check-json
- id: check-merge-conflict
- id: check-shebang-scripts-are-executable
- id: check-symlinks
- id: check-toml
- id: check-vcs-permalinks
- id: check-xml
- id: check-yaml
args: [--unsafe]
- id: debug-statements
- id: destroyed-symlinks
- id: end-of-file-fixer
types: [python]
- id: fix-byte-order-marker
- id: mixed-line-ending
- id: name-tests-test
args: [--pytest-test-first]
- id: no-commit-to-branch
stages:
- pre-push
- id: trailing-whitespace
- id: end-of-file-fixer
- id: debug-statements
4 changes: 2 additions & 2 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ ______________________________________________________
NOTICE
______________________________________________________

This product is a derivative work based on the
NAESB REQ.21 Energy Service Provider Interface Standard
This product is a derivative work based on the
NAESB REQ.21 Energy Service Provider Interface Standard
Version 1.0
Copyright 2011 NAESB

Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@ Simple steps for development setup:
2. `pip install -e .` builds the [python egg](https://stackoverflow.com/questions/2051192/what-is-a-python-egg) for greenbutton_objects and then installs greenbutton_objects
3. `pip install -r requirements-dev.txt` which installs the libraries required to develop greenbutton_objects

Then, you should be able to run `pytest`, also from any directory, and see the test run successfully.
Then, you should be able to run `pytest`, also from any directory, and see the test run successfully.

## Pre-commit Checks

This project uses [pre-commit](https://pre-commit.com/) to run various checks and formatters before each commit. To set it up:

1. Install pre-commit in your environment:
```
pip install pre-commit
```

2. Install the pre-commit hooks:
```
pre-commit install
```

Pre-commit checks will automatically run on every commit.
5 changes: 2 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

* Handle IntervalReading blocks without timePeriod entries.
Synthesize a timePeriod from MeterReading.readingType.intervalLength.

* Handle LocalTimeParameters resources

* Handle ElectricPowerUsageSummary resources

* Handle ElectricPowerQualitySummary resources

102 changes: 83 additions & 19 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
[build-system]
requires = ["setuptools>=61.0.0", "wheel"]
requires = [
"setuptools>=61",
"wheel"
]
build-backend = "setuptools.build_meta"

[project]
name = "greenbutton_objects"
version = "2024.7.11"
description = "Parse Green Button XML files into Python objects."
readme = "README.md"
authors = [{ name = "Phil Gold", email = "[email protected]" },
{ name = "Adam Finkle", email = "[email protected]" },
{ name = "Ethan Strominger", email = "[email protected]" }]
license = { file = "LICENSE" }
authors = [
{name = "Phil Gold", email = "[email protected]"},
{name = "Adam Finkle", email = "[email protected]"},
{name = "Ethan Strominger", email = "[email protected]"}
]
license = {file = "LICENSE"}
classifiers = [
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Development Status :: 4 - Beta",
"License :: OSI Approved :: Apache Software License",
"Intended Audience :: Developers",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3"
]
keywords = ["feed", "reader", "tutorial"]
requires-python = ">=2.7"

[project.optional-dependencies]
dev = [
"black",
"bumpver",
"isort",
"mypy",
"pip-tools",
"pydocstyle",
"pytest"
"black",
"bumpver",
"mypy",
"pip-tools",
"pydocstyle",
"pytest",
"pytest-cov",
"ruff"
]

[project.urls]
Homepage = "https://github.com/codeforboston/greenbutton_objects"
"Homepage" = "https://github.com/codeforboston/greenbutton_objects"
"Source Code" = "https://github.com/codeforboston/greenbutton_objects"

[tool.black]
line-length = 110
target-version = ["py38"]

[tool.bumpver]
current_version = "2024.7.11"
version_pattern = "YYYY.MM.DD"
Expand All @@ -46,9 +61,58 @@ push = true

[tool.bumpver.file_patterns]
"pyproject.toml" = [
'current_version = "{version}"',
'current_version = "{version}"'
]
"README.md" = [
"{version}",
"{pep440_version}",
"{version}",
"{pep440_version}"
]

[tool.isort]
profile = "black"
line_length = 110

[tool.ruff]
line-length = 110
target-version = "py38"
preview = true # https://docs.astral.sh/ruff/preview/

[tool.ruff.lint]
select = [
# docstrings
# "D", <-- TODO: There are no docstrings in this project
# pycodestyle
"E",
"W",
# Pyflakes
"F",
# pep8-naming
# "N", <-- TODO: This project not PEP8 Naming compliant yet
# pyupgrade
# "UP", <-- TODO: This project uses outdated string formating
# flake8-bugbear
# "B", <-- TODO: There are potential bugs in this project
# flake8-simplify
"SIM",
# isort
"I"
]
ignore = [
"UP006", # Allow non standard library generics in type hints
"UP007", # Allow Union in type hints
"SIM114", # Allow if with same arms
"B028", # Allow default warning level
"SIM117", # Allow nested with
"UP015", # Allow redundant open parameters
"UP028" # Allow yield in for loop
]

[tool.ruff.lint.extend-per-file-ignores] # https://docs.astral.sh/ruff/settings/#extend-per-file-ignores
# Ignore missing __init__.py files in tests.
"tests/*" = ["INP001", "PLR2004"]

[tool.ruff.lint.mccabe]
max-complexity = 10

[tool.ruff.lint.pycodestyle]
max-doc-length = 100
3 changes: 0 additions & 3 deletions src/greenbutton_objects/enums.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

from enum import Enum


Expand Down
37 changes: 10 additions & 27 deletions src/greenbutton_objects/objects.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#!/usr/bin/python

import datetime
import functools

from greenbutton_objects import enums
from greenbutton_objects import utils
from greenbutton_objects import enums, utils


@functools.total_ordering
Expand All @@ -13,9 +10,7 @@ def __init__(self, entity):
self.start = utils.getEntity(
entity,
"espi:start",
lambda e: datetime.datetime.fromtimestamp(int(e.text)).astimezone(
datetime.timezone.utc
),
lambda e: datetime.datetime.fromtimestamp(int(e.text)).astimezone(datetime.timezone.utc),
)
self.duration = utils.getEntity(
entity, "espi:duration", lambda e: datetime.timedelta(seconds=int(e.text))
Expand All @@ -39,20 +34,13 @@ def __lt__(self, other):
class IntervalReading:
def __init__(self, entity, parent):
self.intervalBlock = parent
self.cost = utils.getEntity(
entity, "espi:cost", lambda e: int(e.text) / 100000.0
)
self.timePeriod = utils.getEntity(
entity, "espi:timePeriod", lambda e: DateTimeInterval(e)
)
self.cost = utils.getEntity(entity, "espi:cost", lambda e: int(e.text) / 100000.0)
self.timePeriod = utils.getEntity(entity, "espi:timePeriod", lambda e: DateTimeInterval(e))
self._value = utils.getEntity(entity, "espi:value", lambda e: int(e.text))

self.readingQualities = set(
[
ReadingQuality(rq, self)
for rq in entity.findall("espi:ReadingQuality", utils.ns)
]
)
self.readingQualities = set([
ReadingQuality(rq, self) for rq in entity.findall("espi:ReadingQuality", utils.ns)
])

def __repr__(self):
return "<IntervalReading (%s, %s: %s %s)>" % (
Expand All @@ -78,12 +66,9 @@ def value(self):
self.intervalBlock is not None
and self.intervalBlock.meterReading is not None
and self.intervalBlock.meterReading.readingType is not None
and self.intervalBlock.meterReading.readingType.powerOfTenMultiplier
is not None
and self.intervalBlock.meterReading.readingType.powerOfTenMultiplier is not None
):
multiplier = (
10**self.intervalBlock.meterReading.readingType.powerOfTenMultiplier
)
multiplier = 10**self.intervalBlock.meterReading.readingType.powerOfTenMultiplier
else:
multiplier = 1
return self._value * multiplier
Expand Down Expand Up @@ -124,6 +109,4 @@ def value_symbol(self):
class ReadingQuality:
def __init__(self, entity, parent):
self.intervalReading = parent
self.quality = utils.getEntity(
entity, "espi:quality", lambda e: enums.QualityOfReading(int(e.text))
)
self.quality = utils.getEntity(entity, "espi:quality", lambda e: enums.QualityOfReading(int(e.text)))
Loading

0 comments on commit f82c41a

Please sign in to comment.