Package: single-source
Single-source: There is only one truthsingle-sourcehelps to reduce the entropy in your Python project by keeping
single source of truth.The targets of this library are modern Python projects which want to have
one source of truth for version, name and etc.At the moment, the library provides the single point for a package version.It supports Python 3.6+.Quick start# root_package/__init__.pyfrompathlibimportPathfromsingle_sourceimportget_version__version__=get_version(__name__,Path(__file__).parent.parent)Root of the problemYou use modernpyproject.tomland want to keep the version of your package
here:# pyproject.toml[tool.poetry]name="modern-project"version="0.1.0"Let's imagine the version of your package is required in some place of the code.Since you need the version in your Python code, you may want to duplicate the version by putting it as a string variable to some python file:# modern_project/__init__.py__version__="0.1.0"# modern_project/version.pyversion="0.1.0"Then you realize you don't want to have the version in a python file and in pyproject.toml at the same time. It's harder to keep them consistent and easier to forget to bump both versions before release.Also, you don't want to build the wheel by creating some script for auto incrementing the version in both places (and use it in your CI flow, for example). Instead you want usepoetry versioncommands.InstallationYou can installsingle-sourceviapippip3installsingle-sourceor viapoetrypoetryaddsingle-sourceThe library also available asa conda packageinconda-forgechannelcondainstallsingle-source--channelconda-forgeAdvanced usageChanging default valueIf it's not possible to get the version from package metadata or
there is no pyproject.tomlget_versionreturns""- empty string by default.
You can change this value by providing a value as adefault_returnkeyword argument.frompathlibimportPathfromsingle_sourceimportget_versionpath_to_pyproject_dir=Path(__file__).parent.parent__version__=get_version(__name__,path_to_pyproject_dir,default_return=None)Raising an exceptionYou may want to raise an exception in case the version of the package
has not been found.frompathlibimportPathfromsingle_sourceimportget_version,VersionNotFoundErrorpath_to_pyproject_dir=Path(__file__).parent.parenttry:__version__=get_version(__name__,path_to_pyproject_dir,fail=True)exceptVersionNotFoundError:passNot only pyproject.tomlYou can usesingle-sourceeven if you still store the version of your library
insetup.pyor in any otherutf-8encoded text file.First, try without customregex, probably it can parse the versionIf the default internalregexdoes not find the version in your file,
the only thing you need to provide is a customregextoget_version:fromsingle_sourceimportget_versioncustom_regex=r"\s*version\s*=\s*[\"']\s*([-.\w]{3,})\s*[\"']\s*"path_to_file="~/my-project/some_file_with_version.txt"__version__=get_version(__name__,path_to_file,version_regex=custom_regex)Version must be in the first group()in the custom regex.ContributingPull requests are welcome. For major changes, please open an issue first to
discuss what you would like to change.Please make sure to update tests as appropriate.LicenseMIT