ruff-config-generator 2.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ruff_config_generator/__init__.py +0 -0
- ruff_config_generator/__main__.py +42 -0
- ruff_config_generator/app_config.py +75 -0
- ruff_config_generator/default_config.toml +9 -0
- ruff_config_generator/downloader.py +63 -0
- ruff_config_generator/generator.py +363 -0
- ruff_config_generator/py.typed +0 -0
- ruff_config_generator-2.1.0.dist-info/METADATA +41 -0
- ruff_config_generator-2.1.0.dist-info/RECORD +13 -0
- ruff_config_generator-2.1.0.dist-info/WHEEL +4 -0
- ruff_config_generator-2.1.0.dist-info/entry_points.txt +3 -0
- ruff_config_generator-2.1.0.dist-info/licenses/LICENSE +201 -0
- ruff_config_generator-2.1.0.dist-info/licenses/NOTICE +1 -0
|
File without changes
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
import typer
|
|
5
|
+
from loguru import logger
|
|
6
|
+
from typer_config import use_multifile_config
|
|
7
|
+
|
|
8
|
+
from ruff_config_generator.app_config import AppConfiguration
|
|
9
|
+
from ruff_config_generator.downloader import download
|
|
10
|
+
from ruff_config_generator.generator import generate_configuration
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
app = typer.Typer()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
_DEFAULT_CONFIG = (Path(__file__).parent / 'default_config.toml').absolute()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _setup_logger() -> None:
|
|
20
|
+
logger.remove()
|
|
21
|
+
logger.add(
|
|
22
|
+
sys.stdout,
|
|
23
|
+
level='INFO',
|
|
24
|
+
format=(
|
|
25
|
+
'<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | '
|
|
26
|
+
'<level>{level: <8}</level> | '
|
|
27
|
+
'<level>{message}</level>'
|
|
28
|
+
),
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@app.command()
|
|
33
|
+
@use_multifile_config(default_files=[str(_DEFAULT_CONFIG)])
|
|
34
|
+
def _(ctx: typer.Context) -> None:
|
|
35
|
+
config = AppConfiguration.model_validate(ctx.default_map)
|
|
36
|
+
_setup_logger()
|
|
37
|
+
download(config)
|
|
38
|
+
generate_configuration(config)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
if __name__ == '__main__':
|
|
42
|
+
app()
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
from functools import cached_property
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Annotated, Any
|
|
4
|
+
|
|
5
|
+
from pydantic import AfterValidator, BaseModel, ValidationInfo
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _ensure_folder_exists(value: Path, info: ValidationInfo) -> Path:
|
|
9
|
+
if value.exists() and not value.is_dir():
|
|
10
|
+
msg = (
|
|
11
|
+
f'Path "{value}" points to existing element, which is not a directory, '
|
|
12
|
+
f'so cannot be used as "{info.field_name}".'
|
|
13
|
+
)
|
|
14
|
+
raise ValueError(msg)
|
|
15
|
+
value.mkdir(parents=True, exist_ok=True)
|
|
16
|
+
return value
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class AppConfiguration(BaseModel):
|
|
20
|
+
"""
|
|
21
|
+
Application configuration model.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
workdir: Annotated[Path, AfterValidator(_ensure_folder_exists)]
|
|
25
|
+
settings_html_file_name: str
|
|
26
|
+
rules_html_file_name: str
|
|
27
|
+
version_file_name: str
|
|
28
|
+
default_values_file_name: str
|
|
29
|
+
adjusted_values_file_name: str
|
|
30
|
+
overrides: dict[str, dict[str, Any]]
|
|
31
|
+
|
|
32
|
+
@cached_property
|
|
33
|
+
def settings_html_file(self) -> Path:
|
|
34
|
+
"""
|
|
35
|
+
File where HTML version of ruff settings is downloaded.
|
|
36
|
+
|
|
37
|
+
:return: settings.html file path
|
|
38
|
+
"""
|
|
39
|
+
return self.workdir / self.settings_html_file_name
|
|
40
|
+
|
|
41
|
+
@cached_property
|
|
42
|
+
def rules_html_file(self) -> Path:
|
|
43
|
+
"""
|
|
44
|
+
File where HTML version of ruff rules is downloaded.
|
|
45
|
+
|
|
46
|
+
:return: rules.html file path
|
|
47
|
+
"""
|
|
48
|
+
return self.workdir / self.rules_html_file_name
|
|
49
|
+
|
|
50
|
+
@cached_property
|
|
51
|
+
def version_file(self) -> Path:
|
|
52
|
+
"""
|
|
53
|
+
File where latest processed version of ruff is stored.
|
|
54
|
+
|
|
55
|
+
:return: version.txt file path
|
|
56
|
+
"""
|
|
57
|
+
return self.workdir / self.version_file_name
|
|
58
|
+
|
|
59
|
+
@cached_property
|
|
60
|
+
def default_values_file(self) -> Path:
|
|
61
|
+
"""
|
|
62
|
+
File where ruff config with default values is generated.
|
|
63
|
+
|
|
64
|
+
:return: ruff.toml with default values file path
|
|
65
|
+
"""
|
|
66
|
+
return self.workdir / self.default_values_file_name
|
|
67
|
+
|
|
68
|
+
@cached_property
|
|
69
|
+
def adjusted_values_file(self) -> Path:
|
|
70
|
+
"""
|
|
71
|
+
File where ruff config with adjusted values is generated.
|
|
72
|
+
|
|
73
|
+
:return: ruff.toml with adjusted values file path
|
|
74
|
+
"""
|
|
75
|
+
return self.workdir / self.adjusted_values_file_name
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
workdir = "workdir"
|
|
2
|
+
settings_html_file_name = 'settings.html'
|
|
3
|
+
rules_html_file_name = 'rules.html'
|
|
4
|
+
version_file_name = 'version.txt'
|
|
5
|
+
default_values_file_name = 'default.toml'
|
|
6
|
+
adjusted_values_file_name = 'adjusted.toml'
|
|
7
|
+
|
|
8
|
+
[overrides.analyze]
|
|
9
|
+
direction = 'Dependencies'
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
import requests
|
|
4
|
+
from loguru import logger
|
|
5
|
+
|
|
6
|
+
from .app_config import AppConfiguration
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
_RULES_HTML_URL = 'https://docs.astral.sh/ruff/rules/'
|
|
10
|
+
_SETTINGS_HTML_URL = 'https://docs.astral.sh/ruff/settings/'
|
|
11
|
+
_RUFF_PYPI_INFORMATION_URL = 'https://pypi.org/pypi/ruff/json'
|
|
12
|
+
_REQUEST_TIMEOUT = 10 # in seconds
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def download(app_config: AppConfiguration) -> None:
|
|
16
|
+
"""
|
|
17
|
+
Download ruff's settings page and version of PyPI.
|
|
18
|
+
|
|
19
|
+
:param app_config: application configuration
|
|
20
|
+
"""
|
|
21
|
+
_download_page(_SETTINGS_HTML_URL, app_config.settings_html_file, 'settings')
|
|
22
|
+
_download_page(_RULES_HTML_URL, app_config.rules_html_file, 'rules')
|
|
23
|
+
_download_latest_version(app_config)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _download_page(url: str, output_file: Path, name: str) -> None:
|
|
27
|
+
"""
|
|
28
|
+
Download ruff's configuration page as HTML.
|
|
29
|
+
|
|
30
|
+
:param url: page url
|
|
31
|
+
:param output_file: file to save downloaded page to
|
|
32
|
+
:param name: human friendly name of the downloaded page
|
|
33
|
+
"""
|
|
34
|
+
logger.info('Downloading {} page from {}', name, url)
|
|
35
|
+
try:
|
|
36
|
+
response = requests.get(url, timeout=_REQUEST_TIMEOUT)
|
|
37
|
+
response.raise_for_status()
|
|
38
|
+
output_file.write_text(response.text, encoding='utf-8')
|
|
39
|
+
logger.info('Page with {} saved to {}', name, output_file)
|
|
40
|
+
except requests.RequestException:
|
|
41
|
+
logger.exception('Failed to download {} page', name)
|
|
42
|
+
raise
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _download_latest_version(app_config: AppConfiguration) -> None:
|
|
46
|
+
"""
|
|
47
|
+
Fetch latest version of ruff from PyPI and save it to file.
|
|
48
|
+
|
|
49
|
+
:param app_config: application configuration
|
|
50
|
+
"""
|
|
51
|
+
logger.info('Fetching latest ruff version from PyPI')
|
|
52
|
+
try:
|
|
53
|
+
response = requests.get(_RUFF_PYPI_INFORMATION_URL, timeout=_REQUEST_TIMEOUT)
|
|
54
|
+
response.raise_for_status()
|
|
55
|
+
version = response.json()['info']['version']
|
|
56
|
+
app_config.version_file.write_text(version, encoding='utf-8')
|
|
57
|
+
logger.info('Ruff version {} saved to {}', version, app_config.version_file)
|
|
58
|
+
except requests.RequestException:
|
|
59
|
+
logger.exception('Failed to fetch ruff version from PyPI')
|
|
60
|
+
raise
|
|
61
|
+
except KeyError:
|
|
62
|
+
logger.exception('Unexpected PyPI response format')
|
|
63
|
+
raise
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
from copy import deepcopy
|
|
2
|
+
|
|
3
|
+
import bs4
|
|
4
|
+
from loguru import logger
|
|
5
|
+
|
|
6
|
+
from .app_config import AppConfiguration
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Setting:
|
|
10
|
+
"""
|
|
11
|
+
Single ruff setting.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
def __init__(self) -> None:
|
|
15
|
+
self.name: str | None = None
|
|
16
|
+
self.default_value: str | None = None
|
|
17
|
+
self.comments: list[str] = []
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def is_non_empty_dict(self) -> bool:
|
|
21
|
+
"""
|
|
22
|
+
Indicates whether setting's default value is a non-empty dictionary.
|
|
23
|
+
|
|
24
|
+
:return: whether setting's default value is a non-empty dictionary
|
|
25
|
+
"""
|
|
26
|
+
return (
|
|
27
|
+
self.default_value is not None
|
|
28
|
+
and self.default_value != r'{}'
|
|
29
|
+
and self.default_value.startswith('{')
|
|
30
|
+
and self.default_value.endswith('}')
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
def __str__(self) -> str:
|
|
34
|
+
lines: list[str] = []
|
|
35
|
+
lines.extend(self.get_comment_lines())
|
|
36
|
+
if self.default_value in {'None', 'null', None}:
|
|
37
|
+
lines.append(f'#{self.name} =')
|
|
38
|
+
else:
|
|
39
|
+
lines.append(f'{self.name} = {self._processed_default_value()}')
|
|
40
|
+
return '\n'.join(lines)
|
|
41
|
+
|
|
42
|
+
def get_comment_lines(self) -> list[str]:
|
|
43
|
+
"""
|
|
44
|
+
Generate a list of comment lines.
|
|
45
|
+
|
|
46
|
+
:return: list of comment lines
|
|
47
|
+
"""
|
|
48
|
+
return [f'# {comment}'.strip() for comment in self.comments]
|
|
49
|
+
|
|
50
|
+
def _processed_default_value(self) -> str:
|
|
51
|
+
assert self.name is not None
|
|
52
|
+
assert self.default_value is not None
|
|
53
|
+
|
|
54
|
+
# Regex patterns need special escaping
|
|
55
|
+
if self._is_regex_setting():
|
|
56
|
+
return self._process_regex_value()
|
|
57
|
+
|
|
58
|
+
# Pass through booleans, empty collections, and quoted strings
|
|
59
|
+
if self._is_passthrough_value():
|
|
60
|
+
return self.default_value
|
|
61
|
+
|
|
62
|
+
# Integers don't need quotes
|
|
63
|
+
if self._is_integer_value():
|
|
64
|
+
return self.default_value
|
|
65
|
+
|
|
66
|
+
# Format list values with proper indentation
|
|
67
|
+
if self._is_list_value():
|
|
68
|
+
return self._process_list_value()
|
|
69
|
+
|
|
70
|
+
# Convert dict format from JSON to TOML
|
|
71
|
+
if self._is_dict_value():
|
|
72
|
+
return self.default_value.replace('":', '" =')
|
|
73
|
+
|
|
74
|
+
# Default: wrap in quotes # noqa: ERA001
|
|
75
|
+
return f'"{self.default_value}"'
|
|
76
|
+
|
|
77
|
+
def _is_regex_setting(self) -> bool:
|
|
78
|
+
assert self.name is not None
|
|
79
|
+
return self.name.endswith('-rgx')
|
|
80
|
+
|
|
81
|
+
def _process_regex_value(self) -> str:
|
|
82
|
+
assert self.default_value is not None
|
|
83
|
+
value = self.default_value.strip('"').replace('\\', '\\\\')
|
|
84
|
+
return f'"{value}"'
|
|
85
|
+
|
|
86
|
+
def _is_passthrough_value(self) -> bool:
|
|
87
|
+
assert self.default_value is not None
|
|
88
|
+
return self.default_value in {'true', 'false', '[]', r'{}'} or self.default_value.startswith('"')
|
|
89
|
+
|
|
90
|
+
def _is_integer_value(self) -> bool:
|
|
91
|
+
assert self.default_value is not None
|
|
92
|
+
try:
|
|
93
|
+
int(self.default_value)
|
|
94
|
+
except ValueError:
|
|
95
|
+
return False
|
|
96
|
+
else:
|
|
97
|
+
return True
|
|
98
|
+
|
|
99
|
+
def _is_list_value(self) -> bool:
|
|
100
|
+
assert self.default_value is not None
|
|
101
|
+
return self.default_value.startswith('[')
|
|
102
|
+
|
|
103
|
+
def _process_list_value(self) -> str:
|
|
104
|
+
assert self.default_value is not None
|
|
105
|
+
values = ',\n '.join(self.default_value[1:-1].split(', '))
|
|
106
|
+
return f'[\n {values},\n]'
|
|
107
|
+
|
|
108
|
+
def _is_dict_value(self) -> bool:
|
|
109
|
+
assert self.default_value is not None
|
|
110
|
+
return self.default_value.startswith('{')
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class Section:
|
|
114
|
+
"""
|
|
115
|
+
Single ruff configuration section.
|
|
116
|
+
|
|
117
|
+
:param name: name of the section
|
|
118
|
+
"""
|
|
119
|
+
|
|
120
|
+
def __init__(self, name: str) -> None:
|
|
121
|
+
self.name = name
|
|
122
|
+
self.settings: list[Setting] = []
|
|
123
|
+
|
|
124
|
+
def __str__(self) -> str:
|
|
125
|
+
lines: list[str] = []
|
|
126
|
+
dict_value_settings = []
|
|
127
|
+
if self.name != 'Top-level':
|
|
128
|
+
lines.append(f'[{self.name}]')
|
|
129
|
+
for setting in self.settings:
|
|
130
|
+
if setting.is_non_empty_dict:
|
|
131
|
+
dict_value_settings.append(setting)
|
|
132
|
+
continue
|
|
133
|
+
lines.append(str(setting))
|
|
134
|
+
for setting in dict_value_settings:
|
|
135
|
+
assert setting.default_value is not None
|
|
136
|
+
lines.append('')
|
|
137
|
+
lines.extend(setting.get_comment_lines())
|
|
138
|
+
lines.append(f'[{self.name}.{setting.name}]')
|
|
139
|
+
values = '\n'.join(setting.default_value[1:-1].replace('":', '" =').split(', '))
|
|
140
|
+
|
|
141
|
+
lines.append(values)
|
|
142
|
+
lines.append('')
|
|
143
|
+
return '\n'.join(lines)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
class RuffConfiguration:
|
|
147
|
+
"""
|
|
148
|
+
Whole Ruff configuration.
|
|
149
|
+
|
|
150
|
+
:param version: ruff version for which configuration is stored
|
|
151
|
+
:param rules_descriptions: mapping with descriptions of rules
|
|
152
|
+
"""
|
|
153
|
+
|
|
154
|
+
def __init__(self, version: str, rules_descriptions: dict[str, str]) -> None:
|
|
155
|
+
self.version = version
|
|
156
|
+
self.sections: list[Section] = []
|
|
157
|
+
self.rules_descriptions = rules_descriptions
|
|
158
|
+
|
|
159
|
+
def new_section(self, name: str) -> None:
|
|
160
|
+
"""
|
|
161
|
+
Start new section.
|
|
162
|
+
|
|
163
|
+
:param name: section name
|
|
164
|
+
"""
|
|
165
|
+
self.sections.append(Section(name))
|
|
166
|
+
|
|
167
|
+
def add_setting(self, setting: Setting) -> None:
|
|
168
|
+
"""
|
|
169
|
+
Add setting to latest section.
|
|
170
|
+
|
|
171
|
+
:param setting: setting to be added
|
|
172
|
+
"""
|
|
173
|
+
self.sections[-1].settings.append(setting)
|
|
174
|
+
|
|
175
|
+
def __str__(self) -> str:
|
|
176
|
+
lines: list[str] = []
|
|
177
|
+
lines.append(f'### Configuration created for ruff=={self.version}')
|
|
178
|
+
lines.append('')
|
|
179
|
+
lines.extend(str(section) for section in self.sections)
|
|
180
|
+
lines = ('\n'.join(lines)).splitlines()
|
|
181
|
+
for index, line in enumerate(lines):
|
|
182
|
+
rule_id = line.strip(' ",')
|
|
183
|
+
if description := self.rules_descriptions.get(rule_id):
|
|
184
|
+
spaces = ' ' * (9 - len(rule_id))
|
|
185
|
+
lines[index] = f'{line}{spaces}# {description}'
|
|
186
|
+
lines.append('')
|
|
187
|
+
return '\n'.join(lines)
|
|
188
|
+
|
|
189
|
+
def update_default_values(self, update: dict[str, dict[str, str]]) -> None:
|
|
190
|
+
"""
|
|
191
|
+
Update default values in settings according to provided dict.
|
|
192
|
+
|
|
193
|
+
:param update: new default values for settings
|
|
194
|
+
"""
|
|
195
|
+
update = deepcopy(update)
|
|
196
|
+
for section in self.sections:
|
|
197
|
+
if section.name not in update:
|
|
198
|
+
continue
|
|
199
|
+
section_update = update[section.name]
|
|
200
|
+
for setting in section.settings:
|
|
201
|
+
if setting.name not in section_update:
|
|
202
|
+
continue
|
|
203
|
+
setting.default_value = section_update.pop(setting.name)
|
|
204
|
+
if not section_update:
|
|
205
|
+
update.pop(section.name)
|
|
206
|
+
if update:
|
|
207
|
+
logger.warning('Not found overrides: {}', update)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
class _HtmlParser:
|
|
211
|
+
"""
|
|
212
|
+
Parser for ruff settings HTML documentation.
|
|
213
|
+
|
|
214
|
+
:param config: config to be filled in by the parser
|
|
215
|
+
"""
|
|
216
|
+
|
|
217
|
+
def __init__(self, config: RuffConfiguration) -> None:
|
|
218
|
+
self.config = config
|
|
219
|
+
self.current_setting: Setting | None = None
|
|
220
|
+
|
|
221
|
+
def parse_tag(self, tag: bs4.Tag) -> None:
|
|
222
|
+
"""
|
|
223
|
+
Parse a single HTML tag and update configuration.
|
|
224
|
+
|
|
225
|
+
:param tag: HTML tag to parse
|
|
226
|
+
"""
|
|
227
|
+
if tag.name in ('h2', 'h3'):
|
|
228
|
+
self._handle_section_header(tag)
|
|
229
|
+
elif tag.name == 'h4':
|
|
230
|
+
self._handle_setting_header(tag)
|
|
231
|
+
elif tag.name == 'p':
|
|
232
|
+
self._handle_paragraph(tag)
|
|
233
|
+
elif tag.name == 'ul':
|
|
234
|
+
self._handle_list(tag)
|
|
235
|
+
elif tag.name == 'div':
|
|
236
|
+
self._handle_div(tag)
|
|
237
|
+
|
|
238
|
+
def _handle_section_header(self, tag: bs4.Tag) -> None:
|
|
239
|
+
"""
|
|
240
|
+
Handle h2/h3 section headers.
|
|
241
|
+
|
|
242
|
+
:param tag: section header tag to parse
|
|
243
|
+
"""
|
|
244
|
+
self.config.new_section(tag.text)
|
|
245
|
+
logger.debug('Created section: {}', tag.text)
|
|
246
|
+
|
|
247
|
+
def _handle_setting_header(self, tag: bs4.Tag) -> None:
|
|
248
|
+
"""
|
|
249
|
+
Handle h4 setting headers.
|
|
250
|
+
|
|
251
|
+
:param tag: setting header tag to parse
|
|
252
|
+
"""
|
|
253
|
+
assert self.current_setting is None, 'Found h4 while processing previous setting'
|
|
254
|
+
self.current_setting = Setting()
|
|
255
|
+
code_element = tag.find_next('code')
|
|
256
|
+
assert code_element is not None, 'h4 tag must contain a code element'
|
|
257
|
+
self.current_setting.name = code_element.get_text()
|
|
258
|
+
logger.debug('Started setting: {}', self.current_setting.name)
|
|
259
|
+
|
|
260
|
+
def _handle_paragraph(self, tag: bs4.Tag) -> None:
|
|
261
|
+
"""
|
|
262
|
+
Handle paragraph tags (descriptions or default values).
|
|
263
|
+
|
|
264
|
+
:param tag: paragraph tag to parse
|
|
265
|
+
"""
|
|
266
|
+
if self.current_setting is None:
|
|
267
|
+
return
|
|
268
|
+
|
|
269
|
+
text = tag.get_text()
|
|
270
|
+
if text.startswith('Default value:'):
|
|
271
|
+
code_element = tag.find_next('code')
|
|
272
|
+
assert code_element is not None, 'Default value paragraph must contain a code element'
|
|
273
|
+
self.current_setting.default_value = code_element.get_text()
|
|
274
|
+
self.config.add_setting(self.current_setting)
|
|
275
|
+
logger.debug('Completed setting: {}', self.current_setting.name)
|
|
276
|
+
self.current_setting = None
|
|
277
|
+
else:
|
|
278
|
+
self.current_setting.comments.extend(text.splitlines())
|
|
279
|
+
|
|
280
|
+
def _handle_list(self, tag: bs4.Tag) -> None:
|
|
281
|
+
"""
|
|
282
|
+
Handle unordered list tags (setting options/notes).
|
|
283
|
+
|
|
284
|
+
:param tag: unordered list tag to parse
|
|
285
|
+
"""
|
|
286
|
+
if self.current_setting is None:
|
|
287
|
+
return
|
|
288
|
+
|
|
289
|
+
for ul_child in tag.children:
|
|
290
|
+
if not isinstance(ul_child, bs4.Tag) or ul_child.name != 'li':
|
|
291
|
+
continue
|
|
292
|
+
self.current_setting.comments.extend(f'- {ul_child.get_text()}'.splitlines())
|
|
293
|
+
|
|
294
|
+
def _handle_div(self, tag: bs4.Tag) -> None:
|
|
295
|
+
"""
|
|
296
|
+
Handle div tags (code examples or deprecation notices).
|
|
297
|
+
|
|
298
|
+
:param tag: div tag to parse
|
|
299
|
+
"""
|
|
300
|
+
if self.current_setting is None:
|
|
301
|
+
return
|
|
302
|
+
|
|
303
|
+
text = tag.get_text().strip()
|
|
304
|
+
if text.startswith('Deprecated'):
|
|
305
|
+
logger.debug('Skipping deprecated setting: {}', self.current_setting.name)
|
|
306
|
+
self.current_setting = None
|
|
307
|
+
return
|
|
308
|
+
|
|
309
|
+
if tag.get('class') == ['highlight']:
|
|
310
|
+
self.current_setting.comments.extend(['---', *tag.get_text().splitlines(), '---'])
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def generate_configuration(app_config: AppConfiguration) -> None:
|
|
314
|
+
"""
|
|
315
|
+
Generate TOML file configuration.
|
|
316
|
+
|
|
317
|
+
:param app_config: application configuration
|
|
318
|
+
"""
|
|
319
|
+
logger.info('Starting configuration generation')
|
|
320
|
+
rules_descriptions = _extract_rules(app_config)
|
|
321
|
+
|
|
322
|
+
# Load HTML and version
|
|
323
|
+
html_content = app_config.settings_html_file.read_text(encoding='utf-8')
|
|
324
|
+
version = app_config.version_file.read_text(encoding='utf-8').strip()
|
|
325
|
+
logger.info('Generating configuration for ruff version {}', version)
|
|
326
|
+
|
|
327
|
+
# Parse HTML
|
|
328
|
+
soup = bs4.BeautifulSoup(html_content, 'html.parser')
|
|
329
|
+
article = soup.find(name='article')
|
|
330
|
+
if article is None:
|
|
331
|
+
msg = 'Could not find <article> element in settings HTML'
|
|
332
|
+
raise ValueError(msg)
|
|
333
|
+
|
|
334
|
+
# Build configuration
|
|
335
|
+
config = RuffConfiguration(version, rules_descriptions)
|
|
336
|
+
parser = _HtmlParser(config)
|
|
337
|
+
|
|
338
|
+
for tag in article.children: # type: ignore [union-attr]
|
|
339
|
+
if isinstance(tag, bs4.Tag):
|
|
340
|
+
parser.parse_tag(tag)
|
|
341
|
+
|
|
342
|
+
# Write output files
|
|
343
|
+
logger.info('Writing configuration to {}', app_config.default_values_file)
|
|
344
|
+
app_config.default_values_file.write_text(str(config), encoding='utf-8')
|
|
345
|
+
|
|
346
|
+
config.update_default_values(app_config.overrides)
|
|
347
|
+
logger.info('Writing adjusted configuration to {}', app_config.adjusted_values_file)
|
|
348
|
+
app_config.adjusted_values_file.write_text(str(config), encoding='utf-8')
|
|
349
|
+
|
|
350
|
+
logger.info('Configuration generation completed')
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def _extract_rules(app_config: AppConfiguration) -> dict[str, str]:
|
|
354
|
+
html_content = app_config.rules_html_file.read_text(encoding='utf-8')
|
|
355
|
+
soup = bs4.BeautifulSoup(html_content, 'html.parser')
|
|
356
|
+
result = {}
|
|
357
|
+
for table in soup.find_all('tbody', recursive=True):
|
|
358
|
+
assert isinstance(table, bs4.Tag)
|
|
359
|
+
for row in table.find_all('tr'):
|
|
360
|
+
assert isinstance(row, bs4.Tag)
|
|
361
|
+
cells = row.find_all('td')
|
|
362
|
+
result[cells[0].text] = cells[2].text
|
|
363
|
+
return result
|
|
File without changes
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ruff-config-generator
|
|
3
|
+
Version: 2.1.0
|
|
4
|
+
Summary: Generates ruff configuration
|
|
5
|
+
Project-URL: homepage, https://github.com/KRunchPL/ruff-config-generator
|
|
6
|
+
Project-URL: repository, https://github.com/KRunchPL/ruff-config-generator
|
|
7
|
+
Project-URL: documentation, https://github.com/KRunchPL/ruff-config-generator
|
|
8
|
+
Author-email: KRunchPL <krunchfrompoland@gmail.com>
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
License-File: NOTICE
|
|
12
|
+
Requires-Python: <4.0,>=3.14
|
|
13
|
+
Requires-Dist: beautifulsoup4<5.0.0,>=4.15.0
|
|
14
|
+
Requires-Dist: loguru<0.8.0,>=0.7.3
|
|
15
|
+
Requires-Dist: pydantic<3.0.0,>=2.13.4
|
|
16
|
+
Requires-Dist: requests<3.0.0,>=2.32.5
|
|
17
|
+
Requires-Dist: typer-config<2.0.0,>=1.5.1
|
|
18
|
+
Requires-Dist: typer<0.28.0,>=0.27.0
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# Ruff Configuration Generator
|
|
22
|
+
|
|
23
|
+
Tool that generates [`ruff`](https://github.com/astral-sh/ruff) configuration with all available options.
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```console
|
|
28
|
+
python -m ruff_config_generator
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The command will download the settings HTML page, analyze it, and generate two `toml` files. Both `toml` files will contain all available `ruff` options along with their descriptions. The difference between the two files is that one will have all settings set to their default values, while the other will have values adjusted to my personal preferences.
|
|
32
|
+
|
|
33
|
+
Output files will be saved in the `workdir` folder as `settings.html`, `default.toml` (default values) and `adjusted.toml`.
|
|
34
|
+
|
|
35
|
+
On the repository, the `workdir` folder contains result of the above command for latest ruff version I have been using.
|
|
36
|
+
|
|
37
|
+
## Additional documentation
|
|
38
|
+
|
|
39
|
+
[Development documentation](README-DEV.md)
|
|
40
|
+
|
|
41
|
+
[Changelog](CHANGELOG.md)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
ruff_config_generator/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
ruff_config_generator/__main__.py,sha256=uacx_nEdCPKrgizgNFndKtOerKQ81QqjZOKWghf7gnQ,985
|
|
3
|
+
ruff_config_generator/app_config.py,sha256=hZUyDAtEZRbJzQDZ1QpwmxylJvL1uWBf2EaGx8C7O94,2178
|
|
4
|
+
ruff_config_generator/default_config.toml,sha256=DUPcW_NmqzBtRbg5sN5v9Lz8Wk8zxytyrm5WPVIlTjA,266
|
|
5
|
+
ruff_config_generator/downloader.py,sha256=RXvPtmPeVKpVTqjvwJCvy53KLq7VUgiXUzAwtG8A6ew,2195
|
|
6
|
+
ruff_config_generator/generator.py,sha256=GzQnnguKUPZ4m1z2lMLM1Z4yYuwAAXDdKBCNTI0OpeY,12115
|
|
7
|
+
ruff_config_generator/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
ruff_config_generator-2.1.0.dist-info/METADATA,sha256=cvfDb60akla7dZANUHEyIP8WVNYHrL-e2nXMoHO35tM,1661
|
|
9
|
+
ruff_config_generator-2.1.0.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
|
|
10
|
+
ruff_config_generator-2.1.0.dist-info/entry_points.txt,sha256=pzYLUzzusSHMn91R6LV7Hj2pvEM5bzp5aSnlrOukQK4,136
|
|
11
|
+
ruff_config_generator-2.1.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
12
|
+
ruff_config_generator-2.1.0.dist-info/licenses/NOTICE,sha256=EMqWcTeo8IOhScaKlkqLYQaE8WWgp4loRmfXi-v6AtA,66
|
|
13
|
+
ruff_config_generator-2.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Copyright 2023-2026, Jacek Chałupka <krunchfrompoland@gmail.com>
|