Infer EnergyPlus Installation directory + modernize (pyproject.toml, add black+isort+mypy)#7
Infer EnergyPlus Installation directory + modernize (pyproject.toml, add black+isort+mypy)#7jmarrec wants to merge 14 commits intoMyoldmopar:mainfrom
Conversation
…ror handling/checks)
…oding to one specific machine)
| 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] |
There was a problem hiding this comment.
Find the E+ installation directory if not provided
| 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 |
There was a problem hiding this comment.
EPlusAPIHelper:
- If not provided, infer the E+ directory.
- If provided: Handle wrong argument early too.
| 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 |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
Example usage of the helper in one of the demo files
| 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] |
There was a problem hiding this comment.
Add a pre-commit hook for linting / checking
| 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 |
There was a problem hiding this comment.
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
| [build-system] | ||
| requires = ["setuptools"] | ||
| build-backend = "setuptools.build_meta" | ||
|
|
||
| [project] | ||
| name = "energyplus_api_helpers" | ||
| version = "0.4.0" |
|
|
||
| on: | ||
| push: | ||
| branches: [ main ] |
There was a problem hiding this comment.
Release workflow builds the wheel for main too
| - 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 |
There was a problem hiding this comment.
But only upload to pypi when it's a tag
Working actions can be found on my fork: https://github.com/jmarrec/EnergyPlusAPIHelper/actions