Skip to content

Instantly share code, notes, and snippets.

@la-mar
Created April 25, 2020 16:18
Show Gist options
  • Save la-mar/cf1b19002ccb36460e2fde03ae19fb00 to your computer and use it in GitHub Desktop.
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
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