1- """Context definitions."""
2- from typing import List , Optional , Tuple
1+ """Context definitions used in templates ."""
2+ from typing import Callable , List , Optional , Tuple
33
44import collections
55import datetime
66import re
77from dataclasses import dataclass , field
88
9- from generate_changelog .configuration import get_config
9+ from generate_changelog .configuration import Configuration , get_config
10+ from generate_changelog .utilities import diff_index
1011
1112
1213@dataclass
@@ -87,7 +88,7 @@ def author_names(self) -> list:
8788
8889
8990@dataclass
90- class GroupedCommit :
91+ class GroupingContext :
9192 """A combination of a tuple of the sorted values and a list of the CommitContexts in that group."""
9293
9394 grouping : Tuple [str ]
@@ -113,8 +114,49 @@ class VersionContext:
113114 tagger : Optional [str ] = None
114115 """The name and email of the person who tagged this version in `name <email@ex.com>` format."""
115116
116- grouped_commits : List [GroupedCommit ] = field (default_factory = list )
117+ grouped_commits : List [GroupingContext ] = field (default_factory = list )
117118 """The sections that group the commits in this version."""
118119
119120 metadata : dict = field (default_factory = dict )
120121 """Metadata for this version parsed from commits."""
122+
123+
124+ @dataclass
125+ class ChangelogContext :
126+ """The primary context used when rendering a changelog."""
127+
128+ config : Configuration
129+ """The changelog generation configuration."""
130+
131+ versions : List [VersionContext ] = field (default_factory = list )
132+ """The version contexts to render in the changelog."""
133+
134+ # Fields generated from the configuration post init
135+
136+ unreleased_label : str = field (init = False )
137+ """The configured label used as the version title of the changes since the last valid tag."""
138+
139+ valid_author_tokens : List [str ] = field (init = False , default_factory = list )
140+ """The configured tokens in git commit trailers that indicate authorship."""
141+
142+ group_by : list [str ] = field (init = False , default_factory = list )
143+ """The configured grouping aspects for commits within a version."""
144+
145+ group_depth : int = field (init = False )
146+ """The number of levels version commits are grouped by."""
147+
148+ diff_index : Callable = field (init = False )
149+
150+ def __post_init__ (self ):
151+ self .unreleased_label = self .config .unreleased_label
152+ self .valid_author_tokens = self .config .valid_author_tokens
153+ self .group_by = self .config .group_by
154+ self .group_depth = len (self .config .group_by )
155+ self .diff_index = diff_index
156+
157+ for var , val in self .config .rendered_variables .items ():
158+ setattr (self , var , val )
159+
160+ def as_dict (self ):
161+ """Safely generate a dict version of this object."""
162+ return self .__dict__
0 commit comments