55from distutils .errors import DistutilsSetupError
66from typing import Any , Callable , List , Optional , Union
77
8+ import toml
89from setuptools .dist import Distribution
910from six .moves import collections_abc
1011
1516ENV_VARS_REGEXP = re .compile (r"\{env:([^:}]+):?([^}]+}?)?\}" , re .IGNORECASE | re .UNICODE ) # type: re.Pattern
1617TIMESTAMP_REGEXP = re .compile (r"\{timestamp:?([^:}]+)?\}" , re .IGNORECASE | re .UNICODE ) # type: re.Pattern
1718
19+ DEFAULT_CONFIG = {
20+ "template" : DEFAULT_TEMPLATE ,
21+ "dev_template" : DEFAULT_DEV_TEMPLATE ,
22+ "dirty_template" : DEFAULT_DIRTY_TEMPLATE ,
23+ "starting_version" : DEFAULT_STARTING_VERSION ,
24+ "version_callback" : None ,
25+ "version_file" : None ,
26+ "count_commits_from_version_file" : False ,
27+ "branch_formatter" : None ,
28+ "sort_by" : None ,
29+ }
30+
1831
1932def _exec (cmd ): # type: (str) -> List[str]
2033 try :
@@ -92,6 +105,37 @@ def count_since(name): # type: (str) -> Optional[int]
92105 return None
93106
94107
108+ def load_config_from_dict (dictionary ): # type: (Union[dict, collections_abc.Mapping]) -> dict
109+ config = {}
110+ for key , value in DEFAULT_CONFIG .items ():
111+ config [key ] = dictionary .get (key , value )
112+ return config
113+
114+
115+ def load_config_from_toml (file_name ): # type: (str) -> dict
116+ with open (file_name , encoding = "UTF-8" , mode = "r" ) as f :
117+ data = f .read ()
118+ parsed_file = toml .loads (data )
119+
120+ filtered_file = parsed_file .get ("tool" , {}).get ("setuptools_git_versioning" , {})
121+
122+ config = load_config_from_dict (filtered_file )
123+
124+ return config
125+
126+
127+ def infer_version (dist ): # type: (Distribution) -> None
128+ pyproject = "pyproject.toml"
129+ if not os .path .isfile (pyproject ):
130+ return
131+
132+ config = load_config_from_toml (pyproject )
133+
134+ version = version_from_git (** config )
135+
136+ dist .metadata .version = version
137+
138+
95139def parse_config (dist , _ , value ): # type: (Distribution, Any, Any) -> None
96140 if isinstance (value , bool ):
97141 if value :
@@ -104,29 +148,14 @@ def parse_config(dist, _, value): # type: (Distribution, Any, Any) -> None
104148 if not isinstance (value , collections_abc .Mapping ):
105149 raise DistutilsSetupError ("Config in the wrong format" )
106150
107- template = value .get ("template" , DEFAULT_TEMPLATE )
108- dev_template = value .get ("dev_template" , DEFAULT_DEV_TEMPLATE )
109- dirty_template = value .get ("dirty_template" , DEFAULT_DIRTY_TEMPLATE )
110- starting_version = value .get ("starting_version" , DEFAULT_STARTING_VERSION )
111- version_callback = value .get ("version_callback" , None )
112- version_file = value .get ("version_file" , None )
113- count_commits_from_version_file = value .get ("count_commits_from_version_file" , False )
114- branch_formatter = value .get ("branch_formatter" , None )
115- sort_by = value .get ("sort_by" , None )
116-
117- version = version_from_git (
118- template = template ,
119- dev_template = dev_template ,
120- dirty_template = dirty_template ,
121- starting_version = starting_version ,
122- version_callback = version_callback ,
123- version_file = version_file ,
124- count_commits_from_version_file = count_commits_from_version_file ,
125- branch_formatter = branch_formatter ,
126- sort_by = sort_by ,
127- )
151+ config = load_config_from_dict (value )
152+
153+ version = version_from_git (** config )
154+
128155 dist .metadata .version = version
129156
157+ infer_version (dist )
158+
130159
131160def read_version_from_file (path ): # type: (Union[str, os.PathLike]) -> str
132161 with open (path ) as file :
0 commit comments