-
Notifications
You must be signed in to change notification settings - Fork 128
feat(settings): Add support to configure fragment types via tables #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| """Subpackage to handle settings parsing.""" | ||
|
|
||
| from towncrier._settings import load | ||
|
|
||
| load_config = load.load_config | ||
| ConfigError = load.ConfigError | ||
| load_config_from_options = load.load_config_from_options | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "load_config", | ||
| "ConfigError", | ||
| "load_config_from_options", | ||
| ] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import abc | ||
| import collections as clt | ||
|
|
||
|
|
||
| class BaseFragmentTypesLoader: | ||
| """Base class to load fragment types.""" | ||
|
|
||
| __metaclass__ = abc.ABCMeta | ||
|
|
||
| def __init__(self, config): | ||
| """Initialize.""" | ||
| self.config = config | ||
|
|
||
| @classmethod | ||
| def factory(cls, config): | ||
| fragment_types_class = DefaultFragmentTypesLoader | ||
| fragment_types = config.get("fragment", {}) | ||
| types_config = config.get("type", {}) | ||
| if fragment_types: | ||
| fragment_types_class = TableFragmentTypesLoader | ||
| elif types_config: | ||
| fragment_types_class = ArrayFragmentTypesLoader | ||
|
|
||
| new = fragment_types_class(config) | ||
| return new | ||
|
|
||
| @abc.abstractmethod | ||
| def load(self,): | ||
| """Load fragment types.""" | ||
|
|
||
|
|
||
| class DefaultFragmentTypesLoader(BaseFragmentTypesLoader): | ||
| """Default towncrier's fragment types.""" | ||
|
|
||
| _default_types = clt.OrderedDict( | ||
| [ | ||
| (u"feature", {"name": u"Features", "showcontent": True}), | ||
| (u"bugfix", {"name": u"Bugfixes", "showcontent": True}), | ||
| (u"doc", {"name": u"Improved Documentation", "showcontent": True}), | ||
| (u"removal", {"name": u"Deprecations and Removals", "showcontent": True}), | ||
| (u"misc", {"name": u"Misc", "showcontent": False}), | ||
| ] | ||
| ) | ||
|
|
||
| def load(self,): | ||
| """Load default types.""" | ||
| return self._default_types | ||
|
|
||
|
|
||
| class ArrayFragmentTypesLoader(BaseFragmentTypesLoader): | ||
| """Load fragment types from an toml array of tables. | ||
|
|
||
| This loader get the custom fragment types defined through a | ||
| toml array of tables, that ``toml`` parses as an array | ||
| of mappings. | ||
|
|
||
| For example:: | ||
|
|
||
| [tool.towncrier] | ||
| [[tool.towncrier.type]] | ||
| directory = "deprecation" | ||
| name = "Deprecations" | ||
| showcontent = true | ||
|
|
||
| """ | ||
|
|
||
| def load(self,): | ||
| """Load types from toml array of mappings.""" | ||
|
|
||
| types = clt.OrderedDict() | ||
| types_config = self.config["type"] | ||
| for type_config in types_config: | ||
| directory = type_config["directory"] | ||
| fragment_type_name = type_config["name"] | ||
| is_content_required = type_config["showcontent"] | ||
| types[directory] = { | ||
| "name": fragment_type_name, | ||
| "showcontent": is_content_required, | ||
| } | ||
| return types | ||
|
|
||
|
|
||
| class TableFragmentTypesLoader(BaseFragmentTypesLoader): | ||
| """Load fragment types from toml tables. | ||
|
|
||
| This loader get the custom fragment types defined through a | ||
| toml tables, that ``toml`` parses as an nested mapping. | ||
|
|
||
| This loader allows omitting ``name`` and | ||
| ```showcontent`` fields. | ||
| ``name`` by default is the capitalized | ||
| fragment type. | ||
| ``showcontent`` is true by default. | ||
|
|
||
| For example:: | ||
|
|
||
| [tool.towncrier] | ||
| [tool.towncrier.fragment.chore] | ||
| name = "Chores" | ||
| showcontent = False | ||
|
|
||
| [tool.towncrier.fragment.deprecations] | ||
| # name will be "Deprecations" | ||
| # The content will be shown. | ||
|
|
||
| """ | ||
| def __init__(self, config): | ||
| """Initialize.""" | ||
| self.config = config | ||
| self.fragment_options = config.get("fragment", {}) | ||
|
|
||
| def load(self,): | ||
| """Load types from nested mapping.""" | ||
| fragment_types = self.fragment_options.keys() | ||
| fragment_types = sorted(fragment_types) | ||
| custom_types_sequence = [ | ||
| (fragment_type, self._load_options(fragment_type)) | ||
| for fragment_type in fragment_types | ||
| ] | ||
| types = clt.OrderedDict(custom_types_sequence) | ||
| return types | ||
|
|
||
| def _load_options(self, fragment_type): | ||
| """Load fragment options.""" | ||
| capitalized_fragment_type = fragment_type.capitalize() | ||
| options = self.fragment_options.get(fragment_type, {}) | ||
| fragment_description = options.get("name", capitalized_fragment_type) | ||
| show_content = options.get("showcontent", True) | ||
| clean_fragment_options = { | ||
| "name": fragment_description, | ||
| "showcontent": show_content, | ||
| } | ||
| return clean_fragment_options |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Added support to tables in toml settings, which provides a more intuitive | ||
| way to configure custom types. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.