-
Notifications
You must be signed in to change notification settings - Fork 107
/
_settings.py
164 lines (129 loc) · 4.96 KB
/
_settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright (c) Amber Brown, 2015
# See LICENSE for details.
import io
import os
import sys
import pkg_resources
if sys.version_info >= (3, 6):
from tomli import load as _toml_load
else:
from toml import load as _toml_load
from collections import OrderedDict
class ConfigError(Exception):
def __init__(self, *args, **kwargs):
self.failing_option = kwargs.get("failing_option")
super(ConfigError, self).__init__(*args)
_start_string = u".. towncrier release notes start\n"
_title_format = None
_template_fname = "towncrier:default"
_default_types = 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}),
]
)
_underlines = ["=", "-", "~"]
def load_config_from_options(directory, config):
if config is None:
if directory is None:
directory = os.getcwd()
base_directory = os.path.abspath(directory)
config = load_config(base_directory)
else:
config = os.path.abspath(config)
if directory:
base_directory = os.path.abspath(directory)
else:
base_directory = os.path.dirname(config)
config = load_config_from_file(os.path.dirname(config), config)
if config is None:
raise ConfigError(
"No configuration file found.\nLooked in: %s" % (base_directory,)
)
return base_directory, config
def load_config(directory):
towncrier_toml = os.path.join(directory, "towncrier.toml")
pyproject_toml = os.path.join(directory, "pyproject.toml")
if os.path.exists(towncrier_toml):
config_file = towncrier_toml
elif os.path.exists(pyproject_toml):
config_file = pyproject_toml
else:
return None
return load_config_from_file(directory, config_file)
def load_config_from_file(directory, config_file):
with io.open(config_file, "r", encoding="utf8") as conffile:
config = _toml_load(conffile)
return parse_toml(directory, config)
def parse_toml(base_path, config):
if "tool" not in config:
raise ConfigError("No [tool.towncrier] section.", failing_option="all")
config = config["tool"]["towncrier"]
sections = OrderedDict()
types = OrderedDict()
if "section" in config:
for x in config["section"]:
sections[x.get("name", "")] = x["path"]
else:
sections[""] = ""
if "type" in config:
for x in config["type"]:
types[x["directory"]] = {"name": x["name"], "showcontent": x["showcontent"]}
else:
types = _default_types
wrap = config.get("wrap", False)
single_file_wrong = config.get("singlefile")
if single_file_wrong:
raise ConfigError(
"`singlefile` is not a valid option. Did you mean `single_file`?",
failing_option="singlefile",
)
single_file = config.get("single_file", True)
if not isinstance(single_file, bool):
raise ConfigError(
"`single_file` option must be a boolean: false or true.",
failing_option="single_file",
)
all_bullets = config.get("all_bullets", True)
if not isinstance(all_bullets, bool):
raise ConfigError(
"`all_bullets` option must be boolean: false or true.",
failing_option="all_bullets",
)
template = config.get("template", _template_fname)
if template.startswith("towncrier:"):
resource_name = "templates/" + template.split("towncrier:", 1)[1] + ".rst"
if not pkg_resources.resource_exists("towncrier", resource_name):
raise ConfigError(
"Towncrier does not have a template named '%s'."
% (template.split("towncrier:", 1)[1],)
)
template = pkg_resources.resource_filename("towncrier", resource_name)
else:
template = os.path.join(base_path, template)
if not os.path.exists(template):
raise ConfigError(
"The template file '%s' does not exist." % (template,),
failing_option="template",
)
return {
"package": config.get("package", ""),
"package_dir": config.get("package_dir", "."),
"single_file": single_file,
"filename": config.get("filename", "NEWS.rst"),
"directory": config.get("directory"),
"version": config.get("version"),
"name": config.get("name"),
"sections": sections,
"types": types,
"template": template,
"start_string": config.get("start_string", _start_string),
"title_format": config.get("title_format", _title_format),
"issue_format": config.get("issue_format"),
"underlines": config.get("underlines", _underlines),
"wrap": wrap,
"all_bullets": all_bullets,
}