Skip to content

Infer EnergyPlus Installation directory + modernize (pyproject.toml, add black+isort+mypy)#7

Open
jmarrec wants to merge 14 commits intoMyoldmopar:mainfrom
jmarrec:main
Open

Infer EnergyPlus Installation directory + modernize (pyproject.toml, add black+isort+mypy)#7
jmarrec wants to merge 14 commits intoMyoldmopar:mainfrom
jmarrec:main

Conversation

@jmarrec
Copy link

@jmarrec jmarrec commented Sep 11, 2023

Working actions can be found on my fork: https://github.com/jmarrec/EnergyPlusAPIHelper/actions

Comment on lines +9 to +34
def _infer_energyplus_install_dir():
"""Try to locate the EnergyPlus installation path.

Starts by looking at `which energyplus` first then tries the default installation paths.
Returns the most recent version found"""
if ep := shutil.which("energyplus"):
return Path(ep).resolve().parent
ext = ""
if platform.system() == 'Linux':
base_dir = Path('/usr/local')
elif platform.system() == 'Darwin':
base_dir = Path('Applications')
else:
base_dir = Path('C:/')
ext = ".exe"
if not base_dir.is_dir():
raise ValueError(f"{base_dir=} is not a directory")
candidates = [p.parent for p in base_dir.glob(f"EnergyPlus*/energyplus{ext}")]
if not candidates:
raise ValueError("Found zero EnergyPlus installation directories")
candidates = [c for c in candidates if (c / 'pyenergyplus').is_dir()]
if not candidates:
raise ValueError("Found zero EnergyPlus installation directories that have the pyenergyplus directory")
# Sort by version
candidates.sort(key=lambda c: [int(x) for x in c.name.split('-')[1:]])
return candidates[-1]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Find the E+ installation directory if not provided

Comment on lines +73 to +82
def __init__(self, eplus_install_path: Optional[Path] = None):
if eplus_install_path is None:
self.eplus_install_path = _infer_energyplus_install_dir()
print(f"Infered Location of EnergyPlus installation at {self.eplus_install_path}")
else:
if not isinstance(eplus_install_path, Path):
eplus_install_path = Path(eplus_install_path)
if not (eplus_install_path / 'pyenergyplus').is_dir():
raise ValueError(f"Wrong eplus_install_path, '{eplus_install_path}/pyenergyplus' does not exist")
self.eplus_install_path = eplus_install_path
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EPlusAPIHelper:

  • If not provided, infer the E+ directory.
  • If provided: Handle wrong argument early too.

Comment on lines +1 to +10
import sys
from pathlib import Path
from typing import Optional


def get_eplus_path_from_argv1() -> Optional[Path]:
"""If there is one argv, get the installation from it, otherwise leave it be inferred."""
if len(sys.argv) > 1:
return Path(sys.argv[1])
return None
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper in demos, so that for each test it calls this.

eplus_path = argv[1]

e = EPlusAPIHelper(Path(eplus_path))
e = EPlusAPIHelper(get_eplus_path_from_argv1())
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example usage of the helper in one of the demo files

Comment on lines +1 to +23
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 23.9.1
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
hooks:
- id: mypy
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies: [flake8-pyproject]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a pre-commit hook for linting / checking

Comment on lines +1 to +18
name: Lint

on: [push]

jobs:
lint:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v4

- name: Set up Python 3.8
uses: actions/setup-python@v4
with:
python-version: 3.8

- name: Check precommit hooks
uses: pre-commit/action@v3.0.0
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace the "flake8" workflow with this one that will run the precommit hook on all files, and as such do flake8, black, isort and mypy

Comment on lines +1 to +7
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "energyplus_api_helpers"
version = "0.4.0"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pyproject toml


on:
push:
branches: [ main ]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Release workflow builds the wheel for main too

Comment on lines 29 to +31
- name: Deploy on Test PyPi
uses: pypa/gh-action-pypi-publish@37f50c210e3d2f9450da2cd423303d6a14a6e29f # v1.5.1
if: contains(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@release/v1
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But only upload to pypi when it's a tag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant