Created
April 25, 2020 16:18
-
-
Save la-mar/cf1b19002ccb36460e2fde03ae19fb00 to your computer and use it in GitHub Desktop.
Extract project name, version number, and other metadata from a pyproject.toml file in the project root directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from typing import Dict, Optional | |
import tomlkit | |
def _get_project_meta(pyproj_path: str = "./pyproject.toml") -> Dict[str, str]: | |
if os.path.exists(pyproj_path): | |
with open(pyproj_path, "r") as pyproject: | |
file_contents = pyproject.read() | |
return tomlkit.parse(file_contents)["tool"]["poetry"] | |
else: | |
return {} | |
pkg_meta: Dict[str, str] = _get_project_meta() | |
project: Optional[str] = pkg_meta.get("name") | |
version: Optional[str] = pkg_meta.get("version") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment